Skip to content

Commit 4f4d80d

Browse files
committed
Fix config issues + minor refactor
1 parent 4e6f22a commit 4f4d80d

File tree

4 files changed

+85
-38
lines changed

4 files changed

+85
-38
lines changed

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
"again_button_name": "Fail",
44
"good_button_name": "Pass",
55
"again_button_textcolor": "#000000",
6-
"good_button_textcolor": "#000000",
6+
"good_button_textcolor": "#000000"
77
}

config.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# This should maybe be a class, but there should never be multiple
2+
# instances of it, really.
3+
4+
from .logger import log
5+
6+
from aqt import mw
7+
8+
_config = {
9+
'toggle_names_textcolors': "0",
10+
'again_button_name': "Fail",
11+
'good_button_name': "Pass",
12+
'again_button_textcolor': "#000000",
13+
'good_button_textcolor': "#000000"
14+
}
15+
16+
# Required asserts to make config work properly
17+
assert(not any(isinstance(val, dict) for val in _config.values()))
18+
assert(all(isinstance(val, str) for val in _config.values()))
19+
20+
def as_str(key):
21+
return _config[key]
22+
def as_bool(key):
23+
return bool(int(_config[key]))
24+
25+
def update(new_kv):
26+
for k in new_kv.keys():
27+
if k in _config:
28+
_config[k] = new_kv[k]
29+
else:
30+
raise KeyError("Key '%s' does not exist in config.".format(k))
31+
32+
save()
33+
34+
def copy():
35+
return _config.copy()
36+
37+
def load():
38+
try:
39+
fs_config = mw.addonManager.getConfig(__name__)
40+
41+
log.info(fs_config)
42+
log.info(_config)
43+
# We don't reuse update here because we want to silently
44+
# ignore removed keys in newer versions
45+
for k in _config.keys():
46+
try:
47+
_config[k] = fs_config[k]
48+
except:
49+
()
50+
except:
51+
log.warn("Failed to load config. Writing.")
52+
53+
log.info(_config)
54+
55+
save()
56+
57+
def save():
58+
mw.addonManager.writeConfig(__name__, _config)

configuration_menu.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from aqt import mw
22
from aqt.qt import *
33

4+
from . import config
5+
46
from . import passfail2
57
from . import build_info
68

79
class SettingsDialog(QDialog):
810
def __init__(self, parent=None):
911
super(SettingsDialog, self).__init__(parent)
10-
self.preview_config = read_config()
12+
self.preview_config = config.copy()
1113
self.error_label = None
1214
self.mainWindow()
1315

@@ -162,15 +164,11 @@ def colorPick(self, button_number):
162164
self.good_button_textcolor.setText(hex_color)
163165

164166
def prepopulate_fields(self):
165-
config_from_json = read_config()
166-
167-
self.toggle_names_textcolors.setChecked(bool(int(config_from_json['toggle_names_textcolors'])))
168-
self.again_button_name.setText(config_from_json['again_button_name'])
169-
self.good_button_name.setText(config_from_json['good_button_name'])
170-
self.again_button_textcolor.setText(config_from_json['again_button_textcolor'])
171-
self.good_button_textcolor.setText(config_from_json['good_button_textcolor'])
172-
173-
return config_from_json
167+
self.toggle_names_textcolors.setChecked(bool(int(self.preview_config['toggle_names_textcolors'])))
168+
self.again_button_name.setText(self.preview_config['again_button_name'])
169+
self.good_button_name.setText(self.preview_config['good_button_name'])
170+
self.again_button_textcolor.setText(self.preview_config['again_button_textcolor'])
171+
self.good_button_textcolor.setText(self.preview_config['good_button_textcolor'])
174172

175173
def update_preview_config(self):
176174
if self.current_config_is_valid():
@@ -199,7 +197,7 @@ def update_preview_buttons(self):
199197
def write_config(self):
200198
if self.current_config_is_valid():
201199
self.update_preview_config()
202-
mw.addonManager.writeConfig(__name__, self.preview_config)
200+
config.update(self.preview_config)
203201
self.close_config_window()
204202
else:
205203
self.error_label.show()

passfail2.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
def point_version():
3232
return int(version.split(".")[-1])
3333

34-
# from . import button_color
34+
from . import config
3535
from . import configuration_menu
3636

3737
from anki.hooks import wrap
@@ -50,40 +50,30 @@ def point_version():
5050
else:
5151
from anki.lang import _
5252

53-
# Setup config
54-
toggle_names_textcolors = False
55-
again_button_name = "Fail"
56-
good_button_name = "Pass"
57-
again_button_textcolor = ""
58-
good_button_textcolor = ""
59-
60-
try:
61-
config = mw.addonManager.getConfig(__name__)
62-
toggle_names_textcolors = bool(int(config['toggle_names_textcolors']))
63-
again_button_name = config['again_button_name']
64-
good_button_name = config['good_button_name']
65-
again_button_textcolor = config['again_button_textcolor']
66-
good_button_textcolor = config['good_button_textcolor']
67-
except Exception as err:
68-
log.warn("Failed to import configuration, using defaults: %s", err)
69-
7053
# Hooks
7154
def pf2_hook_replace_buttons(
7255
buttons_tuple, # type: tuple[tuple[int, str], ...]
7356
reviewer, # type: Reviewer
7457
card # type: Card
7558
): # type: (...) -> tuple[tuple[int,str], ...]
76-
if toggle_names_textcolors:
77-
return (
78-
(1, f"<font color='{again_button_textcolor}'>{again_button_name}</font>"),
79-
(reviewer._defaultEase(), f"<font color='{good_button_textcolor}'>{good_button_name}</font>")
59+
again_text = "Fail"
60+
good_text = "Pass"
61+
62+
if config.as_bool('toggle_names_textcolors'):
63+
again_text = "<font color='{}'>{}</font>".format(
64+
config.as_str('again_button_textcolor'),
65+
config.as_str('again_button_name')
8066
)
81-
else:
82-
return (
83-
(1, "Fail"),
84-
(reviewer._defaultEase(), "Pass")
67+
good_text = "<font color='{}'>{}</font>".format(
68+
config.as_str('good_button_textcolor'),
69+
config.as_str('good_button_name')
8570
)
8671

72+
return (
73+
(1, again_text),
74+
(reviewer._defaultEase(), good_text)
75+
)
76+
8777
def pf2_hook_remap_answer_ease(
8878
ease_tuple, # type: tuple[bool, Literal[1, 2, 3, 4]]
8979
reviewer, # type: Reviewer
@@ -137,6 +127,7 @@ def pf2_fix_pass_title(
137127
def init():
138128
version = point_version()
139129

130+
config.load()
140131
configuration_menu.configuration_menu_init()
141132

142133
# Answer button list

0 commit comments

Comments
 (0)