98715e593a
* Implement import log screen in Svelte * Show filename in import log screen title * Remove unused NoteRow property * Show number of imported notes * Use a single nid expression * Use 'count' as variable name for consistency * Import from @tslib/backend instead * Fix summary_template typing * Fix clippy warning * Apply suggestions from code review * Fix imports * Contents -> Fields * Increase max length of browser search bar https://github.com/ankitects/anki/pull/2568/files#r1255227035 * Fix race condition in Bootstrap tooltip destruction https://github.com/twbs/bootstrap/issues/37474 * summary_template -> summaryTemplate * Make show link a button * Run import ops on Svelte side * Fix geometry not being restored in CSV Import page * Make VirtualTable fill available height * Keep CSV dialog modal * Reword importing-existing-notes-skipped * Avoid mentioning matching based on first field * Change tick and cross icons * List skipped notes last * Pure CSS spinner * Move set_wants_abort() call to relevant dialogs * Show number of imported cards * Remove bold from first sentence and indent summaries * Update UI after import operations * Add close button to import log page Also make virtual table react to resize event. * Fix typing * Make CSV dialog non-modal again Otherwise user can't interact with browser window. * Update window modality after import * Commit DB and update undo actions after import op * Split frontend proto into separate file, so backend can ignore it Currently the automatically-generated frontend RPC methods get placed in 'backend.js' with all the backend methods; we could optionally split them into a separate 'frontend.js' file in the future. * Migrate import_done from a bridgecmd to a HTTP request * Update plural form of importing-notes-added * Move import response handling to mediasrv * Move task callback to script section * Avoid unnecessary :global() * .log cannot be missing if result exists * Move import log search handling to mediasrv * Type common params of ImportLogDialog * Use else if * Remove console.log() * Add way to test apkg imports in new log screen * Remove unused import * Get actual card count for CSV imports * Use import type * Fix typing error * Ignore import log when checking for changes in Python layer * Apply suggestions from code review * Remove imported card count for now * Avoid non-null assertion in assignment * Change showInBrowser to take an array of notes * Use dataclasses for import log args * Simplify ResultWithChanges in TS * Only abort import when window is modal * Fix ResultWithChanges typing * Fix Rust warnings * Only log one duplicate per incoming note * Update wording about note updates * Remove caveat about found_notes * Reduce font size * Remove redundant map * Give credit to loading.io * Remove unused line --------- Co-authored-by: RumovZ <gp5glkw78@relay.firefox.com>
214 lines
6.1 KiB
Python
214 lines
6.1 KiB
Python
# Copyright: Ankitects Pty Ltd and contributors
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from abc import ABC, abstractmethod
|
|
from itertools import chain
|
|
from typing import Type
|
|
|
|
import aqt.main
|
|
from anki.collection import Collection, Progress
|
|
from anki.errors import Interrupted
|
|
from anki.foreign_data import mnemosyne
|
|
from anki.lang import without_unicode_isolation
|
|
from aqt.import_export.import_csv_dialog import ImportCsvDialog
|
|
from aqt.import_export.import_log_dialog import (
|
|
ApkgArgs,
|
|
ImportLogDialog,
|
|
JsonFileArgs,
|
|
JsonStringArgs,
|
|
)
|
|
from aqt.operations import QueryOp
|
|
from aqt.progress import ProgressUpdate
|
|
from aqt.qt import *
|
|
from aqt.utils import askUser, getFile, showWarning, tooltip, tr
|
|
|
|
|
|
class Importer(ABC):
|
|
accepted_file_endings: list[str]
|
|
|
|
@classmethod
|
|
def can_import(cls, lowercase_filename: str) -> bool:
|
|
return any(
|
|
lowercase_filename.endswith(ending) for ending in cls.accepted_file_endings
|
|
)
|
|
|
|
@classmethod
|
|
@abstractmethod
|
|
def do_import(cls, mw: aqt.main.AnkiQt, path: str) -> None:
|
|
...
|
|
|
|
|
|
class ColpkgImporter(Importer):
|
|
accepted_file_endings = [".apkg", ".colpkg"]
|
|
|
|
@staticmethod
|
|
def can_import(filename: str) -> bool:
|
|
return (
|
|
filename == "collection.apkg"
|
|
or (filename.startswith("backup-") and filename.endswith(".apkg"))
|
|
or filename.endswith(".colpkg")
|
|
)
|
|
|
|
@staticmethod
|
|
def do_import(mw: aqt.main.AnkiQt, path: str) -> None:
|
|
if askUser(
|
|
tr.importing_this_will_delete_your_existing_collection(),
|
|
msgfunc=QMessageBox.warning,
|
|
defaultno=True,
|
|
):
|
|
ColpkgImporter._import(mw, path)
|
|
|
|
@staticmethod
|
|
def _import(mw: aqt.main.AnkiQt, file: str) -> None:
|
|
def on_success() -> None:
|
|
mw.loadCollection()
|
|
tooltip(tr.importing_importing_complete())
|
|
|
|
def on_failure(err: Exception) -> None:
|
|
mw.loadCollection()
|
|
if not isinstance(err, Interrupted):
|
|
showWarning(str(err))
|
|
|
|
QueryOp(
|
|
parent=mw,
|
|
op=lambda _: mw.create_backup_now(),
|
|
success=lambda _: mw.unloadCollection(
|
|
lambda: import_collection_package_op(mw, file, on_success)
|
|
.failure(on_failure)
|
|
.run_in_background()
|
|
),
|
|
).with_progress().run_in_background()
|
|
|
|
|
|
class ApkgImporter(Importer):
|
|
accepted_file_endings = [".apkg", ".zip"]
|
|
|
|
@staticmethod
|
|
def do_import(mw: aqt.main.AnkiQt, path: str) -> None:
|
|
ImportLogDialog(mw, ApkgArgs(path=path))
|
|
|
|
|
|
class MnemosyneImporter(Importer):
|
|
accepted_file_endings = [".db"]
|
|
|
|
@staticmethod
|
|
def do_import(mw: aqt.main.AnkiQt, path: str) -> None:
|
|
QueryOp(
|
|
parent=mw,
|
|
op=lambda col: mnemosyne.serialize(path, col.decks.current()["id"]),
|
|
success=lambda json: ImportLogDialog(
|
|
mw, JsonStringArgs(path=path, json=json)
|
|
),
|
|
).with_progress().run_in_background()
|
|
|
|
|
|
class CsvImporter(Importer):
|
|
accepted_file_endings = [".csv", ".tsv", ".txt"]
|
|
|
|
@staticmethod
|
|
def do_import(mw: aqt.main.AnkiQt, path: str) -> None:
|
|
ImportCsvDialog(mw, path)
|
|
|
|
|
|
class JsonImporter(Importer):
|
|
accepted_file_endings = [".anki-json"]
|
|
|
|
@staticmethod
|
|
def do_import(mw: aqt.main.AnkiQt, path: str) -> None:
|
|
ImportLogDialog(mw, JsonFileArgs(path=path))
|
|
|
|
|
|
IMPORTERS: list[Type[Importer]] = [
|
|
ColpkgImporter,
|
|
ApkgImporter,
|
|
MnemosyneImporter,
|
|
CsvImporter,
|
|
]
|
|
|
|
|
|
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:
|
|
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:
|
|
if importer.can_import(filename):
|
|
importer.do_import(mw, path)
|
|
return
|
|
|
|
showWarning("Unsupported file type.")
|
|
|
|
|
|
def prompt_for_file_then_import(mw: aqt.main.AnkiQt) -> None:
|
|
if path := get_file_path(mw):
|
|
import_file(mw, path)
|
|
|
|
|
|
def get_file_path(mw: aqt.main.AnkiQt) -> str | None:
|
|
filter = without_unicode_isolation(
|
|
tr.importing_all_supported_formats(
|
|
val="({})".format(
|
|
" ".join(f"*{ending}" for ending in all_accepted_file_endings(mw))
|
|
)
|
|
)
|
|
)
|
|
if file := getFile(mw, tr.actions_import(), None, key="import", filter=filter):
|
|
return str(file)
|
|
return None
|
|
|
|
|
|
def all_accepted_file_endings(mw: aqt.main.AnkiQt) -> set[str]:
|
|
return set(
|
|
chain(
|
|
*(importer.accepted_file_endings for importer in IMPORTERS),
|
|
legacy_file_endings(mw.col),
|
|
)
|
|
)
|
|
|
|
|
|
def import_collection_package_op(
|
|
mw: aqt.main.AnkiQt, path: str, success: Callable[[], None]
|
|
) -> QueryOp[None]:
|
|
def op(_: Collection) -> None:
|
|
col_path = mw.pm.collectionPath()
|
|
media_folder = os.path.join(mw.pm.profileFolder(), "collection.media")
|
|
media_db = os.path.join(mw.pm.profileFolder(), "collection.media.db2")
|
|
mw.backend.import_collection_package(
|
|
col_path=col_path,
|
|
backup_path=path,
|
|
media_folder=media_folder,
|
|
media_db=media_db,
|
|
)
|
|
|
|
return QueryOp(parent=mw, op=op, success=lambda _: success()).with_backend_progress(
|
|
import_progress_update
|
|
)
|
|
|
|
|
|
def import_progress_update(progress: Progress, update: ProgressUpdate) -> None:
|
|
if not progress.HasField("importing"):
|
|
return
|
|
update.label = progress.importing
|
|
if update.user_wants_abort:
|
|
update.abort = True
|