-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add logic to automatically search and import the
mara_config.py
file
- Loading branch information
1 parent
16f4e89
commit 5d8dc75
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import os | ||
import pathlib | ||
import sys | ||
import importlib.util | ||
|
||
|
||
def try_import_mara_config(): | ||
"""Tries to find and import a `mara_config.py` file.""" | ||
if 'mara_config' in sys.modules: | ||
print("A module with name 'mara_config' is already imported.", file=sys.stderr) | ||
return | ||
|
||
mara_config_path = pathlib.Path(os.environ.get('MARA_CONFIG', '')) | ||
|
||
if mara_config_path.is_dir(): | ||
mara_config_path = mara_config_path / 'mara_config.py' | ||
|
||
if not mara_config_path.exists(): | ||
# the mara_config file does not exist in the config path | ||
return | ||
|
||
try: | ||
spec = importlib.util.spec_from_file_location('mara_config', location=mara_config_path.absolute()) | ||
module = importlib.util.module_from_spec(spec) | ||
sys.modules[spec.name] = module | ||
spec.loader.exec_module(module) | ||
except Exception as e: | ||
raise ImportError(f"Could not import mara_config.py file '{mara_config_path.absolute()}'") from e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters