add overwriter

This commit is contained in:
Ren Tatsumoto 2022-12-02 16:31:59 +03:00
parent fbbb5dbb74
commit 0bb4f3745e
3 changed files with 41 additions and 2 deletions

View file

@ -8,6 +8,7 @@ from urllib.error import URLError
from .common import ANTPError from .common import ANTPError
from .exporter import export_note_type from .exporter import export_note_type
from .importer import import_note_type from .importer import import_note_type
from .overwriter import overwrite_note_type
from .updater import update_note_type from .updater import update_note_type
@ -24,8 +25,11 @@ def wrap_errors(fn: Callable):
def print_help(): def print_help():
options = ( options = (
("import", "Add one of the available note types to Anki."), ("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."), ("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."), ("export", "Save your note type as a template."),
("-v, --verbose", "Show detailed info when errors occur."), ("-v, --verbose", "Show detailed info when errors occur."),
) )
@ -55,6 +59,8 @@ def main():
action = import_note_type action = import_note_type
case 'update': case 'update':
action = update_note_type action = update_note_type
case 'overwrite':
action = overwrite_note_type
case '-v' | '--verbose': case '-v' | '--verbose':
wrap = False wrap = False

View file

@ -1,6 +1,7 @@
# Copyright: Ren Tatsumoto <tatsu at autistici.org> # Copyright: Ren Tatsumoto <tatsu at autistici.org>
# License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import dataclasses
import re import re
from dataclasses import dataclass from dataclasses import dataclass
@ -25,6 +26,11 @@ class NoteType:
css: str css: str
templates: list[CardTemplate] 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: def read_num(msg: str = "Input number: ", min_val: int = 0, max_val: int = None) -> int:
try: try:

27
antp/overwriter.py Normal file
View file

@ -0,0 +1,27 @@
# Overwriter acts like Updater but allows mapping models with different names.
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
# 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.")