From ec592e05b9c0d8349df44c62c30e878898a8014b Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 26 Nov 2018 18:47:27 +0100 Subject: [PATCH] plugin: Allow both array as well as object params in example plugin Signed-off-by: Christian Decker --- contrib/plugins/helloworld.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/contrib/plugins/helloworld.py b/contrib/plugins/helloworld.py index 7059548a2..6ae5b1ff2 100755 --- a/contrib/plugins/helloworld.py +++ b/contrib/plugins/helloworld.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 """Simple plugin to show how to build new plugins for c-lightning -It demonstrates how a plugin communicates with c-lightning, how it registers -command line arguments that should be passed through and how it can register -JSON-RPC commands. We communicate with the main daemon through STDIN and STDOUT, -reading and writing JSON-RPC requests. +It demonstrates how a plugin communicates with c-lightning, how it +registers command line arguments that should be passed through and how +it can register JSON-RPC commands. We communicate with the main daemon +through STDIN and STDOUT, reading and writing JSON-RPC requests. """ import json @@ -14,8 +14,8 @@ import sys greeting = "World" -def json_hello(request): - greeting = "Hello {}".format(request['params']['name']) +def json_hello(request, name): + greeting = "Hello {}".format(name) return greeting @@ -37,7 +37,7 @@ def json_getmanifest(request): } -def json_init(request): +def json_init(request, options): """The main daemon is telling us the relevant cli options """ global greeting @@ -52,14 +52,23 @@ methods = { 'init': json_init, } + partial = "" for l in sys.stdin: partial += l try: request = json.loads(partial) + result = None + method = methods[request['method']] + params = request['params'] + if isinstance(params, dict): + result = method(request, **params) + else: + result = method(request, *params) + result = { "jsonrpc": "2.0", - "result": methods[request['method']](request), + "result": result, "id": request['id'] } json.dump(result, fp=sys.stdout)