def load_builtins(execer=None, config=None, login=False, ctx=None): """Loads the xonsh builtins into the Python builtins. Sets the BUILTINS_LOADED variable to True. """ global BUILTINS_LOADED # private built-ins builtins.__xonsh_config__ = {} builtins.__xonsh_env__ = env = Env(default_env(config=config, login=login)) builtins.__xonsh_help__ = helper builtins.__xonsh_superhelp__ = superhelper builtins.__xonsh_pathsearch__ = pathsearch builtins.__xonsh_globsearch__ = globsearch builtins.__xonsh_regexsearch__ = regexsearch builtins.__xonsh_glob__ = globpath builtins.__xonsh_expand_path__ = expand_path builtins.__xonsh_exit__ = False builtins.__xonsh_stdout_uncaptured__ = None builtins.__xonsh_stderr_uncaptured__ = None if hasattr(builtins, 'exit'): builtins.__xonsh_pyexit__ = builtins.exit del builtins.exit if hasattr(builtins, 'quit'): builtins.__xonsh_pyquit__ = builtins.quit del builtins.quit builtins.__xonsh_subproc_captured_stdout__ = subproc_captured_stdout builtins.__xonsh_subproc_captured_inject__ = subproc_captured_inject builtins.__xonsh_subproc_captured_object__ = subproc_captured_object builtins.__xonsh_subproc_captured_hiddenobject__ = subproc_captured_hiddenobject builtins.__xonsh_subproc_uncaptured__ = subproc_uncaptured builtins.__xonsh_execer__ = execer builtins.__xonsh_commands_cache__ = CommandsCache() builtins.__xonsh_all_jobs__ = {} builtins.__xonsh_ensure_list_of_strs__ = ensure_list_of_strs builtins.__xonsh_list_of_strs_or_callables__ = list_of_strs_or_callables builtins.__xonsh_completers__ = xonsh.completers.init.default_completers() builtins.__xonsh_call_macro__ = call_macro builtins.__xonsh_enter_macro__ = enter_macro # public built-ins builtins.XonshError = XonshError builtins.XonshBlockError = XonshBlockError builtins.XonshCalledProcessError = XonshCalledProcessError builtins.evalx = None if execer is None else execer.eval builtins.execx = None if execer is None else execer.exec builtins.compilex = None if execer is None else execer.compile builtins.events = events # sneak the path search functions into the aliases # Need this inline/lazy import here since we use locate_binary that relies on __xonsh_env__ in default aliases builtins.default_aliases = builtins.aliases = Aliases(make_default_aliases()) if login: builtins.aliases.update(load_foreign_aliases(issue_warning=False)) # history needs to be started after env and aliases # would be nice to actually include non-detyped versions. builtins.__xonsh_history__ = History(env=env.detype(), ts=[time.time(), None], locked=True) atexit.register(_lastflush) for sig in AT_EXIT_SIGNALS: resetting_signal_handle(sig, _lastflush) BUILTINS_LOADED = True
def load_builtins(execer=None, ctx=None): """Loads the xonsh builtins into the Python builtins. Sets the BUILTINS_LOADED variable to True. """ global BUILTINS_LOADED # private built-ins builtins.__xonsh_config__ = {} builtins.__xonsh_env__ = Env(default_env()) builtins.__xonsh_help__ = helper builtins.__xonsh_superhelp__ = superhelper builtins.__xonsh_pathsearch__ = pathsearch builtins.__xonsh_globsearch__ = globsearch builtins.__xonsh_regexsearch__ = regexsearch builtins.__xonsh_glob__ = globpath builtins.__xonsh_expand_path__ = expand_path builtins.__xonsh_exit__ = False builtins.__xonsh_stdout_uncaptured__ = None builtins.__xonsh_stderr_uncaptured__ = None if hasattr(builtins, "exit"): builtins.__xonsh_pyexit__ = builtins.exit del builtins.exit if hasattr(builtins, "quit"): builtins.__xonsh_pyquit__ = builtins.quit del builtins.quit builtins.__xonsh_subproc_captured_stdout__ = subproc_captured_stdout builtins.__xonsh_subproc_captured_inject__ = subproc_captured_inject builtins.__xonsh_subproc_captured_object__ = subproc_captured_object builtins.__xonsh_subproc_captured_hiddenobject__ = subproc_captured_hiddenobject builtins.__xonsh_subproc_uncaptured__ = subproc_uncaptured builtins.__xonsh_execer__ = execer builtins.__xonsh_commands_cache__ = CommandsCache() builtins.__xonsh_all_jobs__ = {} builtins.__xonsh_ensure_list_of_strs__ = ensure_list_of_strs builtins.__xonsh_list_of_strs_or_callables__ = list_of_strs_or_callables builtins.__xonsh_list_of_list_of_strs_outer_product__ = ( list_of_list_of_strs_outer_product) builtins.__xonsh_completers__ = xonsh.completers.init.default_completers() builtins.__xonsh_call_macro__ = call_macro builtins.__xonsh_enter_macro__ = enter_macro builtins.__xonsh_path_literal__ = path_literal # public built-ins builtins.XonshError = XonshError builtins.XonshCalledProcessError = XonshCalledProcessError builtins.evalx = None if execer is None else execer.eval builtins.execx = None if execer is None else execer.exec builtins.compilex = None if execer is None else execer.compile builtins.events = events # sneak the path search functions into the aliases # Need this inline/lazy import here since we use locate_binary that # relies on __xonsh_env__ in default aliases builtins.default_aliases = builtins.aliases = Aliases( make_default_aliases()) builtins.__xonsh_history__ = None atexit.register(_lastflush) for sig in AT_EXIT_SIGNALS: resetting_signal_handle(sig, _lastflush) BUILTINS_LOADED = True
def session_vars(): """keep costly vars per session""" from xonsh.environ import Env, default_env from xonsh.commands_cache import CommandsCache return { "execer": Execer(unload=False), "env": Env(default_env()), "commands_cache": CommandsCache(), }
def load(self, execer=None, ctx=None): """Loads the session with default values. Parameters ---------- execer : Execer, optional Xonsh execution object, may be None to start ctx : Mapping, optional Context to start xonsh session with. """ if ctx is not None: self.ctx = ctx self.env = Env(default_env()) self.help = helper self.superhelp = superhelper self.pathsearch = pathsearch self.globsearch = globsearch self.regexsearch = regexsearch self.glob = globpath self.expand_path = expand_path self.exit = False self.stdout_uncaptured = None self.stderr_uncaptured = None if hasattr(builtins, "exit"): self.pyexit = builtins.exit del builtins.exit if hasattr(builtins, "quit"): self.pyquit = builtins.quit del builtins.quit self.subproc_captured_stdout = subproc_captured_stdout self.subproc_captured_lines = subproc_captured_lines self.subproc_captured_inject = subproc_captured_inject self.subproc_captured_object = subproc_captured_object self.subproc_captured_hiddenobject = subproc_captured_hiddenobject self.subproc_uncaptured = subproc_uncaptured self.execer = execer self.commands_cache = CommandsCache() self.all_jobs = {} self.ensure_list_of_strs = ensure_list_of_strs self.list_of_strs_or_callables = list_of_strs_or_callables self.list_of_list_of_strs_outer_product = list_of_list_of_strs_outer_product self.eval_fstring_field = eval_fstring_field self.completers = xonsh.completers.init.default_completers() self.call_macro = call_macro self.enter_macro = enter_macro self.path_literal = path_literal self.builtins = _BuiltIns(execer) self.history = None self.shell = None
def load(self, execer=None, ctx=None, **kwargs): """Loads the session with default values. Parameters ---------- execer : Execer, optional Xonsh execution object, may be None to start ctx : Mapping, optional Context to start xonsh session with. """ from xonsh.commands_cache import CommandsCache from xonsh.completers.init import default_completers from xonsh.environ import Env, default_env if not hasattr(builtins, "__xonsh__"): builtins.__xonsh__ = self if ctx is not None: self.ctx = ctx self.env = kwargs.pop("env") if "env" in kwargs else Env(default_env()) self.exit = False self.stdout_uncaptured = None self.stderr_uncaptured = None self._disable_python_exit() self.execer = execer self.commands_cache = (kwargs.pop("commands_cache") if "commands_cache" in kwargs else CommandsCache()) self.modules_cache = {} self.all_jobs = {} self.completers = default_completers(self.commands_cache) self.builtins = get_default_builtins(execer) self._initial_builtin_names = frozenset(vars(self.builtins)) aliases_given = kwargs.pop("aliases", None) for attr, value in kwargs.items(): if hasattr(self, attr): setattr(self, attr, value) self.link_builtins(aliases_given) self.builtins_loaded = True def flush_on_exit(s=None, f=None): if self.history is not None: self.history.flush(at_exit=True) atexit.register(flush_on_exit) # Add one-shot handler for exit for sig in AT_EXIT_SIGNALS: resetting_signal_handle(sig, flush_on_exit)
def test_commands_cache_predictor_default(args): cc = CommandsCache() use_tty, patterns = args f = open('testfile', 'wb') where = list(patterns.keys()) where.sort() pos = 0 for w in where: f.write(b'\x20' * (w - pos)) f.write(patterns[w]) pos = w + len(patterns[w]) f.write(b'\x20' * (pos // 2)) f.close() result = cc.default_predictor_readbin('', os.getcwd() + os.sep + 'testfile', timeout=1, failure=None) expected = predict_false if use_tty else predict_true assert result == expected
def session_vars(): """keep costly vars per session""" from xonsh.environ import Env, default_env from xonsh.commands_cache import CommandsCache execer = Execer() XSH.load(execer=execer) return { "execer": execer, "env": Env(default_env()), "commands_cache": CommandsCache(), }
def __init__(self, *args, **kwargs): # If the lexor is loaded as a pygment plugin, we have to mock # __xonsh_env__ and __xonsh_commands_cache__ if not hasattr(builtins, "__xonsh_env__"): setattr(builtins, "__xonsh_env__", {}) if ON_WINDOWS: pathext = os_environ.get("PATHEXT", [".EXE", ".BAT", ".CMD"]) builtins.__xonsh_env__["PATHEXT"] = pathext.split(os.pathsep) if not hasattr(builtins, "__xonsh_commands_cache__"): setattr(builtins, "__xonsh_commands_cache__", CommandsCache()) _ = builtins.__xonsh_commands_cache__.all_commands # NOQA super().__init__(*args, **kwargs)
def test_commands_cache_predictor_default(args): cc = CommandsCache() use_tty, patterns = args f = open("testfile", "wb") where = list(patterns.keys()) where.sort() pos = 0 for w in where: f.write(b"\x20" * (w - pos)) f.write(patterns[w]) pos = w + len(patterns[w]) f.write(b"\x20" * (pos // 2)) f.close() result = cc.default_predictor_readbin( "", os.getcwd() + os.sep + "testfile", timeout=1, failure=None ) expected = predict_false if use_tty else predict_true assert result == expected
def __init__(self, *args, **kwargs): # If the lexor is loaded as a pygment plugin, we have to mock # __xonsh_env__ and __xonsh_commands_cache__ if not hasattr(builtins, '__xonsh_env__'): setattr(builtins, '__xonsh_env__', {}) if ON_WINDOWS: pathext = os.environ.get('PATHEXT', ['.EXE', '.BAT', '.CMD']) builtins.__xonsh_env__['PATHEXT'] = pathext.split(os.pathsep) if not hasattr(builtins, '__xonsh_commands_cache__'): setattr(builtins, '__xonsh_commands_cache__', CommandsCache()) _ = builtins.__xonsh_commands_cache__.all_commands # NOQA super().__init__(*args, **kwargs)
def test_locate_binary_on_windows(xonsh_builtins): files = ("file1.exe", "FILE2.BAT", "file3.txt") with TemporaryDirectory() as tmpdir: for fname in files: fpath = os.path.join(tmpdir, fname) with open(fpath, "w") as f: f.write(fpath) xonsh_builtins.__xonsh__.env.update( {"PATH": [tmpdir], "PATHEXT": [".COM", ".EXE", ".BAT"]} ) xonsh_builtins.__xonsh__.commands_cache = CommandsCache() assert locate_binary("file1") == os.path.join(tmpdir, "file1.exe") assert locate_binary("file1.exe") == os.path.join(tmpdir, "file1.exe") assert locate_binary("file2") == os.path.join(tmpdir, "FILE2.BAT") assert locate_binary("file2.bat") == os.path.join(tmpdir, "FILE2.BAT") assert locate_binary("file3") is None
def __init__(self, *args, **kwargs): # If the lexer is loaded as a pygment plugin, we have to mock # __xonsh__.env and __xonsh__.commands_cache if not hasattr(builtins, "__xonsh__"): from argparse import Namespace setattr(builtins, "__xonsh__", Namespace()) if not hasattr(builtins.__xonsh__, "env"): setattr(builtins.__xonsh__, "env", {}) if ON_WINDOWS: pathext = os_environ.get("PATHEXT", [".EXE", ".BAT", ".CMD"]) builtins.__xonsh__.env["PATHEXT"] = pathext.split(os.pathsep) if not hasattr(builtins.__xonsh__, "commands_cache"): setattr(builtins.__xonsh__, "commands_cache", CommandsCache()) _ = builtins.__xonsh__.commands_cache.all_commands # NOQA super().__init__(*args, **kwargs)
def test_locate_binary_on_windows(xonsh_builtins): files = ('file1.exe', 'FILE2.BAT', 'file3.txt') with TemporaryDirectory() as tmpdir: for fname in files: fpath = os.path.join(tmpdir, fname) with open(fpath, 'w') as f: f.write(fpath) xonsh_builtins.__xonsh_env__.update({ 'PATH': [tmpdir], 'PATHEXT': ['.COM', '.EXE', '.BAT'], }) xonsh_builtins.__xonsh_commands_cache__ = CommandsCache() assert locate_binary('file1') == os.path.join(tmpdir, 'file1.exe') assert locate_binary('file1.exe') == os.path.join(tmpdir, 'file1.exe') assert locate_binary('file2') == os.path.join(tmpdir, 'FILE2.BAT') assert locate_binary('file2.bat') == os.path.join(tmpdir, 'FILE2.BAT') assert locate_binary('file3') is None
def test_bash_is_only_functional_alias(xession): xession.env["PATH"] = os.environ["PATH"].split(os.pathsep) cc = CommandsCache() assert not cc.is_only_functional_alias("bash")
def test_bash_and_is_alias_is_only_functional_alias(xonsh_builtins): builtins.__xonsh_env__['PATH'] = os.environ['PATH'].split(os.pathsep) cc = CommandsCache() builtins.aliases['bash'] = lambda args: os.chdir(args[0]) assert not cc.is_only_functional_alias('bash')
'.. code-block:: python\n\n' ' from {fullmodname} import {var}\n\n' '{docstr}\n\n' '{rever_xsh}\n\n' '-------\n\n') for var in vars: title = var under = '.' * len(title) act = acts[var][1] docstr = inspect.getdoc(act) rever_xsh = make_activity_rever_xsh_example(act) s += sec.format(var=var, low=var.lower(), under=under, docstr=docstr, fullmodname=acts[var][0], rever_xsh=rever_xsh) s = s[:-9] fname = os.path.join(os.path.dirname(__file__), 'activitiesbody') with open(fname, 'w') as f: f.write(s) with environ.context(): make_activities() make_envvars() builtins.__xonsh_history__ = None builtins.__xonsh_env__ = {} builtins.__xonsh_commands_cache__ = CommandsCache()
def test_commands_cache_lazy(): cc = CommandsCache() assert not cc.lazyin('xonsh') assert 0 == len(list(cc.lazyiter())) assert 0 == cc.lazylen()
def test_bash_is_only_functional_alias(xonsh_builtins): builtins.__xonsh_env__['PATH'] = os.environ['PATH'].split(os.pathsep) cc = CommandsCache() assert not cc.is_only_functional_alias('bash')
for name in names: event = getattr(events, name) title = name docstr = inspect.getdoc(event) if docstr.startswith(name): # Assume the first line is a signature title, docstr = docstr.split("\n", 1) docstr = docstr.strip() under = "." * (len(title) + 4) s += sec.format(low=name.lower(), title=title, under=under, docstr=docstr) s = s[:-9] fname = os.path.join(os.path.dirname(__file__), "eventsbody") with open(fname, "w") as f: f.write(s) make_envvars() make_xontribs() make_events() builtins.__xonsh__.history = None builtins.__xonsh__.env = {} builtins.__xonsh__.commands_cache = CommandsCache() def setup(app): app.add_stylesheet("custom.css")
def test_commands_cache_lazy(xonsh_builtins): cc = CommandsCache() assert not cc.lazyin("xonsh") assert 0 == len(list(cc.lazyiter())) assert 0 == cc.lazylen()
def test_bash_is_only_functional_alias(xonsh_builtins): builtins.__xonsh__.env["PATH"] = os.environ["PATH"].split(os.pathsep) cc = CommandsCache() assert not cc.is_only_functional_alias("bash")
def test_cd_is_only_functional_alias(xonsh_builtins): cc = CommandsCache() builtins.aliases["cd"] = lambda args: os.chdir(args[0]) assert cc.is_only_functional_alias("cd")
def test_non_exist_is_only_functional_alias(xonsh_builtins): cc = CommandsCache() assert not cc.is_only_functional_alias("<not really a command name>")
def test_cd_is_only_functional_alias(xonsh_builtins): cc = CommandsCache() builtins.aliases["cd"] = lambda args: os.chdir(args[0]) assert cc.is_only_functional_alias("cd")
def test_commands_cache_lazy(xonsh_builtins): cc = CommandsCache() assert not cc.lazyin("xonsh") assert 0 == len(list(cc.lazyiter())) assert 0 == cc.lazylen()
def test_bash_and_is_alias_is_only_functional_alias(xonsh_builtins): builtins.__xonsh__.env["PATH"] = os.environ["PATH"].split(os.pathsep) cc = CommandsCache() builtins.aliases["bash"] = lambda args: os.chdir(args[0]) assert not cc.is_only_functional_alias("bash")
def test_bash_is_only_functional_alias(xonsh_builtins): builtins.__xonsh__.env["PATH"] = os.environ["PATH"].split(os.pathsep) cc = CommandsCache() assert not cc.is_only_functional_alias("bash")
def test_non_exist_is_only_functional_alias(xonsh_builtins): cc = CommandsCache() assert not cc.is_only_functional_alias("<not really a command name>")
def load(self, execer=None, ctx=None, **kwargs): """Loads the session with default values. Parameters ---------- execer : Execer, optional Xonsh execution object, may be None to start ctx : Mapping, optional Context to start xonsh session with. """ from xonsh.environ import Env, default_env from xonsh.commands_cache import CommandsCache from xonsh.completers.init import default_completers if not hasattr(builtins, "__xonsh__"): builtins.__xonsh__ = self if ctx is not None: self.ctx = ctx self.env = kwargs.pop("env") if "env" in kwargs else Env(default_env()) self.help = kwargs.pop("help") if "help" in kwargs else helper self.superhelp = superhelper self.pathsearch = pathsearch self.globsearch = globsearch self.regexsearch = regexsearch self.glob = globpath self.expand_path = expand_path self.exit = False self.stdout_uncaptured = None self.stderr_uncaptured = None if hasattr(builtins, "exit"): self.pyexit = builtins.exit del builtins.exit if hasattr(builtins, "quit"): self.pyquit = builtins.quit del builtins.quit self.subproc_captured_stdout = subproc_captured_stdout self.subproc_captured_inject = subproc_captured_inject self.subproc_captured_object = subproc_captured_object self.subproc_captured_hiddenobject = subproc_captured_hiddenobject self.subproc_uncaptured = subproc_uncaptured self.execer = execer self.commands_cache = ( kwargs.pop("commands_cache") if "commands_cache" in kwargs else CommandsCache() ) self.modules_cache = {} self.all_jobs = {} self.ensure_list_of_strs = ensure_list_of_strs self.list_of_strs_or_callables = list_of_strs_or_callables self.list_of_list_of_strs_outer_product = list_of_list_of_strs_outer_product self.eval_fstring_field = eval_fstring_field self.completers = default_completers() self.call_macro = call_macro self.enter_macro = enter_macro self.path_literal = path_literal self.builtins = _BuiltIns(execer) aliases_given = kwargs.pop("aliases", None) for attr, value in kwargs.items(): if hasattr(self, attr): setattr(self, attr, value) self.link_builtins(aliases_given) self.builtins_loaded = True
def test_bash_and_is_alias_is_only_functional_alias(xonsh_builtins): builtins.__xonsh__.env["PATH"] = os.environ["PATH"].split(os.pathsep) cc = CommandsCache() builtins.aliases["bash"] = lambda args: os.chdir(args[0]) assert not cc.is_only_functional_alias("bash")
title = name docstr = inspect.getdoc(event) if docstr.startswith(name): # Assume the first line is a signature title, docstr = docstr.split("\n", 1) docstr = docstr.strip() under = "." * (len(title) + 4) s += sec.format(low=name.lower(), title=title, under=under, docstr=docstr) s = s[:-9] fname = os.path.join(os.path.dirname(__file__), "eventsbody") with open(fname, "w") as f: f.write(s) make_xontribs() make_events() XSH.history = None XSH.env = {} XSH.commands_cache = CommandsCache() def setup(app): from xonsh.pyghooks import XonshConsoleLexer app.add_lexer("xonshcon", XonshConsoleLexer) app.add_css_file("custom.css")
def test_predict_threadable_unknown_command(xonsh_builtins): cc = CommandsCache() result = cc.predict_threadable(["command_should_not_found"]) assert isinstance(result, bool)
def test_bash_and_is_alias_is_only_functional_alias(xession): xession.env["PATH"] = os.environ["PATH"].split(os.pathsep) cc = CommandsCache() xession.aliases["bash"] = lambda args: os.chdir(args[0]) assert not cc.is_only_functional_alias("bash")
def test_cd_is_only_functional_alias(xession): cc = CommandsCache() xession.aliases["cd"] = lambda args: os.chdir(args[0]) assert cc.is_only_functional_alias("cd")
def test_commands_cache_lazy(): cc = CommandsCache() assert not cc.lazyin('xonsh') assert 0 == len(list(cc.lazyiter())) assert 0 == cc.lazylen()