Skip to content
Back to top
View this page
Edit this page

# xonsh.foreign_shells

Tools to help interface with foreign shells, such as Bash.

class xonsh.foreign_shells.ForeignShellBaseAlias(shell, sourcer=None, files=(), extra_args=())

  This class is responsible for calling foreign shell functions as if they were aliases. This does not currently support taking stdin.

  Parameters:
    shell str

      Name or path to shell

    sourcer str or None, optional

      Command to source foreign files with.

    files tuple of str, optional

      Paths to source.

    extra_args tuple of str, optional

      Additional command line options to pass into the shell.

  INPUT = 'echo ForeignShellBaseAlias {shell} {args}\n'

class xonsh.foreign_shells.ForeignShellExecAlias(src, shell, sourcer=None, files=(), extra_args=())

  Provides a callable alias for source code in a foreign shell.

  Parameters:
    src str

      Source code in the shell language

    shell str

      Name or path to shell

    sourcer str or None, optional

      Command to source foreign files with.

    files tuple of str, optional

      Paths to source.

    extra_args tuple of str, optional

      Additional command line options to pass into the shell.

  INPUT = '{src} {args}\n'

class xonsh.foreign_shells.ForeignShellFunctionAlias(funcname, shell, sourcer=None, files=(), extra_args=())

  This class is responsible for calling foreign shell functions as if they were aliases. This does not currently support taking stdin.

  Parameters:
    funcname str

      function name

    shell str

      Name or path to shell

    sourcer str or None, optional

      Command to source foreign files with.

    files tuple of str, optional

      Paths to source.

    extra_args tuple of str, optional

      Additional command line options to pass into the shell.

  INPUT = '{funcname} {args}\n'

xonsh.foreign_shells.ensure_shell(shell)

  Ensures that a mapping follows the shell specification.

xonsh.foreign_shells.foreign_shell_data(shell, interactive=True, login=False, envcmd=None, aliascmd=None, extra_args=(), currenv=None, safe=True, prevcmd='', postcmd='', funcscmd=None, sourcer=None, use_tmpfile=False, tmpfile_ext=None, runcmd=None, seterrprevcmd=None, seterrpostcmd=None, show=False, show_output=False, dryrun=False, files=())

  Extracts data from a foreign (non-xonsh) shells. Currently this gets the environment, aliases, and functions but may be extended in the future.

  Parameters:
    shell str

      The name of the shell, such as ‘bash’ or ‘/bin/sh’.

    interactive bool, optional

      Whether the shell should be run in interactive mode.

    login bool, optional

      Whether the shell should be a login shell.

    envcmd str or None, optional

      The command to generate environment output with.

    aliascmd str or None, optional

      The command to generate alias output with.

    extra_args tuple of str, optional

      Additional command line options to pass into the shell.

    currenv tuple of items or None, optional

      Manual override for the current environment.

    safe bool, optional

      Flag for whether or not to safely handle exceptions and other errors.

    prevcmd str, optional

      A command to run in the shell before anything else, useful for sourcing and other commands that may require environment recovery.

    postcmd str, optional

      A command to run after everything else, useful for cleaning up any damage that the prevcmd may have caused.

    funcscmd str or None, optional

      This is a command or script that can be used to determine the names and locations of any functions that are native to the foreign shell. This command should print only a JSON object that maps function names to the filenames where the functions are defined. If this is None, then a default script will attempted to be looked up based on the shell name. Callable wrappers for these functions will be returned in the aliases dictionary.

    sourcer str or None, optional

      How to source a foreign shell file for purposes of calling functions in that shell. If this is None, a default value will attempt to be looked up based on the shell name.

    use_tmpfile bool, optional

      This specifies if the commands are written to a tmp file or just parsed directly to the shell

    tmpfile_ext str or None, optional

      If tmpfile is True this sets specifies the extension used.

    runcmd str or None, optional

      Command line switches to use when running the script, such as -c for Bash and /C for cmd.exe.

    seterrprevcmd str or None, optional

      Command that enables exit-on-error for the shell that is run at the start of the script. For example, this is “set -e” in Bash. To disable exit-on-error behavior, simply pass in an empty string.

    seterrpostcmd str or None, optional

      Command that enables exit-on-error for the shell that is run at the end of the script. For example, this is “if errorlevel 1 exit 1” in cmd.exe. To disable exit-on-error behavior, simply pass in an empty string.

    show bool, optional

      Whether or not to display the script that will be run.

    show_output bool, optional

      Whether to forward the sourced script’s own stdout/stderr to xonsh’s terminal. When False (the default) the script’s writes are silently discarded — only the xonsh-injected env/alias/func dumps are parsed out and applied. Useful when sourcing scripts that print banners or diagnostic messages, or for debugging a failing source.

    dryrun bool, optional

      Whether or not to actually run and process the command.

    files tuple of str, optional

      Paths to source.

  Returns:
    env dict

      Dictionary of shell’s environment. (None if the subproc command fails)

    aliases dict

      Dictionary of shell’s aliases, this includes foreign function wrappers.(None if the subproc command fails)

xonsh.foreign_shells.load_foreign_aliases(shells)

  Loads aliases from foreign shells.

  Parameters:
    shells sequence of dicts

      An iterable of dicts that can be passed into foreign_shell_data() as keyword arguments.

  Returns:
    aliases dict

      A dictionary of the merged aliases.

xonsh.foreign_shells.load_foreign_envs(shells)

  Loads environments from foreign shells.

  Parameters:
    shells sequence of dicts

      An iterable of dicts that can be passed into foreign_shell_data() as keyword arguments.

  Returns:
    env dict

      A dictionary of the merged environments.

xonsh.foreign_shells.parse_aliases(s, shell, sourcer=None, files=(), extra_args=())

  Parses the aliases portion of string into a dict.

xonsh.foreign_shells.parse_env(s)

  Parses the environment portion of string into a dict.

xonsh.foreign_shells.parse_funcs(s, shell, sourcer=None, files=(), extra_args=())

  Parses the funcs portion of a string into a dict of callable foreign function wrappers.