mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00

- anki._backend stores the protobuf files and rsbackend.py code - pylib modules import protobuf messages directly from the _pb2 files, and explicitly export any will be returned or consumed by public pylib functions, so that calling code can import from pylib - the "rsbackend" no longer imports and re-exports protobuf messages - pylib can just consume them directly. - move errors to errors.py Still todo: - rsbridge - finishing the work on rsbackend, and check what we need to add back to the original file location to avoid breaking add-ons
76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
# Copyright: Ankitects Pty Ltd and contributors
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Callable, List, Tuple
|
|
|
|
from anki._backend import StockNoteType
|
|
from anki.collection import Collection
|
|
from anki.models import NoteType
|
|
from anki.utils import from_json_bytes
|
|
|
|
if TYPE_CHECKING:
|
|
from anki._backend.backend_pb2 import ( # pylint: disable=no-name-in-module
|
|
StockNoteTypeValue,
|
|
)
|
|
|
|
|
|
# add-on authors can add ("note type name", function_like_addBasicModel)
|
|
# to this list to have it shown in the add/clone note type screen
|
|
models: List[Tuple] = []
|
|
|
|
|
|
def add_stock_notetype(col: Collection, kind: StockNoteTypeValue) -> NoteType:
|
|
m = from_json_bytes(col.backend.get_stock_notetype_legacy(kind))
|
|
col.models.add(m)
|
|
return m
|
|
|
|
|
|
def addBasicModel(col: Collection) -> NoteType:
|
|
return add_stock_notetype(col, StockNoteType.STOCK_NOTE_TYPE_BASIC)
|
|
|
|
|
|
def addBasicTypingModel(col: Collection) -> NoteType:
|
|
return add_stock_notetype(col, StockNoteType.STOCK_NOTE_TYPE_BASIC_TYPING)
|
|
|
|
|
|
def addForwardReverse(col: Collection) -> NoteType:
|
|
return add_stock_notetype(col, StockNoteType.STOCK_NOTE_TYPE_BASIC_AND_REVERSED)
|
|
|
|
|
|
def addForwardOptionalReverse(col: Collection) -> NoteType:
|
|
return add_stock_notetype(
|
|
col, StockNoteType.STOCK_NOTE_TYPE_BASIC_OPTIONAL_REVERSED
|
|
)
|
|
|
|
|
|
def addClozeModel(col: Collection) -> NoteType:
|
|
return add_stock_notetype(col, StockNoteType.STOCK_NOTE_TYPE_CLOZE)
|
|
|
|
|
|
def get_stock_notetypes(
|
|
col: Collection,
|
|
) -> List[Tuple[str, Callable[[Collection], NoteType]]]:
|
|
out: List[Tuple[str, Callable[[Collection], NoteType]]] = []
|
|
# add standard
|
|
for (kind, func) in [
|
|
(StockNoteType.STOCK_NOTE_TYPE_BASIC, addBasicModel),
|
|
(StockNoteType.STOCK_NOTE_TYPE_BASIC_TYPING, addBasicTypingModel),
|
|
(StockNoteType.STOCK_NOTE_TYPE_BASIC_AND_REVERSED, addForwardReverse),
|
|
(
|
|
StockNoteType.STOCK_NOTE_TYPE_BASIC_OPTIONAL_REVERSED,
|
|
addForwardOptionalReverse,
|
|
),
|
|
(StockNoteType.STOCK_NOTE_TYPE_CLOZE, addClozeModel),
|
|
]:
|
|
m = from_json_bytes(col.backend.get_stock_notetype_legacy(kind))
|
|
out.append((m["name"], func))
|
|
# add extras from add-ons
|
|
for (name_or_func, func) in models:
|
|
if not isinstance(name_or_func, str):
|
|
name = name_or_func()
|
|
else:
|
|
name = name_or_func
|
|
out.append((name, func))
|
|
return out
|