Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions chinese/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +30 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • it looks like mw.addonManager.addonsFolder(__name__) does what you want here?
  • if this can return None then the type hint should indicate that
  • we shouldn't be mixing old os.path stuff with the new pathlib stuff
  • it feels wrong that this could return None 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.



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']:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But now this forces everyone who updates to have firstRun trigger to true, but it isn't necessarily their first runs.

spath = _get_old_addon_config_file()

if spath and spath.is_file():
# Old MacDonald had a farm. E-I-E-I-O.
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions chinese/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
if config['firstRun']:
dictionary.create_indices()
config['firstRun'] = False
config.save() # need for correct meta.json handling
Copy link
Contributor

Choose a reason for hiding this comment

The 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():
Expand Down
Loading