From b132e357a3964a14a44e38e828e4cd9c408b10af Mon Sep 17 00:00:00 2001 From: SomberNight Date: Wed, 19 Mar 2025 14:52:13 +0000 Subject: [PATCH 1/2] plugin ConfigVars: define vars less dynamically and restore ability to have different internal ConfigVar name and user-visible "key" (Keys are hard to change as that breaks compat, but it is nice to be able to change the internal var name, to reorganise stuff sometimes. After new ConfigVars are added, sometimes we get better insight into how the older ones should have been named.) follow-up https://github.com/spesmilo/electrum/pull/9648 --- electrum/plugin.py | 6 +----- electrum/plugins/payserver/__init__.py | 11 +++++------ electrum/plugins/swapserver/__init__.py | 11 +++++------ electrum/plugins/watchtower/__init__.py | 11 +++++------ 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/electrum/plugin.py b/electrum/plugin.py index 3ef989614..7350e0241 100644 --- a/electrum/plugin.py +++ b/electrum/plugin.py @@ -290,11 +290,7 @@ class Plugins(DaemonThread): else: zipfile = zipimport.zipimporter(metadata['path']) init_spec = zipfile.find_spec(name) - module = self.exec_module_from_spec(init_spec, base_name) - # import config vars - if hasattr(module, 'config_vars'): - for cv in module.config_vars: - setattr(SimpleConfig, cv.key().upper(), cv) + self.exec_module_from_spec(init_spec, base_name) if name == "trustedcoin": # removes trustedcoin after loading to not show it in the list of plugins del self.internal_plugin_metadata[name] diff --git a/electrum/plugins/payserver/__init__.py b/electrum/plugins/payserver/__init__.py index d8b55e48c..9e10d3f76 100644 --- a/electrum/plugins/payserver/__init__.py +++ b/electrum/plugins/payserver/__init__.py @@ -1,6 +1,5 @@ -from electrum.simple_config import ConfigVar -config_vars = [ - ConfigVar('payserver_port', default=8080, type_=int), - ConfigVar('payserver_root', default='/r', type_=str), - ConfigVar('payserver_allow_create_invoice', default=False, type_=bool), -] +from electrum.simple_config import ConfigVar, SimpleConfig + +SimpleConfig.PAYSERVER_PORT = ConfigVar('payserver_port', default=8080, type_=int) +SimpleConfig.PAYSERVER_ROOT = ConfigVar('payserver_root', default='/r', type_=str) +SimpleConfig.PAYSERVER_ALLOW_CREATE_INVOICE = ConfigVar('payserver_allow_create_invoice', default=False, type_=bool) diff --git a/electrum/plugins/swapserver/__init__.py b/electrum/plugins/swapserver/__init__.py index 6868d31b3..0aeeb4629 100644 --- a/electrum/plugins/swapserver/__init__.py +++ b/electrum/plugins/swapserver/__init__.py @@ -1,6 +1,5 @@ -from electrum.simple_config import ConfigVar -config_vars = [ - ConfigVar('swapserver_port', default=None, type_=int), - ConfigVar('swapserver_fee_millionths', default=5000, type_=int), - ConfigVar('swapserver_ann_pow_nonce', default=0, type_=int), -] +from electrum.simple_config import ConfigVar, SimpleConfig + +SimpleConfig.SWAPSERVER_PORT = ConfigVar('swapserver_port', default=None, type_=int) +SimpleConfig.SWAPSERVER_FEE_MILLIONTHS = ConfigVar('swapserver_fee_millionths', default=5000, type_=int) +SimpleConfig.SWAPSERVER_ANN_POW_NONCE = ConfigVar('swapserver_ann_pow_nonce', default=0, type_=int) diff --git a/electrum/plugins/watchtower/__init__.py b/electrum/plugins/watchtower/__init__.py index 1a35470fc..35384fbdc 100644 --- a/electrum/plugins/watchtower/__init__.py +++ b/electrum/plugins/watchtower/__init__.py @@ -1,6 +1,5 @@ -from electrum.simple_config import ConfigVar -config_vars = [ - ConfigVar('watchtower_server_port', default=None, type_=int), - ConfigVar('watchtower_server_user', default=None, type_=str), - ConfigVar('watchtower_server_password', default=None, type_=str), -] +from electrum.simple_config import ConfigVar, SimpleConfig + +SimpleConfig.WATCHTOWER_SERVER_PORT = ConfigVar('watchtower_server_port', default=None, type_=int) +SimpleConfig.WATCHTOWER_SERVER_USER = ConfigVar('watchtower_server_user', default=None, type_=str) +SimpleConfig.WATCHTOWER_SERVER_PASSWORD = ConfigVar('watchtower_server_password', default=None, type_=str) From a99c454c00bb9256ffa4eeccc7c81a579aa66951 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Wed, 19 Mar 2025 16:37:30 +0000 Subject: [PATCH 2/2] plugin ConfigVars: enforce "key" starts with name of plugin --- electrum/plugins/payserver/__init__.py | 6 +++--- electrum/plugins/swapserver/__init__.py | 6 +++--- electrum/plugins/watchtower/__init__.py | 6 +++--- electrum/simple_config.py | 8 ++++++++ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/electrum/plugins/payserver/__init__.py b/electrum/plugins/payserver/__init__.py index 9e10d3f76..28f930788 100644 --- a/electrum/plugins/payserver/__init__.py +++ b/electrum/plugins/payserver/__init__.py @@ -1,5 +1,5 @@ from electrum.simple_config import ConfigVar, SimpleConfig -SimpleConfig.PAYSERVER_PORT = ConfigVar('payserver_port', default=8080, type_=int) -SimpleConfig.PAYSERVER_ROOT = ConfigVar('payserver_root', default='/r', type_=str) -SimpleConfig.PAYSERVER_ALLOW_CREATE_INVOICE = ConfigVar('payserver_allow_create_invoice', default=False, type_=bool) +SimpleConfig.PAYSERVER_PORT = ConfigVar('payserver_port', default=8080, type_=int, plugin=__name__) +SimpleConfig.PAYSERVER_ROOT = ConfigVar('payserver_root', default='/r', type_=str, plugin=__name__) +SimpleConfig.PAYSERVER_ALLOW_CREATE_INVOICE = ConfigVar('payserver_allow_create_invoice', default=False, type_=bool, plugin=__name__) diff --git a/electrum/plugins/swapserver/__init__.py b/electrum/plugins/swapserver/__init__.py index 0aeeb4629..f5a98487f 100644 --- a/electrum/plugins/swapserver/__init__.py +++ b/electrum/plugins/swapserver/__init__.py @@ -1,5 +1,5 @@ from electrum.simple_config import ConfigVar, SimpleConfig -SimpleConfig.SWAPSERVER_PORT = ConfigVar('swapserver_port', default=None, type_=int) -SimpleConfig.SWAPSERVER_FEE_MILLIONTHS = ConfigVar('swapserver_fee_millionths', default=5000, type_=int) -SimpleConfig.SWAPSERVER_ANN_POW_NONCE = ConfigVar('swapserver_ann_pow_nonce', default=0, type_=int) +SimpleConfig.SWAPSERVER_PORT = ConfigVar('swapserver_port', default=None, type_=int, plugin=__name__) +SimpleConfig.SWAPSERVER_FEE_MILLIONTHS = ConfigVar('swapserver_fee_millionths', default=5000, type_=int, plugin=__name__) +SimpleConfig.SWAPSERVER_ANN_POW_NONCE = ConfigVar('swapserver_ann_pow_nonce', default=0, type_=int, plugin=__name__) diff --git a/electrum/plugins/watchtower/__init__.py b/electrum/plugins/watchtower/__init__.py index 35384fbdc..b4d66af1e 100644 --- a/electrum/plugins/watchtower/__init__.py +++ b/electrum/plugins/watchtower/__init__.py @@ -1,5 +1,5 @@ from electrum.simple_config import ConfigVar, SimpleConfig -SimpleConfig.WATCHTOWER_SERVER_PORT = ConfigVar('watchtower_server_port', default=None, type_=int) -SimpleConfig.WATCHTOWER_SERVER_USER = ConfigVar('watchtower_server_user', default=None, type_=str) -SimpleConfig.WATCHTOWER_SERVER_PASSWORD = ConfigVar('watchtower_server_password', default=None, type_=str) +SimpleConfig.WATCHTOWER_SERVER_PORT = ConfigVar('watchtower_server_port', default=None, type_=int, plugin=__name__) +SimpleConfig.WATCHTOWER_SERVER_USER = ConfigVar('watchtower_server_user', default=None, type_=str, plugin=__name__) +SimpleConfig.WATCHTOWER_SERVER_PASSWORD = ConfigVar('watchtower_server_password', default=None, type_=str, plugin=__name__) diff --git a/electrum/simple_config.py b/electrum/simple_config.py index dfbc5ff82..ed3b2eff3 100644 --- a/electrum/simple_config.py +++ b/electrum/simple_config.py @@ -45,6 +45,7 @@ class ConfigVar(property): convert_getter: Callable[[Any], Any] = None, short_desc: Callable[[], str] = None, long_desc: Callable[[], str] = None, + plugin: Optional[str] = None, ): self._key = key self._default = default @@ -56,6 +57,13 @@ class ConfigVar(property): assert long_desc is None or callable(long_desc) self._short_desc = short_desc self._long_desc = long_desc + if plugin: # enforce "key" starts with name of plugin + pkg_prefix = "electrum.plugins." # for internal plugins + if plugin.startswith(pkg_prefix): + plugin = plugin[len(pkg_prefix):] + assert "." not in plugin, plugin + key_prefix = plugin + "_" + assert key.startswith(key_prefix), f"ConfigVar {key=} must be prefixed with the plugin name ({key_prefix})" property.__init__(self, self._get_config_value, self._set_config_value) assert key not in _config_var_from_key, f"duplicate config key str: {key!r}" _config_var_from_key[key] = self