Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fcaa2ae

Browse files
committed
CLI handle _zerorpc_inspect generic format
1 parent 88ea7f1 commit fcaa2ae

File tree

1 file changed

+59
-20
lines changed

1 file changed

+59
-20
lines changed

bin/zerorpc

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,11 @@ def run_server(args):
116116

117117
# this function does a really intricate job to keep backward compatibility
118118
# with a previous version of zerorpc, and lazily retrieving results if possible
119-
def zerorpc_inspect(client, method=None, long_doc=True, include_argspec=True):
120-
try:
121-
remote_detailled_methods = client._zerorpc_inspect(method,
122-
long_doc)['methods']
123-
124-
if include_argspec:
125-
r = [(name + (inspect.formatargspec(*argspec) if argspec else
126-
'(...)'), doc if doc else '<undocumented>')
127-
for name, argspec, doc in remote_detailled_methods]
128-
else:
129-
r = [(name, doc if doc else '<undocumented>')
130-
for name, argspec, doc in remote_detailled_methods]
131-
132-
longest_name_len = max(len(name) for name, doc in r)
133-
return (longest_name_len, r)
134-
except (zerorpc.RemoteError, NameError):
135-
pass
136-
137-
if method is None:
119+
def zerorpc_inspect_legacy(client, filter_method, long_doc, include_argspec):
120+
if filter_method is None:
138121
remote_methods = client._zerorpc_list()
139122
else:
140-
remote_methods = [method]
123+
remote_methods = [filter_method]
141124

142125
def remote_detailled_methods():
143126
for name in remote_methods:
@@ -161,6 +144,62 @@ def zerorpc_inspect(client, method=None, long_doc=True, include_argspec=True):
161144
longest_name_len = max(len(name) for name, doc in r)
162145
return (longest_name_len, r)
163146

147+
# handle the 'python formatted' _zerorpc_inspect, that return the output of
148+
# "getargspec" from the python lib "inspect".
149+
def zerorpc_inspect_python_argspecs(remote_methods, filter_method, long_doc, include_argspec):
150+
def format_method(name, argspec, doc):
151+
if include_argspec:
152+
name += (inspect.formatargspec(*argspec) if argspec else
153+
'(...)')
154+
if not doc:
155+
doc = '<undocumented>'
156+
elif not long_doc:
157+
doc = doc.splitlines()[0]
158+
return (name, doc)
159+
r = [format_method(*methods_info) for methods_info in remote_methods if
160+
filter_method is None or methods_info[0] == filter_method]
161+
longest_name_len = max(len(name) for name, doc in r)
162+
return (longest_name_len, r)
163+
164+
def zerorpc_inspect_generic(remote_methods, filter_method, long_doc, include_argspec):
165+
def format_method(name, args, doc):
166+
if include_argspec:
167+
def format_arg(arg):
168+
def_val = arg.get('default')
169+
if def_val is None:
170+
return arg['name']
171+
return '{0}={1}'.format(arg['name'], def_val)
172+
173+
name += '({0})'.format(', '.join(map(format_arg, args)))
174+
if not doc:
175+
doc = '<undocumented>'
176+
elif not long_doc:
177+
doc = doc.splitlines()[0]
178+
return (name, doc)
179+
180+
methods = [format_method(name, details['args'], details['doc']) for name, details in remote_methods.items()
181+
if filter_method is None or name == filter_method]
182+
183+
longest_name_len = max(len(name) for name, doc in methods)
184+
return (longest_name_len, methods)
185+
186+
def zerorpc_inspect(client, method=None, long_doc=True, include_argspec=True):
187+
try:
188+
remote_methods = client._zerorpc_inspect()['methods']
189+
legacy = False
190+
except (zerorpc.RemoteError, NameError):
191+
legacy = True
192+
193+
if legacy:
194+
return zerorpc_inspect_legacy(client, method,
195+
long_doc, include_argspec)
196+
197+
if not isinstance(remote_methods, dict):
198+
return zerorpc_inspect_python_argspecs(remote_methods, method, long_doc,
199+
include_argspec)
200+
201+
return zerorpc_inspect_generic(remote_methods, method, long_doc,
202+
include_argspec)
164203

165204
def run_client(args):
166205
client = zerorpc.Client(timeout=args.timeout, heartbeat=args.heartbeat,

0 commit comments

Comments
 (0)