pynenc.util.import_app¶
Utilities for discovering and loading Pynenc application instances.
The CLI uses --app to locate the user’s Pynenc instance. Accepted
formats (see :func:find_app_instance):
auto-discovery – no
--appscans the current directory and succeeds when exactly one importable Python file defines aPynencinstance.module.attr –
tasks.appimportstasksmodule, finds thePynencinstance.package.module –
mypackage.tasksstandard Python import.file path –
path/to/tasks.pyloads the file directly.
Key components:
find_app_instance: Main entry point for
--appresolution.extract_module_info: Extracts module metadata from a live Pynenc instance.
create_app_from_info: Re-hydrates a Pynenc instance from stored AppInfo.
Module Contents¶
Functions¶
Reject common format mistakes with actionable error messages. |
|
Check whether the spec looks like a filesystem path rather than a module name. |
|
Scan a loaded module for a |
|
Scan a loaded module for public |
|
Return top-level Python files to inspect for local app auto-discovery. |
|
Cheaply filter out helper scripts before importing app candidates. |
|
Auto-discover a single local |
|
Load a |
|
Resolve an |
|
Resolve a dotted |
|
Find and load a |
|
Scan loaded non-pynenc modules for one that holds |
|
Return the public attribute name in |
|
Extract module name, filepath, and app variable name from a Pynenc instance. |
|
Try to import the original module and retrieve the app by variable name. |
|
Scan already-imported modules for a |
|
Check a single module for a |
|
Re-hydrate a |
Data¶
API¶
- pynenc.util.import_app.logger¶
‘getLogger(…)’
- pynenc.util.import_app.APP_FORMAT_HELP = <Multiline-String>¶
- pynenc.util.import_app._validate_app_spec(app_spec: str) None[source]¶
Reject common format mistakes with actionable error messages.
- Parameters:
app_spec (str) – The raw
--appvalue from the CLI.- Raises:
ValueError – If the format is invalid.
- pynenc.util.import_app._is_file_path(app_spec: str) bool[source]¶
Check whether the spec looks like a filesystem path rather than a module name.
- pynenc.util.import_app._find_pynenc_in_module(module: types.ModuleType) pynenc.app.Pynenc[source]¶
Scan a loaded module for a
Pynencinstance.- Parameters:
module (types.ModuleType) – The module to scan.
- Returns:
The first
Pynencinstance found.- Raises:
ValueError – If no instance is found.
- pynenc.util.import_app._find_pynenc_instances_in_module(module: types.ModuleType) list[tuple[str, pynenc.app.Pynenc]][source]¶
Scan a loaded module for public
Pynencinstances.- Parameters:
module (types.ModuleType) – The module to scan.
- Returns:
(variable_name, app)pairs, de-duplicated by object identity.
- pynenc.util.import_app._iter_auto_discovery_files() list[pathlib.Path][source]¶
Return top-level Python files to inspect for local app auto-discovery.
- pynenc.util.import_app._looks_like_pynenc_app_source(path: pathlib.Path) bool[source]¶
Cheaply filter out helper scripts before importing app candidates.
- pynenc.util.import_app._find_single_app_in_current_directory() pynenc.app.Pynenc[source]¶
Auto-discover a single local
Pynencinstance.This intentionally scans only top-level Python files in the current working directory. If that finds zero or multiple apps, the caller must pass
--appexplicitly.- Returns:
The discovered
Pynencinstance.- Raises:
ValueError – If discovery finds zero or multiple apps.
- pynenc.util.import_app._load_module_from_file(file_path: str) types.ModuleType[source]¶
Load a
.pyfile as a module and register it insys.modules.The file’s directory is added to
sys.pathso child processes (e.g.multiprocessing.spawn) can re-import the module by name.- Parameters:
file_path (str) – Absolute or relative path to the
.pyfile.- Returns:
The loaded module.
- Raises:
ValueError – If the file does not exist or the spec cannot be created.
ModuleNotFoundError – If the file has unresolvable imports.
- pynenc.util.import_app._resolve_file_path(app_spec: str) types.ModuleType[source]¶
Resolve an
--appvalue that is a filesystem path.- Parameters:
app_spec (str) – A path like
path/to/tasks.pyorpath/to/tasks.- Returns:
The loaded module.
- pynenc.util.import_app._resolve_dotted_path(app_spec: str) types.ModuleType[source]¶
Resolve a dotted
--appvalue liketasks.appormypackage.tasks.Strategies (tried in order):
importlib.import_module(app_spec)— works for installed packages.Import the parent module (last component treated as attribute name), e.g.
pkg.mod.app→ importpkg.mod.Treat the first component as a local
.pyfile in cwd, e.g.tasks.app→ loadtasks.py.
- Parameters:
app_spec (str) – The dotted module path.
- Returns:
The loaded module.
- Raises:
ValueError – If no strategy succeeds.
- pynenc.util.import_app.find_app_instance(app_spec: str | None = None) pynenc.app.Pynenc[source]¶
Find and load a
Pynencapplication instance.If
app_specis empty, pynenc scans top-level Python files in the current directory and succeeds only when it finds exactly onePynencinstance.Accepted explicit
--appformats:tasks.app– loadstasks.pyfrom the current directory, scans for aPynenc()instance.mypackage.tasks– standardimportlib.import_module.path/to/tasks.py– loads the file directly.
- Parameters:
app_spec (str | None) – The
--appvalue from the CLI.- Returns:
The
Pynencapplication instance.- Raises:
ValueError – If discovery fails, the spec is malformed, or the target has no Pynenc instance.
- pynenc.util.import_app._find_app_in_user_modules(app: pynenc.app.Pynenc) tuple[str, str, str] | None[source]¶
Scan loaded non-pynenc modules for one that holds
appas a top-level variable.The
Pynencclass is defined inpynenc.app, soapp.__module__always returns"pynenc.app"— not the user module whereapp = Pynenc()was written. This helper finds the correct user module by identity-checking attributes across all loaded modules.- Parameters:
app (Pynenc) – The application instance to locate.
- Returns:
(module_name, module_filepath, variable_name)orNone.
- pynenc.util.import_app._find_app_variable_in_module(module: types.ModuleType, app: pynenc.app.Pynenc) str | None[source]¶
Return the public attribute name in
modulewhose value isapp.- Parameters:
module (types.ModuleType) – Module to inspect.
app (Pynenc) – The instance to match by identity.
- Returns:
Attribute name, or
Noneif not found.
- pynenc.util.import_app.extract_module_info(app: pynenc.app.Pynenc) tuple[str | None, str | None, str | None][source]¶
Extract module name, filepath, and app variable name from a Pynenc instance.
Scans loaded user modules to find the one that holds
appas a top-level variable. Falls back toapp.__module__when no user module claims it (e.g. during unit tests).- Parameters:
app (Pynenc) – Pynenc application instance.
- Returns:
Tuple of (module_name, module_filepath, app_variable_name).
- pynenc.util.import_app._import_app_from_module(app_info: pynenc.app_info.AppInfo) pynenc.app.Pynenc | None[source]¶
Try to import the original module and retrieve the app by variable name.
- Parameters:
app_info (AppInfo) – Application metadata with module path and variable.
- Returns:
The matching
Pynencinstance, orNone.
- pynenc.util.import_app._scan_loaded_modules(app_id: str) pynenc.app.Pynenc | None[source]¶
Scan already-imported modules for a
Pynencinstance matchingapp_id.- Parameters:
app_id (str) – The application ID to match.
- Returns:
The matching instance, or
None.
- pynenc.util.import_app._find_pynenc_by_id_in_module(module: types.ModuleType, mod_name: str, app_id: str) pynenc.app.Pynenc | None[source]¶
Check a single module for a
Pynencinstance with the givenapp_id.- Parameters:
module (types.ModuleType) – The module to inspect.
mod_name (str) – Module name for logging.
app_id (str) – The application ID to match.
- Returns:
The matching instance, or
None.
- pynenc.util.import_app.create_app_from_info(app_info: pynenc.app_info.AppInfo) pynenc.app.Pynenc | None[source]¶
Re-hydrate a
Pynencinstance from storedAppInfometadata.Strategies (tried in order):
Import the original module and retrieve the named variable.
Scan already-imported modules for a matching
app_id.
- Parameters:
app_info (AppInfo) – Application metadata.
- Returns:
The re-hydrated instance, or
Noneif not found.