diff options
author | Devaev Maxim <[email protected]> | 2019-04-12 17:04:48 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2019-04-12 17:04:48 +0300 |
commit | 428e4840ac001fc2edda53285bb1c6e0677d6bac (patch) | |
tree | 7b6f736200f9dc99405695916b0c338a49608fc7 | |
parent | 782aba16a4c347ccb2428a79ebc2a582b0f9b595 (diff) |
yamlconf: fixed multiline default vales dump
-rw-r--r-- | kvmd/yamlconf/__init__.py | 2 | ||||
-rw-r--r-- | kvmd/yamlconf/dumper.py | 52 |
2 files changed, 32 insertions, 22 deletions
diff --git a/kvmd/yamlconf/__init__.py b/kvmd/yamlconf/__init__.py index bfda9b78..99b67255 100644 --- a/kvmd/yamlconf/__init__.py +++ b/kvmd/yamlconf/__init__.py @@ -46,7 +46,7 @@ def build_raw_from_options(options: List[str]) -> Dict[str, Any]: raise ConfigError("No value for key %r" % (key)) section = raw - subs = list(map(str.strip, key.split("/"))) + subs = list(filter(None, map(str.strip, key.split("/")))) for sub in subs[:-1]: section.setdefault(sub, {}) section = section[sub] diff --git a/kvmd/yamlconf/dumper.py b/kvmd/yamlconf/dumper.py index 93bced3a..6dac6baa 100644 --- a/kvmd/yamlconf/dumper.py +++ b/kvmd/yamlconf/dumper.py @@ -23,7 +23,7 @@ import textwrap import operator -from typing import List +from typing import Generator from typing import Any import yaml @@ -32,34 +32,44 @@ from . import Section # ===== -_INDENT = 4 +def make_config_dump(config: Section, indent: int=4) -> str: + return "\n".join(_inner_make_dump(config, indent)) -def make_config_dump(config: Section) -> str: - return "\n".join(_inner_make_dump(config)) - - -def _inner_make_dump(config: Section, _level: int=0) -> List[str]: - lines = [] +def _inner_make_dump(config: Section, indent: int, _level: int=0) -> Generator[str, None, None]: for (key, value) in sorted(config.items(), key=operator.itemgetter(0)): - indent = " " * _INDENT * _level if isinstance(value, Section): - lines.append("%s%s:" % (indent, key)) - lines += _inner_make_dump(value, _level + 1) - lines.append("") + yield "%s%s:" % (" " * indent * _level, key) + yield from _inner_make_dump(value, indent, _level + 1) + yield "" else: default = config._get_default(key) # pylint: disable=protected-access comment = config._get_help(key) # pylint: disable=protected-access if default == value: - lines.append("%s%s: %s # %s" % (indent, key, _make_yaml(value, _level), comment)) + yield _make_yaml_kv(key, value, indent, _level, comment=comment) else: - lines.append("%s# %s: %s # %s" % (indent, key, _make_yaml(default, _level), comment)) - lines.append("%s%s: %s" % (indent, key, _make_yaml(value, _level))) - return lines + yield _make_yaml_kv(key, default, indent, _level, comment=comment, commented=True) + yield _make_yaml_kv(key, value, indent, _level) + + +def _make_yaml_kv(key: str, value: Any, indent: int, level: int, comment: str="", commented: bool=False) -> str: + text = yaml.dump(value, indent=indent, allow_unicode=True) + text = text.replace("\n...\n", "").strip() + if ( + isinstance(value, dict) and text[0] != "{" + or isinstance(value, list) and text[0] != "[" + ): + text = "\n" + textwrap.indent(text, prefix=" " * indent) + else: + text = " " + text + prefix = " " * indent * level + if commented: + prefix = prefix + "# " + text = textwrap.indent("%s:%s" % (key, text), prefix=prefix) -def _make_yaml(value: Any, level: int) -> str: - dump = yaml.dump(value, indent=_INDENT, allow_unicode=True).replace("\n...\n", "").strip() - if isinstance(value, dict) and dump[0] != "{" or isinstance(value, list) and dump[0] != "[": - dump = "\n" + textwrap.indent(dump, prefix=" " * _INDENT * (level + 1)) - return dump + if comment: + lines = text.split("\n") + lines[0] += " # " + comment + text = "\n".join(lines) + return text |