Stvad / CrowdAnki

Plugin for Anki SRS designed to facilitate cooperation on creation of notes and decks.
MIT License
520 stars 44 forks source link

Import from disk not working #154

Open spechtjonas opened 2 years ago

spechtjonas commented 2 years ago

I just tried importing an updated version of deck. To resolve duplicated uuids of notes, I ran the script posted by aplaice here. On import, it asked to change every notetype, whoch shouldn't be the case, right? Not all of them, if any, were modified. After importing, the following error occured. None of the cards was updated or altered in any way.

Debug info: Anki 2.1.49 (dc80804a) Python 3.8.6 Qt 5.14.2 PyQt 5.14.2 Platform: Windows 10 Flags: frz=True ao=True sv=2 Add-ons, last update check: 2021-11-29 19:19:30

Caught exception: Traceback (most recent call last): File "C:\Users\Jonas\AppData\Roaming\Anki2\addons21\1788670778\anki\ui\action_vendor.py", line 31, in lambda: AnkiJsonImporter.import_deck(self.window.col, self.directory_vendor)) File "C:\Users\Jonas\AppData\Roaming\Anki2\addons21\1788670778\importer\anki_importer.py", line 114, in import_deck AnkiJsonImporter.import_deck_from_path(collection, Path(directory_path)) File "C:\Users\Jonas\AppData\Roaming\Anki2\addons21\1788670778\importer\anki_importer.py", line 103, in import_deck_from_path if importer.load_deck(directory_path): File "C:\Users\Jonas\AppData\Roaming\Anki2\addons21\1788670778\importer\anki_importer.py", line 41, in load_deck deck.save_to_collection(self.collection, import_config=import_config) File "C:\Users\Jonas\AppData\Roaming\Anki2\addons21\1788670778\representation\deck.py", line 131, in save_to_collection self.save_decks_and_notes(collection=collection, File "C:\Users\Jonas\AppData\Roaming\Anki2\addons21\1788670778\representation\deck.py", line 147, in save_decks_and_notes child.save_decks_and_notes(collection=collection, File "C:\Users\Jonas\AppData\Roaming\Anki2\addons21\1788670778\representation\deck.py", line 144, in save_decks_and_notes full_name = self._save_deck(collection, parent_name) File "C:\Users\Jonas\AppData\Roaming\Anki2\addons21\1788670778\representation\deck.py", line 173, in _save_deck self.anki_dict["conf"] = self.metadata.deck_configs[self.deck_config_uuid].anki_dict["id"] KeyError: 'd83c6288-3349-11ec-b6d3-287fcffa08a1'

Maybe someone can figure out the issue. Perhaps this is also in any way helpful for further development. :)

aplaice commented 2 years ago

Thanks for the bug report! I think that the issue is linked to #127 (the underlying cause is that the deck config UUID is unnecessarily attached to the deck (within Anki) by CrowdAnki, and can become stale).

You can check by inspecting your deck.json and checking if any of the deck_config_uuids (d83c6288-3349-11ec-b6d3-287fcffa08a1 in particular) do not correspond to a crowdanki_uuid in the deck_configurations section.

If you have direct access to a python interpreter then you could also try this code (for diagnosis) ```python3 #!/usr/bin/env python3 import json def get_deck_config_uuids(deck): uuids = [deck["deck_config_uuid"]] for child in deck.get("children"): uuids += get_deck_config_uuids(child) return uuids with open("deck.json") as f: anki_deck = json.load(f) available_deck_config_uuids = set( [deck_conf["crowdanki_uuid"] for deck_conf in anki_deck["deck_configurations"]] ) used_deck_config_uuids = set(get_deck_config_uuids(anki_deck)) all_used_deck_configs_are_defined = used_deck_config_uuids.issubset( available_deck_config_uuids ) if all_used_deck_configs_are_defined: print("The deck.json is OK — there are no undefined 'deck_config_uuid's.") else: print("The deck.json is not OK — the following 'deck_config_uuid's. are not defined:") for s in used_deck_config_uuids - available_deck_config_uuids: print(s) ```

You could fix the issue in the deck.json by replacing all the problematic deck_config_uuids with a crowdanki_uuid from the deck_configurations section.


Sorry! I should get around to finally getting a release fully ready (this is fixed in master)!

spechtjonas commented 2 years ago

Thanks for your help, spot on as always. I really appreciate the extensive support you provide!

What you point out seems to be the issue. When replacing the uuid from the error massage, it resolves the conflict for that specific uuid. A new one comes up, tho.

Could you ellaborate on how to use the python interpreter? For editing the deck.json I use sublime text if that's of interest (I could use any program on windows). I'd like to check how many uuids I'd have to replace.

Also, is there an ETA for the update to fix this issue? Depending on that I might save myself the effort of going through countless uuids and just wait it out instead 😅 Unless there is a effiecient and smart way of using the uuid list from your script to replace all of them at once via search and replace or a similar function. Maybe you have some thoughts on that aswell. :)

aplaice commented 2 years ago

Also, is there an ETA for the update to fix this issue?

Probably ~ two weeks (no promises).

Could you ellaborate on how to use the python interpreter?

If you have python itself directly installed then you could run the script from python, from the directory containing deck.json, but on second thought, it might not be necessary — a regular expression search will be easier/faster (see below) and will immediately carry out the fix. (OTOH given that (AFAIU) sublime text has a python API, it might allow you to run python directly?)

Regular expression

I think that sublime text (like most text editors) has regular expression find-and-replace, so you could replace the regular expression "deck_config_uuid": "[a-z0-9-]+", with "deck_config_uuid": "PASTE-A-UUID-FROM-THE-DECK_CONFIGURATIONS-SECTION".

(This won't match anything other than the deck config UUIDs.)

Potential disadvantages

If you care about which sub-deck has which deck configuration ("deck configurations"/"deck options" control the number of new cards per day, the graduating interval, the easy interval, the starting "ease" etc.), then this approach has the disadvantage of making all the deck configurations the same.

Also, with the regexp search, all deck_config_uuids will be replaced, irrespective of whether they're "defective" or not.