Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CLI handle _zerorpc_inspect generic format

  • Loading branch information...
commit fcaa2aebe21007b026fb5b6956dafdfc89f5dccf 1 parent 88ea7f1
François-Xavier Bourlet bombela authored
Showing with 59 additions and 20 deletions.
  1. +59 −20 bin/zerorpc
79 bin/zerorpc
View
@@ -116,28 +116,11 @@ def run_server(args):
# this function does a really intricate job to keep backward compatibility
# with a previous version of zerorpc, and lazily retrieving results if possible
-def zerorpc_inspect(client, method=None, long_doc=True, include_argspec=True):
- try:
- remote_detailled_methods = client._zerorpc_inspect(method,
- long_doc)['methods']
-
- if include_argspec:
- r = [(name + (inspect.formatargspec(*argspec) if argspec else
- '(...)'), doc if doc else '<undocumented>')
- for name, argspec, doc in remote_detailled_methods]
- else:
- r = [(name, doc if doc else '<undocumented>')
- for name, argspec, doc in remote_detailled_methods]
-
- longest_name_len = max(len(name) for name, doc in r)
- return (longest_name_len, r)
- except (zerorpc.RemoteError, NameError):
- pass
-
- if method is None:
+def zerorpc_inspect_legacy(client, filter_method, long_doc, include_argspec):
+ if filter_method is None:
remote_methods = client._zerorpc_list()
else:
- remote_methods = [method]
+ remote_methods = [filter_method]
def remote_detailled_methods():
for name in remote_methods:
@@ -161,6 +144,62 @@ def zerorpc_inspect(client, method=None, long_doc=True, include_argspec=True):
longest_name_len = max(len(name) for name, doc in r)
return (longest_name_len, r)
+# handle the 'python formatted' _zerorpc_inspect, that return the output of
+# "getargspec" from the python lib "inspect".
+def zerorpc_inspect_python_argspecs(remote_methods, filter_method, long_doc, include_argspec):
+ def format_method(name, argspec, doc):
+ if include_argspec:
+ name += (inspect.formatargspec(*argspec) if argspec else
+ '(...)')
+ if not doc:
+ doc = '<undocumented>'
+ elif not long_doc:
+ doc = doc.splitlines()[0]
+ return (name, doc)
+ r = [format_method(*methods_info) for methods_info in remote_methods if
+ filter_method is None or methods_info[0] == filter_method]
+ longest_name_len = max(len(name) for name, doc in r)
+ return (longest_name_len, r)
+
+def zerorpc_inspect_generic(remote_methods, filter_method, long_doc, include_argspec):
+ def format_method(name, args, doc):
+ if include_argspec:
+ def format_arg(arg):
+ def_val = arg.get('default')
+ if def_val is None:
+ return arg['name']
+ return '{0}={1}'.format(arg['name'], def_val)
+
+ name += '({0})'.format(', '.join(map(format_arg, args)))
+ if not doc:
+ doc = '<undocumented>'
+ elif not long_doc:
+ doc = doc.splitlines()[0]
+ return (name, doc)
+
+ methods = [format_method(name, details['args'], details['doc']) for name, details in remote_methods.items()
+ if filter_method is None or name == filter_method]
+
+ longest_name_len = max(len(name) for name, doc in methods)
+ return (longest_name_len, methods)
+
+def zerorpc_inspect(client, method=None, long_doc=True, include_argspec=True):
+ try:
+ remote_methods = client._zerorpc_inspect()['methods']
+ legacy = False
+ except (zerorpc.RemoteError, NameError):
+ legacy = True
+
+ if legacy:
+ return zerorpc_inspect_legacy(client, method,
+ long_doc, include_argspec)
+
+ if not isinstance(remote_methods, dict):
+ return zerorpc_inspect_python_argspecs(remote_methods, method, long_doc,
+ include_argspec)
+
+ return zerorpc_inspect_generic(remote_methods, method, long_doc,
+ include_argspec)
def run_client(args):
client = zerorpc.Client(timeout=args.timeout, heartbeat=args.heartbeat,
Please sign in to comment.
Something went wrong with that request. Please try again.