Skip to content

Commit 58050e5

Browse files
committed
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`.
1 parent 2dba2b8 commit 58050e5

File tree

2 files changed

+60
-88
lines changed

2 files changed

+60
-88
lines changed

docker/configuration.docker.py

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,79 @@
1+
## Generic Parts
2+
# These functions are providing the functionality to load
3+
# arbitrary configuration files.
4+
#
5+
# They can be imported by other code (see `ldap_config.py` for an example).
6+
17
from os.path import abspath, isfile
28
from os import scandir
39
import importlib.util
410
import sys
511

6-
_CONFIG_DIR = '/etc/netbox/config/'
7-
_MAIN_CONFIG = 'configuration'
8-
_MODULE = 'netbox.configuration'
9-
_loaded_configurations = []
10-
11-
12-
def __getattr__(name):
13-
for config in _loaded_configurations:
14-
try:
15-
return getattr(config, name)
16-
except:
17-
pass
18-
raise AttributeError
19-
20-
2112
def _filename(f):
2213
return f.name
2314

2415

25-
def _import(module_name, path):
16+
def _import(module_name, path, loaded_configurations):
2617
spec = importlib.util.spec_from_file_location('', path)
2718
module = importlib.util.module_from_spec(spec)
2819
spec.loader.exec_module(module)
2920
sys.modules[module_name] = module
3021

31-
_loaded_configurations.insert(0, module)
22+
loaded_configurations.insert(0, module)
3223

3324
print(f"🧬 loaded config '{path}'")
3425

3526

36-
_main_config_path = abspath(f'{_CONFIG_DIR}/{_MAIN_CONFIG}.py')
37-
if isfile(_main_config_path):
38-
_import(f'{_MODULE}.{_MAIN_CONFIG}', _main_config_path)
39-
else:
40-
print(f"⚠️ Main configuration '{_main_config_path}' not found.")
27+
def read_configurations(config_module, config_dir, main_config):
28+
loaded_configurations = []
29+
30+
main_config_path = abspath(f'{config_dir}/{main_config}.py')
31+
if isfile(main_config_path):
32+
_import(f'{config_module}.{main_config}', main_config_path, loaded_configurations)
33+
else:
34+
print(f"⚠️ Main configuration '{main_config_path}' not found.")
35+
36+
with scandir(config_dir) as it:
37+
for f in sorted(it, key=_filename):
38+
if not f.is_file():
39+
continue
4140

42-
with scandir(_CONFIG_DIR) as it:
43-
for f in sorted(it, key=_filename):
44-
if not f.is_file():
45-
continue
41+
if f.name.startswith('__'):
42+
continue
4643

47-
if f.name.startswith('__'):
48-
continue
44+
if not f.name.endswith('.py'):
45+
continue
4946

50-
if not f.name.endswith('.py'):
51-
continue
47+
if f.name == f'{config_dir}.py':
48+
continue
5249

53-
if f.name == f'{_MAIN_CONFIG}.py':
54-
continue
50+
module_name = f"{config_module}.{f.name[:-len('.py')]}".replace(".", "_")
51+
_import(module_name, f.path, loaded_configurations)
5552

56-
module_name = f"{_MODULE}.{f.name[:-len('.py')]}".replace(".", "_")
57-
_import(module_name, f.path)
53+
if len(loaded_configurations) == 0:
54+
print(f"‼️ No configuration files found in '{config_dir}'.")
55+
raise ImportError(f"No configuration files found in '{config_dir}'.")
5856

59-
if len(_loaded_configurations) == 0:
60-
print(f"‼️ No configuration files found in '{_CONFIG_DIR}'.")
61-
raise ImportError(f"No configuration files found in '{_CONFIG_DIR}'.")
57+
return loaded_configurations
58+
59+
60+
## Specific Parts
61+
# This section's code actually loads the various configuration files
62+
# into the module with the given name.
63+
# It contains the logic to resolve arbitrary configuration options by
64+
# levaraging dynamic programming using `__getattr__`.
65+
66+
67+
_loaded_configurations = read_configurations(
68+
config_dir = '/etc/netbox/config/',
69+
config_module = 'netbox.configuration',
70+
main_config = 'configuration')
71+
72+
73+
def __getattr__(name):
74+
for config in _loaded_configurations:
75+
try:
76+
return getattr(config, name)
77+
except:
78+
pass
79+
raise AttributeError

docker/ldap_config.docker.py

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
from os.path import abspath, isfile
2-
from os import scandir
3-
import importlib.util
4-
import sys
1+
from .configuration import read_configurations
52

6-
_CONFIG_DIR = '/etc/netbox/config/ldap/'
7-
_MAIN_CONFIG = 'ldap_config'
8-
_MODULE = 'netbox.configuration.ldap'
9-
_loaded_configurations = []
3+
_loaded_configurations = read_configurations(
4+
config_dir = '/etc/netbox/config/ldap/',
5+
config_module = 'netbox.configuration.ldap',
6+
main_config = 'ldap_config')
107

118

129
def __getattr__(name):
@@ -16,46 +13,3 @@ def __getattr__(name):
1613
except:
1714
pass
1815
raise AttributeError
19-
20-
21-
def _filename(f):
22-
return f.name
23-
24-
25-
def _import(module_name, path):
26-
spec = importlib.util.spec_from_file_location('', path)
27-
module = importlib.util.module_from_spec(spec)
28-
spec.loader.exec_module(module)
29-
sys.modules[module_name] = module
30-
31-
_loaded_configurations.insert(0, module)
32-
33-
print(f"🧬 loaded config '{path}'")
34-
35-
36-
_main_config_path = abspath(f'{_CONFIG_DIR}/{_MAIN_CONFIG}.py')
37-
if isfile(_main_config_path):
38-
_import(f'{_MODULE}.{_MAIN_CONFIG}', _main_config_path)
39-
else:
40-
print(f"⚠️ Main configuration '{_main_config_path}' not found.")
41-
42-
with scandir(_CONFIG_DIR) as it:
43-
for f in sorted(it, key=_filename):
44-
if not f.is_file():
45-
continue
46-
47-
if f.name.startswith('__'):
48-
continue
49-
50-
if not f.name.endswith('.py'):
51-
continue
52-
53-
if f.name == f'{_MAIN_CONFIG}.py':
54-
continue
55-
56-
module_name = f"{_MODULE}.{f.name[:-len('.py')]}".replace(".", "_")
57-
_import(module_name, f.path)
58-
59-
if len(_loaded_configurations) == 0:
60-
print(f"‼️ No configuration files found in '{_CONFIG_DIR}'.")
61-
raise ImportError(f"No configuration files found in '{_CONFIG_DIR}'.")

0 commit comments

Comments
 (0)