Exemple #1
0
def load_command_cache():
    load_builtins()
    if ON_WINDOWS:
        for key in ('cd', 'bash'):
            builtins.aliases[key] = lambda *args, **kwargs: None
    yield
    unload_builtins()
def load_command_cache(xonsh_builtins):
    gc.collect()
    unload_builtins()
    load_builtins()
    if ON_WINDOWS:
        for key in ("cd", "bash"):
            builtins.aliases[key] = lambda *args, **kwargs: None
Exemple #3
0
def test_simple():
    load_builtins()
    with xonsh_env(Env(CDPATH=PARENT, PWD=PARENT)):
        with chdir(PARENT):
            assert_not_equal(os.getcwd(), HERE)
            dirstack.cd(["tests"])
            assert_equal(os.getcwd(), HERE)
Exemple #4
0
def test_simple():
    load_builtins()
    with xonsh_env(Env(CDPATH=PARENT, PWD=PARENT)):
        with chdir(PARENT):
            assert os.getcwd() !=  HERE
            dirstack.cd(["tests"])
            assert os.getcwd() ==  HERE
Exemple #5
0
def load_command_cache(xonsh_builtins):
    load_builtins()
    if ON_WINDOWS:
        for key in ("cd", "bash"):
            builtins.aliases[key] = lambda *args, **kwargs: None
    yield
    unload_builtins()
Exemple #6
0
 def __init__(self, filename='<xonsh-code>', debug_level=0, parser_args=None,
              unload=True, xonsh_ctx=None, scriptcache=True, cacheall=False):
     """Parameters
     ----------
     filename : str, optional
         File we are to execute.
     debug_level : int, optional
         Debugging level to use in lexing and parsing.
     parser_args : dict, optional
         Arguments to pass down to the parser.
     unload : bool, optional
         Whether or not to unload xonsh builtins upon deletion.
     xonsh_ctx : dict or None, optional
         Xonsh xontext to load as builtins.__xonsh_ctx__
     scriptcache : bool, optional
         Whether or not to use a precompiled bytecode cache when execing
         code, default: True.
     cacheall : bool, optional
         Whether or not to cache all xonsh code, and not just files. If this
         is set to true, it will cache command line input too, default: False.
     """
     parser_args = parser_args or {}
     self.parser = Parser(**parser_args)
     self.filename = filename
     self.debug_level = debug_level
     self.unload = unload
     self.scriptcache = scriptcache
     self.cacheall = cacheall
     self.ctxtransformer = CtxAwareTransformer(self.parser)
     load_builtins(execer=self, ctx=xonsh_ctx)
Exemple #7
0
 def __init__(self,
              filename='<xonsh-code>',
              debug_level=0,
              parser_args=None,
              unload=True,
              config=None,
              login=True,
              xonsh_ctx=None):
     """Parameters
     ----------
     filename : str, optional
         File we are to execute.
     debug_level : int, optional
         Debugging level to use in lexing and parsing.
     parser_args : dict, optional
         Arguments to pass down to the parser.
     unload : bool, optional
         Whether or not to unload xonsh builtins upon deletion.
     config : str, optional
         Path to configuration file.
     xonsh_ctx : dict or None, optional
         Xonsh xontext to load as builtins.__xonsh_ctx__
     """
     parser_args = parser_args or {}
     self.parser = Parser(**parser_args)
     self.filename = filename
     self.debug_level = debug_level
     self.unload = unload
     self.ctxtransformer = CtxAwareTransformer(self.parser)
     load_builtins(execer=self, config=config, login=login, ctx=xonsh_ctx)
def load_command_cache():
    load_builtins()
    if ON_WINDOWS:
        for key in ("cd", "bash"):
            builtins.aliases[key] = lambda *args, **kwargs: None
    yield
    unload_builtins()
Exemple #9
0
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)
Exemple #10
0
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)
Exemple #11
0
 def __init__(self,
              filename='<xonsh-code>',
              debug_level=0,
              parser_args=None):
     """Parameters
     ----------
     filename : str, optional
         File we are to execute.
     debug_level : int, optional
         Debugging level to use in lexing and parsing.
     parser_args : dict, optional
         Arguments to pass down to the parser.
     """
     parser_args = parser_args or {}
     self.parser = Parser(**parser_args)
     self.filename = filename
     self.debug_level = debug_level
     self.ctxtransformer = ast.CtxAwareTransformer(self.parser)
     load_builtins(execer=self)
