plugin commands:
- make plugin commands start with plugin name + underscore
- plugin_name must be passed to the plugin_command decorator
- fixes:
- remove plugin_commands (unneeded)
- func_wrapper must await func()
- setattr(Commands, name, func_wrapper)
- add push/pull commands to labels plugin
This commit is contained in:
22
electrum/plugins/labels/commands.py
Normal file
22
electrum/plugins/labels/commands.py
Normal file
@@ -0,0 +1,22 @@
|
||||
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', wallet=None) -> int:
|
||||
""" push labels to server """
|
||||
plugin: 'LabelsPlugin' = self.daemon._plugins.get_plugin(plugin_name)
|
||||
return await plugin.push_thread(wallet)
|
||||
|
||||
|
||||
@plugin_command('w', plugin_name)
|
||||
async def pull(self: 'Commands', wallet=None) -> int:
|
||||
""" pull labels from server """
|
||||
assert wallet is not None
|
||||
plugin: 'LabelsPlugin' = self.daemon._plugins.get_plugin(plugin_name)
|
||||
return await plugin.pull_thread(wallet, force=False)
|
||||
@@ -108,7 +108,7 @@ class LabelsPlugin(BasePlugin):
|
||||
except Exception as e:
|
||||
raise Exception('Could not decode: ' + await result.text()) from e
|
||||
|
||||
async def push_thread(self, wallet: 'Abstract_Wallet'):
|
||||
async def push_thread(self, wallet: 'Abstract_Wallet') -> int:
|
||||
wallet_data = self.wallets.get(wallet, None)
|
||||
if not wallet_data:
|
||||
raise Exception('Wallet {} not loaded'.format(wallet))
|
||||
@@ -126,8 +126,9 @@ class LabelsPlugin(BasePlugin):
|
||||
bundle["labels"].append({'encryptedLabel': encoded_value,
|
||||
'externalId': encoded_key})
|
||||
await self.do_post("/labels", bundle)
|
||||
return len(bundle['labels'])
|
||||
|
||||
async def pull_thread(self, wallet: 'Abstract_Wallet', force: bool):
|
||||
async def pull_thread(self, wallet: 'Abstract_Wallet', force: bool) -> int:
|
||||
wallet_data = self.wallets.get(wallet, None)
|
||||
if not wallet_data:
|
||||
raise Exception('Wallet {} not loaded'.format(wallet))
|
||||
@@ -140,7 +141,7 @@ class LabelsPlugin(BasePlugin):
|
||||
raise ErrorConnectingServer(e) from e
|
||||
if response["labels"] is None or len(response["labels"]) == 0:
|
||||
self.logger.info('no new labels')
|
||||
return
|
||||
return 0
|
||||
|
||||
self.logger.info(f'received {len(response["labels"])} labels')
|
||||
result = {}
|
||||
@@ -165,6 +166,7 @@ class LabelsPlugin(BasePlugin):
|
||||
self.set_nonce(wallet, response["nonce"] + 1)
|
||||
util.trigger_callback('labels_received', wallet, result)
|
||||
self.on_pulled(wallet)
|
||||
return len(result)
|
||||
|
||||
def on_pulled(self, wallet: 'Abstract_Wallet') -> None:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user