From ed3e5a405ba65e86ed0f6649568837e5dcf4dfd2 Mon Sep 17 00:00:00 2001 From: Glutanimate Date: Mon, 18 Jul 2022 16:34:50 +0200 Subject: [PATCH] Fix filter argument order and add example to docstring --- qt/aqt/import_export/exporting.py | 16 ++++++++-------- qt/tools/genhooks_gui.py | 18 +++++++++++++++--- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/qt/aqt/import_export/exporting.py b/qt/aqt/import_export/exporting.py index f770552e5..a3d3f537c 100644 --- a/qt/aqt/import_export/exporting.py +++ b/qt/aqt/import_export/exporting.py @@ -220,11 +220,11 @@ class ColpkgExporter(Exporter): @staticmethod def export(mw: aqt.main.AnkiQt, options: Options) -> None: - gui_hooks.exporter_will_export(ExportFormat.COLPKG, options) + options = gui_hooks.exporter_will_export(options, ExportFormat.COLPKG) def on_success(_: None) -> None: mw.reopen() - gui_hooks.exporter_did_export(ExportFormat.COLPKG, options) + gui_hooks.exporter_did_export(options, ExportFormat.COLPKG) tooltip(tr.exporting_collection_exported(), parent=mw) def on_failure(exception: Exception) -> None: @@ -258,10 +258,10 @@ class ApkgExporter(Exporter): @staticmethod def export(mw: aqt.main.AnkiQt, options: Options) -> None: - gui_hooks.exporter_will_export(ExportFormat.APKG, options) + options = gui_hooks.exporter_will_export(options, ExportFormat.APKG) def on_success(count: int) -> None: - gui_hooks.exporter_did_export(ExportFormat.APKG, options) + gui_hooks.exporter_did_export(options, ExportFormat.APKG) tooltip(tr.exporting_note_exported(count=count), parent=mw) QueryOp( @@ -292,10 +292,10 @@ class NoteCsvExporter(Exporter): @staticmethod def export(mw: aqt.main.AnkiQt, options: Options) -> None: - gui_hooks.exporter_will_export(ExportFormat.CSV_NOTES, options) + options = gui_hooks.exporter_will_export(options, ExportFormat.CSV_NOTES) def on_success(count: int) -> None: - gui_hooks.exporter_did_export(ExportFormat.CSV_NOTES, options) + gui_hooks.exporter_did_export(options, ExportFormat.CSV_NOTES) tooltip(tr.exporting_note_exported(count=count), parent=mw) QueryOp( @@ -324,10 +324,10 @@ class CardCsvExporter(Exporter): @staticmethod def export(mw: aqt.main.AnkiQt, options: Options) -> None: - gui_hooks.exporter_will_export(ExportFormat.CSV_CARDS, options) + options = gui_hooks.exporter_will_export(options, ExportFormat.CSV_CARDS) def on_success(count: int) -> None: - gui_hooks.exporter_did_export(ExportFormat.CSV_CARDS, options) + gui_hooks.exporter_did_export(options, ExportFormat.CSV_CARDS) 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 11994e182..cac6c7407 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -819,16 +819,28 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest) Hook( name="exporter_will_export", args=[ - "export_format: aqt.import_export.exporting.ExportFormat", "options: aqt.import_export.exporting.Options", + "export_format: aqt.import_export.exporting.ExportFormat", ], - doc="""Called before collection and deck exports.""", + return_type="aqt.import_export.exporting.Options", + doc="""Called before collection and deck exports. + + Allows add-ons to be notified of impending deck exports and potentially + modify the export options. To perform the export unaltered, please return + `options` as is, e.g.: + + def on_exporter_will_export(options: Options, export_format: ExportFormat): + if export_format != ExportFormat.APKG: + return options + options.limit = ... + return options + """, ), Hook( name="exporter_did_export", args=[ - "export_format: aqt.import_export.exporting.ExportFormat", "options: aqt.import_export.exporting.Options", + "export_format: aqt.import_export.exporting.ExportFormat", ], doc="""Called after collection and deck exports.""", ),