1
0

add handling of plugin commands

This commit is contained in:
f321x
2025-03-06 11:43:50 +01:00
committed by ThomasV
parent 2763e14bb3
commit ae64583ebc
4 changed files with 112 additions and 27 deletions

View File

@@ -102,7 +102,8 @@ from electrum.wallet import Wallet
from electrum.storage import WalletStorage
from electrum.util import print_msg, print_stderr, json_encode, json_decode, UserCancelled
from electrum.util import InvalidPassword
from electrum.commands import get_parser, known_commands, Commands, config_variables
from electrum.plugin import Plugins
from electrum.commands import get_parser, known_commands, Commands, config_variables, remove_disabled_plugin_commands
from electrum import daemon
from electrum import keystore
from electrum.util import create_and_start_event_loop, UserFacingException, JsonRPCError
@@ -111,8 +112,6 @@ from electrum.i18n import set_language
if TYPE_CHECKING:
import threading
from electrum.plugin import Plugins
_logger = get_logger(__name__)
@@ -261,11 +260,6 @@ async def run_offline_command(config, config_options, plugins: 'Plugins'):
return result
def init_plugins(config, gui_name):
from electrum.plugin import Plugins
return Plugins(config, gui_name)
loop = None # type: Optional[asyncio.AbstractEventLoop]
stop_loop = None # type: Optional[asyncio.Future]
loop_thread = None # type: Optional[threading.Thread]
@@ -313,7 +307,8 @@ def main():
sys.argv[i] = input("Enter argument:")
elif arg == ':':
sys.argv[i] = prompt_password('Enter argument (will not echo):', confirm=False)
# load (only) the commands modules of plugins so their commands are registered
plugin_commands = Plugins(cmd_only=True)
# parse command line
parser = get_parser()
args = parser.parse_args()
@@ -363,10 +358,13 @@ def main():
if not config_options.get('verbosity'):
warnings.simplefilter('ignore', DeprecationWarning)
config = SimpleConfig(config_options)
cmdname = config.get('cmd')
# now that we know which plugins are enabled we can remove commands of disabled plugins again
# enabled plugins depend on the datadir which is parsed by argparse, so they can only be unloaded afterwards
remove_disabled_plugin_commands(config, plugin_commands.loaded_command_modules)
# set language as early as possible
# Note: we are already too late for strings that are declared in the global scope
# of an already imported module. However, the GUI and the plugins at least have
@@ -498,7 +496,10 @@ def handle_cmd(*, cmdname: str, config: 'SimpleConfig', config_options: dict):
else:
# command line
configure_logging(config, log_to_file=False) # don't spam logfiles for each client-side RPC, but support "-v"
cmd = known_commands[cmdname]
cmd = known_commands.get(cmdname)
if not cmd:
print_stderr("unknown command:", cmdname)
sys_exit(1)
wallet_path = config.get_wallet_path()
if not config.NETWORK_OFFLINE:
init_cmdline(config_options, wallet_path, rpcserver=True, config=config)
@@ -538,7 +539,7 @@ def handle_cmd(*, cmdname: str, config: 'SimpleConfig', config_options: dict):
print_stderr("Run this command without --offline to interact with the daemon")
sys_exit(1)
init_cmdline(config_options, wallet_path, rpcserver=False, config=config)
plugins = init_plugins(config, 'cmdline')
plugins = Plugins(config, 'cmdline')
coro = run_offline_command(config, config_options, plugins)
fut = asyncio.run_coroutine_threadsafe(coro, loop)
try: