2021-12-26 00:27:37 +00:00
|
|
|
# Updater updates previously imported note types.
|
|
|
|
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
|
|
|
|
# License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2021-12-25 20:17:48 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from .ankiconnect import invoke, request_model_names
|
|
|
|
from .common import select, get_used_fonts, NoteType
|
|
|
|
from .consts import *
|
2022-01-30 06:31:09 +00:00
|
|
|
from .importer import read_model, store_fonts
|
2021-12-25 20:17:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
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():
|
2022-01-30 06:31:09 +00:00
|
|
|
stored_models = {
|
|
|
|
(model := read_model(dir_name)).name: model
|
|
|
|
for dir_name in os.listdir(NOTE_TYPES_DIR)
|
|
|
|
}
|
|
|
|
updatable_models = [
|
|
|
|
model_name for model_name in stored_models
|
|
|
|
if model_name in request_model_names()
|
|
|
|
]
|
|
|
|
if not updatable_models:
|
2022-01-18 12:19:18 +00:00
|
|
|
print("No note types can be updated.")
|
|
|
|
return
|
2022-01-30 06:31:09 +00:00
|
|
|
if model_name := select(updatable_models):
|
|
|
|
print(f"Selected note type: {model_name}")
|
|
|
|
model = stored_models[model_name]
|
2022-01-18 12:19:18 +00:00
|
|
|
store_fonts(get_used_fonts(model.css))
|
|
|
|
send_note_type(model)
|
|
|
|
print("Done.")
|