-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration_manager.py
67 lines (54 loc) · 2.83 KB
/
configuration_manager.py
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
64
65
66
67
from base_classes import Component
from typing import Dict, Union, List, NoReturn
import xml.etree.ElementTree as ElementTree
import importlib
import inspect
import logging
class ComponentLoadError(Exception):
""" Raised when a component's file does not exist or when a component
of the correct type cannot be found in the file.
"""
def __init__(self, msg: str):
super(ComponentLoadError, self).__init__(self, msg)
class ComponentConfigureError(Exception):
""" Raised when the configuration file does not contain data for a component.
"""
def __init__(self, msg: str):
super(ComponentConfigureError, self).__init__(self, msg)
class ConfigurationManager:
def __init__(self, config_file_name: str):
self.config_tree = ElementTree.parse(config_file_name)
self.config_root = self.config_tree.getroot()
async def load_all_components(self) -> Dict[str, Union[Component, List[Component]]]:
out = dict()
frame_generator_module_name = self.config_root.find("FrameGenerator").text
out["FRAME_GENERATOR"] = await self.load_component("frame_generators." + frame_generator_module_name)
processor_module_name = self.config_root.find("Processor").text
out["PROCESSOR"] = await self.load_component("processors." + processor_module_name)
postprocessor_root = self.config_tree.find("PostProcessors")
postprocessor_module_names = [element.text for element in postprocessor_root.findall("PostProcessor")]
out["POSTPROCESSORS"] = list()
for name in postprocessor_module_names:
out["POSTPROCESSORS"].append(await self.load_component("postprocessors." + name))
return out
async def load_component(self, module_name: str) -> Component:
try:
module = importlib.import_module(module_name)
except ImportError:
raise ComponentLoadError(module_name) # ImportError
for name, obj in inspect.getmembers(module):
if inspect.isclass(obj) and inspect.getmro(obj)[2] == Component:
component = obj()
await self.setup_component(component, module_name.split(".")[1])
return component
# no components found in file
raise ComponentLoadError("No component class found in " + module_name)
async def setup_component(self, obj: Component, name: str) -> NoReturn:
for element in self.config_root.find("ComponentData").findall("Component"):
if element.attrib.get("name") == name:
logging.debug("Setting up component " + name)
await obj.setup(element)
logging.debug("Setup for component {} complete".format(name))
return
# no configuration found
raise ComponentConfigureError("Missing configuration for " + obj.__class__.__name__)