Пример #1
0
def start_services(shell_kwargs, args):
    """Starts up the essential services in the proper order.
    This returns the environment instance as a convenience.
    """
    install_import_hooks()
    # create execer, which loads builtins
    ctx = shell_kwargs.get("ctx", {})
    debug = to_bool_or_int(os.getenv("XONSH_DEBUG", "0"))
    events.on_timingprobe.fire(name="pre_execer_init")
    execer = Execer(
        xonsh_ctx=ctx,
        debug_level=debug,
        scriptcache=shell_kwargs.get("scriptcache", True),
        cacheall=shell_kwargs.get("cacheall", False),
    )
    events.on_timingprobe.fire(name="post_execer_init")
    # load rc files
    login = shell_kwargs.get("login", True)
    env = builtins.__xonsh__.env
    rc = shell_kwargs.get("rc", None)
    rc = env.get("XONSHRC") if rc is None else rc
    if args.mode != XonshMode.interactive and not args.force_interactive:
        #  Don't load xonshrc if not interactive shell
        rc = None
    events.on_pre_rc.fire()
    xonshrc_context(rcfiles=rc, execer=execer, ctx=ctx, env=env, login=login)
    events.on_post_rc.fire()
    # create shell
    builtins.__xonsh__.shell = Shell(execer=execer, **shell_kwargs)
    ctx["__name__"] = "__main__"
    return env
Пример #2
0
def start_services(shell_kwargs, args):
    """Starts up the essential services in the proper order.
    This returns the environment instance as a convenience.
    """
    install_import_hooks()
    # create execer, which loads builtins
    ctx = shell_kwargs.get('ctx', {})
    debug = to_bool_or_int(os.getenv('XONSH_DEBUG', '0'))
    events.on_timingprobe.fire(name='pre_execer_init')
    execer = Execer(xonsh_ctx=ctx,
                    debug_level=debug,
                    scriptcache=shell_kwargs.get('scriptcache', True),
                    cacheall=shell_kwargs.get('cacheall', False))
    events.on_timingprobe.fire(name='post_execer_init')
    # load rc files
    login = shell_kwargs.get('login', True)
    env = builtins.__xonsh_env__
    rc = shell_kwargs.get('rc', None)
    rc = env.get('XONSHRC') if rc is None else rc
    if args.mode != XonshMode.interactive and not args.force_interactive:
        #  Don't load xonshrc if not interactive shell
        rc = None
    events.on_pre_rc.fire()
    xonshrc_context(rcfiles=rc, execer=execer, ctx=ctx, env=env, login=login)
    events.on_post_rc.fire()
    # create shell
    builtins.__xonsh_shell__ = Shell(execer=execer, **shell_kwargs)
    ctx['__name__'] = '__main__'
    return env
Пример #3
0
def setup(ctx=None, shell_type='none', env=(('RAISE_SUBPROC_ERROR', True), )):
    """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.
    """
    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='none')
    builtins.__xonsh_env__.update(env)
    install_import_hooks()
Пример #4
0
def start_services(shell_kwargs, args):
    """Starts up the essential services in the proper order.
    This returns the environment instance as a convenience.
    """
    install_import_hooks()
    # create execer, which loads builtins
    ctx = shell_kwargs.get("ctx", {})
    debug = to_bool_or_int(os.getenv("XONSH_DEBUG", "0"))
    events.on_timingprobe.fire(name="pre_execer_init")
    execer = Execer(
        xonsh_ctx=ctx,
        debug_level=debug,
        scriptcache=shell_kwargs.get("scriptcache", True),
        cacheall=shell_kwargs.get("cacheall", False),
    )
    events.on_timingprobe.fire(name="post_execer_init")
    # load rc files
    login = shell_kwargs.get("login", True)
    env = builtins.__xonsh__.env
    rc = shell_kwargs.get("rc", None)
    rc = env.get("XONSHRC") if rc is None else rc
    if args.mode != XonshMode.interactive and not args.force_interactive:
        #  Don't load xonshrc if not interactive shell
        rc = None
    events.on_pre_rc.fire()
    xonshrc_context(rcfiles=rc, execer=execer, ctx=ctx, env=env, login=login)
    events.on_post_rc.fire()
    # create shell
    builtins.__xonsh__.shell = Shell(execer=execer, **shell_kwargs)
    ctx["__name__"] = "__main__"
    return env
Пример #5
0
def start_services(shell_kwargs):
    """Starts up the essential services in the proper order.
    This returns the environment instance as a convenience.
    """
    install_import_hooks()
    # create execer, which loads builtins
    ctx = shell_kwargs.get('ctx', {})
    debug = to_bool_or_int(os.getenv('XONSH_DEBUG', '0'))
    events.on_timingprobe.fire(name='pre_execer_init')
    execer = Execer(xonsh_ctx=ctx, debug_level=debug,
                    scriptcache=shell_kwargs.get('scriptcache', True),
                    cacheall=shell_kwargs.get('cacheall', False))
    events.on_timingprobe.fire(name='post_execer_init')
    # load rc files
    login = shell_kwargs.get('login', True)
    env = builtins.__xonsh_env__
    rc = shell_kwargs.get('rc', None)
    rc = env.get('XONSHRC') if rc is None else rc
    events.on_pre_rc.fire()
    xonshrc_context(rcfiles=rc, execer=execer, ctx=ctx, env=env, login=login)
    events.on_post_rc.fire()
    # create shell
    builtins.__xonsh_shell__ = Shell(execer=execer, **shell_kwargs)
    ctx['__name__'] = '__main__'
    return env
