diff --git a/mara_cli/_config.py b/mara_cli/_config.py new file mode 100644 index 0000000..ba7285c --- /dev/null +++ b/mara_cli/_config.py @@ -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 diff --git a/mara_cli/cli.py b/mara_cli/cli.py index d4bac32..32c5fd5 100644 --- a/mara_cli/cli.py +++ b/mara_cli/cli.py @@ -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: