From e61a611af7f6e81bd7200d3d33e4ef77a2f783f5 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 11 Jul 2021 19:27:08 +1000 Subject: [PATCH] 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. --- proto/anki/configs.proto | 73 ++++++++++++++++-------------- proto/anki/scheduler.proto | 2 +- pylib/.pylintrc | 1 + pylib/anki/collection.py | 8 ++-- pylib/anki/config.py | 2 +- pylib/anki/scheduler/base.py | 7 +-- qt/.pylintrc | 2 +- qt/aqt/browser/sidebar/tree.py | 2 +- qt/aqt/operations/scheduling.py | 2 +- rslib/src/backend/config.rs | 12 ++--- rslib/src/backend/scheduler/mod.rs | 2 +- 11 files changed, 57 insertions(+), 56 deletions(-) diff --git a/proto/anki/configs.proto b/proto/anki/configs.proto index 3a7ed87bd..bd8078545 100644 --- a/proto/anki/configs.proto +++ b/proto/anki/configs.proto @@ -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; diff --git a/proto/anki/scheduler.proto b/proto/anki/scheduler.proto index d7d27b444..c058df9a2 100644 --- a/proto/anki/scheduler.proto +++ b/proto/anki/scheduler.proto @@ -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 { diff --git a/pylib/.pylintrc b/pylib/.pylintrc index 0ab9d4fc0..1fc1205de 100644 --- a/pylib/.pylintrc +++ b/pylib/.pylintrc @@ -14,6 +14,7 @@ ignored-classes= NoteFieldsCheckResponse, BackendError, SetDeckCollapsedRequest, + ConfigKey, [REPORTS] output-format=colorized diff --git a/pylib/anki/collection.py b/pylib/anki/collection.py index bdcddf30f..c5ec9a924 100644 --- a/pylib/anki/collection.py +++ b/pylib/anki/collection.py @@ -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) diff --git a/pylib/anki/config.py b/pylib/anki/config.py index bd3de9219..770375d8d 100644 --- a/pylib/anki/config.py +++ b/pylib/anki/config.py @@ -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: diff --git a/pylib/anki/scheduler/base.py b/pylib/anki/scheduler/base.py index 9ce53f157..c8214b36a 100644 --- a/pylib/anki/scheduler/base.py +++ b/pylib/anki/scheduler/base.py @@ -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( diff --git a/qt/.pylintrc b/qt/.pylintrc index 545e7e07f..ea7a8d48d 100644 --- a/qt/.pylintrc +++ b/qt/.pylintrc @@ -9,7 +9,7 @@ ignored-classes= BrowserColumns, BrowserRow, SearchNode, - Config, + ConfigKey, OpChanges, UnburyDeckRequest, CardAnswer, diff --git a/qt/aqt/browser/sidebar/tree.py b/qt/aqt/browser/sidebar/tree.py index 157538633..be0544a0a 100644 --- a/qt/aqt/browser/sidebar/tree.py +++ b/qt/aqt/browser/sidebar/tree.py @@ -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: diff --git a/qt/aqt/operations/scheduling.py b/qt/aqt/operations/scheduling.py index 087b1475d..3e8eb6c89 100644 --- a/qt/aqt/operations/scheduling.py +++ b/qt/aqt/operations/scheduling.py @@ -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: diff --git a/rslib/src/backend/config.rs b/rslib/src/backend/config.rs index 72d0dea63..b9772c66f 100644 --- a/rslib/src/backend/config.rs +++ b/rslib/src/backend/config.rs @@ -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 for StringKey { } } -impl From 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 { 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 { + fn get_config_bool(&self, input: pb::GetConfigBoolRequest) -> Result { 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 { + fn get_config_string(&self, input: pb::GetConfigStringRequest) -> Result { self.with_col(|col| { Ok(pb::String { val: col.get_config_string(input.key().into()), diff --git a/rslib/src/backend/scheduler/mod.rs b/rslib/src/backend/scheduler/mod.rs index 11d6d468d..05d4dbdda 100644 --- a/rslib/src/backend/scheduler/mod.rs +++ b/rslib/src/backend/scheduler/mod.rs @@ -117,7 +117,7 @@ impl SchedulerService for Backend { } fn set_due_date(&self, input: pb::SetDueDateRequest) -> Result { - 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))