def test_xontrib_reload(tmpmod, xession): with tmpmod.mkdir("xontrib").join("script.py").open("w") as x: x.write( """ hello = 'world' def _unload_xontrib_(xsh): del xsh.ctx['hello'] """ ) xontribs_load(["script"]) assert "script" in xontribs_loaded() assert xession.ctx["hello"] == "world" with tmpmod.join("xontrib").join("script.py").open("w") as x: x.write( """ hello = 'world1' def _unload_xontrib_(xsh): del xsh.ctx['hello'] """ ) xontribs_reload(["script"]) assert "script" in xontribs_loaded() assert xession.ctx["hello"] == "world1"
def wrapper(*names: str): from xonsh.xontribs import xontribs_load for name in names: module = f"xontrib.{name}" if module not in sys.modules: to_unload.append(module) xontribs_load([name]) return
def test_xontrib_load_dashed(tmpmod): """ Test that .xsh xontribs are loadable """ with tmpmod.mkdir("xontrib").join("scri-pt.xsh").open("w") as x: x.write(""" hello = 'world' """) xontribs_load(["scri-pt"]) assert 'scri-pt' in xontribs_loaded()
def setup( ctx=None, shell_type="none", env=(("RAISE_SUBPROC_ERROR", True), ), aliases=(), xontribs=(), threadable_predictors=(), ): """Starts up a new xonsh shell. Calling this in function in another packages ``__init__.py`` will allow xonsh to be fully used in the package in headless or headed mode. This function is primarily indended to make starting up xonsh for 3rd party packages easier. Here is example of using this at the top of an ``__init__.py``:: from xonsh.main import setup setup() del setup Parameters ---------- ctx : dict-like or None, optional The xonsh context to start with. If None, an empty dictionary is provided. shell_type : str, optional The type of shell to start. By default this is 'none', indicating we should start in headless mode. env : dict-like, optional Environment to update the current environment with after the shell has been initialized. aliases : dict-like, optional Aliases to add after the shell has been initialized. xontribs : iterable of str, optional Xontrib names to load. threadable_predictors : dict-like, optional Threadable predictors to start up with. These overide the defaults. """ ctx = {} if ctx is None else ctx # setup xonsh ctx and execer if not hasattr(builtins, "__xonsh__"): execer = Execer(xonsh_ctx=ctx) builtins.__xonsh__ = XonshSession(ctx=ctx, execer=execer) load_builtins(ctx=ctx, execer=execer) builtins.__xonsh__.shell = Shell(execer, ctx=ctx, shell_type=shell_type) builtins.__xonsh__.env.update(env) install_import_hooks() builtins.aliases.update(aliases) if xontribs: xontribs_load(xontribs) tp = builtins.__xonsh__.commands_cache.threadable_predictors tp.update(threadable_predictors)
def run_alias(name: str, args=None): import sys from xonsh.main import setup from xonsh.built_ins import subproc_uncaptured from xonsh.xontribs import xontribs_load setup() xontribs_load(["coreutils"]) args = sys.argv[1:] if args is None else args subproc_uncaptured([name] + args)
def setup( ctx=None, shell_type="none", env=(("RAISE_SUBPROC_ERROR", True),), aliases=(), xontribs=(), threadable_predictors=(), ): """Starts up a new xonsh shell. Calling this in function in another packages __init__.py will allow xonsh to be fully used in the package in headless or headed mode. This function is primarily indended to make starting up xonsh for 3rd party packages easier. Parameters ---------- ctx : dict-like or None, optional The xonsh context to start with. If None, an empty dictionary is provided. shell_type : str, optional The type of shell to start. By default this is 'none', indicating we should start in headless mode. env : dict-like, optional Environment to update the current environment with after the shell has been initialized. aliases : dict-like, optional Aliases to add after the shell has been initialized. xontribs : iterable of str, optional Xontrib names to load. threadable_predictors : dict-like, optional Threadable predictors to start up with. These overide the defaults. """ ctx = {} if ctx is None else ctx # setup xonsh ctx and execer if not hasattr(builtins, "__xonsh__"): execer = Execer(xonsh_ctx=ctx) builtins.__xonsh__ = XonshSession(ctx=ctx, execer=execer) load_builtins(ctx=ctx, execer=execer) load_proxies() builtins.__xonsh__.shell = Shell(execer, ctx=ctx, shell_type=shell_type) builtins.__xonsh__.env.update(env) install_import_hooks() builtins.aliases.update(aliases) if xontribs: xontribs_load(xontribs) tp = builtins.__xonsh__.commands_cache.threadable_predictors tp.update(threadable_predictors)
def create(cls) -> "Shell": import signal from xonsh.built_ins import load_builtins from xonsh.built_ins import XonshSession from xonsh.imphooks import install_import_hooks from xonsh.main import _pprint_displayhook from xonsh.xontribs import xontribs_load import xonsh.history.main as xhm ctx: Dict[str, Any] = {} execer = Execer(xonsh_ctx=ctx) builtins.__xonsh__ = XonshSession(ctx=ctx, execer=execer) # type: ignore load_builtins(ctx=ctx, execer=execer) env = builtins.__xonsh__.env # type: ignore env.update({"XONSH_INTERACTIVE": True, "SHELL_TYPE": "prompt_toolkit"}) builtins.__xonsh__.history = xhm.construct_history( # type: ignore env=env.detype(), ts=[time.time(), None], locked=True) builtins.__xonsh__.history.gc.wait_for_shell = False # type: ignore setattr(sys, "displayhook", _pprint_displayhook) install_import_hooks() builtins.aliases.update({"ll": "ls -alF"}) # type: ignore xontribs_load([""]) def func_sig_ttin_ttou(n: Any, f: Any) -> None: pass signal.signal(signal.SIGTTIN, func_sig_ttin_ttou) signal.signal(signal.SIGTTOU, func_sig_ttin_ttou) shell = cls(execer) builtins.__xonsh__.shell = shell # type: ignore builtins.__xonsh__.shell.shell = shell # type: ignore return shell
def load_vox(xession, tmpdir): """load vox into session""" # Set up an isolated venv home xession.env["VIRTUALENV_HOME"] = str(tmpdir) # Set up enough environment for xonsh to function xession.env["PWD"] = os.getcwd() xession.env["DIRSTACK_SIZE"] = 10 xession.env["PATH"] = [] xession.env["XONSH_SHOW_TRACEBACK"] = True module = "xontrib.vox" has_vox = module in sys.modules from xonsh.xontribs import xontribs_load xontribs_load(["vox"]) yield if not has_vox: del sys.modules["xontrib.vox"]
def wrapper(*names: str): from xonsh.xontribs import xontribs_load for name in names: module = f"xontrib.{name}" if module not in sys.modules: to_unload.append(module) _, stderr, res = xontribs_load([name]) if stderr: raise Exception(f"Failed to load xontrib: {stderr}") return
def setup(ctx=None, shell_type='none', env=(('RAISE_SUBPROC_ERROR', True),), aliases=(), xontribs=(), threadable_predictors=()): """Starts up a new xonsh shell. Calling this in function in another packages __init__.py will allow xonsh to be fully used in the package in headless or headed mode. This function is primarily indended to make starting up xonsh for 3rd party packages easier. Parameters ---------- ctx : dict-like or None, optional The xonsh context to start with. If None, an empty dictionary is provided. shell_type : str, optional The type of shell to start. By default this is 'none', indicating we should start in headless mode. env : dict-like, optional Environment to update the current environment with after the shell has been initialized. aliases : dict-like, optional Aliases to add after the shell has been initialized. xontribs : iterable of str, optional Xontrib names to load. threadable_predictors : dict-like, optional Threadable predictors to start up with. These overide the defaults. """ ctx = {} if ctx is None else ctx # setup xonsh ctx and execer builtins.__xonsh_ctx__ = ctx builtins.__xonsh_execer__ = Execer(xonsh_ctx=ctx) builtins.__xonsh_shell__ = Shell(builtins.__xonsh_execer__, ctx=ctx, shell_type=shell_type) builtins.__xonsh_env__.update(env) install_import_hooks() builtins.aliases.update(aliases) if xontribs: xontribs_load(xontribs) tp = builtins.__xonsh_commands_cache__.threadable_predictors tp.update(threadable_predictors)
def post(self, data: "cgi.FieldStorage"): if not data.keys(): return name = data.keys()[0] if self.is_loaded(name): # todo: update rc file del sys.modules[self.mod_name(name)] else: from xonsh.xontribs import xontribs_load _, err, _ = xontribs_load([name]) if err: self.err(err) else: self.update_rc(xontribs=[name])