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

Skip to content

Commit fdac478

Browse files
committed
cli remote inspection: handle empty results better
1 parent e3b3ebb commit fdac478

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

zerorpc/cli.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ def remote_detailled_methods():
147147
r = [(name + (inspect.formatargspec(*argspec)
148148
if argspec else '(...)'), doc)
149149
for name, argspec, doc in remote_detailled_methods()]
150-
longest_name_len = max(len(name) for name, doc in r)
150+
longest_name_len = max(len(name) for name, doc in r) if r else 0
151151
return (longest_name_len, r)
152152

153153

154154
# handle the 'python formatted' _zerorpc_inspect, that return the output of
155-
# "getargspec" from the python lib "inspect".
155+
# "getargspec" from the python lib "inspect". A monstruosity from protocol v2.
156156
def zerorpc_inspect_python_argspecs(remote_methods, filter_method, long_doc, include_argspec):
157157
def format_method(name, argspec, doc):
158158
if include_argspec:
@@ -165,10 +165,13 @@ def format_method(name, argspec, doc):
165165
return (name, doc)
166166
r = [format_method(*methods_info) for methods_info in remote_methods if
167167
filter_method is None or methods_info[0] == filter_method]
168-
longest_name_len = max(len(name) for name, doc in r)
168+
if not r:
169+
return None
170+
longest_name_len = max(len(name) for name, doc in r) if r else 0
169171
return (longest_name_len, r)
170172

171173

174+
# Handles generically formatted arguments (not tied to any specific programming language).
172175
def zerorpc_inspect_generic(remote_methods, filter_method, long_doc, include_argspec):
173176
def format_method(name, args, doc):
174177
if include_argspec:
@@ -178,37 +181,53 @@ def format_arg(arg):
178181
return arg['name']
179182
return '{0}={1}'.format(arg['name'], def_val)
180183

181-
name += '({0})'.format(', '.join(map(format_arg, args)))
184+
if args:
185+
name += '({0})'.format(', '.join(map(format_arg, args)))
186+
else:
187+
name += '(??)'
188+
182189
if not doc:
183190
doc = '<undocumented>'
184191
elif not long_doc:
185192
doc = doc.splitlines()[0]
186193
return (name, doc)
187194

188-
methods = [format_method(name, details['args'], details['doc']) for name, details in remote_methods.items()
195+
methods = [format_method(name, details['args'], details['doc'])
196+
for name, details in remote_methods.items()
189197
if filter_method is None or name == filter_method]
190198

191-
longest_name_len = max(len(name) for name, doc in methods)
199+
longest_name_len = (max(len(name) for name, doc in methods)
200+
if methods else 0)
192201
return (longest_name_len, methods)
193202

194203

195204
def zerorpc_inspect(client, method=None, long_doc=True, include_argspec=True):
196205
try:
197-
remote_methods = client._zerorpc_inspect()['methods']
206+
inspect_result = client._zerorpc_inspect()
207+
remote_methods = inspect_result['methods']
198208
legacy = False
199209
except (zerorpc.RemoteError, NameError):
200210
legacy = True
201211

202212
if legacy:
203-
return zerorpc_inspect_legacy(client, method,
204-
long_doc, include_argspec)
213+
try:
214+
service_name = client._zerorpc_name()
215+
except (zerorpc.RemoteError):
216+
service_name = 'N/A'
205217

206-
if not isinstance(remote_methods, dict):
207-
return zerorpc_inspect_python_argspecs(remote_methods, method, long_doc,
208-
include_argspec)
218+
(longest_name_len, detailled_methods) = zerorpc_inspect_legacy(client,
219+
method, long_doc, include_argspec)
220+
else:
221+
service_name = inspect_result.get('name', 'N/A')
222+
if not isinstance(remote_methods, dict):
223+
(longest_name_len,
224+
detailled_methods) = zerorpc_inspect_python_argspecs(
225+
remote_methods, method, long_doc, include_argspec)
209226

210-
return zerorpc_inspect_generic(remote_methods, method, long_doc,
211-
include_argspec)
227+
(longest_name_len, detailled_methods) = zerorpc_inspect_generic(
228+
remote_methods, method, long_doc, include_argspec)
229+
230+
return longest_name_len, detailled_methods, service_name
212231

213232

214233
def run_client(args):
@@ -218,8 +237,9 @@ def run_client(args):
218237
client.debug = True
219238
setup_links(args, client)
220239
if not args.command:
221-
(longest_name_len, detailled_methods) = zerorpc_inspect(client,
240+
(longest_name_len, detailled_methods, service) = zerorpc_inspect(client,
222241
long_doc=False, include_argspec=args.inspect)
242+
print '[{0}]'.format(service)
223243
if args.inspect:
224244
for (name, doc) in detailled_methods:
225245
print name
@@ -228,10 +248,13 @@ def run_client(args):
228248
print '{0} {1}'.format(name.ljust(longest_name_len), doc)
229249
return
230250
if args.inspect:
231-
(longest_name_len, detailled_methods) = zerorpc_inspect(client,
251+
(longest_name_len, detailled_methods, service) = zerorpc_inspect(client,
232252
method=args.command)
233-
(name, doc) = detailled_methods[0]
234-
print '\n{0}\n\n{1}\n'.format(name, doc)
253+
if detailled_methods:
254+
(name, doc) = detailled_methods[0]
255+
print '[{0}]\n{1}\n\n{2}\n'.format(service, name, doc)
256+
else:
257+
print '[{0}]\nNo documentation for "{1}".'.format(service, args.command)
235258
return
236259
if args.json:
237260
call_args = [json.loads(x) for x in args.params]

0 commit comments

Comments
 (0)