-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate config to standard anki scheme (fixing 91 and 73) #96
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,22 +21,51 @@ | |
from collections import defaultdict | ||
from json import dump, load | ||
from os.path import dirname, exists, join, realpath | ||
from pathlib import Path | ||
|
||
from aqt import mw | ||
from aqt.utils import showInfo | ||
|
||
|
||
def _get_old_addon_config_file() -> Path: | ||
apath = Path(mw.pm.addonFolder()) | ||
# This is mainly for tests, since 'current-module-dir is mocked, but we cannot mock/patch this function, | ||
# it is called at the import time, by main.py, ugh | ||
if exists(apath): | ||
ret = apath.resolve(strict=True) / '1752008591/config_saved.json' | ||
else: | ||
ret = None | ||
|
||
return ret | ||
|
||
|
||
class ConfigManager: | ||
default_path = join(dirname(realpath(__file__)), 'config.json') | ||
saved_path = join(dirname(realpath(__file__)), 'config_saved.json') | ||
|
||
with open(default_path, encoding='utf-8') as f: | ||
config = defaultdict(str, load(f)) | ||
def __init__(self): | ||
self._load_config() | ||
mw.addonManager.setConfigUpdatedAction(__name__, lambda *_: self._config_updated_handler()) | ||
|
||
if self.config['firstRun']: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But now this forces everyone who updates to have |
||
spath = _get_old_addon_config_file() | ||
|
||
if spath and spath.is_file(): | ||
# Old MacDonald had a farm. E-I-E-I-O. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what? |
||
with open(spath, 'r', encoding='utf-8') as f: | ||
config_saved = defaultdict(str, load(f)) | ||
for k in ['enabledModels', 'speech', 'target', 'max_examples', 'fields']: | ||
self.config[k] = config_saved[k] | ||
Comment on lines
+55
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to manually copy select fields, just copy all of it. |
||
showInfo( 'Configuration from the old "Chinese Support 3" was imported.\n\n' | ||
'This is a one-time post install action, if you update the configuration in the original addon, ' | ||
'it will not be synchronized.\n\n' | ||
'It is recommended to disable the old addon.') | ||
Comment on lines
+57
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this, since there is an update there is no addon which needs to be disabled. I think this message should be removed entirely. We can perform this completely unbeknownst to the user. |
||
self.save() | ||
|
||
|
||
def _load_config(self): | ||
self.config = mw.addonManager.getConfig(__name__) | ||
|
||
if exists(saved_path): | ||
with open(saved_path, encoding='utf-8') as f: | ||
config_saved = defaultdict(str, load(f)) | ||
if config_saved['version'] == config['version']: | ||
config = config_saved | ||
def _config_updated_handler(self): | ||
self._load_config() | ||
|
||
def __setitem__(self, key, value): | ||
self.config[key] = value | ||
|
@@ -48,8 +77,6 @@ def update(self, d): | |
self.config.update(d) | ||
|
||
def save(self): | ||
with open(self.saved_path, 'w', encoding='utf-8') as f: | ||
dump(self.config, f) | ||
mw.addonManager.writeConfig(__name__, self.config) | ||
|
||
def get_fields(self, groups=None): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
if config['firstRun']: | ||
dictionary.create_indices() | ||
config['firstRun'] = False | ||
config.save() # need for correct meta.json handling | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't need this. Our view of the config shouldn't get out of sync with the Anki view. |
||
|
||
|
||
def load(): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mw.addonManager.addonsFolder(__name__)
does what you want here?None
then the type hint should indicate thatos.path
stuff with the newpathlib
stuffNone
if the path doesn't exist in one case but return a path that doesn't exist in another case, why do both? I think you can get rid of this function entirely.