commands: getconfig to use default values, add existence checks
- getconfig and setconfig now both check configvars for existence - getconfig returns default values when applicable - setconfig does not side-step type-checks for values fixes https://github.com/spesmilo/electrum/issues/8607 closes https://github.com/spesmilo/electrum/pull/8608
This commit is contained in:
@@ -52,6 +52,9 @@ _logger = get_logger(__name__)
|
||||
FINAL_CONFIG_VERSION = 3
|
||||
|
||||
|
||||
_config_var_from_key = {} # type: Dict[str, 'ConfigVar']
|
||||
|
||||
|
||||
class ConfigVar(property):
|
||||
|
||||
def __init__(
|
||||
@@ -65,6 +68,7 @@ class ConfigVar(property):
|
||||
self._default = default
|
||||
self._type = type_
|
||||
property.__init__(self, self._get_config_value, self._set_config_value)
|
||||
_config_var_from_key[key] = self
|
||||
|
||||
def _get_config_value(self, config: 'SimpleConfig'):
|
||||
with config.lock:
|
||||
@@ -132,6 +136,11 @@ class ConfigVarWithConfig:
|
||||
def __repr__(self):
|
||||
return f"<ConfigVarWithConfig key={self.key()!r}>"
|
||||
|
||||
def __eq__(self, other) -> bool:
|
||||
if not isinstance(other, ConfigVarWithConfig):
|
||||
return False
|
||||
return self._config is other._config and self._config_var is other._config_var
|
||||
|
||||
|
||||
class SimpleConfig(Logger):
|
||||
"""
|
||||
@@ -818,10 +827,18 @@ class SimpleConfig(Logger):
|
||||
"""
|
||||
class CVLookupHelper:
|
||||
def __getattribute__(self, name: str) -> ConfigVarWithConfig:
|
||||
if name in ("from_key", ): # don't apply magic, just use standard lookup
|
||||
return super().__getattribute__(name)
|
||||
config_var = config.__class__.__getattribute__(type(config), name)
|
||||
if not isinstance(config_var, ConfigVar):
|
||||
raise AttributeError()
|
||||
return ConfigVarWithConfig(config=config, config_var=config_var)
|
||||
def from_key(self, key: str) -> ConfigVarWithConfig:
|
||||
try:
|
||||
config_var = _config_var_from_key[key]
|
||||
except KeyError:
|
||||
raise KeyError(f"No ConfigVar with key={key!r}") from None
|
||||
return ConfigVarWithConfig(config=config, config_var=config_var)
|
||||
def __setattr__(self, name, value):
|
||||
raise Exception(
|
||||
f"Cannot assign value to config.cv.{name} directly. "
|
||||
|
||||
Reference in New Issue
Block a user