Fixed bug in which custom config.py module was not accessed

This commit is contained in:
TheNetworkGuy 2025-06-16 14:04:10 +00:00
parent 940f2d6afb
commit dec2cf6996

View File

@ -3,7 +3,7 @@ Module for parsing configuration from the top level config.py file
""" """
from pathlib import Path from pathlib import Path
from importlib import util from importlib import util
from os import environ from os import environ, path
from logging import getLogger from logging import getLogger
logger = getLogger(__name__) logger = getLogger(__name__)
@ -104,10 +104,18 @@ def load_env_variable(config_environvar):
def load_config_file(config_default, config_file="config.py"): def load_config_file(config_default, config_file="config.py"):
"""Returns config from config.py file""" """Returns config from config.py file"""
# Check if config.py exists and load it # Find the script path and config file next to it.
# If it does not exist, return the default config script_dir = path.dirname(path.dirname(path.abspath(__file__)))
config_path = Path(path.join(script_dir, config_file))
# If the script directory is not found, try the current working directory
if not config_path.exists():
config_path = Path(config_file) config_path = Path(config_file)
if config_path.exists():
# If both checks fail then fallback to the default config
if not config_path.exists():
return config_default
dconf = config_default.copy() dconf = config_default.copy()
# Dynamically import the config module # Dynamically import the config module
spec = util.spec_from_file_location("config", config_path) spec = util.spec_from_file_location("config", config_path)
@ -118,4 +126,3 @@ def load_config_file(config_default, config_file="config.py"):
if hasattr(config_module, key): if hasattr(config_module, key):
dconf[key] = getattr(config_module, key) dconf[key] = getattr(config_module, key)
return dconf return dconf
return config_default