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
This commit is contained in:
Damien Elmes 2021-03-26 13:28:21 +10:00
parent 48354931da
commit efb1ce46d4
4 changed files with 29 additions and 23 deletions

View File

@ -1,21 +1,27 @@
# Copyright: Ankitects Pty Ltd and contributors # Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # 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.anki2 import Anki2Importer
from anki.importing.apkg import AnkiPackageImporter from anki.importing.apkg import AnkiPackageImporter
from anki.importing.base import Importer
from anki.importing.csvfile import TextImporter from anki.importing.csvfile import TextImporter
from anki.importing.mnemo import MnemosyneImporter from anki.importing.mnemo import MnemosyneImporter
from anki.importing.pauker import PaukerImporter from anki.importing.pauker import PaukerImporter
from anki.importing.supermemo_xml import SupermemoXmlImporter # type: ignore 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), def importers(col: Collection) -> Sequence[Tuple[str, Type[Importer]]]:
return (
(col.tr(TR.IMPORTING_TEXT_SEPARATED_BY_TABS_OR_SEMICOLONS), TextImporter),
( (
tr_legacyglobal(TR.IMPORTING_PACKAGED_ANKI_DECKCOLLECTION_APKG_COLPKG_ZIP), col.tr(TR.IMPORTING_PACKAGED_ANKI_DECKCOLLECTION_APKG_COLPKG_ZIP),
AnkiPackageImporter, AnkiPackageImporter,
), ),
(tr_legacyglobal(TR.IMPORTING_MNEMOSYNE_20_DECK_DB), MnemosyneImporter), (col.tr(TR.IMPORTING_MNEMOSYNE_20_DECK_DB), MnemosyneImporter),
(tr_legacyglobal(TR.IMPORTING_SUPERMEMO_XML_EXPORT_XML), SupermemoXmlImporter), (col.tr(TR.IMPORTING_SUPERMEMO_XML_EXPORT_XML), SupermemoXmlImporter),
(tr_legacyglobal(TR.IMPORTING_PAUKER_18_LESSON_PAUGZ), PaukerImporter), (col.tr(TR.IMPORTING_PAUKER_18_LESSON_PAUGZ), PaukerImporter),
) )

View File

@ -26,6 +26,14 @@ class Importer:
def run(self) -> None: def run(self) -> None:
pass 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 # Timestamps
###################################################################### ######################################################################
# It's too inefficient to check for existing ids on every object, # It's too inefficient to check for existing ids on every object,

View File

@ -112,14 +112,6 @@ class NoteImporter(Importer):
"Return a list of foreign notes for importing." "Return a list of foreign notes for importing."
return [] 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: def importNotes(self, notes: List[ForeignNote]) -> None:
"Convert each card into a note, apply attributes and add to col." "Convert each card into a note, apply attributes and add to col."
assert self.mappingOk() assert self.mappingOk()

View File

@ -314,7 +314,7 @@ def showUnicodeWarning() -> None:
def onImport(mw: AnkiQt) -> 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) file = getFile(mw, tr(TR.ACTIONS_IMPORT), None, key="import", filter=filt)
if not file: if not file:
return return
@ -335,7 +335,7 @@ def onImport(mw: AnkiQt) -> None:
def importFile(mw: AnkiQt, file: str) -> None: def importFile(mw: AnkiQt, file: str) -> None:
importerClass = None importerClass = None
done = False done = False
for i in importing.Importers: for i in importing.importers(mw.col):
if done: if done:
break break
for mext in re.findall(r"[( ]?\*\.(.+?)[) ]", i[0]): for mext in re.findall(r"[( ]?\*\.(.+?)[) ]", i[0]):
@ -345,7 +345,7 @@ def importFile(mw: AnkiQt, file: str) -> None:
break break
if not importerClass: if not importerClass:
# if no matches, assume TSV # if no matches, assume TSV
importerClass = importing.Importers[0][1] importerClass = importing.importers(mw.col)[0][1]
importer = importerClass(mw.col, file) importer = importerClass(mw.col, file)
# need to show import dialog? # need to show import dialog?
if importer.needMapper: if importer.needMapper:
@ -373,7 +373,7 @@ def importFile(mw: AnkiQt, file: str) -> None:
importer.close() importer.close()
else: else:
# if it's an apkg/zip, first test it's a valid file # if it's an apkg/zip, first test it's a valid file
if importer.__class__.__name__ == "AnkiPackageImporter": if isinstance(importer, AnkiPackageImporter):
try: try:
z = zipfile.ZipFile(importer.file) z = zipfile.ZipFile(importer.file)
z.getinfo("collection.anki2") z.getinfo("collection.anki2")