pynenc.util.import_app

Module Contents

Functions

build_file_path_from_module_name

Builds a file path from a module name.

create_module_spec

Creates a module specification from a file location.

load_module_from_spec

Loads a module from a given specification.

import_module_as_file

Attempts to import a module as a file.

find_pynenc_instance_in_module

Finds and returns the Pynenc app instance from a given module.

find_app_instance

Find and load a Pynenc application instance from a module or file path.

extract_module_info

Extract module filepath and app variable name from a Pynenc instance.

create_app_from_info

Create a Pynenc app instance from AppInfo.

API

pynenc.util.import_app.build_file_path_from_module_name(module_name: str) str[source]

Builds a file path from a module name.

Parameters:

module_name (str) – The name of the module.

Returns:

The file path corresponding to the module.

Raises:

ValueError – If the module name does not specify a path.

pynenc.util.import_app.create_module_spec(app_instance_name: str, file_location: str) importlib.machinery.ModuleSpec[source]

Creates a module specification from a file location.

Parameters:
  • app_instance_name (str) – The name of the app instance.

  • file_location (str) – The location of the file.

Returns:

A ModuleSpec object.

Raises:

ValueError – If the module spec could not be created.

pynenc.util.import_app.load_module_from_spec(spec: importlib.machinery.ModuleSpec) types.ModuleType[source]

Loads a module from a given specification.

Parameters:

spec (importlib.machinery.ModuleSpec) – The module specification.

Returns:

The loaded module.

Raises:

ValueError – If the loader is not found for the given spec.

pynenc.util.import_app.import_module_as_file(module_name: str, ex: Exception) types.ModuleType[source]

Attempts to import a module as a file.

Parameters:
  • module_name (str) – The name of the module.

  • ex (Exception) – The exception to be raised in case of failure.

Returns:

The imported module.

Raises:

ValueError – If the module file cannot be found.

pynenc.util.import_app.find_pynenc_instance_in_module(module: types.ModuleType) pynenc.app.Pynenc[source]

Finds and returns the Pynenc app instance from a given module.

Parameters:

module (types.ModuleType) – The module to search in.

Returns:

The found Pynenc app instance.

Raises:

ValueError – If no Pynenc app instance is found.

pynenc.util.import_app.find_app_instance(app_spec: str | None) pynenc.app.Pynenc[source]

Find and load a Pynenc application instance from a module or file path.

Parameters:

module_name (str | None) – A string that can be a module path (e.g., ‘core.src.api.backtes’) or a file path (e.g., ‘core/src/api/backtes.py’).

Returns:

The Pynenc app instance.

Raises:

ValueError – If the app cannot be loaded or no Pynenc instance is found.

pynenc.util.import_app.extract_module_info(app: pynenc.app.Pynenc) tuple[str | None, str | None][source]

Extract module filepath and app variable name from a Pynenc instance.

Parameters:

app – Pynenc application instance

Returns:

Tuple of (module_filepath, app_variable_name)

pynenc.util.import_app.create_app_from_info(app_info: pynenc.app.AppInfo) Optional[pynenc.app.Pynenc][source]

Create a Pynenc app instance from AppInfo.

This function delegates to Pynenc.from_info class method.

Parameters:

app_info – Application metadata

Returns:

Re-hydrated Pynenc app instance

Raises:

ValueError – If app can’t be re-hydrated