Pylightning: Allow a plugin to set the category of the RPC commands it creates

This commit is contained in:
darosior 2019-05-27 12:58:14 +02:00 committed by Rusty Russell
parent b1bbafb19c
commit 78326a8d90

View file

@ -30,10 +30,11 @@ class Method(object):
- RPC exposed by RPC passthrough - RPC exposed by RPC passthrough
- HOOK registered to be called synchronously by lightningd - HOOK registered to be called synchronously by lightningd
""" """
def __init__(self, name, func, mtype=MethodType.RPCMETHOD): def __init__(self, name, func, mtype=MethodType.RPCMETHOD, category=None):
self.name = name self.name = name
self.func = func self.func = func
self.mtype = mtype self.mtype = mtype
self.category = category
self.background = False self.background = False
@ -118,7 +119,7 @@ class Plugin(object):
self.write_lock = RLock() self.write_lock = RLock()
def add_method(self, name, func, background=False): def add_method(self, name, func, background=False, category=None):
"""Add a plugin method to the dispatch table. """Add a plugin method to the dispatch table.
The function will be expected at call time (see `_dispatch`) The function will be expected at call time (see `_dispatch`)
@ -145,6 +146,9 @@ class Plugin(object):
`request.set_result` or `result.set_exception` to return a result or `request.set_result` or `result.set_exception` to return a result or
raise an exception for the call. raise an exception for the call.
The `category` argument can be used to specify the category of the
newly created rpc command.
""" """
if name in self.methods: if name in self.methods:
raise ValueError( raise ValueError(
@ -152,7 +156,7 @@ class Plugin(object):
) )
# Register the function with the name # Register the function with the name
method = Method(name, func, MethodType.RPCMETHOD) method = Method(name, func, MethodType.RPCMETHOD, category)
method.background = background method.background = background
self.methods[name] = method self.methods[name] = method
@ -211,23 +215,23 @@ class Plugin(object):
else: else:
return self.options[name]['default'] return self.options[name]['default']
def async_method(self, method_name): def async_method(self, method_name, category=None):
"""Decorator to add an async plugin method to the dispatch table. """Decorator to add an async plugin method to the dispatch table.
Internally uses add_method. Internally uses add_method.
""" """
def decorator(f): def decorator(f):
self.add_method(method_name, f, background=True) self.add_method(method_name, f, background=True, category=category)
return f return f
return decorator return decorator
def method(self, method_name): def method(self, method_name, category=None):
"""Decorator to add a plugin method to the dispatch table. """Decorator to add a plugin method to the dispatch table.
Internally uses add_method. Internally uses add_method.
""" """
def decorator(f): def decorator(f):
self.add_method(method_name, f, background=False) self.add_method(method_name, f, background=False, category=category)
return f return f
return decorator return decorator
@ -473,6 +477,7 @@ class Plugin(object):
methods.append({ methods.append({
'name': method.name, 'name': method.name,
'category': method.category if method.category else "plugin",
'usage': " ".join(args), 'usage': " ".join(args),
'description': doc 'description': doc
}) })