embed kind enum in StockNoteType and remove prefix
This commit is contained in:
parent
248e067da7
commit
5974163343
@ -23,9 +23,6 @@ from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Union
|
||||
|
||||
import anki.buildinfo
|
||||
from . import backend_pb2 as pb
|
||||
from . import rsbridge
|
||||
from anki import hooks
|
||||
from anki._backend.generated import RustBackendGenerated
|
||||
from anki.dbproxy import Row as DBRow
|
||||
from anki.dbproxy import ValueForDB
|
||||
@ -33,6 +30,9 @@ from anki.errors import backend_exception_to_pylib
|
||||
from anki.lang import FormatTimeSpanContext
|
||||
from anki.utils import from_json_bytes, to_json_bytes
|
||||
|
||||
from . import backend_pb2 as pb
|
||||
from . import rsbridge
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from anki.lang import FormatTimeSpanContextValue, TRValue
|
||||
|
||||
@ -48,7 +48,7 @@ BackendNote = pb.Note
|
||||
Tag = pb.Tag
|
||||
TagTreeNode = pb.TagTreeNode
|
||||
NoteType = pb.NoteType
|
||||
StockNoteType = pb.StockNoteType
|
||||
BackendNoteTypeID = pb.NoteTypeID
|
||||
ConcatSeparator = pb.ConcatenateSearchesIn.Separator
|
||||
CountsForDeckToday = pb.CountsForDeckTodayOut
|
||||
|
||||
|
@ -13,6 +13,7 @@ import anki._backend.backend_pb2 as _pb
|
||||
from anki.consts import *
|
||||
from anki.errors import NotFoundError
|
||||
from anki.lang import TR, without_unicode_isolation
|
||||
from anki.stdmodels import StockNotetypeKind
|
||||
from anki.utils import (
|
||||
checksum,
|
||||
from_json_bytes,
|
||||
@ -206,9 +207,7 @@ class ModelManager:
|
||||
"Create a new model, and return it."
|
||||
# caller should call save() after modifying
|
||||
nt = from_json_bytes(
|
||||
self.col.backend.get_stock_notetype_legacy(
|
||||
_pb.StockNoteType.STOCK_NOTE_TYPE_BASIC
|
||||
)
|
||||
self.col.backend.get_stock_notetype_legacy(StockNotetypeKind.BASIC)
|
||||
)
|
||||
nt["flds"] = []
|
||||
nt["tmpls"] = []
|
||||
@ -299,9 +298,7 @@ class ModelManager:
|
||||
def new_field(self, name: str) -> Field:
|
||||
assert isinstance(name, str)
|
||||
nt = from_json_bytes(
|
||||
self.col.backend.get_stock_notetype_legacy(
|
||||
_pb.StockNoteType.STOCK_NOTE_TYPE_BASIC
|
||||
)
|
||||
self.col.backend.get_stock_notetype_legacy(StockNotetypeKind.BASIC)
|
||||
)
|
||||
field = nt["flds"][0]
|
||||
field["name"] = name
|
||||
@ -360,9 +357,7 @@ class ModelManager:
|
||||
|
||||
def new_template(self, name: str) -> Template:
|
||||
nt = from_json_bytes(
|
||||
self.col.backend.get_stock_notetype_legacy(
|
||||
_pb.StockNoteType.STOCK_NOTE_TYPE_BASIC
|
||||
)
|
||||
self.col.backend.get_stock_notetype_legacy(StockNotetypeKind.BASIC)
|
||||
)
|
||||
template = nt["tmpls"][0]
|
||||
template["name"] = name
|
||||
|
@ -9,9 +9,9 @@
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from anki.lang import FormatTimeSpanContext
|
||||
from anki.decks import DeckTreeNode
|
||||
from anki.errors import NotFoundError, InvalidInput
|
||||
from anki.errors import InvalidInput, NotFoundError
|
||||
from anki.lang import FormatTimeSpanContext
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from anki.lang import FormatTimeSpanContextValue, TRValue
|
||||
|
@ -5,15 +5,16 @@ 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
|
||||
import anki
|
||||
import anki._backend.backend_pb2 as _pb
|
||||
from anki.utils import from_json_bytes
|
||||
|
||||
# pylint: disable=no-member
|
||||
StockNotetypeKind = _pb.StockNoteType.Kind
|
||||
|
||||
# pylint: disable=no-member
|
||||
if TYPE_CHECKING:
|
||||
from anki._backend.backend_pb2 import ( # pylint: disable=no-name-in-module
|
||||
StockNoteTypeValue,
|
||||
)
|
||||
StockNotetypeKindValue = _pb.StockNoteType.KindValue
|
||||
|
||||
|
||||
# add-on authors can add ("note type name", function_like_addBasicModel)
|
||||
@ -21,48 +22,50 @@ if TYPE_CHECKING:
|
||||
models: List[Tuple] = []
|
||||
|
||||
|
||||
def add_stock_notetype(col: Collection, kind: StockNoteTypeValue) -> NoteType:
|
||||
def _add_stock_notetype(
|
||||
col: anki.collection.Collection, kind: StockNotetypeKindValue
|
||||
) -> anki.models.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 addBasicModel(col: anki.collection.Collection) -> anki.models.NoteType:
|
||||
return _add_stock_notetype(col, StockNotetypeKind.BASIC)
|
||||
|
||||
|
||||
def addBasicTypingModel(col: Collection) -> NoteType:
|
||||
return add_stock_notetype(col, StockNoteType.STOCK_NOTE_TYPE_BASIC_TYPING)
|
||||
def addBasicTypingModel(col: anki.collection.Collection) -> anki.models.NoteType:
|
||||
return _add_stock_notetype(col, StockNotetypeKind.BASIC_TYPING)
|
||||
|
||||
|
||||
def addForwardReverse(col: Collection) -> NoteType:
|
||||
return add_stock_notetype(col, StockNoteType.STOCK_NOTE_TYPE_BASIC_AND_REVERSED)
|
||||
def addForwardReverse(col: anki.collection.Collection) -> anki.models.NoteType:
|
||||
return _add_stock_notetype(col, StockNotetypeKind.BASIC_AND_REVERSED)
|
||||
|
||||
|
||||
def addForwardOptionalReverse(col: Collection) -> NoteType:
|
||||
return add_stock_notetype(
|
||||
col, StockNoteType.STOCK_NOTE_TYPE_BASIC_OPTIONAL_REVERSED
|
||||
)
|
||||
def addForwardOptionalReverse(col: anki.collection.Collection) -> anki.models.NoteType:
|
||||
return _add_stock_notetype(col, StockNotetypeKind.BASIC_OPTIONAL_REVERSED)
|
||||
|
||||
|
||||
def addClozeModel(col: Collection) -> NoteType:
|
||||
return add_stock_notetype(col, StockNoteType.STOCK_NOTE_TYPE_CLOZE)
|
||||
def addClozeModel(col: anki.collection.Collection) -> anki.models.NoteType:
|
||||
return _add_stock_notetype(col, StockNotetypeKind.CLOZE)
|
||||
|
||||
|
||||
def get_stock_notetypes(
|
||||
col: Collection,
|
||||
) -> List[Tuple[str, Callable[[Collection], NoteType]]]:
|
||||
out: List[Tuple[str, Callable[[Collection], NoteType]]] = []
|
||||
col: anki.collection.Collection,
|
||||
) -> List[Tuple[str, Callable[[anki.collection.Collection], anki.models.NoteType]]]:
|
||||
out: List[
|
||||
Tuple[str, Callable[[anki.collection.Collection], anki.models.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),
|
||||
(StockNotetypeKind.BASIC, addBasicModel),
|
||||
(StockNotetypeKind.BASIC_TYPING, addBasicTypingModel),
|
||||
(StockNotetypeKind.BASIC_AND_REVERSED, addForwardReverse),
|
||||
(
|
||||
StockNoteType.STOCK_NOTE_TYPE_BASIC_OPTIONAL_REVERSED,
|
||||
StockNotetypeKind.BASIC_OPTIONAL_REVERSED,
|
||||
addForwardOptionalReverse,
|
||||
),
|
||||
(StockNoteType.STOCK_NOTE_TYPE_CLOZE, addClozeModel),
|
||||
(StockNotetypeKind.CLOZE, addClozeModel),
|
||||
]:
|
||||
m = from_json_bytes(col.backend.get_stock_notetype_legacy(kind))
|
||||
out.append((m["name"], func))
|
||||
|
@ -175,7 +175,7 @@ service BackendService {
|
||||
// note types
|
||||
|
||||
rpc AddOrUpdateNotetype(AddOrUpdateNotetypeIn) returns (NoteTypeID);
|
||||
rpc GetStockNotetypeLegacy(GetStockNotetypeIn) returns (Json);
|
||||
rpc GetStockNotetypeLegacy(StockNoteType) returns (Json);
|
||||
rpc GetNotetypeLegacy(NoteTypeID) returns (Json);
|
||||
rpc GetNotetypeNames(Empty) returns (NoteTypeNames);
|
||||
rpc GetNotetypeNamesAndCounts(Empty) returns (NoteTypeUseCounts);
|
||||
@ -874,16 +874,16 @@ message SetConfigJsonIn {
|
||||
bytes value_json = 2;
|
||||
}
|
||||
|
||||
enum StockNoteType {
|
||||
STOCK_NOTE_TYPE_BASIC = 0;
|
||||
STOCK_NOTE_TYPE_BASIC_AND_REVERSED = 1;
|
||||
STOCK_NOTE_TYPE_BASIC_OPTIONAL_REVERSED = 2;
|
||||
STOCK_NOTE_TYPE_BASIC_TYPING = 3;
|
||||
STOCK_NOTE_TYPE_CLOZE = 4;
|
||||
}
|
||||
message StockNoteType {
|
||||
enum Kind {
|
||||
BASIC = 0;
|
||||
BASIC_AND_REVERSED = 1;
|
||||
BASIC_OPTIONAL_REVERSED = 2;
|
||||
BASIC_TYPING = 3;
|
||||
CLOZE = 4;
|
||||
}
|
||||
|
||||
message GetStockNotetypeIn {
|
||||
StockNoteType kind = 1;
|
||||
Kind kind = 1;
|
||||
}
|
||||
|
||||
message NoteTypeNames {
|
||||
|
@ -1084,7 +1084,7 @@ impl BackendService for Backend {
|
||||
// notetypes
|
||||
//-------------------------------------------------------------------
|
||||
|
||||
fn get_stock_notetype_legacy(&self, input: pb::GetStockNotetypeIn) -> BackendResult<pb::Json> {
|
||||
fn get_stock_notetype_legacy(&self, input: pb::StockNoteType) -> BackendResult<pb::Json> {
|
||||
// fixme: use individual functions instead of full vec
|
||||
let mut all = all_stock_notetypes(&self.i18n);
|
||||
let idx = (input.kind as usize).min(all.len() - 1);
|
||||
|
@ -7,13 +7,13 @@ use crate::{
|
||||
storage::SqliteStorage, timestamp::TimestampSecs,
|
||||
};
|
||||
|
||||
pub use crate::backend_proto::StockNoteType;
|
||||
use crate::backend_proto::stock_note_type::Kind;
|
||||
|
||||
impl SqliteStorage {
|
||||
pub(crate) fn add_stock_notetypes(&self, i18n: &I18n) -> Result<()> {
|
||||
for (idx, mut nt) in all_stock_notetypes(i18n).into_iter().enumerate() {
|
||||
self.add_new_notetype(&mut nt)?;
|
||||
if idx == StockNoteType::Basic as usize {
|
||||
if idx == Kind::Basic as usize {
|
||||
self.set_config_value(
|
||||
ConfigKey::CurrentNoteTypeID.into(),
|
||||
&nt.id,
|
||||
|
Loading…
Reference in New Issue
Block a user