summaryrefslogtreecommitdiff
path: root/kvmd/yamlconf
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2019-02-15 07:20:26 +0300
committerDevaev Maxim <[email protected]>2019-02-15 07:20:26 +0300
commit7d79f48fd87083ffe38158fc456cad4737ab0242 (patch)
tree22e48f8102068e13b4680a34d0e97b7d624abfd2 /kvmd/yamlconf
parentcdddf10b5d2e4a87d8a4276e197e828f89e12be5 (diff)
big config refactoring
Diffstat (limited to 'kvmd/yamlconf')
-rw-r--r--kvmd/yamlconf/__init__.py44
1 files changed, 31 insertions, 13 deletions
diff --git a/kvmd/yamlconf/__init__.py b/kvmd/yamlconf/__init__.py
index 5dd1da3d..60be81a3 100644
--- a/kvmd/yamlconf/__init__.py
+++ b/kvmd/yamlconf/__init__.py
@@ -44,35 +44,52 @@ class Section(dict):
dict.__init__(self)
self.__meta: Dict[str, Dict[str, Any]] = {}
- def _set_meta(self, name: str, default: Any, help: str) -> None: # pylint: disable=redefined-builtin
- self.__meta[name] = {
+ def _unpack_renamed(self) -> Dict[str, Any]:
+ unpacked: Dict[str, Any] = {}
+ for (key, value) in self.items():
+ assert not isinstance(value, Section), (key, value)
+ key = (self.__meta[key]["rename"] or key)
+ unpacked[key] = value
+ return unpacked
+
+ def _set_meta(self, key: str, default: Any, help: str, rename: str) -> None: # pylint: disable=redefined-builtin
+ self.__meta[key] = {
"default": default,
"help": help,
+ "rename": rename,
}
- def _get_default(self, name: str) -> Any:
- return self.__meta[name]["default"]
+ def _get_default(self, key: str) -> Any:
+ return self.__meta[key]["default"]
- def _get_help(self, name: str) -> str:
- return self.__meta[name]["help"]
+ def _get_help(self, key: str) -> str:
+ return self.__meta[key]["help"]
- def __getattribute__(self, name: str) -> Any:
- if name in self:
- return self[name]
+ def __getattribute__(self, key: str) -> Any:
+ if key in self:
+ return self[key]
else: # For pickling
- return dict.__getattribute__(self, name)
+ return dict.__getattribute__(self, key)
class Option:
__type = type
- def __init__(self, default: Any, help: str="", type: Optional[Callable[[Any], Any]]=None) -> None: # pylint: disable=redefined-builtin
+ def __init__(
+ self,
+ default: Any,
+ help: str="", # pylint: disable=redefined-builtin
+ type: Optional[Callable[[Any], Any]]=None, # pylint: disable=redefined-builtin
+ rename: str="",
+ ) -> None:
+
self.default = default
self.help = help
self.type: Callable[[Any], Any] = (type or (self.__type(default) if default is not None else str)) # type: ignore
+ self.rename = rename
def __repr__(self) -> str:
- return "<Option(default={self.default}, type={self.type}, help={self.help})>".format(self=self)
+ return "<Option(default={self.default}, type={self.type}, help={self.help}, rename={self.rename})>".format(self=self)
# =====
@@ -93,9 +110,10 @@ def make_config(raw: Dict[str, Any], scheme: Dict[str, Any], _keys: Tuple[str, .
raise ValueError("Invalid value '{value}' for key '{key}'".format(key=full_name, value=value))
config[key] = value
config._set_meta( # pylint: disable=protected-access
- name=key,
+ key=key,
default=option.default,
help=option.help,
+ rename=option.rename,
)
elif isinstance(option, dict):
config[key] = make_config(raw.get(key, {}), option, full_key)