Add legacy importers to new import screen (#1908)

This commit is contained in:
RumovZ 2022-06-09 02:57:29 +02:00 committed by GitHub
parent 6da5e5b042
commit 3816020085
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@
from __future__ import annotations from __future__ import annotations
import re
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from dataclasses import dataclass from dataclasses import dataclass
from itertools import chain from itertools import chain
@ -145,12 +146,33 @@ IMPORTERS: list[Type[Importer]] = [
] ]
def legacy_file_endings(col: Collection) -> list[str]:
from anki.importing import AnkiPackageImporter
from anki.importing import MnemosyneImporter as LegacyMnemosyneImporter
from anki.importing import TextImporter, importers
return [
ext
for (text, importer) in importers(col)
if importer not in (TextImporter, AnkiPackageImporter, LegacyMnemosyneImporter)
for ext in re.findall(r"[( ]?\*(\..+?)[) ]", text)
]
def import_file(mw: aqt.main.AnkiQt, path: str) -> None: def import_file(mw: aqt.main.AnkiQt, path: str) -> None:
filename = os.path.basename(path).lower() filename = os.path.basename(path).lower()
if any(filename.endswith(ext) for ext in legacy_file_endings(mw.col)):
import aqt.importing
aqt.importing.importFile(mw, path)
return
for importer in IMPORTERS: for importer in IMPORTERS:
if importer.can_import(filename): if importer.can_import(filename):
importer.do_import(mw, path) importer.do_import(mw, path)
return return
showWarning("Unsupported file type.") showWarning("Unsupported file type.")
@ -163,7 +185,7 @@ def get_file_path(mw: aqt.main.AnkiQt) -> str | None:
filter = without_unicode_isolation( filter = without_unicode_isolation(
tr.importing_all_supported_formats( tr.importing_all_supported_formats(
val="({})".format( val="({})".format(
" ".join(f"*{ending}" for ending in all_accepted_file_endings()) " ".join(f"*{ending}" for ending in all_accepted_file_endings(mw))
) )
) )
) )
@ -172,8 +194,13 @@ def get_file_path(mw: aqt.main.AnkiQt) -> str | None:
return None return None
def all_accepted_file_endings() -> set[str]: def all_accepted_file_endings(mw: aqt.main.AnkiQt) -> set[str]:
return set(chain(*(importer.accepted_file_endings for importer in IMPORTERS))) return set(
chain(
*(importer.accepted_file_endings for importer in IMPORTERS),
legacy_file_endings(mw.col),
)
)
def import_collection_package_op( def import_collection_package_op(