summaryrefslogtreecommitdiff
path: root/kvmd/yamlconf/dumper.py
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2019-02-08 06:58:08 +0300
committerDevaev Maxim <[email protected]>2019-02-08 06:58:08 +0300
commit8d3c0ec0106ac8cb779cd71cb55b7a8ff029b65d (patch)
treea7823a69bbe9cab83d73730d8cd60e2d76b6abd8 /kvmd/yamlconf/dumper.py
parent5166891dcd204678e0b5d479fcf47f644be378b5 (diff)
powerful configuration management
Diffstat (limited to 'kvmd/yamlconf/dumper.py')
-rw-r--r--kvmd/yamlconf/dumper.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/kvmd/yamlconf/dumper.py b/kvmd/yamlconf/dumper.py
new file mode 100644
index 00000000..bbee71d2
--- /dev/null
+++ b/kvmd/yamlconf/dumper.py
@@ -0,0 +1,41 @@
+# pylint: skip-file
+# infinite recursion
+
+
+import operator
+
+from typing import Tuple
+from typing import List
+from typing import Any
+
+import yaml
+
+from . import Section
+
+
+# =====
+def make_config_dump(config: Section) -> str:
+ return "\n".join(_inner_make_dump(config))
+
+
+def _inner_make_dump(config: Section, _path: Tuple[str, ...]=()) -> List[str]:
+ lines = []
+ for (key, value) in sorted(config.items(), key=operator.itemgetter(0)):
+ indent = len(_path) * " "
+ if isinstance(value, Section):
+ lines.append("{}{}:".format(indent, key))
+ lines += _inner_make_dump(value, _path + (key,))
+ lines.append("")
+ else:
+ default = config._get_default(key) # pylint: disable=protected-access
+ comment = config._get_help(key) # pylint: disable=protected-access
+ if default == value:
+ lines.append("{}{}: {} # {}".format(indent, key, _make_yaml(value), comment))
+ else:
+ lines.append("{}# {}: {} # {}".format(indent, key, _make_yaml(default), comment))
+ lines.append("{}{}: {}".format(indent, key, _make_yaml(value)))
+ return lines
+
+
+def _make_yaml(value: Any) -> str:
+ return yaml.dump(value, allow_unicode=True).replace("\n...\n", "").strip()