2020-10-26 08:43:11 -05:00
|
|
|
## Generic Parts
|
|
|
|
# These functions are providing the functionality to load
|
|
|
|
# arbitrary configuration files.
|
|
|
|
#
|
|
|
|
# They can be imported by other code (see `ldap_config.py` for an example).
|
|
|
|
|
2018-02-22 04:58:36 -06:00
|
|
|
import importlib.util
|
|
|
|
import sys
|
2021-02-08 04:59:33 -06:00
|
|
|
from os import scandir
|
|
|
|
from os.path import abspath, isfile
|
2018-02-22 04:58:36 -06:00
|
|
|
|
2020-11-10 08:23:07 -06:00
|
|
|
|
2020-10-17 19:34:45 -05:00
|
|
|
def _filename(f):
|
2021-02-08 04:59:33 -06:00
|
|
|
return f.name
|
2020-10-17 19:34:45 -05:00
|
|
|
|
|
|
|
|
2020-10-26 08:43:11 -05:00
|
|
|
def _import(module_name, path, loaded_configurations):
|
2021-02-08 04:59:33 -06:00
|
|
|
spec = importlib.util.spec_from_file_location("", path)
|
|
|
|
module = importlib.util.module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(module)
|
|
|
|
sys.modules[module_name] = module
|
2020-10-17 19:34:45 -05:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
loaded_configurations.insert(0, module)
|
2020-10-17 19:34:45 -05:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
print(f"🧬 loaded config '{path}'")
|
2020-10-17 19:34:45 -05:00
|
|
|
|
|
|
|
|
2020-10-26 08:43:11 -05:00
|
|
|
def read_configurations(config_module, config_dir, main_config):
|
2021-02-08 04:59:33 -06:00
|
|
|
loaded_configurations = []
|
2020-10-26 08:43:11 -05:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
main_config_path = abspath(f"{config_dir}/{main_config}.py")
|
|
|
|
if isfile(main_config_path):
|
|
|
|
_import(f"{config_module}.{main_config}", main_config_path, loaded_configurations)
|
|
|
|
else:
|
|
|
|
print(f"⚠️ Main configuration '{main_config_path}' not found.")
|
2020-10-26 08:43:11 -05:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
with scandir(config_dir) as it:
|
|
|
|
for f in sorted(it, key=_filename):
|
|
|
|
if not f.is_file():
|
|
|
|
continue
|
2020-10-17 19:34:45 -05:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
if f.name.startswith("__"):
|
|
|
|
continue
|
2020-10-17 19:34:45 -05:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
if not f.name.endswith(".py"):
|
|
|
|
continue
|
2020-10-17 19:34:45 -05:00
|
|
|
|
2021-02-23 15:50:16 -06:00
|
|
|
if f.name == f"{main_config}.py":
|
|
|
|
continue
|
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
if f.name == f"{config_dir}.py":
|
|
|
|
continue
|
2020-10-17 19:34:45 -05:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
module_name = f"{config_module}.{f.name[:-len('.py')]}".replace(".", "_")
|
|
|
|
_import(module_name, f.path, loaded_configurations)
|
2020-10-17 19:34:45 -05:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
if len(loaded_configurations) == 0:
|
|
|
|
print(f"‼️ No configuration files found in '{config_dir}'.")
|
|
|
|
raise ImportError(f"No configuration files found in '{config_dir}'.")
|
2020-10-17 19:34:45 -05:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
return loaded_configurations
|
2020-10-26 08:43:11 -05:00
|
|
|
|
|
|
|
|
|
|
|
## Specific Parts
|
|
|
|
# This section's code actually loads the various configuration files
|
|
|
|
# into the module with the given name.
|
|
|
|
# It contains the logic to resolve arbitrary configuration options by
|
|
|
|
# levaraging dynamic programming using `__getattr__`.
|
|
|
|
|
|
|
|
|
|
|
|
_loaded_configurations = read_configurations(
|
2021-02-08 04:59:33 -06:00
|
|
|
config_dir="/etc/netbox/config/",
|
|
|
|
config_module="netbox.configuration",
|
|
|
|
main_config="configuration",
|
|
|
|
)
|
2020-10-26 08:43:11 -05:00
|
|
|
|
|
|
|
|
|
|
|
def __getattr__(name):
|
2021-02-08 04:59:33 -06:00
|
|
|
for config in _loaded_configurations:
|
|
|
|
try:
|
|
|
|
return getattr(config, name)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
raise AttributeError
|
2021-11-30 04:19:49 -06:00
|
|
|
|
|
|
|
|
|
|
|
def __dir__():
|
|
|
|
names = []
|
|
|
|
for config in _loaded_configurations:
|
|
|
|
names.extend(config.__dir__())
|
|
|
|
return names
|