xonsh.foreign_shells#

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

class xonsh.foreign_shells.ForeignShellBaseAlias(shell, sourcer=None, files=(), extra_args=())[source]#

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

Parameters:
shellstr

Name or path to shell

sourcerstr or None, optional

Command to source foreign files with.

filestuple of str, optional

Paths to source.

extra_argstuple 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=())[source]#

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

Parameters:
srcstr

Source code in the shell language

shellstr

Name or path to shell

sourcerstr or None, optional

Command to source foreign files with.

filestuple of str, optional

Paths to source.

extra_argstuple 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=())[source]#

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

Parameters:
funcnamestr

function name

shellstr

Name or path to shell

sourcerstr or None, optional

Command to source foreign files with.

filestuple of str, optional

Paths to source.

extra_argstuple of str, optional

Additional command line options to pass into the shell.

INPUT = '{funcname} {args}\n'#
xonsh.foreign_shells.ensure_shell(shell)[source]#

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, dryrun=False, files=())[source]#

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

Parameters:
shellstr

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

interactivebool, optional

Whether the shell should be run in interactive mode.

loginbool, optional

Whether the shell should be a login shell.

envcmdstr or None, optional

The command to generate environment output with.

aliascmdstr or None, optional

The command to generate alias output with.

extra_argstuple of str, optional

Additional command line options to pass into the shell.

currenvtuple of items or None, optional

Manual override for the current environment.

safebool, optional

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

prevcmdstr, optional

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

postcmdstr, optional

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

funcscmdstr 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.

sourcerstr 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_tmpfilebool, optional

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

tmpfile_extstr or None, optional

If tmpfile is True this sets specifies the extension used.

runcmdstr or None, optional

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

seterrprevcmdstr 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.

seterrpostcmdstr 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.

showbool, optional

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

dryrunbool, optional

Whether or not to actually run and process the command.

filestuple of str, optional

Paths to source.

Returns:
envdict

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

aliasesdict

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

xonsh.foreign_shells.load_foreign_aliases(shells)[source]#

Loads aliases from foreign shells.

Parameters:
shellssequence of dicts

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

Returns:
aliasesdict

A dictionary of the merged aliases.

xonsh.foreign_shells.load_foreign_envs(shells)[source]#

Loads environments from foreign shells.

Parameters:
shellssequence of dicts

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

Returns:
envdict

A dictionary of the merged environments.

xonsh.foreign_shells.parse_aliases(s, shell, sourcer=None, files=(), extra_args=())[source]#

Parses the aliases portion of string into a dict.

xonsh.foreign_shells.parse_env(s)[source]#

Parses the environment portion of string into a dict.

xonsh.foreign_shells.parse_funcs(s, shell, sourcer=None, files=(), extra_args=())[source]#

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