ConfigVar: add "converter" for getter, add use it for "proxy" key
fixes https://github.com/spesmilo/electrum/issues/8837 Alternatively we could do a config upgrade (convert_version_*) to fix that specific issue, but they are not safe against downgrade-upgrade-again, so probably not the best choice for crash-inducing values. And this kind of converter is a generic solution that can be useful for other configvars later (though for most usages I expect we will also need a converter for the setter).
This commit is contained in:
@@ -63,12 +63,14 @@ class ConfigVar(property):
|
||||
*,
|
||||
default: Union[Any, Callable[['SimpleConfig'], Any]], # typically a literal, but can also be a callable
|
||||
type_=None,
|
||||
convert_getter: Callable[[Any], Any] = None,
|
||||
short_desc: Callable[[], str] = None,
|
||||
long_desc: Callable[[], str] = None,
|
||||
):
|
||||
self._key = key
|
||||
self._default = default
|
||||
self._type = type_
|
||||
self._convert_getter = convert_getter
|
||||
# note: the descriptions are callables instead of str literals, to delay evaluating the _() translations
|
||||
# until after the language is set.
|
||||
assert short_desc is None or callable(short_desc)
|
||||
@@ -83,6 +85,10 @@ class ConfigVar(property):
|
||||
with config.lock:
|
||||
if config.is_set(self._key):
|
||||
value = config.get(self._key)
|
||||
# run converter
|
||||
if self._convert_getter is not None:
|
||||
value = self._convert_getter(value)
|
||||
# type-check
|
||||
if self._type is not None:
|
||||
assert value is not None, f"got None for key={self._key!r}"
|
||||
try:
|
||||
@@ -924,7 +930,7 @@ class SimpleConfig(Logger):
|
||||
# config variables ----->
|
||||
NETWORK_AUTO_CONNECT = ConfigVar('auto_connect', default=True, type_=bool)
|
||||
NETWORK_ONESERVER = ConfigVar('oneserver', default=False, type_=bool)
|
||||
NETWORK_PROXY = ConfigVar('proxy', default=None, type_=str)
|
||||
NETWORK_PROXY = ConfigVar('proxy', default=None, type_=str, convert_getter=lambda v: "none" if v is None else v)
|
||||
NETWORK_PROXY_USER = ConfigVar('proxy_user', default=None, type_=str)
|
||||
NETWORK_PROXY_PASSWORD = ConfigVar('proxy_password', default=None, type_=str)
|
||||
NETWORK_SERVER = ConfigVar('server', default=None, type_=str)
|
||||
|
||||
@@ -164,6 +164,14 @@ class Test_SimpleConfig(ElectrumTestCase):
|
||||
finally:
|
||||
constants.set_mainnet()
|
||||
|
||||
def test_configvars_convert_getter(self):
|
||||
config = SimpleConfig(self.options)
|
||||
self.assertEqual(None, config.NETWORK_PROXY)
|
||||
config.user_config[config.cv.NETWORK_PROXY.key()] = None
|
||||
self.assertEqual("none", config.NETWORK_PROXY)
|
||||
config.NETWORK_PROXY = None
|
||||
self.assertEqual(None, config.NETWORK_PROXY)
|
||||
|
||||
def test_configvars_is_set(self):
|
||||
config = SimpleConfig(self.options)
|
||||
self.assertEqual(MAX_MSG_SIZE_DEFAULT, config.NETWORK_MAX_INCOMING_MSG_SIZE)
|
||||
|
||||
Reference in New Issue
Block a user