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
|
# SPDX-License-Identifier: AGPL-3.0-or-later
# Loading Hydrilla server configuration file.
#
# This file is part of Hydrilla
#
# Copyright (C) 2022 Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
#
# I, Wojtek Kosior, thereby promise not to sue for violation of this
# file's license. Although I request that you do not make use this code
# in a proprietary program, I am not going to enforce this in court.
# Enable using with Python 3.7.
from __future__ import annotations
import json
import typing as t
from pathlib import Path
import jsonschema # type: ignore
from ..translations import smart_gettext as _
from ..exceptions import HaketiloException
from .. import json_instances
config_schema = {
'$schema': 'http://json-schema.org/draft-07/schema#',
'type': 'object',
'properties': {
'malcontent_dir': {
'type': 'string'
},
'hydrilla_project_url': {
'type': 'string'
},
'try_configs': {
'type': 'array',
'items': {
'type': 'string'
}
},
'use_configs': {
'type': 'array',
'items': {
'type': 'string'
}
},
'port': {
'type': 'integer',
'minimum': 0,
'maximum': 65535
},
'werror': {
'type': 'boolean'
},
'verify_files': {
'type': 'boolean'
}
}
}
here = Path(__file__).resolve().parent
def load(config_paths: list[Path]=[here / 'config.json'],
can_fail: list[bool]=[]) -> dict:
config: dict[str, t.Any] = {}
bools_missing = max(0, len(config_paths) - len(can_fail))
config_paths = [*config_paths]
can_fail = [*can_fail[:len(config_paths)], *([False] * bools_missing)]
while config_paths:
path = config_paths.pop()
fail_ok = can_fail.pop()
try:
json_text = path.read_text()
except Exception as e:
if fail_ok:
continue
raise e from None
new_config = json.loads(json_instances.strip_json_comments(json_text))
jsonschema.validate(new_config, config_schema)
config.update(new_config)
if 'malcontent_dir' in new_config:
malcontent_path_relative_to = path.parent
for key, failure_ok in [('try_configs', True), ('use_configs', False)]:
paths = new_config.get(key, [])
paths.reverse()
config_paths.extend(paths)
can_fail.extend([failure_ok] * len(paths))
if 'malcontent_dir' in config:
malcontent_dir_str = config['malcontent_dir']
malcontent_dir_path = malcontent_path_relative_to / malcontent_dir_str
config['malcontent_dir'] = str(malcontent_dir_path)
for key in ('try_configs', 'use_configs'):
if key in config:
config.pop(key)
return config
|