Foreign Shell Tools (xonsh.foreign_shells)

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

class xonsh.foreign_shells.ForeignShellFunctionAlias(name, shell, filename, sourcer=None, 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:

name : str

function name

shell : str

Name or path to shell

filename : str

Where the function is defined, path to source.

sourcer : str or None, optional

Command to source foreing files with.

extra_args : tuple of str, optional

Addtional command line options to pass into the shell.

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

Ensures that a mapping follows the shell specification.

xonsh.foreign_shells.load_foreign_aliases(shells=None, config=None, issue_warning=True)[source]

Loads aliases from foreign shells.

Parameters:

shells : sequence of dicts, optional

An iterable of dicts that can be passed into foreign_shell_data() as keyword arguments. Not compatible with config not being None.

config : str of None, optional

Path to the static config file. Not compatible with shell not being None. If both shell and config is None, then it will be read from the $XONSHCONFIG environment variable.

issue_warning : bool, optional

Issues warnings if config file cannot be found.

Returns:

aliases : dict

A dictionary of the merged aliases.

xonsh.foreign_shells.load_foreign_envs(shells=None, config=None, issue_warning=True)[source]

Loads environments from foreign shells.

Parameters:

shells : sequence of dicts, optional

An iterable of dicts that can be passed into foreign_shell_data() as keyword arguments. Not compatible with config not being None.

config : str of None, optional

Path to the static config file. Not compatible with shell not being None. If both shell and config is None, then it will be read from the $XONSHCONFIG environment variable.

issue_warning : bool, optional

Issues warnings if config file cannot be found.

Returns:

env : dict

A dictionary of the merged environments.

xonsh.foreign_shells.parse_aliases(s)[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, extra_args=())[source]

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

xonsh.foreign_shells.foreign_shell_data[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:

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

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

dryrun : bool, optional

Whether or not to actually run and process the command.

Returns:

env : dict

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

aliases : dict

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