Пример #6
0
def start_services(shell_kwargs, args, pre_env=None):
    """Starts up the essential services in the proper order.
    This returns the environment instance as a convenience.
    """
    if pre_env is None:
        pre_env = {}
    # create execer, which loads builtins
    ctx = shell_kwargs.get("ctx", {})
    debug = to_bool_or_int(os.getenv("XONSH_DEBUG", "0"))
    events.on_timingprobe.fire(name="pre_execer_init")
    execer = Execer(
        filename="<stdin>",
        debug_level=debug,
        scriptcache=shell_kwargs.get("scriptcache", True),
        cacheall=shell_kwargs.get("cacheall", False),
    )
    XSH.load(ctx=ctx, execer=execer)
    events.on_timingprobe.fire(name="post_execer_init")

    install_import_hooks(execer)

    env = XSH.env
    for k, v in pre_env.items():
        env[k] = v

    _autoload_xontribs(env)
    _load_rc_files(shell_kwargs, args, env, execer, ctx)
    # create shell
    XSH.shell = Shell(execer=execer, **shell_kwargs)
    ctx["__name__"] = "__main__"
    return env
Пример #7
0
def setup(ctx=None, shell_type='none', env=(('RAISE_SUBPROC_ERROR', True),)):
    """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.
    """
    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='none')
    builtins.__xonsh_env__.update(env)
    install_import_hooks()
Пример #8
0
def start_services(shell_kwargs, args, pre_env=None):
    """Starts up the essential services in the proper order.
    This returns the environment instance as a convenience.
    """
    if pre_env is None:
        pre_env = {}
    # create execer, which loads builtins
    ctx = shell_kwargs.get("ctx", {})
    debug = to_bool_or_int(os.getenv("XONSH_DEBUG", "0"))
    events.on_timingprobe.fire(name="pre_execer_init")
    execer = Execer(
        debug_level=debug,
        scriptcache=shell_kwargs.get("scriptcache", True),
        cacheall=shell_kwargs.get("cacheall", False),
    )
    XSH.load(ctx=ctx, execer=execer)
    events.on_timingprobe.fire(name="post_execer_init")

    install_import_hooks(execer)

    # load rc files
    login = shell_kwargs.get("login", True)
    rc_cli = shell_kwargs.get("rc")
    env = XSH.env
    for k, v in pre_env.items():
        env[k] = v

    # determine which RC files to load, including whether any RC directories
    # should be scanned for such files
    if shell_kwargs.get("norc") or (args.mode != XonshMode.interactive
                                    and not args.force_interactive
                                    and not args.login):
        # if --no-rc was passed, or we're not in an interactive shell and
        # interactive mode was not forced, then disable loading RC files and dirs
        rc = ()
        rcd = ()
    elif rc_cli:
        # if an explicit --rc was passed, then we should load only that RC
        # file, and nothing else (ignore both XONSHRC and XONSHRC_DIR)
        rc = [r for r in rc_cli if os.path.isfile(r)]
        rcd = [r for r in rc_cli if os.path.isdir(r)]
    else:
        # otherwise, get the RC files from XONSHRC, and RC dirs from XONSHRC_DIR
        rc = env.get("XONSHRC")
        rcd = env.get("XONSHRC_DIR")

    events.on_pre_rc.fire()
    XSH.rc_files = xonshrc_context(rcfiles=rc,
                                   rcdirs=rcd,
                                   execer=execer,
                                   ctx=ctx,
                                   env=env,
                                   login=login)
    events.on_post_rc.fire()
    # create shell
    XSH.shell = Shell(execer=execer, **shell_kwargs)
    ctx["__name__"] = "__main__"
    return env
Пример #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)
Пример #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)
Пример #11
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
Пример #12
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
    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)
Пример #13
0
def imp_env(xession):
    xession.env = Env({"PATH": [], "PATHEXT": []})
    imphooks.install_import_hooks(xession.execer)
    yield
    XSH.unload()
Пример #14
0
"""Testing xonsh import hooks"""
import os
from importlib import import_module

import pytest

from xonsh import imphooks
from xonsh.execer import Execer
from xonsh.environ import Env
from xonsh.built_ins import XSH

imphooks.install_import_hooks()


@pytest.fixture(autouse=True)
def imp_env(xession):
    Execer(unload=False)
    xession.env = Env({"PATH": [], "PATHEXT": []})
    yield
    XSH.unload()


def test_import():
    import sample

    assert "hello mom jawaka\n" == sample.x


def test_absolute_import():
    from xpack import sample
Пример #15
0
def imp_env(xession):
    xession.env.update({"PATH": [], "PATHEXT": []})
    imphooks.install_import_hooks(xession.execer)
    yield
Пример #16
0
def pytest_configure(config):
    install_import_hooks()
Пример #17
0
def pytest_configure(config):
    install_import_hooks()
Пример #18
0
# -*- coding: utf-8 -*-
"""Testing xonsh import hooks"""
import os
import builtins

import pytest

from xonsh import imphooks
from xonsh.execer import Execer
from xonsh.environ import Env
from xonsh.built_ins import unload_builtins

imphooks.install_import_hooks()


@pytest.yield_fixture(autouse=True)
def imp_env():
    execer = Execer(unload=False)
    builtins.__xonsh__.env = Env({"PATH": [], "PATHEXT": []})
    yield
    unload_builtins()


def test_import():
    import sample

    assert "hello mom jawaka\n" == sample.x


def test_absolute_import():
    from xpack import sample