def _find_histfile_var(file_list=None, default=None): if file_list is None: return None hist_file = None found_hist = False for f in file_list: f = expanduser_abs_path(f) if not os.path.isfile(f): continue with open(f, 'r') as rc_file: for line in rc_file: if "HISTFILE=" in line: evar = line.split(' ', 1)[-1] hist_file = evar.split('=', 1)[-1] for char in ['"', "'", '\n']: hist_file = hist_file.replace(char, '') hist_file = expanduser_abs_path(hist_file) if os.path.isfile(hist_file): found_hist = True break if found_hist: break if hist_file is None: default = expanduser_abs_path(default) if os.path.isfile(default): hist_file = default return hist_file
def _xhj_get_history_files(sort=True, newest_first=False): """Find and return the history files. Optionally sort files by modify time. """ data_dir = builtins.__xonsh__.env.get("XONSH_DATA_DIR") data_dir = xt.expanduser_abs_path(data_dir) try: files = [ os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.startswith("xonsh-") and f.endswith(".json") ] except OSError: files = [] if builtins.__xonsh__.env.get("XONSH_DEBUG"): xt.print_exception("Could not collect xonsh history files.") if sort: files.sort(key=os.path.getmtime, reverse=newest_first) custom_history_file = builtins.__xonsh__.env.get("XONSH_HISTORY_FILE", None) if custom_history_file: custom_history_file = xt.expanduser_abs_path(custom_history_file) if custom_history_file not in files: files.insert(0, custom_history_file) return files
def _xhj_get_history_files(sort=True, newest_first=False): """Find and return the history files. Optionally sort files by modify time. """ data_dirs = [ _xhj_get_data_dir(), XSH.env.get( "XONSH_DATA_DIR"), # backwards compatibility, remove in the future ] files = [] for data_dir in data_dirs: data_dir = xt.expanduser_abs_path(data_dir) try: files += [ os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.startswith("xonsh-") and f.endswith(".json") ] except OSError: if XSH.env.get("XONSH_DEBUG"): xt.print_exception( f"Could not collect xonsh history json files from {data_dir}" ) if sort: files.sort(key=lambda x: os.path.getmtime(x), reverse=newest_first) custom_history_file = XSH.env.get("XONSH_HISTORY_FILE", None) if custom_history_file: custom_history_file = xt.expanduser_abs_path(custom_history_file) if custom_history_file not in files: files.insert(0, custom_history_file) return files
def _xh_sqlite_get_file_name(): envs = builtins.__xonsh_env__ file_name = envs.get('XONSH_HISTORY_SQLITE_FILE') if not file_name: data_dir = envs.get('XONSH_DATA_DIR') file_name = os.path.join(data_dir, 'xonsh-history.sqlite') return xt.expanduser_abs_path(file_name)
def _all_xonsh_parser(*args): """ Returns all history as found in XONSH_DATA_DIR. return format: (name, start_time, index) """ data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') data_dir = expanduser_abs_path(data_dir) files = [ os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.startswith('xonsh-') and f.endswith('.json') ] file_hist = [] for f in files: try: json_file = LazyJSON(f, reopen=False) file_hist.append(json_file.load()['cmds']) except ValueError: # Invalid json file pass commands = [(c['inp'][:-1] if c['inp'].endswith('\n') else c['inp'], c['ts'][0]) for commands in file_hist for c in commands if c] commands.sort(key=operator.itemgetter(1)) return [(c, t, ind) for ind, (c, t) in enumerate(commands)]
def files(self, only_unlocked=False): """Find and return the history files. Optionally locked files may be excluded. This is sorted by the last closed time. Returns a list of (timestamp, file) tuples. """ # pylint: disable=no-member xdd = builtins.__xonsh_env__.get('XONSH_DATA_DIR') xdd = expanduser_abs_path(xdd) fs = [f for f in glob.iglob(os.path.join(xdd, 'xonsh-*.json'))] files = [] for f in fs: try: lj = LazyJSON(f, reopen=False) if only_unlocked and lj['locked']: continue # info: closing timestamp, number of commands, filename files.append((lj['ts'][1] or time.time(), len(lj.sizes['cmds']) - 1, f)) lj.close() except (IOError, OSError, ValueError): continue files.sort() return files
def _xh_sqlite_get_file_name(): envs = builtins.__xonsh__.env file_name = envs.get("XONSH_HISTORY_SQLITE_FILE") if not file_name: data_dir = envs.get("XONSH_DATA_DIR") file_name = os.path.join(data_dir, "xonsh-history.sqlite") return xt.expanduser_abs_path(file_name)
def _xhj_get_data_dir(): dir = xt.expanduser_abs_path( os.path.join(builtins.__xonsh__.env.get("XONSH_DATA_DIR"), "history_json")) if not os.path.exists(dir): os.makedirs(dir) return dir
def _xhj_get_history_files(sort=True, reverse=False): """Find and return the history files. Optionally sort files by modify time. """ data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') data_dir = xt.expanduser_abs_path(data_dir) files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.startswith('xonsh-') and f.endswith('.json')] if sort: files.sort(key=lambda x: os.path.getmtime(x), reverse=reverse) return files
def _xh_find_histfile_var(file_list, default=None): """Return the path of the history file from the value of the envvar HISTFILE. """ for f in file_list: f = xt.expanduser_abs_path(f) if not os.path.isfile(f): continue with open(f, 'r') as rc_file: for line in rc_file: if line.startswith('HISTFILE='): hist_file = line.split('=', 1)[1].strip('\'"\n') hist_file = xt.expanduser_abs_path(hist_file) if os.path.isfile(hist_file): return hist_file else: if default: default = xt.expanduser_abs_path(default) if os.path.isfile(default): return default
def _find_histfile_var(file_list, default=None): """Return the path of the history file from the value of the envvar HISTFILE. """ for f in file_list: f = expanduser_abs_path(f) if not os.path.isfile(f): continue with open(f, 'r') as rc_file: for line in rc_file: if line.startswith('HISTFILE='): hist_file = line.split('=', 1)[1].strip('\'"\n') hist_file = expanduser_abs_path(hist_file) if os.path.isfile(hist_file): return hist_file else: if default: default = expanduser_abs_path(default) if os.path.isfile(default): return default
def _get_history_files(sort=True, reverse=False): """Find and return the history files. Optionally sort files by modify time. """ data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') data_dir = expanduser_abs_path(data_dir) files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.startswith('xonsh-') and f.endswith('.json')] if sort: files.sort(key=lambda x: os.path.getmtime(x), reverse=reverse) return files
def _xh_find_histfile_var(file_list, default=None): """Return the path of the history file from the value of the envvar HISTFILE. """ for f in file_list: f = xt.expanduser_abs_path(f) if not os.path.isfile(f): continue with open(f, "r") as rc_file: for line in rc_file: if line.startswith("HISTFILE="): hist_file = line.split("=", 1)[1].strip("'\"\n") hist_file = xt.expanduser_abs_path(hist_file) if os.path.isfile(hist_file): return hist_file else: if default: default = xt.expanduser_abs_path(default) if os.path.isfile(default): return default
def _xhj_get_history_files(sort=True, newest_first=False): """Find and return the history files. Optionally sort files by modify time. """ data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') data_dir = xt.expanduser_abs_path(data_dir) try: files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.startswith('xonsh-') and f.endswith('.json')] except OSError: files = [] if builtins.__xonsh_env__.get('XONSH_DEBUG'): xt.print_exception("Could not collect xonsh history files.") if sort: files.sort(key=lambda x: os.path.getmtime(x), reverse=newest_first) return files
def _xhj_get_history_files(sort=True, reverse=False): """Find and return the history files. Optionally sort files by modify time. """ data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') data_dir = xt.expanduser_abs_path(data_dir) try: files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.startswith('xonsh-') and f.endswith('.json')] except OSError: files = [] if builtins.__xonsh_env__.get('XONSH_DEBUG'): xt.print_exception("Could not collect xonsh history files.") if sort: files.sort(key=lambda x: os.path.getmtime(x), reverse=reverse) return files
def _all_xonsh_parser(**kwargs): """ Returns all history as found in XONSH_DATA_DIR. return format: (cmd, start_time, index) """ data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') data_dir = expanduser_abs_path(data_dir) files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.startswith('xonsh-') and f.endswith('.json')] ind = 0 for f in files: try: json_file = LazyJSON(f, reopen=False) except ValueError: # Invalid json file pass commands = json_file.load()['cmds'] for c in commands: yield (c['inp'].rstrip(), c['ts'][0], ind) ind += 1
def _all_xonsh_parser(**kwargs): """ Returns all history as found in XONSH_DATA_DIR. return format: (name, start_time, index) """ data_dir = builtins.__xonsh_env__.get('XONSH_DATA_DIR') data_dir = expanduser_abs_path(data_dir) files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.startswith('xonsh-') and f.endswith('.json')] file_hist = [] for f in files: try: json_file = LazyJSON(f, reopen=False) file_hist.append(json_file.load()['cmds']) except ValueError: # Invalid json file pass commands = [(c['inp'][:-1] if c['inp'].endswith('\n') else c['inp'], c['ts'][0]) for commands in file_hist for c in commands if c] commands.sort(key=operator.itemgetter(1)) return [(c, t, ind) for ind, (c, t) in enumerate(commands)]