File size: 2,364 Bytes
2d31d6d 7fb97e6 2d31d6d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import os
import importlib
import pkgutil
def discover_injectors():
injectors = {}
package_dir = os.path.dirname(__file__)
for _, module_name, is_pkg in pkgutil.iter_modules([package_dir]):
if is_pkg or module_name.startswith('_'):
continue
full_module_name = f"chain_injectors.{module_name}"
try:
module = importlib.import_module(full_module_name)
if hasattr(module, 'inject') and callable(module.inject):
feature_name = getattr(module, 'FEATURE_NAME', None)
if not feature_name:
feature_name = module_name[:-9] if module_name.endswith('_injector') else module_name
chain_type = getattr(module, 'CHAIN_TYPE', None)
if not chain_type:
chain_type = f"dynamic_{feature_name}_chains"
injectors[chain_type] = module.inject
else:
print(f"Warning: Module '{full_module_name}' does not have a callable 'inject' function.")
except Exception as e:
print(f"Error importing injector module '{full_module_name}': {e}")
return injectors
def get_registered_features():
features = {}
package_dir = os.path.dirname(__file__)
for _, module_name, is_pkg in pkgutil.iter_modules([package_dir]):
if is_pkg or module_name.startswith('_'):
continue
feature_name = module_name[:-9] if module_name.endswith('_injector') else module_name
full_module_name = f"chain_injectors.{module_name}"
chain_type = f"dynamic_{feature_name}_chains"
# Honour an explicit CHAIN_TYPE the same way discover_injectors() does.
# Without this, a module that declares CHAIN_TYPE = "foo" is registered
# under "foo" by discover_injectors() but looked up here as
# "dynamic_foo_chains", so the assembler silently skips it.
try:
module = importlib.import_module(full_module_name)
declared = getattr(module, 'CHAIN_TYPE', None)
if declared:
chain_type = declared
except Exception:
pass
features[feature_name] = {
'module': full_module_name,
'chain_type': chain_type
}
return features
|