File: Config.py

package info (click to toggle)
plastex 3.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,132 kB
  • sloc: python: 23,341; xml: 18,076; javascript: 7,755; ansic: 46; makefile: 40; sh: 26
file content (161 lines) | stat: -rw-r--r-- 4,767 bytes parent folder | download | duplicates (2)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from plasTeX.Config import defaultConfig
from plasTeX.ConfigManager import *
from argparse import ArgumentParser
from pathlib import Path
import pytest

def test_interpolate():
    config = defaultConfig()
    config["general"]["renderer"] = "HTML5"
    config["files"]["split-level"] = 5
    config["general"]["theme"] = "%(renderer)s-%%-theme-%(split-level)d"
    assert config["general"]["theme"] == "HTML5-%-theme-5"

    config["document"]["disable-charsub"] = ["%(renderer)s-foo", "%(theme)s-bar"]
    assert config["document"]["disable-charsub"] == ["HTML5-foo", "HTML5-%-theme-5-bar"]

def test_interpolate_recursion():
    config = defaultConfig()
    config["general"]["theme"] = "%(renderer)s"
    config["general"]["renderer"] = "%(theme)s"
    with pytest.raises(RecursionError):
        x = config["general"]["theme"]

def test_interpolate_fail():
    config = defaultConfig()
    config["general"]["theme"] = "%(nonexistent)s"
    with pytest.raises(KeyError):
        x = config["general"]["theme"]

def test_stringoption():
    option = StringOption("Test", "--test", "")

    parser = ArgumentParser()
    group = parser.add_argument_group("test")
    option.registerArgparse(group)

    data = vars(parser.parse_args(["--test", "foo"]))
    option.updateFromDict(data)

    assert option.value == "foo"

def test_booloption():
    option = BooleanOption("Test", "--test !--notest", False)

    parser = ArgumentParser()
    group = parser.add_argument_group("test")
    option.registerArgparse(group)

    data = vars(parser.parse_args(["--test"]))
    option.updateFromDict(data)

    assert option.value

    data = vars(parser.parse_args(["--notest"]))
    option.updateFromDict(data)

    assert not option.value

def test_multistringoption():
    option = MultiStringOption("Test", "--test", ["a"])

    parser = ArgumentParser()
    group = parser.add_argument_group("test")
    option.registerArgparse(group)

    data = vars(parser.parse_args(["--test", "c", "d", "--test", "a", "c"]))
    option.updateFromDict(data)

    assert option.value == ["a", "c", "d", "a", "c"]

def test_dict_option(tmpdir):
    with tmpdir.as_cwd():
        Path("test.ini").write_text('''
[test]
foo=3
bar=5
test=test=7,item2=8
''')
        config = ConfigManager()
        sect = config.addSection("test")

        class TestOption(DictOption[str]):
            @classmethod
            def entryFromString(cls, entry: str) -> str:
                return entry

            def registerArgparse(self, group):
                pass

        sect["test"] = TestOption("", "", {"baz": "6"})
        config.read("test.ini")

        assert config["test"]["test"] == {"baz": "6", "foo": "3", "bar": "5", "test": "7", "item2": "8"}

def test_counter(tmpdir):
    with tmpdir.as_cwd():
        Path("test.ini").write_text('''
[counters]
chapter=4
part=2
''')
        config = defaultConfig()
        config.read("test.ini")

        parser = ArgumentParser()
        config.registerArgparse(parser)
        data = vars(parser.parse_args(["--counter", "section", "4", "--counter", "subsection", "7"]))
        config.updateFromDict(data)

        assert config["counters"]["counters"] == \
                {
                    "chapter": 4,
                    "part": 2,
                    "section": 4,
                    "subsection": 7
                }

def test_logging(tmpdir):
    with tmpdir.as_cwd():
        Path("test.ini").write_text('''
[logging]
parse.environments=DEBUG
''')
        config = defaultConfig()
        config.read("test.ini")

        parser = ArgumentParser()
        config.registerArgparse(parser)
        data = vars(parser.parse_args(["--logging", "test", "INFO"]))
        config.updateFromDict(data)

        assert config["logging"]["logging"] == \
                {
                    "parse.environments": "DEBUG",
                    "test": "INFO",
                }

def test_links(tmpdir):
    with tmpdir.as_cwd():
        Path("test.ini").write_text('''
[links]
next-url=https://example.com
next-title=The Next Document
mylink-title=AnotherTitle
''')
        config = defaultConfig()
        config.read("test.ini")

        parser = ArgumentParser()
        config.registerArgparse(parser)
        data = vars(parser.parse_args(["--link", "prev", "https://example.com/2", "Prev Document", "--link", "secondlink", "ATitle"]))
        config.updateFromDict(data)

        assert config["links"]["links"] == {
                "next-url": "https://example.com",
                "next-title": "The Next Document",
                "mylink-title": "AnotherTitle",
                "prev-url": "https://example.com/2",
                "prev-title": "Prev Document",
                "secondlink-title": "ATitle",
            }