Repository navigation
Expand file tree
/
Copy pathmodel_utils.py
More file actions
55 lines (41 loc) · 1.62 KB
/
Copy pathmodel_utils.py
File metadata and controls
55 lines (41 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright: Ajatt-Tools and contributors; https://github.com/Ajatt-Tools
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import typing
from collections.abc import Iterable
from aqt import mw
AnkiCardSide = typing.Literal["qfmt", "afmt"]
class AnkiCardTemplateDict(typing.TypedDict):
qfmt: str
afmt: str
name: str # card template name, e.g. "recognition", "production".
class AnkiNoteTypeFieldDict(typing.TypedDict):
name: str
ord: int
# omitted other keys
class AnkiNoteTypeDict(typing.TypedDict):
tmpls: list[AnkiCardTemplateDict]
css: str
name: str # model name
flds: list[AnkiNoteTypeFieldDict]
def get_model_field_names(model_dict: AnkiNoteTypeDict | None) -> Iterable[str]:
"""
Returns all field names found in the note type.
"""
if model_dict is None:
raise ValueError("note type is None.")
return (field["name"] for field in model_dict["flds"])
def relevant_field_names(note_type_name_fuzzy: str | None = None) -> Iterable[str]:
"""
Return an iterable of field names present in note types whose names contain the first parameter.
"""
for model in mw.col.models.all_names_and_ids():
if not note_type_name_fuzzy or note_type_name_fuzzy.lower() in model.name.lower():
yield from get_model_field_names(mw.col.models.get(model.id))
def gather_all_field_names() -> Iterable[str]:
"""
Returns all field names found in all note types found in the collection.
"""
if not mw:
# running from playground
return ["anki", "is not", "running"]
return relevant_field_names()