Imrove type of apkg export selector/limit

This commit is contained in:
RumovZ 2022-04-27 22:43:49 +02:00
parent 20322fb753
commit 7c184cfeb9
2 changed files with 40 additions and 11 deletions

View file

@ -92,6 +92,33 @@ class LegacyCheckpoint:
LegacyUndoResult = Union[None, LegacyCheckpoint, LegacyReviewUndo] 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): class Collection(DeprecatedNamesMixin):
sched: V1Scheduler | V2Scheduler | V3Scheduler sched: V1Scheduler | V2Scheduler | V3Scheduler
@ -371,7 +398,7 @@ class Collection(DeprecatedNamesMixin):
self, self,
*, *,
out_path: str, out_path: str,
selector: DeckId | list[NoteId] | None, limit: ExportLimit,
with_scheduling: bool, with_scheduling: bool,
with_media: bool, with_media: bool,
) -> int: ) -> int:
@ -381,12 +408,12 @@ class Collection(DeprecatedNamesMixin):
with_media=with_media, with_media=with_media,
legacy=True, legacy=True,
) )
if selector is None: if limit.deck_id is not None:
request.whole_collection.SetInParent() request.deck_id = limit.deck_id
elif isinstance(selector, list): elif limit.note_ids is not None:
request.note_ids.note_ids.extend(selector) request.note_ids.note_ids.extend(limit.note_ids)
else: else:
request.deck_id = selector request.whole_collection.SetInParent()
return self._backend.export_anki_package(request) return self._backend.export_anki_package(request)
# Object helpers # Object helpers

View file

@ -13,6 +13,7 @@ from typing import Sequence, Type
import aqt.forms import aqt.forms
import aqt.main import aqt.main
from anki.collection import ExportLimit
from anki.decks import DeckId, DeckNameId from anki.decks import DeckId, DeckNameId
from anki.notes import NoteId from anki.notes import NoteId
from aqt import gui_hooks from aqt import gui_hooks
@ -106,14 +107,16 @@ class ExportDialog(QDialog):
return path return path
def options(self, out_path: str) -> Options: def options(self, out_path: str) -> Options:
limit = ExportLimit()
limit.deck_id = self.current_deck_id()
limit.note_ids = self.nids
return Options( return Options(
out_path=out_path, out_path=out_path,
include_scheduling=self.frm.includeSched.isChecked(), include_scheduling=self.frm.includeSched.isChecked(),
include_media=self.frm.includeMedia.isChecked(), include_media=self.frm.includeMedia.isChecked(),
include_tags=self.frm.includeTags.isChecked(), include_tags=self.frm.includeTags.isChecked(),
include_html=self.frm.includeHTML.isChecked(), include_html=self.frm.includeHTML.isChecked(),
deck_id=self.current_deck_id(), limit=limit,
note_ids=self.nids,
) )
def current_deck_id(self) -> DeckId | None: def current_deck_id(self) -> DeckId | None:
@ -142,8 +145,7 @@ class Options:
include_media: bool include_media: bool
include_tags: bool include_tags: bool
include_html: bool include_html: bool
deck_id: DeckId | None limit: ExportLimit
note_ids: Sequence[NoteId] | None
class Exporter(ABC): class Exporter(ABC):
@ -210,7 +212,7 @@ class ApkgExporter(Exporter):
parent=mw, parent=mw,
op=lambda col: col.export_anki_package( op=lambda col: col.export_anki_package(
out_path=options.out_path, out_path=options.out_path,
selector=options.note_ids or options.deck_id or None, limit=options.limit,
with_scheduling=options.include_scheduling, with_scheduling=options.include_scheduling,
with_media=options.include_media, with_media=options.include_media,
), ),