mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-22 06:41:44 +01:00
pylightning: use different decoration for init msg.
The next patch wants to decorate the methods with a compulsory 'usage' option, which doesn't make sense for init. So I wanted to change the init to its own decoration. Made-to-work-by: @cdecker Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
5770e0c700
commit
0c89fc5d70
2 changed files with 16 additions and 15 deletions
|
@ -19,7 +19,7 @@ def hello(plugin, name="world"):
|
|||
return s
|
||||
|
||||
|
||||
@plugin.method("init")
|
||||
@plugin.init()
|
||||
def init(options, configuration, plugin):
|
||||
plugin.log("Plugin helloworld.py initialized")
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ class Plugin(object):
|
|||
"""
|
||||
|
||||
def __init__(self, stdout=None, stdin=None, autopatch=True):
|
||||
self.methods = {}
|
||||
self.methods = {'init': (self._init, MethodType.RPCMETHOD)}
|
||||
self.options = {}
|
||||
|
||||
# A dict from topics to handler functions
|
||||
|
@ -43,7 +43,7 @@ class Plugin(object):
|
|||
self.rpc_filename = None
|
||||
self.lightning_dir = None
|
||||
self.rpc = None
|
||||
self.init = None
|
||||
self.child_init = None
|
||||
|
||||
def add_method(self, name, func):
|
||||
"""Add a plugin method to the dispatch table.
|
||||
|
@ -158,6 +158,16 @@ class Plugin(object):
|
|||
return f
|
||||
return decorator
|
||||
|
||||
def init(self, *args, **kwargs):
|
||||
"""Decorator to add a function called after plugin initialization
|
||||
"""
|
||||
def decorator(f):
|
||||
if self.child_init is not None:
|
||||
raise ValueError('The @plugin.init decorator should only be used once')
|
||||
self.child_init = f
|
||||
return f
|
||||
return decorator
|
||||
|
||||
def _exec_func(self, func, request):
|
||||
params = request['params']
|
||||
sig = inspect.signature(func)
|
||||
|
@ -265,12 +275,6 @@ class Plugin(object):
|
|||
return msgs[-1]
|
||||
|
||||
def run(self):
|
||||
# Stash the init method handler, we'll handle opts first and
|
||||
# then unstash this and call it.
|
||||
if 'init' in self.methods:
|
||||
self.init = self.methods['init']
|
||||
self.methods['init'] = (self._init, MethodType.RPCMETHOD)
|
||||
|
||||
partial = ""
|
||||
for l in self.stdin:
|
||||
partial += l
|
||||
|
@ -322,12 +326,9 @@ class Plugin(object):
|
|||
for name, value in options.items():
|
||||
self.options[name]['value'] = value
|
||||
|
||||
# Swap the registered `init` method handler back in and
|
||||
# re-dispatch
|
||||
if self.init:
|
||||
self.methods['init'], _ = self.init
|
||||
self.init = None
|
||||
return self._exec_func(self.methods['init'], request)
|
||||
# Dispatch the plugin's init handler if any
|
||||
if self.child_init:
|
||||
return self._exec_func(self.child_init, request)
|
||||
return None
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue