From 1a4c4373d214b25799a8aa06384c2197d8b54031 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 4 Apr 2021 20:45:37 +1000 Subject: [PATCH] expose read-only access to new notetype objects --- pylib/anki/collection.py | 6 +++++- pylib/anki/models.py | 1 + rslib/backend.proto | 1 + rslib/src/backend/notetypes.rs | 9 +++++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pylib/anki/collection.py b/pylib/anki/collection.py index 933f5b6a7..253999e81 100644 --- a/pylib/anki/collection.py +++ b/pylib/anki/collection.py @@ -43,7 +43,7 @@ from anki.decks import Deck, DeckId, DeckManager from anki.errors import AbortSchemaModification, DBError from anki.lang import FormatTimeSpan from anki.media import MediaManager, media_paths_from_col_path -from anki.models import ModelManager, NotetypeDict, NotetypeId +from anki.models import ModelManager, NotetypeDict, NotetypeId, Notetype from anki.notes import Note, NoteId from anki.scheduler.v1 import Scheduler as V1Scheduler from anki.scheduler.v2 import Scheduler as V2Scheduler @@ -335,6 +335,10 @@ class Collection: "Get a new-style deck object. Currently read-only." return self._backend.get_deck(id) + def get_notetype(self, id: NotetypeId) -> Notetype: + """Get a new-style notetype object. This is not cached; avoid calling frequently.""" + return self._backend.get_notetype(id) + getCard = get_card getNote = get_note diff --git a/pylib/anki/models.py b/pylib/anki/models.py index fb394c5bc..8bf365e07 100644 --- a/pylib/anki/models.py +++ b/pylib/anki/models.py @@ -27,6 +27,7 @@ from anki.utils import ( ) # public exports +Notetype = _pb.Notetype NotetypeNameId = _pb.NotetypeNameId NotetypeNameIdUseCount = _pb.NotetypeNameIdUseCount diff --git a/rslib/backend.proto b/rslib/backend.proto index 331e1407f..c13f4fcf8 100644 --- a/rslib/backend.proto +++ b/rslib/backend.proto @@ -198,6 +198,7 @@ service ConfigService { service NotetypesService { rpc AddOrUpdateNotetype(AddOrUpdateNotetypeIn) returns (NotetypeId); rpc GetStockNotetypeLegacy(StockNotetype) returns (Json); + rpc GetNotetype(NotetypeId) returns (Notetype); rpc GetNotetypeLegacy(NotetypeId) returns (Json); rpc GetNotetypeNames(Empty) returns (NotetypeNames); rpc GetNotetypeNamesAndCounts(Empty) returns (NotetypeUseCounts); diff --git a/rslib/src/backend/notetypes.rs b/rslib/src/backend/notetypes.rs index 454cfdedc..29708a55a 100644 --- a/rslib/src/backend/notetypes.rs +++ b/rslib/src/backend/notetypes.rs @@ -34,6 +34,15 @@ impl NotetypesService for Backend { .map(Into::into) } + fn get_notetype(&self, input: pb::NotetypeId) -> Result { + self.with_col(|col| { + col.storage + .get_notetype(input.into())? + .ok_or(AnkiError::NotFound) + .map(Into::into) + }) + } + fn get_notetype_legacy(&self, input: pb::NotetypeId) -> Result { self.with_col(|col| { let schema11: NotetypeSchema11 = col