From 5b673bdaceb738acb0475ed7ea704f142d6b4dc8 Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Wed, 22 Mar 2023 04:04:10 +0300 Subject: [PATCH] update importer --- antp/importer.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/antp/importer.py b/antp/importer.py index 82e04c4..0b4b406 100644 --- a/antp/importer.py +++ b/antp/importer.py @@ -3,7 +3,7 @@ # License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import json -from typing import Any +from typing import Any, Collection, Iterable from .ankiconnect import invoke, request_model_names from .common import select, get_used_fonts, NoteType, CardTemplate @@ -20,8 +20,8 @@ def read_card_templates(model_dir_name: str, template_names: list[str]) -> list[ for template_name in template_names: dir_path = os.path.join(NOTE_TYPES_DIR, model_dir_name, template_name) 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 + open(os.path.join(dir_path, FRONT_FILENAME), encoding='utf8') as front, + open(os.path.join(dir_path, BACK_FILENAME), encoding='utf8') as back ): templates.append(CardTemplate(template_name, front.read(), back.read())) return templates @@ -65,10 +65,16 @@ def send_note_type(model: NoteType): invoke("createModel", **template_json) -def store_fonts(fonts: list[str]): +def available_fonts(required_fonts: Collection[str]) -> Iterable[str]: + """ Filter required fonts and leave only those available on disk. """ for file in os.listdir(FONTS_DIR): - if file in fonts: - invoke("storeMediaFile", filename=file, path=os.path.join(FONTS_DIR, file)) + if file in required_fonts: + yield file + + +def store_fonts(fonts: list[str]): + for file in available_fonts(fonts): + invoke("storeMediaFile", filename=file, path=os.path.join(FONTS_DIR, file)) def import_note_type():