Consistently use exporter instances rather than classes

This commit is contained in:
Glutanimate 2022-07-19 14:17:28 +02:00
parent 5e4178daa4
commit 7619bce417
2 changed files with 22 additions and 27 deletions

View file

@ -42,21 +42,21 @@ class ExportDialog(QDialog):
self.col = mw.col.weakref() self.col = mw.col.weakref()
self.frm = aqt.forms.exporting.Ui_ExportDialog() self.frm = aqt.forms.exporting.Ui_ExportDialog()
self.frm.setupUi(self) self.frm.setupUi(self)
self.exporter: Type[Exporter] = None self.exporter: Exporter
self.nids = nids self.nids = nids
disable_help_button(self) disable_help_button(self)
self.setup(did) self.setup(did)
self.open() self.open()
def setup(self, did: DeckId | None) -> None: def setup(self, did: DeckId | None) -> None:
self.exporters: list[Type[Exporter]] = [ self.exporter_classes: list[Type[Exporter]] = [
ApkgExporter, ApkgExporter,
ColpkgExporter, ColpkgExporter,
NoteCsvExporter, NoteCsvExporter,
CardCsvExporter, CardCsvExporter,
] ]
self.frm.format.insertItems( self.frm.format.insertItems(
0, [f"{e.name()} (.{e.extension})" for e in self.exporters] 0, [f"{e.name()} (.{e.extension})" for e in self.exporter_classes]
) )
qconnect(self.frm.format.activated, self.exporter_changed) qconnect(self.frm.format.activated, self.exporter_changed)
if self.nids is None and not did: if self.nids is None and not did:
@ -86,7 +86,7 @@ class ExportDialog(QDialog):
self.frm.includeSched.setChecked(False) self.frm.includeSched.setChecked(False)
def exporter_changed(self, idx: int) -> None: def exporter_changed(self, idx: int) -> None:
self.exporter = self.exporters[idx] self.exporter = self.exporter_classes[idx]()
self.frm.includeSched.setVisible(self.exporter.show_include_scheduling) self.frm.includeSched.setVisible(self.exporter.show_include_scheduling)
self.frm.includeMedia.setVisible(self.exporter.show_include_media) self.frm.includeMedia.setVisible(self.exporter.show_include_media)
self.frm.includeTags.setVisible(self.exporter.show_include_tags) self.frm.includeTags.setVisible(self.exporter.show_include_tags)
@ -190,9 +190,8 @@ class Exporter(ABC):
show_include_notetype = False show_include_notetype = False
show_include_guid = False show_include_guid = False
@classmethod
@abstractmethod @abstractmethod
def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None: def export(self, mw: aqt.main.AnkiQt, options: ExportOptions) -> None:
pass pass
@staticmethod @staticmethod
@ -210,13 +209,12 @@ class ColpkgExporter(Exporter):
def name() -> str: def name() -> str:
return tr.exporting_anki_collection_package() return tr.exporting_anki_collection_package()
@classmethod def export(self, mw: aqt.main.AnkiQt, options: ExportOptions) -> None:
def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None: options = gui_hooks.exporter_will_export(options, self)
options = gui_hooks.exporter_will_export(options, cls)
def on_success(_: None) -> None: def on_success(_: None) -> None:
mw.reopen() mw.reopen()
gui_hooks.exporter_did_export(options, cls) gui_hooks.exporter_did_export(options, self)
tooltip(tr.exporting_collection_exported(), parent=mw) tooltip(tr.exporting_collection_exported(), parent=mw)
def on_failure(exception: Exception) -> None: def on_failure(exception: Exception) -> None:
@ -248,12 +246,11 @@ class ApkgExporter(Exporter):
def name() -> str: def name() -> str:
return tr.exporting_anki_deck_package() return tr.exporting_anki_deck_package()
@classmethod def export(self, mw: aqt.main.AnkiQt, options: ExportOptions) -> None:
def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None: options = gui_hooks.exporter_will_export(options, self)
options = gui_hooks.exporter_will_export(options, cls)
def on_success(count: int) -> None: def on_success(count: int) -> None:
gui_hooks.exporter_did_export(options, cls) gui_hooks.exporter_did_export(options, self)
tooltip(tr.exporting_note_exported(count=count), parent=mw) tooltip(tr.exporting_note_exported(count=count), parent=mw)
QueryOp( QueryOp(
@ -282,12 +279,11 @@ class NoteCsvExporter(Exporter):
def name() -> str: def name() -> str:
return tr.exporting_notes_in_plain_text() return tr.exporting_notes_in_plain_text()
@classmethod def export(self, mw: aqt.main.AnkiQt, options: ExportOptions) -> None:
def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None: options = gui_hooks.exporter_will_export(options, self)
options = gui_hooks.exporter_will_export(options, cls)
def on_success(count: int) -> None: def on_success(count: int) -> None:
gui_hooks.exporter_did_export(options, cls) gui_hooks.exporter_did_export(options, self)
tooltip(tr.exporting_note_exported(count=count), parent=mw) tooltip(tr.exporting_note_exported(count=count), parent=mw)
QueryOp( QueryOp(
@ -314,12 +310,11 @@ class CardCsvExporter(Exporter):
def name() -> str: def name() -> str:
return tr.exporting_cards_in_plain_text() return tr.exporting_cards_in_plain_text()
@classmethod def export(self, mw: aqt.main.AnkiQt, options: ExportOptions) -> None:
def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None: options = gui_hooks.exporter_will_export(options, self)
options = gui_hooks.exporter_will_export(options, cls)
def on_success(count: int) -> None: def on_success(count: int) -> None:
gui_hooks.exporter_did_export(options, cls) gui_hooks.exporter_did_export(options, self)
tooltip(tr.exporting_card_exported(count=count), parent=mw) tooltip(tr.exporting_card_exported(count=count), parent=mw)
QueryOp( QueryOp(

View file

@ -19,7 +19,7 @@ prefix = """\
from __future__ import annotations from __future__ import annotations
from typing import Any, Callable, Sequence, Literal, Type from typing import Any, Callable, Sequence, Literal
import anki import anki
import aqt import aqt
@ -822,7 +822,7 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest)
name="exporter_will_export", name="exporter_will_export",
args=[ args=[
"export_options: aqt.import_export.exporting.ExportOptions", "export_options: aqt.import_export.exporting.ExportOptions",
"exporter: Type[aqt.import_export.exporting.Exporter]", "exporter: aqt.import_export.exporting.Exporter",
], ],
return_type="aqt.import_export.exporting.ExportOptions", return_type="aqt.import_export.exporting.ExportOptions",
doc="""Called before collection and deck exports. doc="""Called before collection and deck exports.
@ -831,8 +831,8 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest)
modify the export options. To perform the export unaltered, please return modify the export options. To perform the export unaltered, please return
`export_options` as is, e.g.: `export_options` as is, e.g.:
def on_exporter_will_export(export_options: ExportOptions, exporter: Type[Exporter]): def on_exporter_will_export(export_options: ExportOptions, exporter: Exporter):
if not exporter == ApkgExporter: if not isinstance(exporter, ApkgExporter):
return export_options return export_options
export_options.limit = ... export_options.limit = ...
return export_options return export_options
@ -842,7 +842,7 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest)
name="exporter_did_export", name="exporter_did_export",
args=[ args=[
"export_options: aqt.import_export.exporting.ExportOptions", "export_options: aqt.import_export.exporting.ExportOptions",
"exporter: Type[aqt.import_export.exporting.Exporter]", "exporter: aqt.import_export.exporting.Exporter",
], ],
doc="""Called after collection and deck exports.""", doc="""Called after collection and deck exports.""",
), ),