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 = 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_get_portions(inp, exp): obs = get_portions(*inp) assert list(obs) == exp