From 08ad9f33b998a9dd3677e925809aa169e45b8a01 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Thu, 9 Dec 2021 02:20:29 +0100 Subject: [PATCH] The importer list have a Hook (#1543) * The importer list have a Hook Previously, add-on 175027074 simply edited the list once. It became impossible since the list became a function. Hence I need a filter to add the list here. @kelciour (nice to meet you by the way), you may be interested by it too (at least if I believe efb1ce46d4b6982833b0ed324b9d105ba83ba394 ) I would have preferred to use `anki.importing.base.Importer` instead of `Any`. However, this leads to > Name "anki.importing.base.Importer" is not defined [name-defined] when I run test. Helps to solve this would be welcomed * mention the hook may not last too long (dae) --- pylib/anki/importing/__init__.py | 7 +++++-- pylib/tools/genhooks.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pylib/anki/importing/__init__.py b/pylib/anki/importing/__init__.py index c5636f6b1..da0ca01ec 100644 --- a/pylib/anki/importing/__init__.py +++ b/pylib/anki/importing/__init__.py @@ -3,6 +3,7 @@ from typing import Any, Callable, Sequence, Type, Union +from anki import hooks from anki.collection import Collection from anki.importing.anki2 import Anki2Importer from anki.importing.apkg import AnkiPackageImporter @@ -15,7 +16,7 @@ from anki.lang import TR def importers(col: Collection) -> Sequence[tuple[str, type[Importer]]]: - return ( + importers = [ (col.tr.importing_text_separated_by_tabs_or_semicolons(), TextImporter), ( col.tr.importing_packaged_anki_deckcollection_apkg_colpkg_zip(), @@ -24,4 +25,6 @@ def importers(col: Collection) -> Sequence[tuple[str, type[Importer]]]: (col.tr.importing_mnemosyne_20_deck_db(), MnemosyneImporter), (col.tr.importing_supermemo_xml_export_xml(), SupermemoXmlImporter), (col.tr.importing_pauker_18_lesson_paugz(), PaukerImporter), - ) + ] + hooks.importing_importers(importers) + return importers diff --git a/pylib/tools/genhooks.py b/pylib/tools/genhooks.py index e89bc054b..0627b32a5 100644 --- a/pylib/tools/genhooks.py +++ b/pylib/tools/genhooks.py @@ -93,6 +93,17 @@ hooks = [ doc="""Allows changing the number of rev card for this deck (without considering descendants).""", ), + Hook( + name="importing_importers", + args=["importers: list[tuple[str, Any]]"], + doc="""Allows updating the list of importers. + The resulting list is not saved and should be changed each time the + filter is called. + + NOTE: Updates to the import/export code are expected in the coming + months, and this hook may be replaced with another solution at that + time. Tracked on https://github.com/ankitects/anki/issues/1018""", + ), # obsolete Hook( name="deck_added",