-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig.py
More file actions
53 lines (42 loc) · 1.29 KB
/
config.py
File metadata and controls
53 lines (42 loc) · 1.29 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
# This should maybe be a class, but there should never be multiple
# instances of it, really.
from .logger import log
from aqt import mw
_config = {
'toggle_names_textcolors': "0",
'again_button_name': "Fail",
'good_button_name': "Pass",
'again_button_textcolor': "#000000",
'good_button_textcolor': "#000000"
}
# Required asserts to make config work properly
assert(not any(isinstance(val, dict) for val in _config.values()))
assert(all(isinstance(val, str) for val in _config.values()))
def as_str(key):
return _config[key]
def as_bool(key):
return bool(int(_config[key]))
def update(new_kv):
for k in new_kv.keys():
if k in _config:
_config[k] = new_kv[k]
else:
raise KeyError("Key '%s' does not exist in config.".format(k))
save()
def copy():
return _config.copy()
def load():
try:
fs_config = mw.addonManager.getConfig(__name__)
# We don't reuse update here because we want to silently
# ignore removed keys in newer versions
for k in _config.keys():
try:
_config[k] = fs_config[k]
except:
()
except:
log.warn("Failed to load config. Writing.")
save()
def save():
mw.addonManager.writeConfig(__name__, _config)