Skip to content

Commit

Permalink
store color keys as lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsumoto-ren committed Dec 4, 2022
1 parent 5cce369 commit 045191e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
8 changes: 4 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"colors": {
"Again": "FireBrick",
"Hard": "DarkGoldenRod",
"Good": "ForestGreen",
"Easy": "DodgerBlue"
"again": "FireBrick",
"hard": "DarkGoldenRod",
"good": "ForestGreen",
"easy": "DodgerBlue"
},
"remove_buttons": true,
"prevent_clicks": true,
Expand Down
49 changes: 37 additions & 12 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

from typing import Dict, Iterable
from typing import Dict, Iterable, Union, overload

from aqt import mw

Expand All @@ -18,17 +18,24 @@ def _get_config():
return mw.addonManager.getConfig(__name__)

def __init__(self, default: bool = False):
self._config = self._get_config() if not default else self._get_default_config()
self._default = default
self._default_config = self._get_default_config()
self._config = self._default_config if default else self._get_config()

def _get(self, key: str):
return self._config.get(key, self._default_config[key])

def __getitem__(self, key: str) -> bool:
assert type(self._config[key]) == bool
return self._config[key]
assert type(val := self._get(key)) == bool
return val

def __setitem__(self, key, value):
assert type(self._config[key]) == bool
assert type(self._get(key)) == bool
self._config[key] = value

@property
def default(self) -> bool:
return self._default_config is self._config

@staticmethod
def get_label(ease: int, default_ease: int = 3) -> str:
if ease == 1:
Expand All @@ -41,19 +48,37 @@ def get_label(ease: int, default_ease: int = 3) -> str:
return "Easy"
return "Unknown"

def get_color(self, ease: int, default_ease: int = 3) -> str:
return self._config['colors'][self.get_label(ease, default_ease)]
@overload
def get_color(self, ease: int, default_ease: int) -> str:
...

@overload
def get_color(self, label: str) -> str:
...

def get_color(self, ease_or_label: Union[int, str], default_ease: int = 3) -> str:
label = (
ease_or_label
if type(ease_or_label) == str
else self.get_label(ease_or_label, default_ease)
).lower()
return self._config['colors'].get(label, self._default_config['colors'].get(label, "Pink"))

@property
def colors(self) -> Dict[str, str]:
"""Returns a dict mapping buttons' labels to their colors."""
return dict(self._config['colors'])
return {
label.lower(): self._config['colors'].get(label.lower(), color_text)
for label, color_text in
self._default_config['colors'].items()
}

def get_toggleables(self) -> Iterable[str]:
return (key for key, value in self._config.items() if isinstance(value, bool))
"""Returns an iterable of boolean keys in the config."""
return (key for key, value in self._default_config.items() if isinstance(value, bool))

def set_color(self, btn_label: str, color: str):
self._config['colors'][btn_label] = color
self._config['colors'][btn_label.lower()] = color

def get_zoom_state(self, state: str) -> float:
return self._config.setdefault('zoom_states', {}).get(state, 1)
Expand All @@ -62,7 +87,7 @@ def set_zoom_state(self, state: str, value: float) -> None:
self._config.setdefault('zoom_states', {})[state] = value

def write_config(self):
if self._default:
if self.default:
raise RuntimeError("Can't write default config.")
mw.addonManager.writeConfig(__name__, self._config)

Expand Down

0 comments on commit 045191e

Please sign in to comment.