Tidy up import log

This commit is contained in:
RumovZ 2022-04-28 17:49:34 +02:00
parent 19664a0e47
commit 5e1b5d3586

View file

@ -3,7 +3,8 @@
from __future__ import annotations from __future__ import annotations
from typing import Sequence from itertools import chain
from typing import Any
import aqt.main import aqt.main
from anki.collection import Collection, ImportLogWithChanges, Progress from anki.collection import Collection, ImportLogWithChanges, Progress
@ -105,26 +106,36 @@ def import_anki_package(mw: aqt.main.AnkiQt, path: str) -> None:
def show_import_log(log_with_changes: ImportLogWithChanges) -> None: def show_import_log(log_with_changes: ImportLogWithChanges) -> None:
log = log_with_changes.log # type: ignore showText(stringify_log(log_with_changes.log), plain_text_edit=True)
def stringify_log(log: Any) -> str:
total = len(log.conflicting) + len(log.updated) + len(log.new) + len(log.duplicate) total = len(log.conflicting) + len(log.updated) + len(log.new) + len(log.duplicate)
return "\n".join(
text = f"""{tr.importing_notes_found_in_file(val=total)} chain(
{tr.importing_notes_that_could_not_be_imported(val=len(log.conflicting))} (tr.importing_notes_found_in_file(val=total), ""),
{tr.importing_notes_updated_as_file_had_newer(val=len(log.updated))} (
{tr.importing_notes_added_from_file(val=len(log.new))} template_string(val=len(row))
{tr.importing_notes_skipped_as_theyre_already_in(val=len(log.duplicate))} for (row, template_string) in (
(log.conflicting, tr.importing_notes_that_could_not_be_imported),
{log_rows(log.conflicting, tr.importing_skipped())} (log.updated, tr.importing_notes_updated_as_file_had_newer),
{log_rows(log.updated, tr.importing_updated())} (log.new, tr.importing_notes_added_from_file),
{log_rows(log.new, tr.adding_added())} (log.duplicate, tr.importing_notes_skipped_as_theyre_already_in),
{log_rows(log.duplicate, tr.importing_identical())} )
""" if row
),
showText(text, plain_text_edit=True) ("",),
*(
(f"[{action}] {', '.join(note.fields)}" for note in rows)
def log_rows(rows: Sequence, action: str) -> str: for (rows, action) in (
return "\n".join(f"[{action}] {', '.join(note.fields)}" for note in rows) (log.conflicting, tr.importing_skipped()),
(log.updated, tr.importing_updated()),
(log.new, tr.adding_added()),
(log.duplicate, tr.importing_identical()),
)
),
)
)
def import_progress_label(progress: Progress) -> str | None: def import_progress_label(progress: Progress) -> str | None: