summaryrefslogtreecommitdiff
path: root/kvmd/yamlconf/dumper.py
blob: 517965fea1dd4283477851d0fc42a7769c7fbf07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# ========================================================================== #
#                                                                            #
#    KVMD - The main Pi-KVM daemon.                                          #
#                                                                            #
#    Copyright (C) 2018  Maxim Devaev <[email protected]>                    #
#                                                                            #
#    This program is free software: you can redistribute it and/or modify    #
#    it under the terms of the GNU General Public License as published by    #
#    the Free Software Foundation, either version 3 of the License, or       #
#    (at your option) any later version.                                     #
#                                                                            #
#    This program is distributed in the hope that it will be useful,         #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#    GNU General Public License for more details.                            #
#                                                                            #
#    You should have received a copy of the GNU General Public License       #
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.  #
#                                                                            #
# ========================================================================== #


import textwrap
import operator

from typing import List
from typing import Any

import yaml

from . import Section


# =====
_INDENT = 4


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 = []
    for (key, value) in sorted(config.items(), key=operator.itemgetter(0)):
        indent = " " * _INDENT * _level
        if isinstance(value, Section):
            lines.append("{}{}:".format(indent, key))
            lines += _inner_make_dump(value, _level + 1)
            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, _level), comment))
            else:
                lines.append("{}# {}: {} # {}".format(indent, key, _make_yaml(default, _level), comment))
                lines.append("{}{}: {}".format(indent, key, _make_yaml(value, _level)))
    return lines


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