Skip to content

Commit

Permalink
pyln-client: refactors usage string generation to reduce code duplica…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
m-schmoock committed Sep 19, 2024
1 parent 770b4e4 commit 4ddda37
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions contrib/pyln-client/pyln/client/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,35 @@ def __init__(self, name: str, func: Callable[..., JSONType],
self.description = description
self.before: List[str] = []
self.after: List[str] = []
self.description = description

def get_usage(self):
# Handles out-of-order use of parameters like:
#
# ```python3
#
# def hello_obfus(arg1, arg2, plugin, thing3, request=None,
# thing5='at', thing6=21)
#
# ```
argspec = inspect.getfullargspec(self.func)
defaults = argspec.defaults
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)

if self.description is not None:
args.append("\n%s" % self.description)

return " ".join(args)


class RpcException(Exception):
Expand Down Expand Up @@ -778,7 +806,7 @@ def _multi_dispatch(self, msgs: List[bytes]) -> bytes:

return msgs[-1]

def print_usage(self):
def get_usage(self):
import textwrap

executable = os.path.abspath(sys.argv[0])
Expand Down Expand Up @@ -833,7 +861,7 @@ def print_usage(self):
methods_header = None

parts.append(method_tpl.format(
name=method.name,
name="%s %s" % (method.name, method.get_usage()),
))

options_header = textwrap.dedent("""
Expand Down Expand Up @@ -868,8 +896,10 @@ def print_usage(self):
default=opt.default,
typ=opt.opt_type,
))
return "".join(parts)

sys.stdout.write("".join(parts))
def print_usage(self):
sys.stdout.write(self.get_usage())
sys.stdout.write("\n")

def run(self) -> None:
Expand Down Expand Up @@ -918,35 +948,9 @@ def _getmanifest(self, **kwargs) -> JSONType:
doc = "Undocumented RPC method from a plugin."
doc = re.sub('\n+', ' ', doc)

# Handles out-of-order use of parameters like:
#
# ```python3
#
# def hello_obfus(arg1, arg2, plugin, thing3, request=None,
# thing5='at', thing6=21)
#
# ```
argspec = inspect.getfullargspec(method.func)
defaults = argspec.defaults
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)

if method.description:
args.append("\n%s" % method.description)

methods.append({
'name': method.name,
'usage': " ".join(args)
'usage': method.get_usage()
})

manifest = {
Expand Down

0 comments on commit 4ddda37

Please sign in to comment.