add updater

This commit is contained in:
Ren Tatsumoto 2021-12-25 23:17:48 +03:00
parent f705a853a1
commit b7b8f0e7b9
2 changed files with 49 additions and 0 deletions

View file

@ -7,6 +7,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 .updater import update_note_type
def main(): def main():
@ -14,6 +15,7 @@ def main():
return print( return print(
"No action provided.\n\n" "No action provided.\n\n"
f"import\tAdd one of the available note types to Anki.\n" f"import\tAdd one of the available note types to Anki.\n"
f"update\tOverwrite a previously imported note type with new data.\n"
f"export\tSave your note type as a template.\n" f"export\tSave your note type as a template.\n"
) )
@ -23,6 +25,10 @@ def main():
export_note_type() export_note_type()
case 'import': case 'import':
import_note_type() import_note_type()
case 'update':
update_note_type()
case _:
print("Unknown action.")
except URLError: except URLError:
print("Couldn't connect. Make sure Anki is open and AnkiConnect is installed.") print("Couldn't connect. Make sure Anki is open and AnkiConnect is installed.")
except ANTPError as ex: except ANTPError as ex:

43
antp/updater.py Normal file
View file

@ -0,0 +1,43 @@
from typing import Any
from .ankiconnect import invoke, request_model_names
from .common import select, get_used_fonts, NoteType
from .consts import *
from .importer import read_model, store_fonts
def format_templates(model: NoteType) -> dict[str, Any]:
return {
"model": {
"name": model.name,
"templates": {
template.name: {"Front": template.front, "Back": template.back} for template in model.templates
}
}
}
def format_styling(model: NoteType) -> dict[str, Any]:
return {
"model": {
"name": model.name,
"css": model.css
}
}
def send_note_type(model: NoteType):
invoke("updateModelTemplates", **format_templates(model))
invoke("updateModelStyling", **format_styling(model))
def update_note_type():
if model_dir_name := select(os.listdir(NOTE_TYPES_DIR)):
print(f"Selected model: {model_dir_name}")
model = read_model(model_dir_name)
if model.name in request_model_names():
store_fonts(get_used_fonts(model.css))
send_note_type(model)
print("Done.")
else:
print("This note type hasn't been imported yet. Aborted.")