From 960664337f2afc1aa9aac7618e11a0cb07503560 Mon Sep 17 00:00:00 2001 From: Conor Scott Date: Thu, 7 Feb 2019 13:44:53 +1030 Subject: [PATCH] pylightning: handle method introspection more generally. [ I just cut & paste from @conscott's comment on GitHub -- RR ] Signed-off-by: Rusty Russell --- contrib/pylightning/lightning/plugin.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/contrib/pylightning/lightning/plugin.py b/contrib/pylightning/lightning/plugin.py index 2b7a46558..b6f298a7d 100644 --- a/contrib/pylightning/lightning/plugin.py +++ b/contrib/pylightning/lightning/plugin.py @@ -306,15 +306,22 @@ class Plugin(object): doc = "Undocumented RPC method from a plugin." doc = re.sub('\n+', ' ', doc) + # Handles out-of-order use of parameters like: + # def hello_obfus(arg1, arg2, plugin, thing3, request=None, thing5='at', thing6=21) argspec = inspect.getargspec(func) - args = argspec.args[1:] defaults = argspec.defaults - - # Make optional args be surrounded by square brackets - # list regular lightning-cli commands args - if defaults: - for idx in range(-len(defaults), 0): - args[idx] = '[' + args[idx] + ']' + num_defaults = len(defaults) if defaults else 0 + start_kwargs_idx = len(argspec.args) - num_defaults + args = [] + for idx, arg in enumerate(argspec.args): + if arg in ('plugin', 'request'): + continue + # Positional arg + if idx < start_kwargs_idx: + args.append("%s" % arg) + # Keyword arg + else: + args.append("[%s]" % arg) methods.append({ 'name': name,