def compile(self, src): """Compiles source code and returns the (possibly modified) source and a valid code object. """ _cache = should_use_cache(self.execer, 'single') if _cache: codefname = code_cache_name(src) cachefname = get_cache_filename(codefname, code=True) usecache, code = code_cache_check(cachefname) if usecache: self.reset_buffer() return src, code try: code = self.execer.compile(src, mode='single', glbs=self.ctx, locs=None) if _cache: update_cache(code, cachefname) self.reset_buffer() except SyntaxError: partial_string_info = check_for_partial_string(src) in_partial_string = (partial_string_info[0] is not None and partial_string_info[1] is None) if (src == '\n' or src.endswith('\n\n')) and not in_partial_string: self.reset_buffer() print_exception() return src, None self.need_more_lines = True code = None except Exception: # pylint: disable=broad-except self.reset_buffer() print_exception() code = None return src, code
def default(self, line): """Implements code execution.""" line = line if line.endswith('\n') else line + '\n' src, code = self.push(line) if code is None: return env = builtins.__xonsh_env__ hist = builtins.__xonsh_history__ # pylint: disable=no-member ts1 = None enc = env.get('XONSH_ENCODING') err = env.get('XONSH_ENCODING_ERRORS') tee = Tee(encoding=enc, errors=err) try: ts0 = time.time() run_compiled_code(code, self.ctx, None, 'single') ts1 = time.time() if hist is not None and hist.last_cmd_rtn is None: hist.last_cmd_rtn = 0 # returncode for success except XonshError as e: print(e.args[0], file=sys.stderr) if hist is not None and hist.last_cmd_rtn is None: hist.last_cmd_rtn = 1 # return code for failure except Exception: # pylint: disable=broad-except print_exception() if hist is not None and hist.last_cmd_rtn is None: hist.last_cmd_rtn = 1 # return code for failure finally: ts1 = ts1 or time.time() self._append_history(inp=src, ts=[ts0, ts1], tee_out=tee.getvalue()) tee.close() self._fix_cwd() if builtins.__xonsh_exit__: # pylint: disable=no-member return True
def default(self, line): """Implements code execution.""" line = line if line.endswith('\n') else line + '\n' src, code = self.push(line) if code is None: return hist = builtins.__xonsh_history__ ts1 = None tee = Tee() if builtins.__xonsh_env__.get('XONSH_STORE_STDOUT') \ else io.StringIO() try: ts0 = time.time() self.execer.exec(code, mode='single', glbs=self.ctx) # no locals ts1 = time.time() if hist.last_cmd_rtn is None: hist.last_cmd_rtn = 0 # returncode for success except XonshError as e: print(e.args[0], file=sys.stderr) if hist.last_cmd_rtn is None: hist.last_cmd_rtn = 1 # return code for failure except Exception: print_exception() if hist.last_cmd_rtn is None: hist.last_cmd_rtn = 1 # return code for failure finally: ts1 = ts1 or time.time() self._append_history(inp=src, ts=[ts0, ts1], tee_out=tee.getvalue()) tee.close() if builtins.__xonsh_exit__: return True
def prompt(self): """Obtains the current prompt string.""" global RL_LIB, RL_CAN_RESIZE if RL_CAN_RESIZE: # This is needed to support some system where line-wrapping doesn't # work. This is a bug in upstream Python, or possibly readline. RL_LIB.rl_reset_screen_size() if self.need_more_lines: if self.mlprompt is None: try: self.mlprompt = multiline_prompt(curr=self._current_prompt) except Exception: # pylint: disable=broad-except print_exception() self.mlprompt = "<multiline prompt error> " return self.mlprompt env = XSH.env # pylint: disable=no-member p = env.get("PROMPT") try: p = self.prompt_formatter(p) except Exception: # pylint: disable=broad-except print_exception() hide = True if self._force_hide is None else self._force_hide p = ansi_partial_color_format(p, style=env.get("XONSH_COLOR_STYLE"), hide=hide) self._current_prompt = p self.settitle() return p
def push(self, line): """Pushes a line onto the buffer and compiles the code in a way that enables multiline input. """ code = None self.buffer.append(line) if self.need_more_lines: return None, code src = "".join(self.buffer) _cache = should_use_cache(self.execer, "single") if _cache: codefname = code_cache_name(src) cachefname = get_cache_filename(codefname, code=True) usecache, code = code_cache_check(cachefname) if usecache: self.reset_buffer() return src, code try: code = self.execer.compile(src, mode="single", glbs=self.ctx, locs=None) if _cache: update_cache(code, cachefname) self.reset_buffer() except SyntaxError: if line == "\n": self.reset_buffer() print_exception() return src, None self.need_more_lines = True except Exception: # pylint: disable=broad-except self.reset_buffer() print_exception() return src, None return src, code
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 transform_command(src, show_diff=True): """Returns the results of firing the precommand handles.""" i = 0 limit = sys.getrecursionlimit() lst = "" raw = src while src != lst: lst = src srcs = events.on_transform_command.fire(cmd=src) for s in srcs: if s != lst: src = s break i += 1 if i == limit: print_exception( "Modifications to source input took more than " "the recursion limit number of iterations to " "converge." ) debug_level = builtins.__xonsh__.env.get("XONSH_DEBUG") if show_diff and debug_level > 1 and src != raw: sys.stderr.writelines( difflib.unified_diff( raw.splitlines(keepends=True), src.splitlines(keepends=True), fromfile="before precommand event", tofile="after precommand event", ) ) return src
def xontribs_load( names: Annotated[ tp.Sequence[str], Arg(nargs="+", completer=xontrib_names_completer), ] = (), verbose=False, ): """Load xontribs from a list of names Parameters ---------- names names of xontribs verbose : -v, --verbose verbose output """ ctx = XSH.ctx res = ExitCode.OK for name in names: if verbose: print(f"loading xontrib {name!r}") try: update_context(name, ctx=ctx) except Exception: res = ExitCode.INIT_FAILED print_exception(f"Failed to load xontrib {name}.") if hasattr(update_context, "bad_imports"): res = ExitCode.NOT_FOUND prompt_xontrib_install(update_context.bad_imports) # type: ignore del update_context.bad_imports # type: ignore return res
def wrapped_simple_command(args, stdin, stdout, stderr): try: i = stdin.read() if bgable: with redirect_stdout(stdout), redirect_stderr(stderr): r = f(args, i) else: r = f(args, i) cmd_result = 0 if isinstance(r, str): stdout.write(r) elif isinstance(r, Sequence): if r[0] is not None: stdout.write(r[0]) if r[1] is not None: stderr.write(r[1]) if len(r) > 2 and r[2] is not None: cmd_result = r[2] elif r is not None: stdout.write(str(r)) return cmd_result except Exception: print_exception() return 1 # returncode for failure
def compile(self, src): """Compiles source code and returns the (possibly modified) source and a valid code object. """ _cache = should_use_cache(self.execer, 'single') if _cache: codefname = code_cache_name(src) cachefname = get_cache_filename(codefname, code=True) usecache, code = code_cache_check(cachefname) if usecache: self.reset_buffer() return src, code try: code = self.execer.compile(src, mode='single', glbs=self.ctx, locs=None) if _cache: update_cache(code, cachefname) self.reset_buffer() except SyntaxError: partial_string_info = check_for_partial_string(src) in_partial_string = (partial_string_info[0] is not None and partial_string_info[1] is None) if (src == '\n' or src.endswith('\n\n')) and not in_partial_string: self.reset_buffer() print_exception() return src, None self.need_more_lines = True except Exception: # pylint: disable=broad-except self.reset_buffer() print_exception() return src, None return src, code
def wrapped_simple_command(args, stdin, stdout, stderr): try: i = stdin.read() if bgable: with redirect_stdout(stdout), redirect_stderr(stderr): r = f(args, i) else: r = f(args, i) cmd_result = 0 if isinstance(r, str): stdout.write(r) elif isinstance(r, abc.Sequence): if r[0] is not None: stdout.write(r[0]) if r[1] is not None: stderr.write(r[1]) if len(r) > 2 and r[2] is not None: cmd_result = r[2] elif r is not None: stdout.write(str(r)) return cmd_result except Exception: print_exception() return 1 # returncode for failure
def push(self, line): """Pushes a line onto the buffer and compiles the code in a way that enables multiline input. """ code = None self.buffer.append(line) if self.need_more_lines: return None, code src = ''.join(self.buffer) try: code = self.execer.compile(src, mode='single', glbs=None, locs=self.ctx) self.reset_buffer() except SyntaxError: if line == '\n': self.reset_buffer() print_exception() return src, None self.need_more_lines = True except Exception: # pylint: disable=broad-except self.reset_buffer() print_exception() return src, None return src, code
def fire(self, **kwargs): """ Fires an event, calling registered handlers with the given arguments. A non-unique iterable of the results is returned. Each handler is called immediately. Exceptions are turned in to warnings. Parameters ---------- **kwargs : Keyword arguments to pass to each handler Returns ------- vals : iterable Return values of each handler. If multiple handlers return the same value, it will appear multiple times. """ vals = [] for handler in self._filterhandlers(self._handlers, **kwargs): try: rv = handler(**kwargs) except Exception: print_exception("Exception raised in event handler; ignored.") else: vals.append(rv) return vals
def _wizard(ns): env = builtins.__xonsh__.env shell = builtins.__xonsh__.shell.shell fname = env.get("XONSHRC")[-1] if ns.file is None else ns.file no_wiz = os.path.join(env.get("XONSH_CONFIG_DIR"), "no-wizard") w = make_xonfig_wizard( default_file=fname, confirm=ns.confirm, no_wizard_file=no_wiz ) tempenv = {"PROMPT": "", "XONSH_STORE_STDOUT": False} pv = wiz.PromptVisitor(w, store_in_history=False, multiline=False) @contextlib.contextmanager def force_hide(): if env.get("XONSH_STORE_STDOUT") and hasattr(shell, "_force_hide"): orig, shell._force_hide = shell._force_hide, False yield shell._force_hide = orig else: yield with force_hide(), env.swap(tempenv): try: pv.visit() except (KeyboardInterrupt, Exception): print() print_exception()
def _get_prompt_tokens(self, env_name: str, prompt_name: str, **kwargs): env = XSH.env # type:ignore p = env.get(env_name) if not p and "default" in kwargs: return kwargs.pop("default") try: p = self.prompt_formatter(template=p, threaded=env["ENABLE_ASYNC_PROMPT"], prompt_name=prompt_name) except Exception: # pylint: disable=broad-except print_exception() p, osc_tokens = remove_ansi_osc(p) if kwargs.get("handle_osc_tokens"): # handle OSC tokens for osc in osc_tokens: if osc[2:4] == "0;": env["TITLE"] = osc[4:-1] else: print(osc, file=sys.__stdout__, flush=True) toks = partial_color_tokenize(p) return tokenize_ansi(PygmentsTokens(toks))
def prompt(self): """Obtains the current prompt string.""" global RL_LIB, RL_CAN_RESIZE if RL_CAN_RESIZE: # This is needed to support some system where line-wrapping doesn't # work. This is a bug in upstream Python, or possibly readline. RL_LIB.rl_reset_screen_size() if self.need_more_lines: if self.mlprompt is None: try: self.mlprompt = multiline_prompt(curr=self._current_prompt) except Exception: # pylint: disable=broad-except print_exception() self.mlprompt = '<multiline prompt error> ' return self.mlprompt env = builtins.__xonsh_env__ # pylint: disable=no-member p = env.get('PROMPT') try: p = partial_format_prompt(p) except Exception: # pylint: disable=broad-except print_exception() hide = True if self._force_hide is None else self._force_hide p = ansi_partial_color_format(p, style=env.get('XONSH_COLOR_STYLE'), hide=hide) self._current_prompt = p self.settitle() return p
def transform_command(src, show_diff=True): """Returns the results of firing the precommand handles.""" i = 0 limit = sys.getrecursionlimit() lst = "" raw = src while src != lst: lst = src srcs = events.on_transform_command.fire(cmd=src) for s in srcs: if s != lst: src = s break i += 1 if i == limit: print_exception("Modifications to source input took more than " "the recursion limit number of iterations to " "converge.") debug_level = builtins.__xonsh__.env.get("XONSH_DEBUG") if show_diff and debug_level > 1 and src != raw: sys.stderr.writelines( difflib.unified_diff( raw.splitlines(keepends=True), src.splitlines(keepends=True), fromfile="before precommand event", tofile="after precommand event", )) return src
def wait(self, timeout=None): """Runs the function and returns the result. Timeout argument only present for API compatibility. """ if self.f is None: return 0 env = XSH.env enc = env.get("XONSH_ENCODING") err = env.get("XONSH_ENCODING_ERRORS") spec = self._wait_and_getattr("spec") # set file handles if self.stdin is None: stdin = None else: if isinstance(self.stdin, int): inbuf = open(self.stdin, "rb", -1) else: inbuf = self.stdin stdin = io.TextIOWrapper(inbuf, encoding=enc, errors=err) stdout = self._pick_buf(self.stdout, sys.stdout, enc, err) stderr = self._pick_buf(self.stderr, sys.stderr, enc, err) # run the actual function try: with XSH.env.swap(self.env): r = self.f(self.args, stdin, stdout, stderr, spec, spec.stack) except Exception: xt.print_exception() r = 1 self.returncode = parse_proxy_return(r, stdout, stderr) safe_flush(stdout) safe_flush(stderr) return self.returncode
def _wizard(ns): env = builtins.__xonsh__.env shell = builtins.__xonsh__.shell.shell fname = env.get("XONSHRC")[-1] if ns.file is None else ns.file no_wiz = os.path.join(env.get("XONSH_CONFIG_DIR"), "no-wizard") w = make_xonfig_wizard(default_file=fname, confirm=ns.confirm, no_wizard_file=no_wiz) tempenv = {"PROMPT": "", "XONSH_STORE_STDOUT": False} pv = wiz.PromptVisitor(w, store_in_history=False, multiline=False) @contextlib.contextmanager def force_hide(): if env.get("XONSH_STORE_STDOUT") and hasattr(shell, "_force_hide"): orig, shell._force_hide = shell._force_hide, False yield shell._force_hide = orig else: yield with force_hide(), env.swap(tempenv): try: pv.visit() except (KeyboardInterrupt, Exception): print() print_exception()
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 default(self, line): """Implements code execution.""" line = line if line.endswith('\n') else line + '\n' src, code = self.push(line) if code is None: return hist = builtins.__xonsh_history__ # pylint: disable=no-member ts1 = None store_stdout = builtins.__xonsh_env__.get('XONSH_STORE_STDOUT') # pylint: disable=no-member tee = Tee() if store_stdout else io.StringIO() try: ts0 = time.time() run_compiled_code(code, self.ctx, None, 'single') ts1 = time.time() if hist.last_cmd_rtn is None: hist.last_cmd_rtn = 0 # returncode for success except XonshError as e: print(e.args[0], file=sys.stderr) if hist.last_cmd_rtn is None: hist.last_cmd_rtn = 1 # return code for failure except Exception: # pylint: disable=broad-except print_exception() if hist.last_cmd_rtn is None: hist.last_cmd_rtn = 1 # return code for failure finally: ts1 = ts1 or time.time() self._append_history(inp=src, ts=[ts0, ts1], tee_out=tee.getvalue()) tee.close() if builtins.__xonsh_exit__: # pylint: disable=no-member return True
def fire_precommand(src, show_diff=True): """Returns the results of firing the precommand handles.""" i = 0 limit = sys.getrecursionlimit() lst = '' raw = src while src != lst: lst = src srcs = events.on_precommand.fire(src) for s in srcs: if s != lst: src = s break i += 1 if i == limit: print_exception('Modifcations to source input took more than ' 'the recursion limit number of interations to ' 'converge.') debug_level = builtins.__xonsh_env__.get('XONSH_DEBUG') if show_diff and debug_level > 1 and src != raw: sys.stderr.writelines(difflib.unified_diff( raw.splitlines(keepends=True), src.splitlines(keepends=True), fromfile='before precommand event', tofile='after precommand event', )) return src
def prompt(self): """Obtains the current prompt string.""" global RL_LIB, RL_CAN_RESIZE if RL_CAN_RESIZE: # This is needed to support some system where line-wrapping doesn't # work. This is a bug in upstream Python, or possibly readline. RL_LIB.rl_reset_screen_size() #return super().prompt if self.need_more_lines: if self.mlprompt is None: try: self.mlprompt = multiline_prompt(curr=self._current_prompt) except Exception: # pylint: disable=broad-except print_exception() self.mlprompt = '<multiline prompt error> ' return self.mlprompt env = builtins.__xonsh_env__ # pylint: disable=no-member p = env.get('PROMPT') try: p = partial_format_prompt(p) except Exception: # pylint: disable=broad-except print_exception() p = partial_color_format(p, style=env.get('XONSH_COLOR_STYLE'), hide=True) self._current_prompt = p self.settitle() return p
def fire_precommand(src, show_diff=True): """Returns the results of firing the precommand handles.""" i = 0 limit = sys.getrecursionlimit() lst = '' raw = src while src != lst: lst = src srcs = events.on_precommand.fire(src) for s in srcs: if s != lst: src = s break i += 1 if i == limit: print_exception('Modifcations to source input took more than ' 'the recursion limit number of interations to ' 'converge.') if show_diff and builtins.__xonsh_env__.get('XONSH_DEBUG') and src != raw: sys.stderr.writelines( difflib.unified_diff( raw.splitlines(keepends=True), src.splitlines(keepends=True), fromfile='before precommand event', tofile='after precommand event', )) return src
def __init__(self, specs): """ Parameters ---------- specs : list of SubprocSpec Process specifications Attributes ---------- spec : SubprocSpec The last specification in specs proc : Popen-like The process in procs ended : bool Boolean for if the command has stopped executing. input : str A string of the standard input. output : str A string of the standard output. errors : str A string of the standard error. lines : list of str The output lines starttime : floats or None Pipeline start timestamp. """ self.starttime = None self.ended = False self.procs = [] self.specs = specs self.spec = specs[-1] self.captured = specs[-1].captured self.input = self._output = self.errors = self.endtime = None self._closed_handle_cache = {} self.lines = [] self._raw_output = self._raw_error = b"" self._stderr_prefix = self._stderr_postfix = None self.term_pgid = None background = self.spec.background pipeline_group = None for spec in specs: if self.starttime is None: self.starttime = time.time() try: proc = spec.run(pipeline_group=pipeline_group) except Exception: xt.print_exception() self._return_terminal() self.proc = None return if (proc.pid and pipeline_group is None and not spec.is_proxy and self.captured != "object"): pipeline_group = proc.pid if update_fg_process_group(pipeline_group, background): self.term_pgid = pipeline_group self.procs.append(proc) self.proc = self.procs[-1]
def _no_cache_field_value(self, field, field_value, **_): try: value = field_value() if callable(field_value) else field_value self.cache[field_value] = value except Exception: print("prompt: error: on field {!r}" "".format(field), file=sys.stderr) xt.print_exception() value = "{{BACKGROUND_RED}}{{ERROR:{}}}{{RESET}}".format(field) return value
def default(self, line, raw_line=None): """Implements code execution.""" line = line if line.endswith("\n") else line + "\n" if not self.need_more_lines: # this is the first line if not raw_line: self.src_starts_with_space = False else: self.src_starts_with_space = raw_line[0].isspace() src, code = self.push(line) if code is None: return events.on_precommand.fire(cmd=src) env = XSH.env hist = XSH.history # pylint: disable=no-member ts1 = None enc = env.get("XONSH_ENCODING") err = env.get("XONSH_ENCODING_ERRORS") tee = Tee(encoding=enc, errors=err) ts0 = time.time() try: exc_info = run_compiled_code(code, self.ctx, None, "single") if exc_info != (None, None, None): raise exc_info[1] ts1 = time.time() if hist is not None and hist.last_cmd_rtn is None: hist.last_cmd_rtn = 0 # returncode for success except XonshError as e: print(e.args[0], file=sys.stderr) if hist is not None and hist.last_cmd_rtn is None: hist.last_cmd_rtn = 1 # return code for failure except (SystemExit, KeyboardInterrupt) as err: raise err except BaseException: print_exception(exc_info=exc_info) if hist is not None and hist.last_cmd_rtn is None: hist.last_cmd_rtn = 1 # return code for failure finally: ts1 = ts1 or time.time() tee_out = tee.getvalue() self._append_history( inp=src, ts=[ts0, ts1], spc=self.src_starts_with_space, tee_out=tee_out, cwd=self.precwd, ) self.accumulated_inputs += src if (tee_out and env.get("XONSH_APPEND_NEWLINE") and not tee_out.endswith(os.linesep)): print(os.linesep, end="") tee.close() self._fix_cwd() if XSH.exit: # pylint: disable=no-member return True
def _get_field_value(self, field, **_): try: return self.fields.pick(field) except Exception: # noqa print("prompt: error: on field {!r}" "".format(field), file=sys.stderr) xt.print_exception() value = f"{{BACKGROUND_RED}}{{ERROR:{field}}}{{RESET}}" return value
def prompt_tokens(self, cli): """Returns a list of (token, str) tuples for the current prompt.""" p = builtins.__xonsh_env__.get('PROMPT') try: p = partial_format_prompt(p) except Exception: # pylint: disable=broad-except print_exception() toks = partial_color_tokenize(p) self.settitle() return toks
def _failover_template_format(template): if callable(template): try: # Exceptions raises from function of producing $PROMPT # in user's xonshrc should not crash xonsh return template() except Exception: xt.print_exception() return "$ " return template
def prompt_tokens(self, cli): """Returns a list of (token, str) tuples for the current prompt.""" p = builtins.__xonsh_env__.get('PROMPT') try: p = self.prompt_formatter(p) except Exception: # pylint: disable=broad-except print_exception() toks = partial_color_tokenize(p) self.settitle() return toks
def _wizard(ns): env = builtins.__xonsh_env__ fname = env.get('XONSHCONFIG') if ns.file is None else ns.file wiz = make_wizard(default_file=fname, confirm=ns.confirm) tempenv = {'PROMPT': '', 'XONSH_STORE_STDOUT': False} pv = PromptVisitor(wiz, store_in_history=False, multiline=False) with env.swap(tempenv): try: pv.visit() except (KeyboardInterrupt, Exception): tools.print_exception()
def _get_field_value(self, field): field_value = self.fields[field] if field_value in self.cache: return self.cache[field_value] try: value = field_value() if callable(field_value) else field_value self.cache[field_value] = value except Exception: print("prompt: error: on field {!r}" "".format(field), file=sys.stderr) xt.print_exception() value = "(ERROR:{})".format(field) return value
def bottom_toolbar_tokens(self): """Returns a list of (token, str) tuples for the current bottom toolbar. """ p = builtins.__xonsh__.env.get("BOTTOM_TOOLBAR") if not p: return try: p = self.prompt_formatter(p) except Exception: # pylint: disable=broad-except print_exception() toks = partial_color_tokenize(p) return PygmentsTokens(toks)
def prompt_tokens(self): """Returns a list of (token, str) tuples for the current prompt.""" p = builtins.__xonsh__.env.get("PROMPT") try: p = self.prompt_formatter(p) except Exception: # pylint: disable=broad-except print_exception() toks = partial_color_tokenize(p) if self._first_prompt: carriage_return() self._first_prompt = False self.settitle() return PygmentsTokens(toks)
def rprompt_tokens(self, cli): """Returns a list of (token, str) tuples for the current right prompt. """ p = builtins.__xonsh_env__.get('RIGHT_PROMPT') if len(p) == 0: return [] try: p = partial_format_prompt(p) except Exception: # pylint: disable=broad-except print_exception() toks = partial_color_tokenize(p) return toks
def wrapper(*args, **kwargs): rootdir = os.path.splitdrive(os.getcwd())[0] + '\\' os.chdir(rootdir) try: out = func(*args, **kwargs) finally: try: pwd = env.get('PWD', rootdir) os.chdir(pwd) except (FileNotFoundError, NotADirectoryError): print_exception() newpath = _chdir_up(pwd) builtins.__xonsh_env__['PWD'] = newpath raise KeyboardInterrupt return out
def wrapper(*args, **kwargs): anchor = Path(os.getcwd()).anchor os.chdir(anchor) try: out = func(*args, **kwargs) finally: try: pwd = env.get('PWD', anchor) os.chdir(pwd) except (FileNotFoundError, NotADirectoryError): print_exception() newpath = _chdir_up(pwd) builtins.__xonsh_env__['PWD'] = newpath raise KeyboardInterrupt return out
def setup_readline(): """Sets up the readline module and completion suppression, if available.""" global RL_COMPLETION_SUPPRESS_APPEND, RL_LIB, RL_CAN_RESIZE, RL_STATE, readline if RL_COMPLETION_SUPPRESS_APPEND is not None: return try: import readline except ImportError: return import ctypes import ctypes.util readline.set_completer_delims(' \t\n') # Cygwin seems to hang indefinitely when querying the readline lib if (not ON_CYGWIN) and (not readline.__file__.endswith('.py')): RL_LIB = lib = ctypes.cdll.LoadLibrary(readline.__file__) try: RL_COMPLETION_SUPPRESS_APPEND = ctypes.c_int.in_dll( lib, 'rl_completion_suppress_append') except ValueError: # not all versions of readline have this symbol, ie Macs sometimes RL_COMPLETION_SUPPRESS_APPEND = None try: RL_STATE = ctypes.c_int.in_dll(lib, 'rl_readline_state') except: pass RL_CAN_RESIZE = hasattr(lib, 'rl_reset_screen_size') env = builtins.__xonsh_env__ # reads in history readline.set_history_length(-1) ReadlineHistoryAdder() # sets up IPython-like history matching with up and down readline.parse_and_bind('"\e[B": history-search-forward') readline.parse_and_bind('"\e[A": history-search-backward') # Setup Shift-Tab to indent readline.parse_and_bind('"\e[Z": "{0}"'.format(env.get('INDENT'))) # handle tab completion differences found in libedit readline compatibility # as discussed at http://stackoverflow.com/a/7116997 if readline.__doc__ and 'libedit' in readline.__doc__: readline.parse_and_bind("bind ^I rl_complete") else: readline.parse_and_bind("tab: complete") # try to load custom user settings try: readline.read_init_file() except Exception: # this seems to fail with libedit print_exception('xonsh: could not load readline default init file.')
def rprompt_tokens(self, cli): """Returns a list of (token, str) tuples for the current right prompt. """ p = builtins.__xonsh_env__.get('RIGHT_PROMPT') # partial_format_prompt does handle empty strings properly, # but this avoids descending into it in the common case of # $RIGHT_PROMPT == ''. if isinstance(p, str) and len(p) == 0: return [] try: p = partial_format_prompt(p) except Exception: # pylint: disable=broad-except print_exception() toks = partial_color_tokenize(p) return toks
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 bottom_toolbar_tokens(self, cli): """Returns a list of (token, str) tuples for the current bottom toolbar. """ p = builtins.__xonsh_env__.get('BOTTOM_TOOLBAR') # self.prompt_formatter does handle empty strings properly, # but this avoids descending into it in the common case of # $TOOLBAR == ''. if isinstance(p, str) and len(p) == 0: return [] try: p = self.prompt_formatter(p) except Exception: # pylint: disable=broad-except print_exception() toks = partial_color_tokenize(p) return toks
def load_static_config(ctx, config=None): """Loads a static configuration file from a given context, rather than the current environment. Optionally may pass in configuration file name. """ env = {} env['XDG_CONFIG_HOME'] = ctx.get('XDG_CONFIG_HOME', DEFAULT_VALUES['XDG_CONFIG_HOME']) env['XONSH_CONFIG_DIR'] = ctx['XONSH_CONFIG_DIR'] if 'XONSH_CONFIG_DIR' in ctx \ else xonsh_config_dir(env) if config is not None: env['XONSHCONFIG'] = ctx['XONSHCONFIG'] = config elif 'XONSHCONFIG' in ctx: config = env['XONSHCONFIG'] = ctx['XONSHCONFIG'] else: # don't set in ctx in order to maintain default config = env['XONSHCONFIG'] = xonshconfig(env) if os.path.isfile(config): # Note that an Env instance at __xonsh_env__ has not been started yet, # per se, so we have to use os.environ encoding = os.environ.get('XONSH_ENCODING', DEFAULT_VALUES.get('XONSH_ENCODING', 'utf8')) errors = os.environ.get('XONSH_ENCODING_ERRORS', DEFAULT_VALUES.get('XONSH_ENCODING_ERRORS', 'surrogateescape')) with open(config, 'r', encoding=encoding, errors=errors) as f: try: conf = json.load(f) assert isinstance(conf, cabc.Mapping) ctx['LOADED_CONFIG'] = True except Exception as e: conf = {} ctx['LOADED_CONFIG'] = False print_exception() # JSONDecodeError was added in Python v3.5 jerr = json.JSONDecodeError \ if hasattr(json, 'JSONDecodeError') else ValueError if isinstance(e, jerr): msg = 'Xonsh config file is not valid JSON.' else: msg = 'Could not load xonsh config.' print(msg, file=sys.stderr) else: conf = {} ctx['LOADED_CONFIG'] = False builtins.__xonsh_config__ = conf return conf
def prompt(self): """Obtains the current prompt string.""" if self.need_more_lines: if self.mlprompt is None: try: self.mlprompt = multiline_prompt() except Exception: print_exception() self.mlprompt = '<multiline prompt error> ' return self.mlprompt env = builtins.__xonsh_env__ p = env.get('PROMPT') try: p = format_prompt(p) except Exception: print_exception() self.settitle() return p
def _push(self, line): """Pushes a line onto the buffer and compiles the code in a way that enables multiline input. """ code = None self.buffer.append(line) if self.need_more_lines: return None, code src = "".join(self.buffer) src = transform_command(src) try: code = self.execer.compile(src, mode="single", glbs=self.ctx, locs=None) self.reset_buffer() except Exception: # pylint: disable=broad-except self.reset_buffer() print_exception() return src, None return src, code
def wrapped_simple_command(args, stdin, stdout, stderr): try: i = stdin.read() with redirect_stdout(stdout), redirect_stderr(stderr): r = f(args, i) if isinstance(r, str): stdout.write(r) elif isinstance(r, Sequence): if r[0] is not None: stdout.write(r[0]) if r[1] is not None: stderr.write(r[1]) elif r is not None: stdout.write(str(r)) return 0 # returncode for succees except Exception: print_exception() return 1 # returncode for failure
def prompt(self): """Obtains the current prompt string.""" if self.need_more_lines: if self.mlprompt is None: try: self.mlprompt = multiline_prompt() except Exception: # pylint: disable=broad-except print_exception() self.mlprompt = '<multiline prompt error> ' return self.mlprompt env = builtins.__xonsh_env__ # pylint: disable=no-member p = env.get('PROMPT') try: p = partial_format_prompt(p) except Exception: # pylint: disable=broad-except print_exception() self.settitle() return p