mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 09:10:30 +08:00
yamlconf: fixed multiline default vales dump
This commit is contained in:
parent
782aba16a4
commit
428e4840ac
@ -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]
|
||||
|
||||
@ -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(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
|
||||
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)
|
||||
|
||||
if comment:
|
||||
lines = text.split("\n")
|
||||
lines[0] += " # " + comment
|
||||
text = "\n".join(lines)
|
||||
return text
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user