Merge core functionality into configuration.py
Which is the file `docker/configuration.docker.py` in our repo. The common code is then imported by `docker/ldap_config.docker.py`.
This commit is contained in:
parent
2dba2b8811
commit
58050e5287
@ -1,12 +1,73 @@
|
||||
## 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).
|
||||
|
||||
from os.path import abspath, isfile
|
||||
from os import scandir
|
||||
import importlib.util
|
||||
import sys
|
||||
|
||||
_CONFIG_DIR = '/etc/netbox/config/'
|
||||
_MAIN_CONFIG = 'configuration'
|
||||
_MODULE = 'netbox.configuration'
|
||||
_loaded_configurations = []
|
||||
def _filename(f):
|
||||
return f.name
|
||||
|
||||
|
||||
def _import(module_name, path, loaded_configurations):
|
||||
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
|
||||
|
||||
loaded_configurations.insert(0, module)
|
||||
|
||||
print(f"🧬 loaded config '{path}'")
|
||||
|
||||
|
||||
def read_configurations(config_module, config_dir, main_config):
|
||||
loaded_configurations = []
|
||||
|
||||
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.")
|
||||
|
||||
with scandir(config_dir) as it:
|
||||
for f in sorted(it, key=_filename):
|
||||
if not f.is_file():
|
||||
continue
|
||||
|
||||
if f.name.startswith('__'):
|
||||
continue
|
||||
|
||||
if not f.name.endswith('.py'):
|
||||
continue
|
||||
|
||||
if f.name == f'{config_dir}.py':
|
||||
continue
|
||||
|
||||
module_name = f"{config_module}.{f.name[:-len('.py')]}".replace(".", "_")
|
||||
_import(module_name, f.path, loaded_configurations)
|
||||
|
||||
if len(loaded_configurations) == 0:
|
||||
print(f"‼️ No configuration files found in '{config_dir}'.")
|
||||
raise ImportError(f"No configuration files found in '{config_dir}'.")
|
||||
|
||||
return loaded_configurations
|
||||
|
||||
|
||||
## 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(
|
||||
config_dir = '/etc/netbox/config/',
|
||||
config_module = 'netbox.configuration',
|
||||
main_config = 'configuration')
|
||||
|
||||
|
||||
def __getattr__(name):
|
||||
@ -16,46 +77,3 @@ def __getattr__(name):
|
||||
except:
|
||||
pass
|
||||
raise AttributeError
|
||||
|
||||
|
||||
def _filename(f):
|
||||
return f.name
|
||||
|
||||
|
||||
def _import(module_name, path):
|
||||
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
|
||||
|
||||
_loaded_configurations.insert(0, module)
|
||||
|
||||
print(f"🧬 loaded config '{path}'")
|
||||
|
||||
|
||||
_main_config_path = abspath(f'{_CONFIG_DIR}/{_MAIN_CONFIG}.py')
|
||||
if isfile(_main_config_path):
|
||||
_import(f'{_MODULE}.{_MAIN_CONFIG}', _main_config_path)
|
||||
else:
|
||||
print(f"⚠️ Main configuration '{_main_config_path}' not found.")
|
||||
|
||||
with scandir(_CONFIG_DIR) as it:
|
||||
for f in sorted(it, key=_filename):
|
||||
if not f.is_file():
|
||||
continue
|
||||
|
||||
if f.name.startswith('__'):
|
||||
continue
|
||||
|
||||
if not f.name.endswith('.py'):
|
||||
continue
|
||||
|
||||
if f.name == f'{_MAIN_CONFIG}.py':
|
||||
continue
|
||||
|
||||
module_name = f"{_MODULE}.{f.name[:-len('.py')]}".replace(".", "_")
|
||||
_import(module_name, f.path)
|
||||
|
||||
if len(_loaded_configurations) == 0:
|
||||
print(f"‼️ No configuration files found in '{_CONFIG_DIR}'.")
|
||||
raise ImportError(f"No configuration files found in '{_CONFIG_DIR}'.")
|
||||
|
@ -1,12 +1,9 @@
|
||||
from os.path import abspath, isfile
|
||||
from os import scandir
|
||||
import importlib.util
|
||||
import sys
|
||||
from .configuration import read_configurations
|
||||
|
||||
_CONFIG_DIR = '/etc/netbox/config/ldap/'
|
||||
_MAIN_CONFIG = 'ldap_config'
|
||||
_MODULE = 'netbox.configuration.ldap'
|
||||
_loaded_configurations = []
|
||||
_loaded_configurations = read_configurations(
|
||||
config_dir = '/etc/netbox/config/ldap/',
|
||||
config_module = 'netbox.configuration.ldap',
|
||||
main_config = 'ldap_config')
|
||||
|
||||
|
||||
def __getattr__(name):
|
||||
@ -16,46 +13,3 @@ def __getattr__(name):
|
||||
except:
|
||||
pass
|
||||
raise AttributeError
|
||||
|
||||
|
||||
def _filename(f):
|
||||
return f.name
|
||||
|
||||
|
||||
def _import(module_name, path):
|
||||
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
|
||||
|
||||
_loaded_configurations.insert(0, module)
|
||||
|
||||
print(f"🧬 loaded config '{path}'")
|
||||
|
||||
|
||||
_main_config_path = abspath(f'{_CONFIG_DIR}/{_MAIN_CONFIG}.py')
|
||||
if isfile(_main_config_path):
|
||||
_import(f'{_MODULE}.{_MAIN_CONFIG}', _main_config_path)
|
||||
else:
|
||||
print(f"⚠️ Main configuration '{_main_config_path}' not found.")
|
||||
|
||||
with scandir(_CONFIG_DIR) as it:
|
||||
for f in sorted(it, key=_filename):
|
||||
if not f.is_file():
|
||||
continue
|
||||
|
||||
if f.name.startswith('__'):
|
||||
continue
|
||||
|
||||
if not f.name.endswith('.py'):
|
||||
continue
|
||||
|
||||
if f.name == f'{_MAIN_CONFIG}.py':
|
||||
continue
|
||||
|
||||
module_name = f"{_MODULE}.{f.name[:-len('.py')]}".replace(".", "_")
|
||||
_import(module_name, f.path)
|
||||
|
||||
if len(_loaded_configurations) == 0:
|
||||
print(f"‼️ No configuration files found in '{_CONFIG_DIR}'.")
|
||||
raise ImportError(f"No configuration files found in '{_CONFIG_DIR}'.")
|
||||
|
Loading…
Reference in New Issue
Block a user