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

Make uuids persistent when exporting with Anki 2.1.35 (≥ 2.1.28) #110

Merged
merged 3 commits into from
Dec 13, 2020
Merged
Changes from 2 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
33 changes: 24 additions & 9 deletions crowd_anki/export/anki_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,36 @@ def export_to_directory(self, deck: AnkiDeck, output_dir=Path("."), copy_media=T
indent=4,
ensure_ascii=False))

self._save_changes()
self._save_changes(deck)

if copy_media:
self._copy_media(deck, deck_directory)

return deck_directory

def _save_changes(self):
"""Save updates that were maid during the export. E.g. UUID fields"""
# This saves decks and deck configurations
self.collection.decks.save()
self.collection.decks.flush()
def _save_changes(self, deck, is_export_child=False):
Copy link
Owner

Choose a reason for hiding this comment

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

does this preserve changes to note uuid btw?

Copy link
Collaborator Author

@aplaice aplaice Dec 12, 2020

Choose a reason for hiding this comment

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

Do you mean note model uuid? (If so, yes, I think so.) (Sorry, I might not have fully woken up yet, which is pretty embarrassing as it's already rather late here...)

Copy link
Owner

Choose a reason for hiding this comment

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

no, I meant actual note uid. but it seems it falls back on using the native guid, nvm :p

"""Save updates that were made during the export. E.g. UUID fields

self.collection.models.save()
self.collection.models.flush()
It saves decks, deck configurations and models.
aplaice marked this conversation as resolved.
Show resolved Hide resolved

is_export_child refers to whether this deck is a child for the
_purposes of the current export operation_. For instance, if we're
exporting or snapshotting a specific subdeck, then it's considered the
"parent" here. We need the argument to avoid duplicately saving deck
configs and note models.

"""

self.collection.decks.save(deck.anki_dict)
for child_deck in deck.children:
self._save_changes(child_deck, is_export_child=True)

if not is_export_child:
for deck_config in deck.metadata.deck_configs.values():
self.collection.decks.save(deck_config.anki_dict)
Comment on lines +76 to +77
Copy link
Owner

Choose a reason for hiding this comment

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

this does not seem right? we try to save deck config as a deck?

Copy link
Collaborator Author

@aplaice aplaice Dec 12, 2020

Choose a reason for hiding this comment

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

It's pretty weird, but it's how it seems to work, according to Anki's source. FWIW decks.save() (without arguments) used to save both all decks and all deck configs, previously.

Copy link
Owner

Choose a reason for hiding this comment

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

weird indeed


for model in deck.metadata.models.values():
self.collection.models.save(model.anki_dict)

# Notes?

Expand All @@ -76,4 +91,4 @@ def _copy_media(self, deck, deck_directory):
shutil.copy(os.path.join(self.collection.media.dir(), file_src),
str(media_directory.resolve()))
except IOError as ioerror:
print("Failed to copy a file {}. Full error: {}".format(file_src, ioerror))
print("Failed to copy a file {}. Full error: {}".format(file_src, ioerror))