From 85c3c7709686c701d3e108dd79a6abba96b63c9d Mon Sep 17 00:00:00 2001 From: ThomasV Date: Wed, 4 Jun 2025 12:44:07 +0200 Subject: [PATCH] CLI: make 'wallet_path' relative to wallets dir If the wallet_path passed to the RPC is a simple filename, interpret it as relative to the user wallets directory, rather than to the current working directory. This is a breaking change, it might affect existing scripts --- RELEASE-NOTES | 3 +++ electrum/commands.py | 6 ++---- electrum/simple_config.py | 13 +++++++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index b12882371..ed8f73dec 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -97,6 +97,9 @@ * CLI: - The command line help has been improved; parameters are documented in the same docstring as the command they belong to. + - If the --wallet parameter passed to a command is a simple filename, + it is now interpreted as relative to the users wallets directory, + rather than to the current working directory - Plugins may add extra commands to the CLI. Plugin commands must be prefixed with the plugin's internal name - support for hold invoices diff --git a/electrum/commands.py b/electrum/commands.py index 59a559e26..04e1bfc23 100644 --- a/electrum/commands.py +++ b/electrum/commands.py @@ -163,10 +163,8 @@ def command(s): password = kwargs.get('password') daemon = cmd_runner.daemon if daemon: - if 'wallet_path' in cmd.options and kwargs.get('wallet_path') is None: - kwargs['wallet_path'] = daemon.config.get_wallet_path() - if cmd.requires_wallet and kwargs.get('wallet') is None: - kwargs['wallet_path'] = daemon.config.get_wallet_path() + if 'wallet_path' in cmd.options or cmd.requires_wallet: + kwargs['wallet_path'] = daemon.config.maybe_complete_wallet_path(kwargs.get('wallet_path')) if 'wallet' in cmd.options: wallet_path = kwargs.pop('wallet_path', None) # unit tests may set wallet and not wallet_path wallet = kwargs.get('wallet', None) # run_offline_command sets both diff --git a/electrum/simple_config.py b/electrum/simple_config.py index a48e29521..5daf25e87 100644 --- a/electrum/simple_config.py +++ b/electrum/simple_config.py @@ -458,11 +458,20 @@ class SimpleConfig(Logger): else: return self.WALLET_BACKUP_DIRECTORY + def maybe_complete_wallet_path(self, path: Optional[str]) -> str: + return self._complete_wallet_path(path) if path is not None else self.get_wallet_path() + + def _complete_wallet_path(self, path: str) -> str: + """ add user wallets directory if needed """ + if os.path.split(path)[0] == '': + path = os.path.join(self.get_datadir_wallet_path(), path) + return path + def get_wallet_path(self) -> str: """Returns the wallet path.""" # command line -w option - if self.get('wallet_path'): - return os.path.join(self.get('cwd', ''), self.get('wallet_path')) + if path:= self.get('wallet_path'): + return self._complete_wallet_path(path) # current wallet path = self.CURRENT_WALLET if path and os.path.exists(path):