diff --git a/antp/__main__.py b/antp/__main__.py index 047426d..e770632 100644 --- a/antp/__main__.py +++ b/antp/__main__.py @@ -8,6 +8,7 @@ from urllib.error import URLError from .common import ANTPError from .exporter import export_note_type from .importer import import_note_type +from .overwriter import overwrite_note_type from .updater import update_note_type @@ -24,8 +25,11 @@ def wrap_errors(fn: Callable): def print_help(): options = ( - ("import", "Add one of the available note types to Anki."), - ("update", "Overwrite a previously imported note type with new data. Fields will not be updated."), + ("import", "Add one of the stored note types to Anki."), + ("update", "Overwrite a previously imported note type with new data. " + "Fields will not be updated."), + ("overwrite", "Overwrite a note type in Anki with new data from a stored note type. " + "Fields will not be updated."), ("export", "Save your note type as a template."), ("-v, --verbose", "Show detailed info when errors occur."), ) @@ -55,6 +59,8 @@ def main(): action = import_note_type case 'update': action = update_note_type + case 'overwrite': + action = overwrite_note_type case '-v' | '--verbose': wrap = False diff --git a/antp/common.py b/antp/common.py index 11f7f41..9f00021 100644 --- a/antp/common.py +++ b/antp/common.py @@ -1,6 +1,7 @@ # Copyright: Ren Tatsumoto # License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +import dataclasses import re from dataclasses import dataclass @@ -25,6 +26,11 @@ class NoteType: css: str templates: list[CardTemplate] + def rename(self, new_name: str): + _d = dataclasses.asdict(self) + del _d['name'] + return type(self)(name=new_name, **_d) + def read_num(msg: str = "Input number: ", min_val: int = 0, max_val: int = None) -> int: try: diff --git a/antp/overwriter.py b/antp/overwriter.py new file mode 100644 index 0000000..51239c8 --- /dev/null +++ b/antp/overwriter.py @@ -0,0 +1,27 @@ +# Overwriter acts like Updater but allows mapping models with different names. +# Copyright: Ren Tatsumoto +# License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +from .ankiconnect import request_model_names +from .common import select, get_used_fonts +from .consts import * +from .importer import read_model, store_fonts +from .updater import send_note_type + + +def overwrite_note_type(): + anki_models = request_model_names() + models_on_disk = { + (model := read_model(dir_name)).name: model + for dir_name in os.listdir(NOTE_TYPES_DIR) + } + model_name_on_disk = select(list(models_on_disk), "Take stored model: ") + model_name_in_anki = select(anki_models, "Replace templates in model: ") + + print(f"Writing templates from {model_name_on_disk} onto {model_name_in_anki}...") + + model = models_on_disk[model_name_on_disk] + store_fonts(get_used_fonts(model.css)) + send_note_type(model.rename(model_name_in_anki)) + + print("Done.")