rename Config in protobuf to avoid conflict with module name

+ use the enum directly, instead of wrapping it in an object

Python code retains the old "Config" name.
This commit is contained in:
Damien Elmes 2021-07-11 19:27:08 +10:00
parent 7ac1fa17e6
commit e61a611af7
11 changed files with 57 additions and 56 deletions

View file

@ -14,17 +14,16 @@ service ConfigsService {
rpc SetConfigJsonNoUndo(SetConfigJsonRequest) returns (generic.Empty); rpc SetConfigJsonNoUndo(SetConfigJsonRequest) returns (generic.Empty);
rpc RemoveConfig(generic.String) returns (collection.OpChanges); rpc RemoveConfig(generic.String) returns (collection.OpChanges);
rpc GetAllConfig(generic.Empty) returns (generic.Json); rpc GetAllConfig(generic.Empty) returns (generic.Json);
rpc GetConfigBool(Config.Bool) returns (generic.Bool); rpc GetConfigBool(GetConfigBoolRequest) returns (generic.Bool);
rpc SetConfigBool(SetConfigBoolRequest) returns (collection.OpChanges); rpc SetConfigBool(SetConfigBoolRequest) returns (collection.OpChanges);
rpc GetConfigString(Config.String) returns (generic.String); rpc GetConfigString(GetConfigStringRequest) returns (generic.String);
rpc SetConfigString(SetConfigStringRequest) returns (collection.OpChanges); rpc SetConfigString(SetConfigStringRequest) returns (collection.OpChanges);
rpc GetPreferences(generic.Empty) returns (Preferences); rpc GetPreferences(generic.Empty) returns (Preferences);
rpc SetPreferences(Preferences) returns (collection.OpChanges); rpc SetPreferences(Preferences) returns (collection.OpChanges);
} }
message Config { message ConfigKey {
message Bool { enum Bool {
enum Key {
BROWSER_TABLE_SHOW_NOTES_MODE = 0; BROWSER_TABLE_SHOW_NOTES_MODE = 0;
PREVIEW_BOTH_SIDES = 3; PREVIEW_BOTH_SIDES = 3;
COLLAPSE_TAGS = 4; COLLAPSE_TAGS = 4;
@ -42,32 +41,38 @@ message Config {
PASTE_STRIPS_FORMATTING = 16; PASTE_STRIPS_FORMATTING = 16;
NORMALIZE_NOTE_TEXT = 17; NORMALIZE_NOTE_TEXT = 17;
} }
Key key = 1; enum String {
}
message String {
enum Key {
SET_DUE_BROWSER = 0; SET_DUE_BROWSER = 0;
SET_DUE_REVIEWER = 1; SET_DUE_REVIEWER = 1;
DEFAULT_SEARCH_TEXT = 2; DEFAULT_SEARCH_TEXT = 2;
CARD_STATE_CUSTOMIZER = 3; CARD_STATE_CUSTOMIZER = 3;
} }
Key key = 1; }
}
message GetConfigBoolRequest {
ConfigKey.Bool key = 1;
} }
message SetConfigBoolRequest { message SetConfigBoolRequest {
Config.Bool.Key key = 1; ConfigKey.Bool key = 1;
bool value = 2; bool value = 2;
bool undoable = 3; bool undoable = 3;
} }
message GetConfigStringRequest {
ConfigKey.String key = 1;
}
message SetConfigStringRequest { message SetConfigStringRequest {
Config.String.Key key = 1; ConfigKey.String key = 1;
string value = 2; string value = 2;
bool undoable = 3; bool undoable = 3;
} }
message OptionalStringConfigKey {
ConfigKey.String key = 1;
}
message SetConfigJsonRequest { message SetConfigJsonRequest {
string key = 1; string key = 1;
bytes value_json = 2; bytes value_json = 2;

View file

@ -178,7 +178,7 @@ message ScheduleCardsAsNewRequest {
message SetDueDateRequest { message SetDueDateRequest {
repeated int64 card_ids = 1; repeated int64 card_ids = 1;
string days = 2; string days = 2;
configs.Config.String config_key = 3; configs.OptionalStringConfigKey config_key = 3;
} }
message SortCardsRequest { message SortCardsRequest {

View file

@ -14,6 +14,7 @@ ignored-classes=
NoteFieldsCheckResponse, NoteFieldsCheckResponse,
BackendError, BackendError,
SetDeckCollapsedRequest, SetDeckCollapsedRequest,
ConfigKey,
[REPORTS] [REPORTS]
output-format=colorized output-format=colorized

View file

@ -732,19 +732,19 @@ class Collection(DeprecatedNamesMixin):
"This is a debugging aid. Prefer .get_config() when you know the key you need." "This is a debugging aid. Prefer .get_config() when you know the key you need."
return from_json_bytes(self._backend.get_all_config()) return from_json_bytes(self._backend.get_all_config())
def get_config_bool(self, key: Config.Bool.Key.V) -> bool: def get_config_bool(self, key: Config.Bool.V) -> bool:
return self._backend.get_config_bool(key) return self._backend.get_config_bool(key)
def set_config_bool( def set_config_bool(
self, key: Config.Bool.Key.V, value: bool, *, undoable: bool = False self, key: Config.Bool.V, value: bool, *, undoable: bool = False
) -> OpChanges: ) -> OpChanges:
return self._backend.set_config_bool(key=key, value=value, undoable=undoable) return self._backend.set_config_bool(key=key, value=value, undoable=undoable)
def get_config_string(self, key: Config.String.Key.V) -> str: def get_config_string(self, key: Config.String.V) -> str:
return self._backend.get_config_string(key) return self._backend.get_config_string(key)
def set_config_string( def set_config_string(
self, key: Config.String.Key.V, value: str, undoable: bool = False self, key: Config.String.V, value: str, undoable: bool = False
) -> OpChanges: ) -> OpChanges:
return self._backend.set_config_string(key=key, value=value, undoable=undoable) return self._backend.set_config_string(key=key, value=value, undoable=undoable)

View file

@ -30,7 +30,7 @@ from anki.collection import OpChanges
from anki.errors import NotFoundError from anki.errors import NotFoundError
from anki.utils import from_json_bytes, to_json_bytes from anki.utils import from_json_bytes, to_json_bytes
Config = configs_pb2.Config Config = configs_pb2.ConfigKey
class ConfigManager: class ConfigManager:

View file

@ -17,6 +17,7 @@ FilteredDeckForUpdate = decks_pb2.FilteredDeckForUpdate
from typing import List, Optional, Sequence from typing import List, Optional, Sequence
from anki import configs_pb2
from anki.cards import CardId from anki.cards import CardId
from anki.consts import CARD_TYPE_NEW, NEW_CARDS_RANDOM, QUEUE_TYPE_NEW, QUEUE_TYPE_REV from anki.consts import CARD_TYPE_NEW, NEW_CARDS_RANDOM, QUEUE_TYPE_NEW, QUEUE_TYPE_REV
from anki.decks import DeckConfigDict, DeckId, DeckTreeNode from anki.decks import DeckConfigDict, DeckId, DeckTreeNode
@ -161,14 +162,14 @@ select id from cards where did in %s and queue = {QUEUE_TYPE_REV} and due <= ? l
self, self,
card_ids: Sequence[CardId], card_ids: Sequence[CardId],
days: str, days: str,
config_key: Optional[Config.String.Key.V] = None, config_key: Optional[Config.String.V] = None,
) -> OpChanges: ) -> OpChanges:
"""Set cards to be due in `days`, turning them into review cards if necessary. """Set cards to be due in `days`, turning them into review cards if necessary.
`days` can be of the form '5' or '5..7' `days` can be of the form '5' or '5..7'
If `config_key` is provided, provided days will be remembered in config.""" If `config_key` is provided, provided days will be remembered in config."""
key: Optional[Config.String] key: Optional[configs_pb2.OptionalStringConfigKey]
if config_key is not None: if config_key is not None:
key = Config.String(key=config_key) key = configs_pb2.OptionalStringConfigKey(key=config_key)
else: else:
key = None key = None
return self.col._backend.set_due_date( return self.col._backend.set_due_date(

View file

@ -9,7 +9,7 @@ ignored-classes=
BrowserColumns, BrowserColumns,
BrowserRow, BrowserRow,
SearchNode, SearchNode,
Config, ConfigKey,
OpChanges, OpChanges,
UnburyDeckRequest, UnburyDeckRequest,
CardAnswer, CardAnswer,

View file

@ -502,7 +502,7 @@ class SidebarTreeView(QTreeView):
root: SidebarItem, root: SidebarItem,
name: str, name: str,
icon: Union[str, ColoredIcon], icon: Union[str, ColoredIcon],
collapse_key: Config.Bool.Key.V, collapse_key: Config.Bool.V,
type: Optional[SidebarItemType] = None, type: Optional[SidebarItemType] = None,
) -> SidebarItem: ) -> SidebarItem:
def update(expanded: bool) -> None: def update(expanded: bool) -> None:

View file

@ -29,7 +29,7 @@ def set_due_date_dialog(
*, *,
parent: QWidget, parent: QWidget,
card_ids: Sequence[CardId], card_ids: Sequence[CardId],
config_key: Optional[Config.String.Key.V], config_key: Optional[Config.String.V],
) -> Optional[CollectionOp[OpChanges]]: ) -> Optional[CollectionOp[OpChanges]]:
assert aqt.mw assert aqt.mw
if not card_ids: if not card_ids:

View file

@ -7,7 +7,7 @@ use super::Backend;
pub(super) use crate::backend_proto::configs_service::Service as ConfigsService; pub(super) use crate::backend_proto::configs_service::Service as ConfigsService;
use crate::{ use crate::{
backend_proto as pb, backend_proto as pb,
backend_proto::config::{bool::Key as BoolKeyProto, string::Key as StringKeyProto}, backend_proto::config_key::{Bool as BoolKeyProto, String as StringKeyProto},
config::{BoolKey, StringKey}, config::{BoolKey, StringKey},
prelude::*, prelude::*,
}; };
@ -46,12 +46,6 @@ impl From<StringKeyProto> for StringKey {
} }
} }
impl From<pb::config::String> for StringKey {
fn from(key: pb::config::String) -> Self {
key.key().into()
}
}
impl ConfigsService for Backend { impl ConfigsService for Backend {
fn get_config_json(&self, input: pb::String) -> Result<pb::Json> { fn get_config_json(&self, input: pb::String) -> Result<pb::Json> {
self.with_col(|col| { self.with_col(|col| {
@ -91,7 +85,7 @@ impl ConfigsService for Backend {
.map(Into::into) .map(Into::into)
} }
fn get_config_bool(&self, input: pb::config::Bool) -> Result<pb::Bool> { fn get_config_bool(&self, input: pb::GetConfigBoolRequest) -> Result<pb::Bool> {
self.with_col(|col| { self.with_col(|col| {
Ok(pb::Bool { Ok(pb::Bool {
val: col.get_config_bool(input.key().into()), val: col.get_config_bool(input.key().into()),
@ -104,7 +98,7 @@ impl ConfigsService for Backend {
.map(Into::into) .map(Into::into)
} }
fn get_config_string(&self, input: pb::config::String) -> Result<pb::String> { fn get_config_string(&self, input: pb::GetConfigStringRequest) -> Result<pb::String> {
self.with_col(|col| { self.with_col(|col| {
Ok(pb::String { Ok(pb::String {
val: col.get_config_string(input.key().into()), val: col.get_config_string(input.key().into()),

View file

@ -117,7 +117,7 @@ impl SchedulerService for Backend {
} }
fn set_due_date(&self, input: pb::SetDueDateRequest) -> Result<pb::OpChanges> { fn set_due_date(&self, input: pb::SetDueDateRequest) -> Result<pb::OpChanges> {
let config = input.config_key.map(Into::into); let config = input.config_key.map(|v| v.key().into());
let days = input.days; let days = input.days;
let cids = input.card_ids.into_newtype(CardId); let cids = input.card_ids.into_newtype(CardId);
self.with_col(|col| col.set_due_date(&cids, &days, config).map(Into::into)) self.with_col(|col| col.set_due_date(&cids, &days, config).map(Into::into))