-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigfy_build.py
98 lines (75 loc) · 2.84 KB
/
configfy_build.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import importlib
from typing import Any, Dict, List, Callable
class BuildConfigException(Exception):
"""Raised in case of Configuration missmatch"""
pass
class Build:
def __init__(self, function_name, context, scope, name):
self.function_name = function_name
self.scope = scope
self.name = name
self.context = context
self.build_function = None
self._built_instance = None
def build(self, config=None):
if self.build_function is None:
self.build_function = _load_function_dyn(self.function_name)
return self.build_function(config or self.scope)
def build_once(self, config=None):
if self._built_instance is None:
self._built_instance = self.build(config)
return self._built_instance
def build_component(self, component_name):
return self.context.build(self.scope[component_name])
@classmethod
def from_scope(cls, scope, context):
return cls(
function_name=scope["function_name"],
context=context,
scope=scope,
name=scope["name"],
)
class ConfigBuild:
def __init__(self):
self._registry: Dict[str, Build] = {}
self.entrypoint = None
def build_one(self, name) -> Any:
return self.get(name).build_once()
def build_entrypoint(self):
return self.build(self.entrypoint)
def build(self, name: str) -> Any:
return self.get(name).build()
def get(self, name: str) -> Build:
try:
builder = self.typename_registry[name]
except KeyError as e:
raise BuildConfigException(
f"typename `{name}` should be found in the registry"
) from e
return builder
def load_dict(self, config_dict: Dict[str, Any]):
try:
components: List[Dict[str, Any]] = config_dict["components"]
except KeyError as e:
raise BuildConfigException(
"`components` field missing from the config"
) from e
for component_config in components:
builder = Build.from_scope(component_config, self)
self._registry[builder.name] = builder
self.entrypoint = config_dict["entrypoint"]
def load_json(self, json_filename: str) -> None:
import json
with open(json_filename, "r") as json_file:
config_dict = json.load(json_file)
self.load_dict(config_dict)
@classmethod
def from_json(cls, json_filename: str) -> "ConfigBuild":
obj = cls()
obj.load_json(json_filename)
return obj
def _load_function_dyn(function_path: str) -> Callable[[Build], Any]:
from_where, function_name = function_path.rsplit(".", 1)
module = importlib.import_module(from_where)
function = getattr(module, function_name)
return function