|
| 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 | + |
1 | 7 | from os.path import abspath, isfile
|
2 | 8 | from os import scandir
|
3 | 9 | import importlib.util
|
4 | 10 | import sys
|
5 | 11 |
|
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 |
| - |
21 | 12 | def _filename(f):
|
22 | 13 | return f.name
|
23 | 14 |
|
24 | 15 |
|
25 |
| -def _import(module_name, path): |
| 16 | +def _import(module_name, path, loaded_configurations): |
26 | 17 | spec = importlib.util.spec_from_file_location('', path)
|
27 | 18 | module = importlib.util.module_from_spec(spec)
|
28 | 19 | spec.loader.exec_module(module)
|
29 | 20 | sys.modules[module_name] = module
|
30 | 21 |
|
31 |
| - _loaded_configurations.insert(0, module) |
| 22 | + loaded_configurations.insert(0, module) |
32 | 23 |
|
33 | 24 | print(f"🧬 loaded config '{path}'")
|
34 | 25 |
|
35 | 26 |
|
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 |
41 | 40 |
|
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 |
46 | 43 |
|
47 |
| - if f.name.startswith('__'): |
48 |
| - continue |
| 44 | + if not f.name.endswith('.py'): |
| 45 | + continue |
49 | 46 |
|
50 |
| - if not f.name.endswith('.py'): |
51 |
| - continue |
| 47 | + if f.name == f'{config_dir}.py': |
| 48 | + continue |
52 | 49 |
|
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) |
55 | 52 |
|
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}'.") |
58 | 56 |
|
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 |
0 commit comments