confingy.utils.imports#
imports
#
Import utility functions for confingy.
get_module_name
#
get_module_name(obj: Any) -> str
Get the actual module name for an object, resolving main to the real module name.
When a Python script is run directly (e.g., python script.py), classes and functions
defined in that script have module == 'main'. This function attempts to resolve
the actual module name by inspecting the file path.
Source code in src/confingy/utils/imports.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
derive_module_name
#
derive_module_name(file_path: Path) -> str
Derive the module name from a file path relative to the current working directory.
The current working directory is assumed to be the root of the module structure. For example, if cwd is /root/project and file is /root/project/foo/bar/baz.py, the module name will be foo.bar.baz.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Path
|
Path to the Python file |
required |
Returns:
| Type | Description |
|---|---|
str
|
The derived module name (e.g., "training.fingys.apollo.my_module") |
Source code in src/confingy/utils/imports.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
load_variable_from_file
#
load_variable_from_file(
file_spec: str, default_name: str = "config"
) -> Any
Load a variable from a Python file or dotted module path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_spec
|
str
|
File path or dotted module path with optional variable name. Supported formats: - "path/to/file.py" (file path) - "path/to/file.py::variable_name" (file path with variable) - "path.to.module" (dotted module path) - "path.to.module::variable_name" (dotted module path with variable) If variable name is not specified, defaults to default_name. |
required |
default_name
|
str
|
Default variable name to use if not specified in file_spec |
'config'
|
Returns:
| Type | Description |
|---|---|
Any
|
The variable loaded from the file |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file_spec format is invalid or variable is not found |
FileNotFoundError
|
If the file doesn't exist |
Source code in src/confingy/utils/imports.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |