Switch away from ExportFormat, opting to pass exporter class/instance instead

This commit is contained in:
Glutanimate 2022-07-19 13:46:05 +02:00
parent 423a87ab7c
commit 5e4178daa4
3 changed files with 25 additions and 33 deletions

View file

@ -196,7 +196,7 @@ class ExportDialog(QDialog):
else: else:
self.on_export_finished() self.on_export_finished()
gui_hooks.legacy_exporter_will_export(self.exporter.ext) gui_hooks.legacy_exporter_will_export(self.exporter)
if self.isVerbatim: if self.isVerbatim:
gui_hooks.collection_will_temporarily_close(self.mw.col) gui_hooks.collection_will_temporarily_close(self.mw.col)
self.mw.progress.start() self.mw.progress.start()
@ -214,7 +214,7 @@ class ExportDialog(QDialog):
msg = tr.exporting_note_exported(count=self.exporter.count) msg = tr.exporting_note_exported(count=self.exporter.count)
else: else:
msg = tr.exporting_card_exported(count=self.exporter.count) msg = tr.exporting_card_exported(count=self.exporter.count)
gui_hooks.legacy_exporter_did_export(self.exporter.ext) gui_hooks.legacy_exporter_did_export(self.exporter)
tooltip(msg, period=3000) tooltip(msg, period=3000)
QDialog.reject(self) QDialog.reject(self)

View file

@ -8,7 +8,6 @@ import re
import time import time
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from dataclasses import dataclass from dataclasses import dataclass
from enum import Enum
from typing import Sequence, Type from typing import Sequence, Type
import aqt.forms import aqt.forms
@ -57,7 +56,7 @@ class ExportDialog(QDialog):
CardCsvExporter, CardCsvExporter,
] ]
self.frm.format.insertItems( self.frm.format.insertItems(
0, [f"{e.name()} (.{e.format.value})" for e in self.exporters] 0, [f"{e.name()} (.{e.extension})" for e in self.exporters]
) )
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:
@ -112,7 +111,7 @@ class ExportDialog(QDialog):
title=tr.actions_export(), title=tr.actions_export(),
dir_description="export", dir_description="export",
key=self.exporter.name(), key=self.exporter.name(),
ext="." + self.exporter.format.value, ext="." + self.exporter.extension,
fname=filename, fname=filename,
) )
if not path: if not path:
@ -162,7 +161,7 @@ class ExportDialog(QDialog):
else: else:
time_str = time.strftime("%Y-%m-%d@%H-%M-%S", time.localtime(time.time())) time_str = time.strftime("%Y-%m-%d@%H-%M-%S", time.localtime(time.time()))
stem = f"{tr.exporting_collection()}-{time_str}" stem = f"{tr.exporting_collection()}-{time_str}"
return f"{stem}.{self.exporter.format.value}" return f"{stem}.{self.exporter.extension}"
@dataclass @dataclass
@ -179,15 +178,8 @@ class ExportOptions:
limit: ExportLimit limit: ExportLimit
class ExportFormat(Enum):
COLPKG = "colpkg"
APKG = "apkg"
CSV_NOTES = "txt"
CSV_CARDS = "txt"
class Exporter(ABC): class Exporter(ABC):
format: ExportFormat extension: str
show_deck_list = False show_deck_list = False
show_include_scheduling = False show_include_scheduling = False
show_include_media = False show_include_media = False
@ -210,7 +202,7 @@ class Exporter(ABC):
class ColpkgExporter(Exporter): class ColpkgExporter(Exporter):
format = ExportFormat.COLPKG extension = "colpkg"
show_include_media = True show_include_media = True
show_legacy_support = True show_legacy_support = True
@ -220,11 +212,11 @@ class ColpkgExporter(Exporter):
@classmethod @classmethod
def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None: def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None:
options = gui_hooks.exporter_will_export(options, cls.format) 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.format) gui_hooks.exporter_did_export(options, cls)
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:
@ -246,7 +238,7 @@ class ColpkgExporter(Exporter):
class ApkgExporter(Exporter): class ApkgExporter(Exporter):
format = ExportFormat.APKG extension = "apkg"
show_deck_list = True show_deck_list = True
show_include_scheduling = True show_include_scheduling = True
show_include_media = True show_include_media = True
@ -258,10 +250,10 @@ class ApkgExporter(Exporter):
@classmethod @classmethod
def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None: def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None:
options = gui_hooks.exporter_will_export(options, cls.format) 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.format) gui_hooks.exporter_did_export(options, cls)
tooltip(tr.exporting_note_exported(count=count), parent=mw) tooltip(tr.exporting_note_exported(count=count), parent=mw)
QueryOp( QueryOp(
@ -278,7 +270,7 @@ class ApkgExporter(Exporter):
class NoteCsvExporter(Exporter): class NoteCsvExporter(Exporter):
format = ExportFormat.CSV_NOTES extension = "txt"
show_deck_list = True show_deck_list = True
show_include_html = True show_include_html = True
show_include_tags = True show_include_tags = True
@ -292,10 +284,10 @@ class NoteCsvExporter(Exporter):
@classmethod @classmethod
def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None: def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None:
options = gui_hooks.exporter_will_export(options, cls.format) 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.format) gui_hooks.exporter_did_export(options, cls)
tooltip(tr.exporting_note_exported(count=count), parent=mw) tooltip(tr.exporting_note_exported(count=count), parent=mw)
QueryOp( QueryOp(
@ -314,7 +306,7 @@ class NoteCsvExporter(Exporter):
class CardCsvExporter(Exporter): class CardCsvExporter(Exporter):
format = ExportFormat.CSV_CARDS extension = "txt"
show_deck_list = True show_deck_list = True
show_include_html = True show_include_html = True
@ -324,10 +316,10 @@ class CardCsvExporter(Exporter):
@classmethod @classmethod
def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None: def export(cls, mw: aqt.main.AnkiQt, options: ExportOptions) -> None:
options = gui_hooks.exporter_will_export(options, cls.format) 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.format) gui_hooks.exporter_did_export(options, cls)
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 from typing import Any, Callable, Sequence, Literal, Type
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",
"export_format: aqt.import_export.exporting.ExportFormat", "exporter: Type[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, export_format: ExportFormat): def on_exporter_will_export(export_options: ExportOptions, exporter: Type[Exporter]):
if export_format != ExportFormat.APKG: if not exporter == ApkgExporter:
return export_options return export_options
export_options.limit = ... export_options.limit = ...
return export_options return export_options
@ -842,18 +842,18 @@ 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",
"export_format: aqt.import_export.exporting.ExportFormat", "exporter: Type[aqt.import_export.exporting.Exporter]",
], ],
doc="""Called after collection and deck exports.""", doc="""Called after collection and deck exports.""",
), ),
Hook( Hook(
name="legacy_exporter_will_export", name="legacy_exporter_will_export",
args=["file_extension: str"], args=["legacy_exporter: anki.exporting.Exporter"],
doc="""Called before collection and deck exports performed by legacy exporters.""", doc="""Called before collection and deck exports performed by legacy exporters.""",
), ),
Hook( Hook(
name="legacy_exporter_did_export", name="legacy_exporter_did_export",
args=["file_extension: str"], args=["legacy_exporter: anki.exporting.Exporter"],
doc="""Called after collection and deck exports performed by legacy exporters.""", doc="""Called after collection and deck exports performed by legacy exporters.""",
), ),
# Dialog Manager # Dialog Manager