updated for json history replay

This commit is contained in:
Hugo Wang 2016-11-26 15:38:58 +08:00
parent 2e2f023840
commit 6ac2f1be3a
3 changed files with 32 additions and 28 deletions

View file

@ -395,7 +395,7 @@ class JsonHistory(HistoryBase):
if len(self.buffer) == 0:
return
hf = JsonHistoryFlusher(self.filename, tuple(self.buffer), self._queue,
self._cond, at_exit=at_exit)
self._cond, at_exit=at_exit)
self.buffer.clear()
return hf
@ -449,6 +449,11 @@ class JsonHistory(HistoryBase):
def on_diff(self, ns, stdout=None, stderr=None):
xdh.dh_main_action(ns)
def on_replay(self, ns, stdout=None, stderr=None):
"""Replay a xonsh history file."""
import xonsh.replay as xrp
xrp.replay_main_action(self, ns, stdout=stdout, stderr=stderr)
def __getitem__(self, item):
"""Retrieve history parts based on filtering rules,
see ``History`` docs for more info. Accepts one of

View file

@ -21,7 +21,7 @@ HISTORY_BACKENDS = {
}
def construct_history(env, ts, locked, gc=True, filename=None):
def construct_history(**kwargs):
env = builtins.__xonsh_env__
backend = env.get('XONSH_HISTORY_BACKEND', 'json')
if backend not in HISTORY_BACKENDS:
@ -30,13 +30,7 @@ def construct_history(env, ts, locked, gc=True, filename=None):
kls_history = JsonHistory
else:
kls_history = HISTORY_BACKENDS[backend]
return kls_history(
env=env.detype(),
ts=ts,
locked=locked,
gc=gc,
filename=filename,
)
return kls_history(**kwargs)
def _xh_session_parser(hist=None, **kwargs):
@ -211,6 +205,9 @@ def _XH_HISTORY_SESSIONS():
'bash': _xh_bash_hist_parser}
_XH_MAIN_ACTIONS = {'show', 'id', 'file', 'info', 'diff', 'gc'}
@functools.lru_cache()
def _xh_create_parser():
"""Create a parser for the "history" command."""
@ -250,15 +247,7 @@ def _xh_create_parser():
'current history'))
info.add_argument('--json', dest='json', default=False,
action='store_true', help='print in JSON format')
# diff
diff = subp.add_parser('diff', help='diff two xonsh history files')
xdh.dh_create_parser(p=diff)
# replay, dynamically
from xonsh import replay
rp = subp.add_parser('replay', help='replay a xonsh history file')
replay._rp_create_parser(p=rp)
# _XH_MAIN_ACTIONS['replay'] = replay._rp_main_action
_XH_MAIN_ACTIONS.add('replay')
# gc
gcp = subp.add_parser(
'gc', help='launches a new history garbage collector')
@ -272,12 +261,20 @@ def _xh_create_parser():
'default True'))
bgcp.add_argument('--non-blocking', dest='blocking', action='store_false',
help='makes the gc non-blocking, and thus return sooner')
hist = builtins.__xonsh_history__
if hasattr(hist, 'on_diff'):
diff = subp.add_parser('diff', help='diff two xonsh history files')
xdh.dh_create_parser(p=diff)
if hasattr(hist, 'on_replay'):
import xonsh.replay as xrp
replay = subp.add_parser('replay', help='replay a xonsh history file')
xrp.replay_create_parser(p=replay)
_XH_MAIN_ACTIONS.add('replay')
return p
_XH_MAIN_ACTIONS = {'show', 'id', 'file', 'info', 'diff', 'gc'}
def _xh_parse_args(args):
"""Prepare and parse arguments for the history command.
@ -314,5 +311,7 @@ def history_main(args=None, stdin=None, stdout=None, stderr=None):
return
method_name = 'on_{}'.format(ns.action)
method = getattr(hist, method_name, None)
if method:
method(ns, stdout=stdout, stderr=stderr)
if not method:
print('Unknown history action {}'.format(method_name), file=sys.stderr)
return
method(ns, stdout=stdout, stderr=stderr)

View file

@ -79,7 +79,7 @@ class Replayer(object):
_REPLAY_PARSER = None
def _rp_create_parser(p=None):
def replay_create_parser(p=None):
global _REPLAY_PARSER
p_was_none = (p is None)
if _REPLAY_PARSER is not None and p_was_none:
@ -105,17 +105,17 @@ def _rp_create_parser(p=None):
return p
def _rp_main_action(ns, h=None, stdout=None, stderr=None):
def replay_main_action(h, ns, stdout=None, stderr=None):
replayer = Replayer(ns.path)
hist = replayer.replay(merge_envs=ns.merge_envs, target=ns.target)
print('----------------------------------------------------------------')
print('Just replayed history, new history has the following information')
print('----------------------------------------------------------------')
hist.show_info(ns)
hist.on_info(ns, stdout=stdout, stderr=stderr)
def replay_main(args, stdin=None):
"""Acts as main function for replaying a xonsh history file."""
parser = _rp_create_parser()
parser = replay_create_parser()
ns = parser.parse_args(args)
_rp_main_action(ns)
replay_main_action(ns)