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
# 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),
)

View File

@ -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,

View File

@ -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()

View File

@ -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")