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,60 +14,65 @@ service ConfigsService {
rpc SetConfigJsonNoUndo(SetConfigJsonRequest) returns (generic.Empty);
rpc RemoveConfig(generic.String) returns (collection.OpChanges);
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 GetConfigString(Config.String) returns (generic.String);
rpc GetConfigString(GetConfigStringRequest) returns (generic.String);
rpc SetConfigString(SetConfigStringRequest) returns (collection.OpChanges);
rpc GetPreferences(generic.Empty) returns (Preferences);
rpc SetPreferences(Preferences) returns (collection.OpChanges);
}
message Config {
message Bool {
enum Key {
BROWSER_TABLE_SHOW_NOTES_MODE = 0;
PREVIEW_BOTH_SIDES = 3;
COLLAPSE_TAGS = 4;
COLLAPSE_NOTETYPES = 5;
COLLAPSE_DECKS = 6;
COLLAPSE_SAVED_SEARCHES = 7;
COLLAPSE_TODAY = 8;
COLLAPSE_CARD_STATE = 9;
COLLAPSE_FLAGS = 10;
SCHED_2021 = 11;
ADDING_DEFAULTS_TO_CURRENT_DECK = 12;
HIDE_AUDIO_PLAY_BUTTONS = 13;
INTERRUPT_AUDIO_WHEN_ANSWERING = 14;
PASTE_IMAGES_AS_PNG = 15;
PASTE_STRIPS_FORMATTING = 16;
NORMALIZE_NOTE_TEXT = 17;
}
Key key = 1;
message ConfigKey {
enum Bool {
BROWSER_TABLE_SHOW_NOTES_MODE = 0;
PREVIEW_BOTH_SIDES = 3;
COLLAPSE_TAGS = 4;
COLLAPSE_NOTETYPES = 5;
COLLAPSE_DECKS = 6;
COLLAPSE_SAVED_SEARCHES = 7;
COLLAPSE_TODAY = 8;
COLLAPSE_CARD_STATE = 9;
COLLAPSE_FLAGS = 10;
SCHED_2021 = 11;
ADDING_DEFAULTS_TO_CURRENT_DECK = 12;
HIDE_AUDIO_PLAY_BUTTONS = 13;
INTERRUPT_AUDIO_WHEN_ANSWERING = 14;
PASTE_IMAGES_AS_PNG = 15;
PASTE_STRIPS_FORMATTING = 16;
NORMALIZE_NOTE_TEXT = 17;
}
message String {
enum Key {
SET_DUE_BROWSER = 0;
SET_DUE_REVIEWER = 1;
DEFAULT_SEARCH_TEXT = 2;
CARD_STATE_CUSTOMIZER = 3;
}
Key key = 1;
enum String {
SET_DUE_BROWSER = 0;
SET_DUE_REVIEWER = 1;
DEFAULT_SEARCH_TEXT = 2;
CARD_STATE_CUSTOMIZER = 3;
}
}
message GetConfigBoolRequest {
ConfigKey.Bool key = 1;
}
message SetConfigBoolRequest {
Config.Bool.Key key = 1;
ConfigKey.Bool key = 1;
bool value = 2;
bool undoable = 3;
}
message GetConfigStringRequest {
ConfigKey.String key = 1;
}
message SetConfigStringRequest {
Config.String.Key key = 1;
ConfigKey.String key = 1;
string value = 2;
bool undoable = 3;
}
message OptionalStringConfigKey {
ConfigKey.String key = 1;
}
message SetConfigJsonRequest {
string key = 1;
bytes value_json = 2;

View file

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

View file

@ -14,6 +14,7 @@ ignored-classes=
NoteFieldsCheckResponse,
BackendError,
SetDeckCollapsedRequest,
ConfigKey,
[REPORTS]
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."
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)
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:
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)
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:
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.utils import from_json_bytes, to_json_bytes
Config = configs_pb2.Config
Config = configs_pb2.ConfigKey
class ConfigManager:

View file

@ -17,6 +17,7 @@ FilteredDeckForUpdate = decks_pb2.FilteredDeckForUpdate
from typing import List, Optional, Sequence
from anki import configs_pb2
from anki.cards import CardId
from anki.consts import CARD_TYPE_NEW, NEW_CARDS_RANDOM, QUEUE_TYPE_NEW, QUEUE_TYPE_REV
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,
card_ids: Sequence[CardId],
days: str,
config_key: Optional[Config.String.Key.V] = None,
config_key: Optional[Config.String.V] = None,
) -> OpChanges:
"""Set cards to be due in `days`, turning them into review cards if necessary.
`days` can be of the form '5' or '5..7'
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:
key = Config.String(key=config_key)
key = configs_pb2.OptionalStringConfigKey(key=config_key)
else:
key = None
return self.col._backend.set_due_date(

View file

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

View file

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

View file

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

View file

@ -7,7 +7,7 @@ use super::Backend;
pub(super) use crate::backend_proto::configs_service::Service as ConfigsService;
use crate::{
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},
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 {
fn get_config_json(&self, input: pb::String) -> Result<pb::Json> {
self.with_col(|col| {
@ -91,7 +85,7 @@ impl ConfigsService for Backend {
.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| {
Ok(pb::Bool {
val: col.get_config_bool(input.key().into()),
@ -104,7 +98,7 @@ impl ConfigsService for Backend {
.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| {
Ok(pb::String {
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> {
let config = input.config_key.map(Into::into);
let config = input.config_key.map(|v| v.key().into());
let days = input.days;
let cids = input.card_ids.into_newtype(CardId);
self.with_col(|col| col.set_due_date(&cids, &days, config).map(Into::into))