-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
56 lines (43 loc) · 1.57 KB
/
settings.py
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
# pylint: disable=missing-function-docstring
# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
# pylint: disable=broad-exception-caught
# pylint: disable=multiple-imports
import os
import toml
from appdirs import user_config_dir
class Config:
def __init__(self, config_path=None):
if config_path is None or not os.path.exists(config_path):
config_path = os.path.join(user_config_dir(), "qthon", "config.toml")
if not os.path.exists(config_path):
self.make_config(config_path)
super().__init__()
self.config_path = config_path
self.parsed_cfg = self.load_config(config_path)
def print_config(self):
__import__("pprint").pprint(self.parsed_cfg)
def load_config(self, path):
with open(path, "r") as f:
return toml.load(f)
def update_config(self, new_settings):
with open(self.config_path, "w") as f:
toml.dump(new_settings, f)
def make_config(self, path):
defaults = {
"default_zoom": 128,
"water_port": 9742,
"undo_limit": 0,
"hide_item": {
"sidebar": False,
"statusbar": False,
"toolbar": False,
},
"move_item": {"sidebar": False, "toolbar": False},
}
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as f:
toml.dump(defaults, f)
if __name__ == "__main__":
cfg = Config("./config.toml")
print(cfg.parsed_cfg["default_zoom"])