xonsh.imphooks#

Import hooks for importing xonsh source files.

This module registers the hooks it defines when it is imported.

class xonsh.imphooks.XonshImportEventHook(*args, **kwargs)[source]#

Implements the import hook for firing xonsh events on import.

append_stack(fullname)[source]#

A context manager for appending and then removing a name from the fullname stack.

find_module(fullname, path)#

Return a loader for the module.

If no module is found, return None. The fullname is a str and the path is a list of strings or None.

This method is deprecated since Python 3.4 in favor of finder.find_spec(). If find_spec() exists then backwards-compatible functionality is provided for this method.

find_spec(fullname, path, target=None)[source]#

Finds the spec for a xonsh module if it exists.

invalidate_caches()#

An optional method for clearing the finder’s cache, if any. This method is used by importlib.invalidate_caches().

class xonsh.imphooks.XonshImportEventLoader(loader)[source]#

A class that dispatches loader calls to another loader and fires relevant xonsh events.

create_module(spec)[source]#

Creates and returns the module object.

exec_module(module)[source]#

Executes the module in its own namespace.

load_module(fullname)#

Return the loaded module.

The module must be added to sys.modules and have import-related attributes set properly. The fullname is a str.

ImportError is raised on failure.

This method is deprecated in favor of loader.exec_module(). If exec_module() exists then it is used to provide a backwards-compatible functionality for this method.

module_repr(module)#

Return a module’s repr.

Used by the module type when the method does not raise NotImplementedError.

This method is deprecated.

class xonsh.imphooks.XonshImportHook(execer, *args, **kwargs)[source]#

Implements the import hook for xonsh source files.

create_module(spec)[source]#

Create a xonsh module with the appropriate attributes.

exec_module(module)#

Execute the module.

find_module(fullname, path)#

Return a loader for the module.

If no module is found, return None. The fullname is a str and the path is a list of strings or None.

This method is deprecated since Python 3.4 in favor of finder.find_spec(). If find_spec() exists then backwards-compatible functionality is provided for this method.

find_spec(fullname, path, target=None)[source]#

Finds the spec for a xonsh module if it exists.

get_code(fullname)[source]#

Gets the code object for a xonsh file.

get_data(path)[source]#

Gets the bytes for a path.

get_filename(fullname)[source]#

Returns the filename for a module’s fullname.

get_source(fullname)[source]#

Concrete implementation of InspectLoader.get_source.

invalidate_caches()#

An optional method for clearing the finder’s cache, if any. This method is used by importlib.invalidate_caches().

is_package(fullname)#

Concrete implementation of InspectLoader.is_package by checking if the path returned by get_filename has a filename of ‘__init__.py’.

load_module(fullname)#

This method is deprecated.

module_repr(module)#

Return a module’s repr.

Used by the module type when the method does not raise NotImplementedError.

This method is deprecated.

path_mtime(path)#

Return the (int) modification time for the path (str).

path_stats(path)#

Return a metadata dict for the source pointed to by the path (str). Possible keys: - ‘mtime’ (mandatory) is the numeric timestamp of last source

code modification;

  • ‘size’ (optional) is the size in bytes of the source code.

set_data(path, data)#

Write the bytes to the path (if possible).

Accepts a str path and data as bytes.

Any needed intermediary directories are to be created. If for some reason the file cannot be written because of permissions, fail silently.

source_to_code(data, path, *, _optimize=- 1)#

Return the code object compiled from source.

The ‘data’ argument can be any object type that compile() supports.

xonsh.imphooks.find_source_encoding(src)[source]#

Finds the source encoding given bytes representing a file by checking a special comment at either the first or second line of the source file. https://docs.python.org/3/howto/unicode.html#unicode-literals-in-python-source-code If no encoding is found, UTF-8 codec with BOM signature will be returned as it skips an optional UTF-8 encoded BOM at the start of the data and is otherwise the same as UTF-8 https://docs.python.org/3/library/codecs.html#module-encodings.utf_8_sig

xonsh.imphooks.install_hook(execer=<object object>)#

Install Xonsh import hooks in sys.meta_path in order for .xsh files to be importable and import events to be fired.

Can safely be called many times, will be no-op if xonsh import hooks are already present.

xonsh.imphooks.install_import_hooks(execer=<object object>)[source]#

Install Xonsh import hooks in sys.meta_path in order for .xsh files to be importable and import events to be fired.

Can safely be called many times, will be no-op if xonsh import hooks are already present.