Exemple #12
0
    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
Exemple #13
0
 def __init__(
     self,
     filename="<xonsh-code>",
     debug_level=0,
     parser_args=None,
     unload=True,
     xonsh_ctx=None,
     scriptcache=True,
     cacheall=False,
 ):
     """Parameters
     ----------
     filename : str, optional
         File we are to execute.
     debug_level : int, optional
         Debugging level to use in lexing and parsing.
     parser_args : dict, optional
         Arguments to pass down to the parser.
     unload : bool, optional
         Whether or not to unload xonsh builtins upon deletion.
     xonsh_ctx : dict or None, optional
         Xonsh xontext to load as builtins.__xonsh__.ctx
     scriptcache : bool, optional
         Whether or not to use a precompiled bytecode cache when execing
         code, default: True.
     cacheall : bool, optional
         Whether or not to cache all xonsh code, and not just files. If this
         is set to true, it will cache command line input too, default: False.
     """
     parser_args = parser_args or {}
     self.parser = Parser(**parser_args)
     self.filename = filename
     self._default_filename = filename
     self.debug_level = debug_level
     self.unload = unload
     self.scriptcache = scriptcache
     self.cacheall = cacheall
     self.ctxtransformer = CtxAwareTransformer(self.parser)
     load_builtins(execer=self, ctx=xonsh_ctx)
Exemple #14
0
 def __init__(self, filename='<xonsh-code>', debug_level=0, parser_args=None,
              unload=True, config=None):
     """Parameters
     ----------
     filename : str, optional
         File we are to execute.
     debug_level : int, optional
         Debugging level to use in lexing and parsing.
     parser_args : dict, optional
         Arguments to pass down to the parser.
     unload : bool, optional
         Whether or not to unload xonsh builtins upon deletion.
     config : str, optional
         Path to configuration file.
     """
     parser_args = parser_args or {}
     self.parser = Parser(**parser_args)
     self.filename = filename
     self.debug_level = debug_level
     self.unload = unload
     self.ctxtransformer = ast.CtxAwareTransformer(self.parser)
     load_builtins(execer=self, config=config)
Exemple #15
0
def setup_module():
    global LOADED_HERE
    if built_ins.BUILTINS_LOADED:
        unload_builtins()  # make sure we have a clean env from other tests.
        load_builtins(execer=Execer())
        LOADED_HERE = True
Exemple #16
0
def test_simple():
    load_builtins()
    with chdir(PARENT):
        assert_not_equal(os.getcwd(), HERE)
        dirstack.cd(["tests"])
        assert_equal(os.getcwd(), HERE)
Exemple #17
0
def xonsh_env(env):
    load_builtins()
    old_env = builtins.__xonsh_env__
    builtins.__xonsh_env__ = env
    yield
    builtins.__xonsh_env__ = old_env
Exemple #18
0
def xonsh_env(env):
    load_builtins()
    old_env = builtins.__xonsh_env__
    builtins.__xonsh_env__ = env
    yield
    builtins.__xonsh_env__ = old_env
Exemple #19
0
def setup():
    global LOADED_HERE
    if built_ins.BUILTINS_LOADED:
        unload_builtins()  # make sure we have a clean env from other tests.
        load_builtins(execer=Execer())
        LOADED_HERE = True
Exemple #20
0
def imp_env(xonsh_execer):
    """Call `load_builtins` with `xonsh_execer`"""
    load_builtins(execer=xonsh_execer)
    builtins.__xonsh_env__ = Env({'PATH': [], 'PATHEXT': []})
    yield
    unload_builtins()
Exemple #21
0
def test_simple():
    load_builtins()
    with chdir(PARENT):
        assert_not_equal(os.getcwd(), HERE)
        dirstack.cd(["tests"])
        assert_equal(os.getcwd(), HERE)