From 7c184cfeb9f9dc47d0992c58f575f740f665d197 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Wed, 27 Apr 2022 22:43:49 +0200 Subject: [PATCH] Imrove type of apkg export selector/limit --- pylib/anki/collection.py | 39 ++++++++++++++++++++++++++----- qt/aqt/import_export/exporting.py | 12 ++++++---- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/pylib/anki/collection.py b/pylib/anki/collection.py index 0b8979739..06005f858 100644 --- a/pylib/anki/collection.py +++ b/pylib/anki/collection.py @@ -92,6 +92,33 @@ class LegacyCheckpoint: LegacyUndoResult = Union[None, LegacyCheckpoint, LegacyReviewUndo] +class ExportLimit: + """Limit to what will be exported. Either specific notes, or a deck, or the + whole collection (if neither is set). Only the last set value is preserved. + """ + + _note_ids: Sequence[NoteId] | None + _deck_id: DeckId | None + + @property + def note_ids(self) -> Sequence[NoteId] | None: + return self._note_ids + + @note_ids.setter + def note_ids(self, note_ids: Sequence[NoteId] | None) -> None: + self._note_ids = note_ids + self._deck_id = None + + @property + def deck_id(self) -> DeckId | None: + return self._deck_id + + @deck_id.setter + def deck_id(self, deck_id: DeckId | None) -> None: + self._deck_id = deck_id + self._note_ids = None + + class Collection(DeprecatedNamesMixin): sched: V1Scheduler | V2Scheduler | V3Scheduler @@ -371,7 +398,7 @@ class Collection(DeprecatedNamesMixin): self, *, out_path: str, - selector: DeckId | list[NoteId] | None, + limit: ExportLimit, with_scheduling: bool, with_media: bool, ) -> int: @@ -381,12 +408,12 @@ class Collection(DeprecatedNamesMixin): with_media=with_media, legacy=True, ) - if selector is None: - request.whole_collection.SetInParent() - elif isinstance(selector, list): - request.note_ids.note_ids.extend(selector) + if limit.deck_id is not None: + request.deck_id = limit.deck_id + elif limit.note_ids is not None: + request.note_ids.note_ids.extend(limit.note_ids) else: - request.deck_id = selector + request.whole_collection.SetInParent() return self._backend.export_anki_package(request) # Object helpers diff --git a/qt/aqt/import_export/exporting.py b/qt/aqt/import_export/exporting.py index d50ab6dac..0bce0a306 100644 --- a/qt/aqt/import_export/exporting.py +++ b/qt/aqt/import_export/exporting.py @@ -13,6 +13,7 @@ from typing import Sequence, Type import aqt.forms import aqt.main +from anki.collection import ExportLimit from anki.decks import DeckId, DeckNameId from anki.notes import NoteId from aqt import gui_hooks @@ -106,14 +107,16 @@ class ExportDialog(QDialog): return path def options(self, out_path: str) -> Options: + limit = ExportLimit() + limit.deck_id = self.current_deck_id() + limit.note_ids = self.nids return Options( out_path=out_path, include_scheduling=self.frm.includeSched.isChecked(), include_media=self.frm.includeMedia.isChecked(), include_tags=self.frm.includeTags.isChecked(), include_html=self.frm.includeHTML.isChecked(), - deck_id=self.current_deck_id(), - note_ids=self.nids, + limit=limit, ) def current_deck_id(self) -> DeckId | None: @@ -142,8 +145,7 @@ class Options: include_media: bool include_tags: bool include_html: bool - deck_id: DeckId | None - note_ids: Sequence[NoteId] | None + limit: ExportLimit class Exporter(ABC): @@ -210,7 +212,7 @@ class ApkgExporter(Exporter): parent=mw, op=lambda col: col.export_anki_package( out_path=options.out_path, - selector=options.note_ids or options.deck_id or None, + limit=options.limit, with_scheduling=options.include_scheduling, with_media=options.include_media, ),