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

In order to split backend.proto into a more manageable size, the protobuf handling needed to be updated. This took more time than I would have liked, as each language handles protobuf differently: - The Python Protobuf code ignores "package" directives, and relies solely on how the files are laid out on disk. While it would have been nice to keep the generated files in a private subpackage, Protobuf gets confused if the files are located in a location that does not match their original .proto layout, so the old approach of storing them in _backend/ will not work. They now clutter up pylib/anki instead. I'm rather annoyed by that, but alternatives seem to be having to add an extra level to the Protobuf path, making the other languages suffer, or trying to hack around the issue by munging sys.modules. - Protobufjs fails to expose packages if they don't start with a capital letter, despite the fact that lowercase packages are the norm in most languages :-( This required a patch to fix. - Rust was the easiest, as Prost is relatively straightforward compared to Google's tools. The Protobuf files are now stored in /proto/anki, with a separate package for each file. I've split backend.proto into a few files as a test, but the majority of that work is still to come. The Python Protobuf building is a bit of a hack at the moment, hard-coding "proto" as the top level folder, but it seems to get the job done for now. Also changed the workspace name, as there seems to be a number of Bazel repos moving away from the more awkward reverse DNS naming style.
92 lines
2.7 KiB
Python
92 lines
2.7 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 Any, Callable, List, Tuple
|
|
|
|
import anki
|
|
import anki.backend_pb2 as _pb
|
|
from anki.utils import from_json_bytes
|
|
|
|
# pylint: disable=no-member
|
|
StockNotetypeKind = _pb.StockNotetype.Kind
|
|
|
|
# add-on authors can add ("note type name", function)
|
|
# to this list to have it shown in the add/clone note type screen
|
|
models: List[Tuple] = []
|
|
|
|
|
|
def _get_stock_notetype(
|
|
col: anki.collection.Collection, kind: StockNotetypeKind.V
|
|
) -> anki.models.NotetypeDict:
|
|
return from_json_bytes(col._backend.get_stock_notetype_legacy(kind))
|
|
|
|
|
|
def get_stock_notetypes(
|
|
col: anki.collection.Collection,
|
|
) -> List[Tuple[str, Callable[[anki.collection.Collection], anki.models.NotetypeDict]]]:
|
|
out: List[
|
|
Tuple[str, Callable[[anki.collection.Collection], anki.models.NotetypeDict]]
|
|
] = []
|
|
# add standard
|
|
for kind in [
|
|
StockNotetypeKind.BASIC,
|
|
StockNotetypeKind.BASIC_TYPING,
|
|
StockNotetypeKind.BASIC_AND_REVERSED,
|
|
StockNotetypeKind.BASIC_OPTIONAL_REVERSED,
|
|
StockNotetypeKind.CLOZE,
|
|
]:
|
|
m = from_json_bytes(col._backend.get_stock_notetype_legacy(kind))
|
|
|
|
def instance_getter(
|
|
model: Any,
|
|
) -> Callable[[anki.collection.Collection], anki.models.NotetypeDict]:
|
|
return lambda col: model
|
|
|
|
out.append((m["name"], instance_getter(m)))
|
|
# 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
|
|
|
|
|
|
#
|
|
# Legacy functions that added the notetype before returning it
|
|
#
|
|
|
|
|
|
def addBasicModel(col: anki.collection.Collection) -> anki.models.NotetypeDict:
|
|
nt = _get_stock_notetype(col, StockNotetypeKind.BASIC)
|
|
col.models.add(nt)
|
|
return nt
|
|
|
|
|
|
def addBasicTypingModel(col: anki.collection.Collection) -> anki.models.NotetypeDict:
|
|
nt = _get_stock_notetype(col, StockNotetypeKind.BASIC_TYPING)
|
|
col.models.add(nt)
|
|
return nt
|
|
|
|
|
|
def addForwardReverse(col: anki.collection.Collection) -> anki.models.NotetypeDict:
|
|
nt = _get_stock_notetype(col, StockNotetypeKind.BASIC_AND_REVERSED)
|
|
col.models.add(nt)
|
|
return nt
|
|
|
|
|
|
def addForwardOptionalReverse(
|
|
col: anki.collection.Collection,
|
|
) -> anki.models.NotetypeDict:
|
|
nt = _get_stock_notetype(col, StockNotetypeKind.BASIC_OPTIONAL_REVERSED)
|
|
col.models.add(nt)
|
|
return nt
|
|
|
|
|
|
def addClozeModel(col: anki.collection.Collection) -> anki.models.NotetypeDict:
|
|
nt = _get_stock_notetype(col, StockNotetypeKind.CLOZE)
|
|
col.models.add(nt)
|
|
return nt
|