1
0

Plugins call with cmd_only:

- pass temporary config to Plugins
 - load only enabled plugins
 - parse the command line again after plugins are loaded
This commit is contained in:
ThomasV
2025-03-15 11:40:26 +01:00
parent 4f79e516e4
commit cb39737a39
3 changed files with 22 additions and 34 deletions

View File

@@ -65,19 +65,20 @@ class Plugins(DaemonThread):
pkgpath = os.path.dirname(plugins.__file__)
@profiler
def __init__(self, config: SimpleConfig = None, gui_name = None, cmd_only: bool = False):
def __init__(self, config: SimpleConfig, gui_name = None, cmd_only: bool = False):
self.config = config
self.cmd_only = cmd_only # type: bool
self.internal_plugin_metadata = {}
self.external_plugin_metadata = {}
self.loaded_command_modules = set() # type: set[str]
if cmd_only:
# only import the command modules of plugins
Logger.__init__(self)
self.find_plugins()
return
DaemonThread.__init__(self)
self.device_manager = DeviceMgr(config)
self.name = 'Plugins' # set name of thread
self.config = config
self.hw_wallets = {}
self.plugins = {} # type: Dict[str, BasePlugin]
self.gui_name = gui_name
@@ -86,15 +87,6 @@ class Plugins(DaemonThread):
self.add_jobs(self.device_manager.thread_jobs())
self.start()
def __getattr__(self, item):
# to prevent accessing of a cmd_only instance of this class
if self.cmd_only:
if item == 'logger':
# if something tries to access logger and it is not initialized it gets initialized here
Logger.__init__(self)
return self.logger
raise Exception(f"This instance of Plugins is only for command importing, cannot access {item}")
@property
def descriptions(self):
return dict(list(self.internal_plugin_metadata.items()) + list(self.external_plugin_metadata.items()))
@@ -108,6 +100,8 @@ class Plugins(DaemonThread):
# we exclude the ones packaged as *code*, here:
if loader.__class__.__qualname__ == "PyiFrozenImporter":
continue
if self.cmd_only and self.config.get('enable_plugin_' + name) is not True:
continue
full_name = f'electrum.plugins.{name}' + ('.commands' if self.cmd_only else '')
spec = importlib.util.find_spec(full_name)
if spec is None:
@@ -233,6 +227,8 @@ class Plugins(DaemonThread):
spec = zipfile.find_spec(name)
module = self.exec_module_from_spec(spec, module_path)
if self.cmd_only:
if self.config.get('enable_plugin_' + name) is not True:
continue
spec2 = importlib.util.find_spec(module_path + '.commands')
if spec2 is None: # no commands module in this plugin
continue