@@ -50,10 +50,10 @@ def __init__(self, channel, methods=None, name=None, context=None,
50
50
methods = self
51
51
52
52
self ._context = context or Context .get_instance ()
53
- self ._name = name or repr ( methods )
53
+ self ._name = name or self . _extract_name ( )
54
54
self ._task_pool = gevent .pool .Pool (size = pool_size )
55
55
self ._acceptor_task = None
56
- self ._methods = self ._zerorpc_filter_methods (ServerBase , self , methods )
56
+ self ._methods = self ._filter_methods (ServerBase , self , methods )
57
57
58
58
self ._inject_builtins ()
59
59
self ._heartbeat_freq = heartbeat
@@ -63,7 +63,7 @@ def __init__(self, channel, methods=None, name=None, context=None,
63
63
self ._methods [k ] = rep (functor )
64
64
65
65
@staticmethod
66
- def _zerorpc_filter_methods (cls , self , methods ):
66
+ def _filter_methods (cls , self , methods ):
67
67
if hasattr (methods , '__getitem__' ):
68
68
return methods
69
69
server_methods = set (getattr (self , k ) for k in dir (cls ) if not
@@ -75,20 +75,29 @@ def _zerorpc_filter_methods(cls, self, methods):
75
75
and getattr (methods , k ) not in server_methods
76
76
)
77
77
78
+ @staticmethod
79
+ def _extract_name (methods ):
80
+ return getattr (type (methods ), '__name__' , None ) or repr (methods )
81
+
78
82
def close (self ):
79
83
self .stop ()
80
84
self ._multiplexer .close ()
81
85
82
- def _zerorpc_inspect (self , method = None , long_doc = True ):
83
- if method :
84
- methods = {method : self ._methods [method ]}
85
- else :
86
- methods = dict ((m , f ) for m , f in self ._methods .items ()
86
+
87
+ def _format_args_spec (self , args_spec ):
88
+ r = [dict (name = name ) for name in args_spec [0 ]]
89
+ default_values = args_spec [3 ]
90
+ if default_values is not None :
91
+ for arg , def_val in zip (reversed (r ), reversed (default_values )):
92
+ arg ['default' ] = def_val
93
+ return r
94
+
95
+ def _zerorpc_inspect (self ):
96
+ methods = dict ((m , f ) for m , f in self ._methods .items ()
87
97
if not m .startswith ('_' ))
88
- detailled_methods = [(m , f ._zerorpc_args (),
89
- f .__doc__ if long_doc else
90
- f .__doc__ .split ('\n ' , 1 )[0 ] if f .__doc__ else None )
91
- for (m , f ) in methods .items ()]
98
+ detailled_methods = dict ((m ,
99
+ dict (args = self ._format_args_spec (f ._zerorpc_args ()),
100
+ doc = f ._zerorpc_doc ())) for (m , f ) in methods .items ())
92
101
return {'name' : self ._name ,
93
102
'methods' : detailled_methods }
94
103
@@ -97,7 +106,8 @@ def _inject_builtins(self):
97
106
if not m .startswith ('_' )]
98
107
self ._methods ['_zerorpc_name' ] = lambda : self ._name
99
108
self ._methods ['_zerorpc_ping' ] = lambda : ['pong' , self ._name ]
100
- self ._methods ['_zerorpc_help' ] = lambda m : self ._methods [m ].__doc__
109
+ self ._methods ['_zerorpc_help' ] = lambda m : \
110
+ self ._methods [m ]._zerorpc_doc ()
101
111
self ._methods ['_zerorpc_args' ] = \
102
112
lambda m : self ._methods [m ]._zerorpc_args ()
103
113
self ._methods ['_zerorpc_inspect' ] = self ._zerorpc_inspect
@@ -244,9 +254,12 @@ class Server(SocketBase, ServerBase):
244
254
def __init__ (self , methods = None , name = None , context = None , pool_size = None ,
245
255
heartbeat = 5 ):
246
256
SocketBase .__init__ (self , zmq .XREP , context )
257
+
247
258
if methods is None :
248
259
methods = self
249
- methods = ServerBase ._zerorpc_filter_methods (Server , self , methods )
260
+
261
+ name = name or ServerBase ._extract_name (methods )
262
+ methods = ServerBase ._filter_methods (Server , self , methods )
250
263
ServerBase .__init__ (self , self ._events , methods , name , context ,
251
264
pool_size , heartbeat )
252
265
@@ -291,7 +304,7 @@ def __init__(self, methods=None, context=None, zmq_socket=zmq.PULL):
291
304
if methods is None :
292
305
methods = self
293
306
294
- self ._methods = ServerBase ._zerorpc_filter_methods (Puller , self , methods )
307
+ self ._methods = ServerBase ._filter_methods (Puller , self , methods )
295
308
self ._receiver_task = None
296
309
297
310
def close (self ):
0 commit comments