Merge pull request #14 from nbelikov/select-updatable

Offer the user a choice of models that can be updated
This commit is contained in:
Ren Tatsumoto 2022-01-18 14:40:29 +00:00 committed by GitHub
commit d8c2ce8a23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 11 deletions

View file

@ -27,10 +27,13 @@ def read_card_templates(model_dir_name: str, template_names: list[str]) -> list[
return templates
def read_model(model_dir_name: str) -> NoteType:
def read_model_dict(model_dir_name: str) -> dict[str, Any]:
with open(os.path.join(NOTE_TYPES_DIR, model_dir_name, JSON_FILENAME), encoding='utf8') as f:
model_dict = json.load(f)
return json.load(f)
def read_model(model_dir_name: str) -> NoteType:
model_dict = read_model_dict(model_dir_name)
return NoteType(
name=model_dict['modelName'],
fields=model_dict['inOrderFields'],

View file

@ -7,7 +7,11 @@ 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
from .importer import read_model_dict, read_model, store_fonts
def read_model_name(model_dir_name: str) -> str:
return read_model_dict(model_dir_name)['modelName']
def format_templates(model: NoteType) -> dict[str, Any]:
@ -36,12 +40,15 @@ def send_note_type(model: NoteType):
def update_note_type():
if model_dir_name := select(os.listdir(NOTE_TYPES_DIR)):
print(f"Selected model: {model_dir_name}")
anki_models = set(request_model_names())
dir_names = [dir_name for dir_name in os.listdir(NOTE_TYPES_DIR)
if read_model_name(dir_name) in anki_models]
if not dir_names:
print("No note types can be updated.")
return
if model_dir_name := select(dir_names):
print(f"Selected note type: {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.")
store_fonts(get_used_fonts(model.css))
send_note_type(model)
print("Done.")