Merge pull request #9649 from f321x/move_commands_to_init
Move plugin commands to init file of plugin
This commit is contained in:
@@ -1568,10 +1568,13 @@ def plugin_command(s, plugin_name):
|
|||||||
in the plugins root."""
|
in the plugins root."""
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
global known_commands
|
global known_commands
|
||||||
|
assert len(plugin_name) > 0, "Plugin name must not be empty"
|
||||||
func.plugin_name = plugin_name
|
func.plugin_name = plugin_name
|
||||||
name = plugin_name + '_' + func.__name__
|
name = plugin_name + '_' + func.__name__
|
||||||
if name in known_commands or hasattr(Commands, name):
|
if name in known_commands or hasattr(Commands, name):
|
||||||
raise Exception(f"Plugins should not override other commands: {name}")
|
# electrum plugins are always loaded before the plugin commands,
|
||||||
|
# so plugin commands cannot overwrite them
|
||||||
|
return
|
||||||
assert asyncio.iscoroutinefunction(func), f"Plugin commands must be a coroutine: {name}"
|
assert asyncio.iscoroutinefunction(func), f"Plugin commands must be a coroutine: {name}"
|
||||||
@command(s)
|
@command(s)
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
|
|||||||
@@ -104,13 +104,13 @@ class Plugins(DaemonThread):
|
|||||||
if self.cmd_only and self.config.get('enable_plugin_' + name) is not True:
|
if self.cmd_only and self.config.get('enable_plugin_' + name) is not True:
|
||||||
continue
|
continue
|
||||||
base_name = 'electrum.plugins' if not external else 'electrum_external_plugins'
|
base_name = 'electrum.plugins' if not external else 'electrum_external_plugins'
|
||||||
full_name = f'{base_name}.{name}' + ('.commands' if self.cmd_only else '')
|
full_name = f'{base_name}.{name}'
|
||||||
if external:
|
if external:
|
||||||
module_path = os.path.join(pkg_path, name)
|
module_path = os.path.join(pkg_path, name)
|
||||||
if not self._has_recursive_root_permissions(module_path):
|
if not self._has_recursive_root_permissions(module_path):
|
||||||
self.logger.info(f"Not loading plugin {module_path}: directory has user write permissions")
|
self.logger.info(f"Not loading plugin {module_path}: directory has user write permissions")
|
||||||
continue
|
continue
|
||||||
module_path = os.path.join(module_path, 'commands.py' if self.cmd_only else '__init__.py')
|
module_path = os.path.join(module_path, '__init__.py')
|
||||||
if not os.path.exists(module_path):
|
if not os.path.exists(module_path):
|
||||||
continue
|
continue
|
||||||
spec = importlib.util.spec_from_file_location(full_name, module_path)
|
spec = importlib.util.spec_from_file_location(full_name, module_path)
|
||||||
@@ -122,7 +122,7 @@ class Plugins(DaemonThread):
|
|||||||
raise Exception(f"Error pre-loading {full_name}: no spec")
|
raise Exception(f"Error pre-loading {full_name}: no spec")
|
||||||
module = self.exec_module_from_spec(spec, full_name)
|
module = self.exec_module_from_spec(spec, full_name)
|
||||||
if self.cmd_only:
|
if self.cmd_only:
|
||||||
assert name not in self.loaded_command_modules, f"duplicate command modules for: {name}"
|
assert name not in self.loaded_command_modules, f"tried to load commands of {name} twice"
|
||||||
self.loaded_command_modules.add(name)
|
self.loaded_command_modules.add(name)
|
||||||
continue
|
continue
|
||||||
d = module.__dict__
|
d = module.__dict__
|
||||||
@@ -235,17 +235,13 @@ class Plugins(DaemonThread):
|
|||||||
raise Exception(f"duplicate plugins for name={name}")
|
raise Exception(f"duplicate plugins for name={name}")
|
||||||
if name in self.external_plugin_metadata:
|
if name in self.external_plugin_metadata:
|
||||||
raise Exception(f"duplicate plugins for name={name}")
|
raise Exception(f"duplicate plugins for name={name}")
|
||||||
|
if self.cmd_only and not self.config.get('enable_plugin_' + name):
|
||||||
|
continue
|
||||||
module_path = f'electrum_external_plugins.{name}' if external else f'electrum.plugins.{name}'
|
module_path = f'electrum_external_plugins.{name}' if external else f'electrum.plugins.{name}'
|
||||||
spec = zipfile.find_spec(name)
|
spec = zipfile.find_spec(name)
|
||||||
module = self.exec_module_from_spec(spec, module_path)
|
module = self.exec_module_from_spec(spec, module_path)
|
||||||
if self.cmd_only:
|
if self.cmd_only:
|
||||||
if self.config.get('enable_plugin_' + name) is not True:
|
assert name not in self.loaded_command_modules, f"tried to load commands of {name} twice"
|
||||||
continue
|
|
||||||
spec2 = importlib.util.find_spec(module_path + '.commands')
|
|
||||||
if spec2 is None: # no commands module in this plugin
|
|
||||||
continue
|
|
||||||
self.exec_module_from_spec(spec2, module_path + '.commands')
|
|
||||||
assert name not in self.loaded_command_modules, f"duplicate command modules for: {name}"
|
|
||||||
self.loaded_command_modules.add(name)
|
self.loaded_command_modules.add(name)
|
||||||
continue
|
continue
|
||||||
d = module.__dict__
|
d = module.__dict__
|
||||||
|
|||||||
@@ -1,4 +1,11 @@
|
|||||||
from electrum.i18n import _
|
from electrum.i18n import _
|
||||||
|
from electrum.commands import plugin_command
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .labels import LabelsPlugin
|
||||||
|
from electrum.commands import Commands
|
||||||
|
|
||||||
|
|
||||||
fullname = _('LabelSync')
|
fullname = _('LabelSync')
|
||||||
description = ' '.join([
|
description = ' '.join([
|
||||||
@@ -7,3 +14,15 @@ description = ' '.join([
|
|||||||
])
|
])
|
||||||
available_for = ['qt', 'qml', 'cmdline']
|
available_for = ['qt', 'qml', 'cmdline']
|
||||||
|
|
||||||
|
plugin_name = "labels"
|
||||||
|
|
||||||
|
@plugin_command('w', plugin_name)
|
||||||
|
async def push(self: 'Commands', plugin: 'LabelsPlugin' = None, wallet=None) -> int:
|
||||||
|
""" push labels to server """
|
||||||
|
return await plugin.push_thread(wallet)
|
||||||
|
|
||||||
|
|
||||||
|
@plugin_command('w', plugin_name)
|
||||||
|
async def pull(self: 'Commands', plugin: 'LabelsPlugin' = None, wallet=None, force=False) -> int:
|
||||||
|
""" pull labels from server """
|
||||||
|
return await plugin.pull_thread(wallet, force=force)
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
from electrum.commands import plugin_command
|
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from .labels import LabelsPlugin
|
|
||||||
from electrum.commands import Commands
|
|
||||||
|
|
||||||
plugin_name = "labels"
|
|
||||||
|
|
||||||
|
|
||||||
@plugin_command('w', plugin_name)
|
|
||||||
async def push(self: 'Commands', plugin: 'LabelsPlugin' = None, wallet=None) -> int:
|
|
||||||
""" push labels to server """
|
|
||||||
return await plugin.push_thread(wallet)
|
|
||||||
|
|
||||||
|
|
||||||
@plugin_command('w', plugin_name)
|
|
||||||
async def pull(self: 'Commands', plugin: 'LabelsPlugin' = None, wallet=None, force=False) -> int:
|
|
||||||
""" pull labels from server """
|
|
||||||
return await plugin.pull_thread(wallet, force=force)
|
|
||||||
Reference in New Issue
Block a user