From efb1ce46d4b6982833b0ed324b9d105ba83ba394 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 26 Mar 2021 13:28:21 +1000 Subject: [PATCH] switch the Importers global to a callable for i18n I18n is not set up at init time, so the strings can't be generated at import. @kelciour you have a few importing add-ons, so wanted to give you a heads-up. The importing code is likely to change more in future months, but for now this should be the only change --- pylib/anki/importing/__init__.py | 28 +++++++++++++++++----------- pylib/anki/importing/base.py | 8 ++++++++ pylib/anki/importing/noteimp.py | 8 -------- qt/aqt/importing.py | 8 ++++---- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/pylib/anki/importing/__init__.py b/pylib/anki/importing/__init__.py index ec7e05080..5a8f928c1 100644 --- a/pylib/anki/importing/__init__.py +++ b/pylib/anki/importing/__init__.py @@ -1,21 +1,27 @@ # Copyright: Ankitects Pty Ltd and contributors # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +from typing import Any, Callable, Sequence, Tuple, Type, Union + +from anki import Collection from anki.importing.anki2 import Anki2Importer from anki.importing.apkg import AnkiPackageImporter +from anki.importing.base import Importer from anki.importing.csvfile import TextImporter from anki.importing.mnemo import MnemosyneImporter from anki.importing.pauker import PaukerImporter from anki.importing.supermemo_xml import SupermemoXmlImporter # type: ignore -from anki.lang import TR, tr_legacyglobal +from anki.lang import TR -Importers = ( - (tr_legacyglobal(TR.IMPORTING_TEXT_SEPARATED_BY_TABS_OR_SEMICOLONS), TextImporter), - ( - tr_legacyglobal(TR.IMPORTING_PACKAGED_ANKI_DECKCOLLECTION_APKG_COLPKG_ZIP), - AnkiPackageImporter, - ), - (tr_legacyglobal(TR.IMPORTING_MNEMOSYNE_20_DECK_DB), MnemosyneImporter), - (tr_legacyglobal(TR.IMPORTING_SUPERMEMO_XML_EXPORT_XML), SupermemoXmlImporter), - (tr_legacyglobal(TR.IMPORTING_PAUKER_18_LESSON_PAUGZ), PaukerImporter), -) + +def importers(col: Collection) -> Sequence[Tuple[str, Type[Importer]]]: + return ( + (col.tr(TR.IMPORTING_TEXT_SEPARATED_BY_TABS_OR_SEMICOLONS), TextImporter), + ( + col.tr(TR.IMPORTING_PACKAGED_ANKI_DECKCOLLECTION_APKG_COLPKG_ZIP), + AnkiPackageImporter, + ), + (col.tr(TR.IMPORTING_MNEMOSYNE_20_DECK_DB), MnemosyneImporter), + (col.tr(TR.IMPORTING_SUPERMEMO_XML_EXPORT_XML), SupermemoXmlImporter), + (col.tr(TR.IMPORTING_PAUKER_18_LESSON_PAUGZ), PaukerImporter), + ) diff --git a/pylib/anki/importing/base.py b/pylib/anki/importing/base.py index 959059251..4ee6626a8 100644 --- a/pylib/anki/importing/base.py +++ b/pylib/anki/importing/base.py @@ -26,6 +26,14 @@ class Importer: def run(self) -> None: pass + def open(self) -> None: + "Open file and ensure it's in the right format." + return + + def close(self) -> None: + "Closes the open file." + return + # Timestamps ###################################################################### # It's too inefficient to check for existing ids on every object, diff --git a/pylib/anki/importing/noteimp.py b/pylib/anki/importing/noteimp.py index 8ba0c05cd..1865fac1d 100644 --- a/pylib/anki/importing/noteimp.py +++ b/pylib/anki/importing/noteimp.py @@ -112,14 +112,6 @@ class NoteImporter(Importer): "Return a list of foreign notes for importing." return [] - def open(self) -> None: - "Open file and ensure it's in the right format." - return - - def close(self) -> None: - "Closes the open file." - return - def importNotes(self, notes: List[ForeignNote]) -> None: "Convert each card into a note, apply attributes and add to col." assert self.mappingOk() diff --git a/qt/aqt/importing.py b/qt/aqt/importing.py index 768062091..3ba98998a 100644 --- a/qt/aqt/importing.py +++ b/qt/aqt/importing.py @@ -314,7 +314,7 @@ def showUnicodeWarning() -> None: def onImport(mw: AnkiQt) -> None: - filt = ";;".join([x[0] for x in importing.Importers]) + filt = ";;".join([x[0] for x in importing.importers(mw.col)]) file = getFile(mw, tr(TR.ACTIONS_IMPORT), None, key="import", filter=filt) if not file: return @@ -335,7 +335,7 @@ def onImport(mw: AnkiQt) -> None: def importFile(mw: AnkiQt, file: str) -> None: importerClass = None done = False - for i in importing.Importers: + for i in importing.importers(mw.col): if done: break for mext in re.findall(r"[( ]?\*\.(.+?)[) ]", i[0]): @@ -345,7 +345,7 @@ def importFile(mw: AnkiQt, file: str) -> None: break if not importerClass: # if no matches, assume TSV - importerClass = importing.Importers[0][1] + importerClass = importing.importers(mw.col)[0][1] importer = importerClass(mw.col, file) # need to show import dialog? if importer.needMapper: @@ -373,7 +373,7 @@ def importFile(mw: AnkiQt, file: str) -> None: importer.close() else: # if it's an apkg/zip, first test it's a valid file - if importer.__class__.__name__ == "AnkiPackageImporter": + if isinstance(importer, AnkiPackageImporter): try: z = zipfile.ZipFile(importer.file) z.getinfo("collection.anki2")