From 5e4178daa4d01c99a241160fb496d62c14d41b61 Mon Sep 17 00:00:00 2001 From: Glutanimate Date: Tue, 19 Jul 2022 13:46:05 +0200 Subject: [PATCH] Switch away from ExportFormat, opting to pass exporter class/instance instead --- qt/aqt/exporting.py | 4 ++-- qt/aqt/import_export/exporting.py | 40 +++++++++++++------------------ qt/tools/genhooks_gui.py | 14 +++++------ 3 files changed, 25 insertions(+), 33 deletions(-) diff --git a/qt/aqt/exporting.py b/qt/aqt/exporting.py index 2298aa641..a3eeb96a9 100644 --- a/qt/aqt/exporting.py +++ b/qt/aqt/exporting.py @@ -196,7 +196,7 @@ class ExportDialog(QDialog): else: self.on_export_finished() - gui_hooks.legacy_exporter_will_export(self.exporter.ext) + gui_hooks.legacy_exporter_will_export(self.exporter) if self.isVerbatim: gui_hooks.collection_will_temporarily_close(self.mw.col) self.mw.progress.start() @@ -214,7 +214,7 @@ class ExportDialog(QDialog): msg = tr.exporting_note_exported(count=self.exporter.count) else: 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) QDialog.reject(self) diff --git a/qt/aqt/import_export/exporting.py b/qt/aqt/import_export/exporting.py index 220faa917..1b18c35e3 100644 --- a/qt/aqt/import_export/exporting.py +++ b/qt/aqt/import_export/exporting.py @@ -8,7 +8,6 @@ import re import time from abc import ABC, abstractmethod from dataclasses import dataclass -from enum import Enum from typing import Sequence, Type import aqt.forms @@ -57,7 +56,7 @@ class ExportDialog(QDialog): CardCsvExporter, ] 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) if self.nids is None and not did: @@ -112,7 +111,7 @@ class ExportDialog(QDialog): title=tr.actions_export(), dir_description="export", key=self.exporter.name(), - ext="." + self.exporter.format.value, + ext="." + self.exporter.extension, fname=filename, ) if not path: @@ -162,7 +161,7 @@ class ExportDialog(QDialog): else: time_str = time.strftime("%Y-%m-%d@%H-%M-%S", time.localtime(time.time())) stem = f"{tr.exporting_collection()}-{time_str}" - return f"{stem}.{self.exporter.format.value}" + return f"{stem}.{self.exporter.extension}" @dataclass @@ -179,15 +178,8 @@ class ExportOptions: limit: ExportLimit -class ExportFormat(Enum): - COLPKG = "colpkg" - APKG = "apkg" - CSV_NOTES = "txt" - CSV_CARDS = "txt" - - class Exporter(ABC): - format: ExportFormat + extension: str show_deck_list = False show_include_scheduling = False show_include_media = False @@ -210,7 +202,7 @@ class Exporter(ABC): class ColpkgExporter(Exporter): - format = ExportFormat.COLPKG + extension = "colpkg" show_include_media = True show_legacy_support = True @@ -220,11 +212,11 @@ class ColpkgExporter(Exporter): @classmethod 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: mw.reopen() - gui_hooks.exporter_did_export(options, cls.format) + gui_hooks.exporter_did_export(options, cls) tooltip(tr.exporting_collection_exported(), parent=mw) def on_failure(exception: Exception) -> None: @@ -246,7 +238,7 @@ class ColpkgExporter(Exporter): class ApkgExporter(Exporter): - format = ExportFormat.APKG + extension = "apkg" show_deck_list = True show_include_scheduling = True show_include_media = True @@ -258,10 +250,10 @@ class ApkgExporter(Exporter): @classmethod 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: - gui_hooks.exporter_did_export(options, cls.format) + gui_hooks.exporter_did_export(options, cls) tooltip(tr.exporting_note_exported(count=count), parent=mw) QueryOp( @@ -278,7 +270,7 @@ class ApkgExporter(Exporter): class NoteCsvExporter(Exporter): - format = ExportFormat.CSV_NOTES + extension = "txt" show_deck_list = True show_include_html = True show_include_tags = True @@ -292,10 +284,10 @@ class NoteCsvExporter(Exporter): @classmethod 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: - gui_hooks.exporter_did_export(options, cls.format) + gui_hooks.exporter_did_export(options, cls) tooltip(tr.exporting_note_exported(count=count), parent=mw) QueryOp( @@ -314,7 +306,7 @@ class NoteCsvExporter(Exporter): class CardCsvExporter(Exporter): - format = ExportFormat.CSV_CARDS + extension = "txt" show_deck_list = True show_include_html = True @@ -324,10 +316,10 @@ class CardCsvExporter(Exporter): @classmethod 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: - gui_hooks.exporter_did_export(options, cls.format) + gui_hooks.exporter_did_export(options, cls) tooltip(tr.exporting_card_exported(count=count), parent=mw) QueryOp( diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 7bf6959d3..e62713f32 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -19,7 +19,7 @@ prefix = """\ from __future__ import annotations -from typing import Any, Callable, Sequence, Literal +from typing import Any, Callable, Sequence, Literal, Type import anki import aqt @@ -822,7 +822,7 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest) name="exporter_will_export", args=[ "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", 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 `export_options` as is, e.g.: - def on_exporter_will_export(export_options: ExportOptions, export_format: ExportFormat): - if export_format != ExportFormat.APKG: + def on_exporter_will_export(export_options: ExportOptions, exporter: Type[Exporter]): + if not exporter == ApkgExporter: return export_options export_options.limit = ... return export_options @@ -842,18 +842,18 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest) name="exporter_did_export", args=[ "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.""", ), Hook( 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.""", ), Hook( 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.""", ), # Dialog Manager