mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 07:34:24 +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:
@@ -19,7 +19,7 @@ def hello(plugin, name="world"):
|
|||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
@plugin.method("init")
|
@plugin.init()
|
||||||
def init(options, configuration, plugin):
|
def init(options, configuration, plugin):
|
||||||
plugin.log("Plugin helloworld.py initialized")
|
plugin.log("Plugin helloworld.py initialized")
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class Plugin(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, stdout=None, stdin=None, autopatch=True):
|
def __init__(self, stdout=None, stdin=None, autopatch=True):
|
||||||
self.methods = {}
|
self.methods = {'init': (self._init, MethodType.RPCMETHOD)}
|
||||||
self.options = {}
|
self.options = {}
|
||||||
|
|
||||||
# A dict from topics to handler functions
|
# A dict from topics to handler functions
|
||||||
@@ -43,7 +43,7 @@ class Plugin(object):
|
|||||||
self.rpc_filename = None
|
self.rpc_filename = None
|
||||||
self.lightning_dir = None
|
self.lightning_dir = None
|
||||||
self.rpc = None
|
self.rpc = None
|
||||||
self.init = None
|
self.child_init = None
|
||||||
|
|
||||||
def add_method(self, name, func):
|
def add_method(self, name, func):
|
||||||
"""Add a plugin method to the dispatch table.
|
"""Add a plugin method to the dispatch table.
|
||||||
@@ -158,6 +158,16 @@ class Plugin(object):
|
|||||||
return f
|
return f
|
||||||
return decorator
|
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):
|
def _exec_func(self, func, request):
|
||||||
params = request['params']
|
params = request['params']
|
||||||
sig = inspect.signature(func)
|
sig = inspect.signature(func)
|
||||||
@@ -265,12 +275,6 @@ class Plugin(object):
|
|||||||
return msgs[-1]
|
return msgs[-1]
|
||||||
|
|
||||||
def run(self):
|
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 = ""
|
partial = ""
|
||||||
for l in self.stdin:
|
for l in self.stdin:
|
||||||
partial += l
|
partial += l
|
||||||
@@ -322,12 +326,9 @@ class Plugin(object):
|
|||||||
for name, value in options.items():
|
for name, value in options.items():
|
||||||
self.options[name]['value'] = value
|
self.options[name]['value'] = value
|
||||||
|
|
||||||
# Swap the registered `init` method handler back in and
|
# Dispatch the plugin's init handler if any
|
||||||
# re-dispatch
|
if self.child_init:
|
||||||
if self.init:
|
return self._exec_func(self.child_init, request)
|
||||||
self.methods['init'], _ = self.init
|
|
||||||
self.init = None
|
|
||||||
return self._exec_func(self.methods['init'], request)
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user