-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
65 lines (47 loc) · 1.99 KB
/
Copy pathconfig.py
File metadata and controls
65 lines (47 loc) · 1.99 KB
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
from __future__ import annotations
import configparser as cfgp
from pathlib import Path
from paraEmoji import configEmoji
conf: Conf = None
class Conf:
def __init__(self, configfile, section_name: str = "DEFAULT"):
self.configfile = configfile
self.config = cfgp.ConfigParser(
converters={"intlist": self._getintlist, "list": self._getlist, "emoji": configEmoji.from_str},
)
self.config.read(configfile)
self.section_name = section_name if section_name in self.config else "DEFAULT"
self.default = self.config["DEFAULT"]
self.section = self.config[self.section_name]
self.emojis = self.config["EMOJIS"] if "EMOJIS" in self.config else self.section # noqa
# Config file recursion, read in configuration files specified in every "ALSO_READ" key.
more_to_read = self.section.getlist("ALSO_READ", [])
read = set()
while more_to_read:
to_read = more_to_read.pop(0)
read.add(to_read)
self.config.read(to_read)
new_paths = [
path for path in self.section.getlist("ALSO_READ", []) if path not in read and path not in more_to_read
]
more_to_read.extend(new_paths)
global conf
conf = self
def __getitem__(self, key):
return self.section[key].strip()
def __getattr__(self, name):
return getattr(self.section, name)
def get(self, name, fallback=None):
result = self.section.get(name, fallback)
return result.strip() if result else result
def _getintlist(self, value):
return [int(item.strip()) for item in value.split(",")]
def _getlist(self, value):
return [item.strip() for item in value.split(",")]
def write(self):
with Path(self.configfile).open("w") as conffile:
self.config.write(conffile)
def get_conf():
if conf is None:
raise Exception("Retrieving configuration without initialisation.")
return conf