AnkiNoteTemplate/antp/common.py

38 lines
1,019 B
Python
Raw Normal View History

2021-04-20 10:27:47 +00:00
import os
2021-04-20 13:23:50 +00:00
import re
2021-04-20 10:27:47 +00:00
from typing import Optional
JSON_INDENT = 4
JSON_FILENAME = 'template.json'
2021-04-20 11:02:13 +00:00
README_FILENAME = 'README.md'
2021-04-20 10:27:47 +00:00
SCRIPT_DIR = os.path.dirname(__file__)
NOTE_TYPES_DIR = os.path.join(SCRIPT_DIR, os.pardir, 'templates')
2021-04-20 13:23:50 +00:00
FONTS_DIR = os.path.join(SCRIPT_DIR, os.pardir, 'fonts')
if not os.path.isdir(FONTS_DIR):
os.mkdir(FONTS_DIR)
2021-04-20 10:27:47 +00:00
def read_num(msg: str = "Input number: ", min_val: int = 0, max_val: int = None) -> int:
resp = int(input(msg))
if resp < min_val or (max_val and resp > max_val):
raise ValueError("Value out of range.")
return resp
2021-04-22 20:03:51 +00:00
def select(items: list[str], msg: str = "Select item number: ") -> Optional[str]:
2021-04-20 10:27:47 +00:00
if not items:
print("Nothing to show.")
return
2021-04-22 20:03:51 +00:00
2021-04-20 10:27:47 +00:00
for idx, model in enumerate(items):
print(f"{idx}: {model}")
2021-04-22 20:03:51 +00:00
print()
idx = read_num(msg, max_val=len(items) - 1)
return items[idx]
2021-04-20 13:23:50 +00:00
def get_used_fonts(template_css: str):
return re.findall(r"url\([\"'](\w+\.[ot]tf)[\"']\)", template_css, re.IGNORECASE)