mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-22 14:42:40 +01:00
pylightning: Migrate the test plugin to use the new wrapper
Should be a lot easier to see what happens :-) Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
parent
d88228280e
commit
643480cfd8
3 changed files with 20 additions and 104 deletions
|
@ -1,111 +1,28 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Simple plugin to show how to build new plugins for c-lightning
|
from lightning import Plugin
|
||||||
|
|
||||||
It demonstrates how a plugin communicates with c-lightning, how it
|
|
||||||
registers command line arguments that should be passed through and how
|
plugin = Plugin(autopatch=True)
|
||||||
it can register JSON-RPC commands. We communicate with the main daemon
|
|
||||||
through STDIN and STDOUT, reading and writing JSON-RPC requests.
|
|
||||||
|
@plugin.method("hello")
|
||||||
|
def hello(name, plugin):
|
||||||
|
"""This is the documentation string for the hello-function.
|
||||||
|
|
||||||
|
It gets reported as the description when registering the function
|
||||||
|
as a method with `lightningd`.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import json
|
greeting = plugin.get_option('greeting')
|
||||||
import sys
|
s = '{} {}'.format(greeting, name)
|
||||||
|
plugin.log(s)
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
greeting = "World"
|
@plugin.method("init")
|
||||||
|
def init(options, configuration, plugin):
|
||||||
|
plugin.log("Plugin helloworld.py initialized")
|
||||||
|
|
||||||
|
|
||||||
def json_hello(request, name):
|
plugin.add_option('greeting', 'Hello', 'The greeting I should use.')
|
||||||
greeting = "Hello {}".format(name)
|
plugin.run()
|
||||||
return greeting
|
|
||||||
|
|
||||||
|
|
||||||
def json_fail(request):
|
|
||||||
raise ValueError("This will fail")
|
|
||||||
|
|
||||||
|
|
||||||
def json_getmanifest(request):
|
|
||||||
global greeting
|
|
||||||
return {
|
|
||||||
"options": [
|
|
||||||
{"name": "greeting",
|
|
||||||
"type": "string",
|
|
||||||
"default": greeting,
|
|
||||||
"description": "What name should I call you?"},
|
|
||||||
],
|
|
||||||
"rpcmethods": [
|
|
||||||
{
|
|
||||||
"name": "hello",
|
|
||||||
"description": "Returns a personalized greeting for {name}",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "fail",
|
|
||||||
"description": "Always returns a failure for testing",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def plugin_log(message, level="info"):
|
|
||||||
payload = {
|
|
||||||
"jsonrpc": "2.0",
|
|
||||||
"method": "log",
|
|
||||||
"params": {
|
|
||||||
"level": level,
|
|
||||||
"message": message,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
json.dump(payload, fp=sys.stdout)
|
|
||||||
sys.stdout.write('\n\n')
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
|
|
||||||
def json_init(request, options, configuration):
|
|
||||||
"""The main daemon is telling us the relevant cli options
|
|
||||||
"""
|
|
||||||
global greeting
|
|
||||||
greeting = request['params']['options']['greeting']
|
|
||||||
plugin_log("Plugin helloworld.py initialized with greeting \"{}\"".format(greeting), "debug")
|
|
||||||
|
|
||||||
return "ok"
|
|
||||||
|
|
||||||
|
|
||||||
methods = {
|
|
||||||
'hello': json_hello,
|
|
||||||
'fail': json_fail,
|
|
||||||
'getmanifest': json_getmanifest,
|
|
||||||
'init': json_init,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
partial = ""
|
|
||||||
for l in sys.stdin:
|
|
||||||
try:
|
|
||||||
partial += l
|
|
||||||
request = json.loads(partial)
|
|
||||||
except Exception:
|
|
||||||
continue
|
|
||||||
|
|
||||||
result = None
|
|
||||||
method = methods[request['method']]
|
|
||||||
params = request['params']
|
|
||||||
try:
|
|
||||||
if isinstance(params, dict):
|
|
||||||
result = method(request, **params)
|
|
||||||
else:
|
|
||||||
result = method(request, *params)
|
|
||||||
result = {
|
|
||||||
"jsonrpc": "2.0",
|
|
||||||
"result": result,
|
|
||||||
"id": request['id']
|
|
||||||
}
|
|
||||||
except Exception:
|
|
||||||
result = {
|
|
||||||
"jsonrpc": "2.0",
|
|
||||||
"error": "Error while processing {}".format(request['method']),
|
|
||||||
"id": request['id']
|
|
||||||
}
|
|
||||||
|
|
||||||
json.dump(result, fp=sys.stdout)
|
|
||||||
sys.stdout.write('\n')
|
|
||||||
sys.stdout.flush()
|
|
||||||
partial = ""
|
|
||||||
|
|
|
@ -180,7 +180,6 @@ class Plugin(object):
|
||||||
|
|
||||||
def _getmanifest(self):
|
def _getmanifest(self):
|
||||||
methods = []
|
methods = []
|
||||||
|
|
||||||
for name, func in self.methods.items():
|
for name, func in self.methods.items():
|
||||||
# Skip the builtin ones, they don't get reported
|
# Skip the builtin ones, they don't get reported
|
||||||
if name in ['getmanifest', 'init']:
|
if name in ['getmanifest', 'init']:
|
||||||
|
|
|
@ -28,7 +28,7 @@ def test_option_passthrough(node_factory):
|
||||||
|
|
||||||
# Now try to see if it gets accepted, would fail to start if the
|
# Now try to see if it gets accepted, would fail to start if the
|
||||||
# option didn't exist
|
# option didn't exist
|
||||||
n = node_factory.get_node(options={'plugin': plugin_path, 'greeting': 'Mars'})
|
n = node_factory.get_node(options={'plugin': plugin_path, 'greeting': 'Ciao'})
|
||||||
n.stop()
|
n.stop()
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ def test_rpc_passthrough(node_factory):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
plugin_path = 'contrib/plugins/helloworld.py'
|
plugin_path = 'contrib/plugins/helloworld.py'
|
||||||
n = node_factory.get_node(options={'plugin': plugin_path, 'greeting': 'Mars'})
|
n = node_factory.get_node(options={'plugin': plugin_path, 'greeting': 'Ciao'})
|
||||||
|
|
||||||
# Make sure that the 'hello' command that the helloworld.py plugin
|
# Make sure that the 'hello' command that the helloworld.py plugin
|
||||||
# has registered is available.
|
# has registered is available.
|
||||||
|
@ -52,8 +52,8 @@ def test_rpc_passthrough(node_factory):
|
||||||
assert n.daemon.is_in_log('Plugin helloworld.py initialized')
|
assert n.daemon.is_in_log('Plugin helloworld.py initialized')
|
||||||
|
|
||||||
# Now try to call it and see what it returns:
|
# Now try to call it and see what it returns:
|
||||||
greet = n.rpc.hello(name='Sun')
|
greet = n.rpc.hello(name='World')
|
||||||
assert(greet == "Hello Sun")
|
assert(greet == "Ciao World")
|
||||||
with pytest.raises(RpcError):
|
with pytest.raises(RpcError):
|
||||||
n.rpc.fail()
|
n.rpc.fail()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue