Skip to content

Commit e8ba365

Browse files
committed
pyln-client: refactors usage string generation to reduce code duplication
1 parent c7d2a49 commit e8ba365

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

contrib/pyln-client/pyln/client/plugin.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,35 @@ def __init__(self, name: str, func: Callable[..., JSONType],
5858
self.description = description
5959
self.before: List[str] = []
6060
self.after: List[str] = []
61-
self.description = description
61+
62+
def get_usage(self):
63+
# Handles out-of-order use of parameters like:
64+
#
65+
# ```python3
66+
#
67+
# def hello_obfus(arg1, arg2, plugin, thing3, request=None,
68+
# thing5='at', thing6=21)
69+
#
70+
# ```
71+
argspec = inspect.getfullargspec(self.func)
72+
defaults = argspec.defaults
73+
num_defaults = len(defaults) if defaults else 0
74+
start_kwargs_idx = len(argspec.args) - num_defaults
75+
args = []
76+
for idx, arg in enumerate(argspec.args):
77+
if arg in ('plugin', 'request'):
78+
continue
79+
# Positional arg
80+
if idx < start_kwargs_idx:
81+
args.append("%s" % arg)
82+
# Keyword arg
83+
else:
84+
args.append("[%s]" % arg)
85+
86+
if self.description is not None:
87+
args.append("\n%s" % self.description)
88+
89+
return " ".join(args)
6290

6391

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

779807
return msgs[-1]
780808

781-
def print_usage(self):
809+
def get_usage(self):
782810
import textwrap
783811

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

835863
parts.append(method_tpl.format(
836-
name=method.name,
864+
name="%s %s" % (method.name, method.get_usage()),
837865
))
838866

839867
options_header = textwrap.dedent("""
@@ -868,8 +896,10 @@ def print_usage(self):
868896
default=opt.default,
869897
typ=opt.opt_type,
870898
))
899+
return "".join(parts)
871900

872-
sys.stdout.write("".join(parts))
901+
def print_usage(self):
902+
sys.stdout.write(self.get_usage())
873903
sys.stdout.write("\n")
874904

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

921-
# Handles out-of-order use of parameters like:
922-
#
923-
# ```python3
924-
#
925-
# def hello_obfus(arg1, arg2, plugin, thing3, request=None,
926-
# thing5='at', thing6=21)
927-
#
928-
# ```
929-
argspec = inspect.getfullargspec(method.func)
930-
defaults = argspec.defaults
931-
num_defaults = len(defaults) if defaults else 0
932-
start_kwargs_idx = len(argspec.args) - num_defaults
933-
args = []
934-
for idx, arg in enumerate(argspec.args):
935-
if arg in ('plugin', 'request'):
936-
continue
937-
# Positional arg
938-
if idx < start_kwargs_idx:
939-
args.append("%s" % arg)
940-
# Keyword arg
941-
else:
942-
args.append("[%s]" % arg)
943-
944-
if method.description:
945-
args.append("\n%s" % method.description)
946-
947951
methods.append({
948952
'name': method.name,
949-
'usage': " ".join(args)
953+
'usage': method.get_usage()
950954
})
951955

952956
manifest = {

0 commit comments

Comments
 (0)