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