Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add logic to automatically search and import the mara_config.py file #6

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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