2021-04-20 10:32:25 +00:00
|
|
|
# Importer imports note types from ../templates/ to Anki.
|
2021-11-27 11:54:42 +00:00
|
|
|
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
|
|
|
|
# License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2021-04-20 10:32:25 +00:00
|
|
|
|
2021-04-20 10:20:20 +00:00
|
|
|
import json
|
2021-11-27 11:54:42 +00:00
|
|
|
from typing import Any
|
2021-04-20 10:20:20 +00:00
|
|
|
|
2021-12-25 20:17:08 +00:00
|
|
|
from .ankiconnect import invoke, request_model_names
|
2021-11-27 11:54:42 +00:00
|
|
|
from .common import select, get_used_fonts, NoteType, CardTemplate
|
|
|
|
from .consts import *
|
2021-04-20 10:20:20 +00:00
|
|
|
|
|
|
|
|
2021-11-27 15:34:04 +00:00
|
|
|
def read_css(model_dir_name: str) -> str:
|
2022-01-10 01:01:02 +00:00
|
|
|
with open(os.path.join(NOTE_TYPES_DIR, model_dir_name, CSS_FILENAME), encoding='utf8') as f:
|
2021-11-27 11:54:42 +00:00
|
|
|
return f.read()
|
2021-04-20 10:20:20 +00:00
|
|
|
|
|
|
|
|
2021-11-27 15:34:04 +00:00
|
|
|
def read_card_templates(model_dir_name: str, template_names: list[str]) -> list[CardTemplate]:
|
2021-11-27 11:54:42 +00:00
|
|
|
templates = []
|
|
|
|
for template_name in template_names:
|
2021-11-27 15:34:04 +00:00
|
|
|
dir_path = os.path.join(NOTE_TYPES_DIR, model_dir_name, template_name)
|
2022-01-10 01:01:02 +00:00
|
|
|
with (
|
|
|
|
open(os.path.join(dir_path, FRONT_FILENAME), encoding='utf8') as front,
|
|
|
|
open(os.path.join(dir_path, BACK_FILENAME), encoding='utf8') as back
|
|
|
|
):
|
2021-11-27 11:54:42 +00:00
|
|
|
templates.append(CardTemplate(template_name, front.read(), back.read()))
|
|
|
|
return templates
|
|
|
|
|
|
|
|
|
2022-01-18 12:19:18 +00:00
|
|
|
def read_model_dict(model_dir_name: str) -> dict[str, Any]:
|
2022-01-10 01:01:02 +00:00
|
|
|
with open(os.path.join(NOTE_TYPES_DIR, model_dir_name, JSON_FILENAME), encoding='utf8') as f:
|
2022-01-18 12:19:18 +00:00
|
|
|
return json.load(f)
|
|
|
|
|
2021-11-27 15:34:04 +00:00
|
|
|
|
2022-01-18 12:19:18 +00:00
|
|
|
def read_model(model_dir_name: str) -> NoteType:
|
|
|
|
model_dict = read_model_dict(model_dir_name)
|
2021-11-27 11:54:42 +00:00
|
|
|
return NoteType(
|
|
|
|
name=model_dict['modelName'],
|
|
|
|
fields=model_dict['inOrderFields'],
|
2021-11-27 15:34:04 +00:00
|
|
|
css=read_css(model_dir_name),
|
|
|
|
templates=read_card_templates(model_dir_name, model_dict['cardTemplates']),
|
2021-11-27 11:54:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def format_import(model: NoteType) -> dict[str, Any]:
|
|
|
|
return {
|
|
|
|
"modelName": model.name,
|
|
|
|
"inOrderFields": model.fields,
|
|
|
|
"css": model.css,
|
|
|
|
"cardTemplates": [
|
|
|
|
{
|
|
|
|
"Name": template.name,
|
|
|
|
"Front": template.front,
|
|
|
|
"Back": template.back,
|
|
|
|
}
|
|
|
|
for template in model.templates
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def send_note_type(model: NoteType):
|
|
|
|
template_json = format_import(model)
|
2021-12-25 20:17:08 +00:00
|
|
|
while template_json["modelName"] in request_model_names():
|
2021-04-20 10:55:41 +00:00
|
|
|
template_json["modelName"] = input("Model with this name already exists. Enter new name: ")
|
2021-04-20 10:20:20 +00:00
|
|
|
invoke("createModel", **template_json)
|
|
|
|
|
|
|
|
|
2021-04-20 13:23:50 +00:00
|
|
|
def store_fonts(fonts: list[str]):
|
2021-11-27 11:54:42 +00:00
|
|
|
for file in os.listdir(FONTS_DIR):
|
2021-04-20 13:23:50 +00:00
|
|
|
if file in fonts:
|
2021-11-27 11:54:42 +00:00
|
|
|
invoke("storeMediaFile", filename=file, path=os.path.join(FONTS_DIR, file))
|
2021-04-20 13:23:50 +00:00
|
|
|
|
|
|
|
|
2021-04-20 10:20:20 +00:00
|
|
|
def import_note_type():
|
2021-11-27 15:34:04 +00:00
|
|
|
if model_dir_name := select(os.listdir(NOTE_TYPES_DIR)):
|
|
|
|
print(f"Selected model: {model_dir_name}")
|
|
|
|
model = read_model(model_dir_name)
|
2021-11-27 11:54:42 +00:00
|
|
|
store_fonts(get_used_fonts(model.css))
|
|
|
|
send_note_type(model)
|
|
|
|
print("Done.")
|