-
Notifications
You must be signed in to change notification settings - Fork 5
/
config_mgr.py
executable file
·70 lines (56 loc) · 1.76 KB
/
config_mgr.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
import json
from os import path
"""
Configuration Manager
Configuration is located in ./user_conf.json
It will include:
- Canvas configuration
- Wallpaper configuration
- All courses configuration
"""
class ConfigMGR:
configuration = {}
def __init__(self):
if not path.exists("user_conf.json"):
# Create this configuration file
self.configuration = {
"version": 1,
}
self.write_conf()
else:
self.force_read()
if self.configuration["version"] != 1:
raise Exception("Error: Configuration file version mismatch!")
def write_conf(self):
"""
Write configuration to the local file.
"""
self.check_health()
with open("./user_conf.json", "w", encoding="utf-8", errors="ignore") as f:
json.dump(self.configuration, f, ensure_ascii=False, indent=4)
def get_conf(self):
return self.configuration
def remove_key(self, key: str):
self.configuration.pop(key)
self.write_conf()
def force_read(self):
"""
Read configuration file.
"""
with open("./user_conf.json", "r", encoding="utf-8", errors="ignore") as f:
self.configuration = json.load(f)
def check_health(self):
if not self.configuration:
raise Exception("No configuration found")
def set_key_value(self, key, value):
self.configuration[key] = value
self.write_conf()
def update_conf(self, conf):
"""
Update the whole configuration
"""
self.configuration = conf
self.write_conf()
def set_wallpaper_path(self, path):
self.configuration["wallpaper_path"] = path