1
0

config: introduce ConfigVars

A new config API is introduced, and ~all of the codebase is adapted to it.
The old API is kept but mainly only for dynamic usage where its extra flexibility is needed.

Using examples, the old config API looked this:
```
>>> config.get("request_expiry", 86400)
604800
>>> config.set_key("request_expiry", 86400)
>>>
```

The new config API instead:
```
>>> config.WALLET_PAYREQ_EXPIRY_SECONDS
604800
>>> config.WALLET_PAYREQ_EXPIRY_SECONDS = 86400
>>>
```

The old API operated on arbitrary string keys, the new one uses
a static ~enum-like list of variables.

With the new API:
- there is a single centralised list of config variables, as opposed to
  these being scattered all over
- no more duplication of default values (in the getters)
- there is now some (minimal for now) type-validation/conversion for
  the config values

closes https://github.com/spesmilo/electrum/pull/5640
closes https://github.com/spesmilo/electrum/pull/5649

Note: there is yet a third API added here, for certain niche/abstract use-cases,
where we need a reference to the config variable itself.
It should only be used when needed:
```
>>> var = config.cv.WALLET_PAYREQ_EXPIRY_SECONDS
>>> var
<ConfigVarWithConfig key='request_expiry'>
>>> var.get()
604800
>>> var.set(3600)
>>> var.get_default_value()
86400
>>> var.is_set()
True
>>> var.is_modifiable()
True
```
This commit is contained in:
SomberNight
2023-05-24 17:41:44 +00:00
parent 03ab33f4b2
commit 24980feab7
60 changed files with 781 additions and 471 deletions

View File

@@ -341,8 +341,8 @@ def main():
config_options = {
'verbosity': '*' if util.is_android_debug_apk() else '',
'cmd': 'gui',
'gui': android_gui,
'single_password': True,
SimpleConfig.GUI_NAME.key(): android_gui,
SimpleConfig.WALLET_USE_SINGLE_PASSWORD.key(): True,
}
if util.get_android_package_name() == "org.electrum.testnet.electrum":
# ~hack for easier testnet builds. pkgname subject to change.
@@ -394,8 +394,8 @@ def main():
# to not-yet-evaluated strings.
if cmdname == 'gui':
from electrum.gui.default_lang import get_default_language
gui_name = config.get('gui', 'qt')
lang = config.get('language')
gui_name = config.GUI_NAME
lang = config.LOCALIZATION_LANGUAGE
if not lang:
lang = get_default_language(gui_name=gui_name)
_logger.info(f"get_default_language: detected default as {lang=!r}")
@@ -459,7 +459,7 @@ def handle_cmd(*, cmdname: str, config: 'SimpleConfig', config_options: dict):
configure_logging(config)
fd = daemon.get_file_descriptor(config)
if fd is not None:
plugins = init_plugins(config, config.get('gui', 'qt'))
plugins = init_plugins(config, config.GUI_NAME)
d = daemon.Daemon(config, fd, start_network=False)
try:
d.run_gui(config, plugins)
@@ -490,10 +490,9 @@ def handle_cmd(*, cmdname: str, config: 'SimpleConfig', config_options: dict):
configure_logging(config, log_to_file=False) # don't spam logfiles for each client-side RPC, but support "-v"
cmd = known_commands[cmdname]
wallet_path = config.get_wallet_path()
if not config.get('offline'):
if not config.NETWORK_OFFLINE:
init_cmdline(config_options, wallet_path, True, config=config)
timeout = config.get('timeout', 60)
if timeout: timeout = int(timeout)
timeout = config.CLI_TIMEOUT
try:
result = daemon.request(config, 'run_cmdline', (config_options,), timeout)
except daemon.DaemonNotRunning: