Skip to content

Commit

Permalink
add logic to automatically search and import the mara_config.py file
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-schick committed Dec 6, 2023
1 parent 16f4e89 commit 5d8dc75
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
28 changes: 28 additions & 0 deletions mara_cli/_config.py
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
4 changes: 4 additions & 0 deletions mara_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def setup_commandline_commands():
logging.root.setLevel(logging.DEBUG)
log.debug("Enabled debug output via commandline")

# tries to import a mara config file if it can be found
from ._config import try_import_mara_config
try_import_mara_config()

if sys.version_info < (3, 10):
from importlib_metadata import entry_points
else:
Expand Down

0 comments on commit 5d8dc75

Please sign in to comment.