yamlconf: fixed multiline default vales dump

This commit is contained in:
Devaev Maxim 2019-04-12 17:04:48 +03:00
parent 782aba16a4
commit 428e4840ac
2 changed files with 32 additions and 22 deletions

View File

@ -46,7 +46,7 @@ def build_raw_from_options(options: List[str]) -> Dict[str, Any]:
raise ConfigError("No value for key %r" % (key)) raise ConfigError("No value for key %r" % (key))
section = raw section = raw
subs = list(map(str.strip, key.split("/"))) subs = list(filter(None, map(str.strip, key.split("/"))))
for sub in subs[:-1]: for sub in subs[:-1]:
section.setdefault(sub, {}) section.setdefault(sub, {})
section = section[sub] section = section[sub]

View File

@ -23,7 +23,7 @@
import textwrap import textwrap
import operator import operator
from typing import List from typing import Generator
from typing import Any from typing import Any
import yaml 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: def _inner_make_dump(config: Section, indent: int, _level: int=0) -> Generator[str, None, None]:
return "\n".join(_inner_make_dump(config))
def _inner_make_dump(config: Section, _level: int=0) -> List[str]:
lines = []
for (key, value) in sorted(config.items(), key=operator.itemgetter(0)): for (key, value) in sorted(config.items(), key=operator.itemgetter(0)):
indent = " " * _INDENT * _level
if isinstance(value, Section): if isinstance(value, Section):
lines.append("%s%s:" % (indent, key)) yield "%s%s:" % (" " * indent * _level, key)
lines += _inner_make_dump(value, _level + 1) yield from _inner_make_dump(value, indent, _level + 1)
lines.append("") yield ""
else: else:
default = config._get_default(key) # pylint: disable=protected-access default = config._get_default(key) # pylint: disable=protected-access
comment = config._get_help(key) # pylint: disable=protected-access comment = config._get_help(key) # pylint: disable=protected-access
if default == value: 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: else:
lines.append("%s# %s: %s # %s" % (indent, key, _make_yaml(default, _level), comment)) yield _make_yaml_kv(key, default, indent, _level, comment=comment, commented=True)
lines.append("%s%s: %s" % (indent, key, _make_yaml(value, _level))) yield _make_yaml_kv(key, value, indent, _level)
return lines
def _make_yaml(value: Any, level: int) -> str: def _make_yaml_kv(key: str, value: Any, indent: int, level: int, comment: str="", commented: bool=False) -> str:
dump = yaml.dump(value, indent=_INDENT, allow_unicode=True).replace("\n...\n", "").strip() text = yaml.dump(value, indent=indent, allow_unicode=True)
if isinstance(value, dict) and dump[0] != "{" or isinstance(value, list) and dump[0] != "[": text = text.replace("\n...\n", "").strip()
dump = "\n" + textwrap.indent(dump, prefix=" " * _INDENT * (level + 1)) if (
return dump 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)
if comment:
lines = text.split("\n")
lines[0] += " # " + comment
text = "\n".join(lines)
return text