Source-level Documentation

inline_importer

The inliner package assembles the various components of the InlineImporter library.

The inline_importer.inline_importer module should be kept clean, as it will be inlined with any script produced by this.

exception inline_importer.InlinerException

inline_importer.builder

inline_importer.builder.build_file(inlined_modules, entrypoint, importer_module='inline_importer.importer', shebang=None, namespace_packages=None)

Builds an single file script containing the importer module.

This function returns the inlined script as a string.

Parameters
  • (Repository or dict(str, (inlined_modules) – ModuleDefinition)): Repository of modules

  • entrypoint (str) – the source code of the entrypoint

  • importer_module (str, optional) – the fully-qualified name of the importer module to inline with the script

  • shebang (bool, optional) – whether to include a shebang at the top of the script

  • namespace_packages (bool, optional) – Whether to treat packages as PEP 420 namespace packages.

Returns

The source of the self-contained script.

Return type

str

inline_importer.builder.write_file(file_or_filename, *args, **kwargs)

Build a single file script and write it to filename.

Other than a filename, the rest of the arguments are passed verbatim to build_file.

Parameters
  • file_or_filename (file-like object or str-like) – Either a file-like object with a write method or a str-like object representing a filename.

  • *args – parameters from build_file

  • **kwargs – parameters from build_file

Returns

The number of bytes written to file_or_filename

Return type

int

inline_importer.inliner

class inline_importer.inliner.ModuleDefinition(name, is_package, source)

A named tuple that represents a module’s definition during inlining.

property is_package

Alias for field number 1

property name

Alias for field number 0

property source

Alias for field number 2

class inline_importer.inliner.Repository

Repository contains the modules being inlined.

It’s a specialized dictionary.

It is equivalent to a dict(str, ModuleDefinition).

insert_module(name, source, is_package=False)

Convenience method that inserts a module and its source in the repository.

Parameters
  • name (str) – fully qualified name of the module or package

  • source (str) – The source code of the module or package

  • is_package (bool) – is this module is a package

Raises

InlinerException – If the given name is already present in the repository.

inline_importer.inliner.build_inlined(modules, packages)

Builds a Repository of inlined modules and packages.

Parameters
  • modules (list(str)) – A list of paths to individual modules to inline.

  • packages (list(str)) – A list of paths to packages to recursively inline.

Returns

A repository of inlined modules and packages.

Return type

Repository

Raises

InlinerException – If an entry in packages is not a valid python package.

inline_importer.inliner.extract_module_name(path)

Convenience method to extract a module name from a path.

Example

>>> extract_module_name('nested/deep/module.py')
'module'
>>> extract_module_name('shallow.py')
'shallow'
Parameters

path (str) – The path of the module file

Returns

The name the module

Return type

str

Raises
  • InlinerException – If unable to determine the name of the module (there is no . in the

  • filename)

inline_importer.inliner.extract_package_name(path)

Convenience method to extract a package name from a path.

Example

>>> extract_package_name('nested/deep/module.py')
'deep'
>>> extract_package_name('nested/deep/')
'deep'
>>> extract_package_name('nested/deep')
'deep'
Parameters

path (str) – The path of the package or a module within that package.

Returns

The name of the package for the given path.

Return type

str

Raises

InlinerException – If unable to determine the name of the package.

inline_importer.inliner.get_file_source(filename)

Read the source of a file.

This convenience function is present to match the interface of get_module_source.

Parameters

filename (str) – the path of the file to read

Returns

the contents of the file

Return type

str

inline_importer.inliner.get_module_source(fullname_or_module)

Get the source of a module or file.

This function will invoke python’s import machinery to find a module (or package), then read the source.

Parameters

fullname_or_module (Union[str, ModuleType]) – Either the fully qualified module name or a module object.

Returns

the contents of the module

Return type

str

Raises

InlinerException – If unable to load the source for the given module.

inline_importer.importer

class inline_importer.importer.InlineImporter

Implements at the class level both PEP 302’s Finder and Loader protocols.

classmethod create_module(spec)

Create a module using the default machinery.

classmethod exec_module(module)

Method to execute the module.

Raises ImportError if the module has no code object.

classmethod find_spec(fullname, path=None, target=None)

Find a spec for a given module.

Because we only deal with our inlined module, we don’t have to care about path or target. The import machinery also takes care of fully resolving all names, so we just have to deal with the fullnames.

classmethod get_code(fullname)

Method to return the code object for fullname.

Should return None if not applicable (e.g. built-in module). Raise ImportError if the module cannot be found.

classmethod get_filename(fullname)

Method to return the generated filename for fullname.

Raises ImportError if the module cannot be found.

classmethod get_source(fullname)

Method to return the source for fullname.

Raise ImportError if the module cannot be found.

classmethod is_package(fullname)

Method to return whether fullname is a package.

Raise ImportError if the module cannot be found.