def _hist_get_portion(commands, slices): """Yield from portions of history commands.""" if len(slices) == 1: s = ensure_slice(slices[0]) try: yield from itertools.islice(commands, s.start, s.stop, s.step) return except ValueError: # islice failed pass commands = list(commands) for s in slices: s = ensure_slice(s) yield from commands[s]
def _args_filter(cmds, pat): args = None if isinstance(pat, (int, slice)): s = xt.ensure_slice(pat) for command in cmds: yield ' '.join(command.split()[s]) else: raise TypeError('Argument filter must be int or slice') return args
def _cmd_filter(cmds, pat): if isinstance(pat, (int, slice)): s = xt.ensure_slice(pat) yield from xt.get_portions(cmds, s) elif xt.is_string(pat): for command in reversed(list(cmds)): if pat in command: yield command return else: raise TypeError('Command filter must be string, int or slice')
def _xh_get_history( session="session", *, slices=None, datetime_format=None, start_time=None, end_time=None, location=None, ): """Get the requested portion of shell history. Parameters ---------- session: {'session', 'all', 'xonsh', 'bash', 'zsh'} The history session to get. slices : list of slice-like objects, optional Get only portions of history. start_time, end_time: float, optional Filter commands by timestamp. location: string, optional The history file location (bash or zsh) Returns ------- generator A filtered list of commands """ cmds = [] for i, item in enumerate(_XH_HISTORY_SESSIONS[session](location=location)): item["ind"] = i cmds.append(item) if slices: # transform/check all slices slices = [xt.ensure_slice(s) for s in slices] cmds = xt.get_portions(cmds, slices) if start_time or end_time: if start_time is None: start_time = 0.0 else: start_time = xt.ensure_timestamp(start_time, datetime_format) if end_time is None: end_time = float("inf") else: end_time = xt.ensure_timestamp(end_time, datetime_format) cmds = _xh_filter_ts(cmds, start_time, end_time) return cmds
def _xh_get_history( session="session", *, slices=None, datetime_format=None, start_time=None, end_time=None, location=None ): """Get the requested portion of shell history. Parameters ---------- session: {'session', 'all', 'xonsh', 'bash', 'zsh'} The history session to get. slices : list of slice-like objects, optional Get only portions of history. start_time, end_time: float, optional Filter commands by timestamp. location: string, optional The history file location (bash or zsh) Returns ------- generator A filtered list of commands """ cmds = [] for i, item in enumerate(_XH_HISTORY_SESSIONS[session](location=location)): item["ind"] = i cmds.append(item) if slices: # transform/check all slices slices = [xt.ensure_slice(s) for s in slices] cmds = xt.get_portions(cmds, slices) if start_time or end_time: if start_time is None: start_time = 0.0 else: start_time = xt.ensure_timestamp(start_time, datetime_format) if end_time is None: end_time = float("inf") else: end_time = xt.ensure_timestamp(end_time, datetime_format) cmds = _xh_filter_ts(cmds, start_time, end_time) return cmds
def _hist_get(session='session', *, slices=None, datetime_format=None, start_time=None, end_time=None, location=None): """Get the requested portion of shell history. Parameters ---------- session: {'session', 'all', 'xonsh', 'bash', 'zsh'} The history session to get. slices : list of slice-like objects, optional Get only portions of history. start_time, end_time: float, optional Filter commands by timestamp. location: string, optional The history file location (bash or zsh) Returns ------- generator A filtered list of commands """ cmds = _HIST_SESSIONS[session](location=location) if slices: # transform/check all slices slices = [ensure_slice(s) for s in slices] cmds = _hist_get_portion(cmds, slices) if start_time or end_time: if start_time is None: start_time = 0.0 else: start_time = ensure_timestamp(start_time, datetime_format) if end_time is None: end_time = float('inf') else: end_time = ensure_timestamp(end_time, datetime_format) cmds = _hist_filter_ts(cmds, start_time, end_time) return cmds
def _hist_get(session='session', *, slices=None, datetime_format=None, start_time=None, end_time=None, location=None): """Get the requested portion of shell history. Parameters ---------- session: {'session', 'all', 'xonsh', 'bash', 'zsh'} The history session to get. slices : list of slice-like objects, optional Get only portions of history. start_time, end_time: float, optional Filter commands by timestamp. location: string, optional The history file location (bash or zsh) Returns ------- generator A filtered list of commands """ cmds = _HIST_SESSIONS[session](location=location) if slices: # transform/check all slices slices = [ensure_slice(s) for s in slices] cmds = get_portions(cmds, slices) if start_time or end_time: if start_time is None: start_time = 0.0 else: start_time = ensure_timestamp(start_time, datetime_format) if end_time is None: end_time = float('inf') else: end_time = ensure_timestamp(end_time, datetime_format) cmds = _hist_filter_ts(cmds, start_time, end_time) return cmds
def test_ensure_slice_invalid(inp): with pytest.raises(ValueError): ensure_slice(inp)
def test_ensure_slice(inp, exp): obs = ensure_slice(inp) assert exp == obs
def _hist_show(ns=None, hist=None, start_index=None, end_index=None, start_time=None, end_time=None, location=None): """Show the requested portion of shell history. Accepts multiple history sources (xonsh, bash, zsh) May be invoked as an alias with history all/bash/zsh which will provide history as stdout or with __xonsh_history__.show() which will return the history as a list with each item in the tuple form (name, start_time, index). If invoked via __xonsh_history__.show() then the ns parameter can be supplied as a str with the follow options:: session - returns xonsh history from current session xonsh - returns xonsh history from all sessions all - alias of xonsh zsh - returns all zsh history bash - returns all bash history """ # Check if ns is a string, meaning it was invoked from if hist is None: hist = bultins.__xonsh_history__ alias = True if isinstance(ns, str) and ns in _HIST_SESSIONS: ns = _hist_create_parser().parse_args([ns]) alias = False if not ns: ns = _hist_create_parser().parse_args(['show', 'xonsh']) alias = False try: commands = _HIST_SESSIONS[ns.session](hist=hist, location=location) except KeyError: print("{} is not a valid history session".format(ns.action)) return None if not commands: return None if start_time: if isinstance(start_time, datetime.datetime): start_time = start_time.timestamp() if isinstance(start_time, float): commands = [c for c in commands if c[1] >= start_time] else: print("Invalid start time, must be float or datetime.") if end_time: if isinstance(end_time, datetime.datetime): end_time = end_time.timestamp() if isinstance(end_time, float): commands = [c for c in commands if c[1] <= end_time] else: print("Invalid end time, must be float or datetime.") idx = None if ns: _commands = [] for s in ns.slices: try: s = ensure_slice(s) except (ValueError, TypeError): print('{!r} is not a valid slice format'.format(s), file=sys.stderr) return if s: try: _commands.extend(commands[s]) except IndexError: err = "Index likely not in range. Only {} commands." print(err.format(len(commands)), file=sys.stderr) return else: if _commands: commands = _commands else: idx = slice(start_index, end_index) if (isinstance(idx, slice) and start_time is None and end_time is None): commands = commands[idx] if ns and ns.reverse: commands = list(reversed(commands)) if commands: digits = len(str(max([i for c, t, i in commands]))) if alias: for c, t, i in commands: for line_ind, line in enumerate(c.split('\n')): if line_ind == 0: print('{:>{width}}: {}'.format(i, line, width=digits + 1)) else: print(' {:>>{width}} {}'.format('', line, width=digits + 1)) else: return commands
def test_ensure_slice_invalid(inp): with pytest.raises(ValueError): obs = ensure_slice(inp)
def test_ensure_slice_invalid(inp, error): with pytest.raises(error): obs = ensure_slice(inp)