From 87d4864642663fa340379975e13e7c67a7c5abde Mon Sep 17 00:00:00 2001 From: Adam Plaice Date: Sat, 3 Jul 2021 00:13:23 +0200 Subject: [PATCH] Avoid duplicating crowdanki_uuid when cloning deck config Fix #118. Testing, it seems that I somehow haven't broken the tests despite fiddling with anki/hook_vendor without adding to the overrides. Note that deck_conf_did_add_config is not available as a "legacy" hook, so the new hook mechanism has to be used. It might (or might not) be worth converting the existing hooks to the new mechanism. --- crowd_anki/anki/hook_vendor.py | 7 +++++++ crowd_anki/utils/deckconf.py | 10 ++++++++++ 2 files changed, 17 insertions(+) create mode 100644 crowd_anki/utils/deckconf.py diff --git a/crowd_anki/anki/hook_vendor.py b/crowd_anki/anki/hook_vendor.py index 6572c85..99c5b28 100644 --- a/crowd_anki/anki/hook_vendor.py +++ b/crowd_anki/anki/hook_vendor.py @@ -1,3 +1,5 @@ +from aqt import gui_hooks + from dataclasses import dataclass from typing import Any @@ -5,6 +7,7 @@ from ..anki.adapters.hook_manager import AnkiHookManager from ..export.anki_exporter_wrapper import exporters_hook from ..history.archiver_vendor import ArchiverVendor +from ..utils.deckconf import disambiguate_crowdanki_uuid @dataclass @@ -16,6 +19,7 @@ class HookVendor: def setup_hooks(self): self.setup_exporter_hook() self.setup_snapshot_hooks() + self.setup_add_config_hook() def setup_exporter_hook(self): self.hook_manager.hook("exportersList", exporters_hook) @@ -24,3 +28,6 @@ def setup_snapshot_hooks(self): snapshot_handler = ArchiverVendor(self.window, self.config).snapshot_on_sync self.hook_manager.hook('profileLoaded', snapshot_handler) self.hook_manager.hook('unloadProfile', snapshot_handler) + + def setup_add_config_hook(self): + gui_hooks.deck_conf_did_add_config.append(disambiguate_crowdanki_uuid) diff --git a/crowd_anki/utils/deckconf.py b/crowd_anki/utils/deckconf.py new file mode 100644 index 0000000..d352927 --- /dev/null +++ b/crowd_anki/utils/deckconf.py @@ -0,0 +1,10 @@ +from .constants import UUID_FIELD_NAME + +def disambiguate_crowdanki_uuid(deck_conf, deck, + config, new_name, + new_conf_id): + new_deck_conf = deck_conf.mw.col.decks.get_config(new_conf_id) + if (new_deck_conf and (UUID_FIELD_NAME in new_deck_conf)): + # Delete rather than generating anew, (with uuid1()) to avoid code duplication. + del new_deck_conf[UUID_FIELD_NAME] + deck_conf.mw.col.decks.update_config(new_deck_conf)