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

Skip to content

Commit 1ba3781

Browse files
committed
Wilcard support for zerorpc.Server.
This feature is totally useless. This commit will never be released.
1 parent 6174228 commit 1ba3781

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

zerorpc/core.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,34 @@ def __init__(self, channel, methods=None, name=None, context=None,
6464

6565
@staticmethod
6666
def _zerorpc_filter_methods(cls, self, methods):
67-
if hasattr(methods, '__getitem__'):
67+
getattr_ = getattr(methods, '__getattr__', None)
68+
if getattr_ is None and hasattr(methods, '__getitem__'):
6869
return methods
6970
server_methods = set(getattr(self, k) for k in dir(cls) if not
7071
k.startswith('_'))
71-
return dict((k, getattr(methods, k))
72+
r = dict((k, getattr(methods, k))
7273
for k in dir(methods)
7374
if callable(getattr(methods, k))
7475
and not k.startswith('_')
7576
and getattr(methods, k) not in server_methods
7677
)
78+
if getattr_ is not None:
79+
def functor(method, *args, **kargs):
80+
'''Dynamically resolve methods
81+
82+
Some methods are dynamically resolved and so can't be infererd.
83+
'''
84+
return getattr_(method)(*args, **kargs)
85+
r['*'] = functor
86+
return r
7787

7888
def close(self):
7989
self.stop()
8090
self._multiplexer.close()
8191

8292
def _zerorpc_inspect(self, method=None, long_doc=True):
8393
if method:
84-
methods = {method: self._methods[method]}
94+
methods = {method: self._get_method(method)}
8595
else:
8696
methods = dict((m, f) for m, f in self._methods.items()
8797
if not m.startswith('_'))
@@ -97,15 +107,26 @@ def _inject_builtins(self):
97107
if not m.startswith('_')]
98108
self._methods['_zerorpc_name'] = lambda: self._name
99109
self._methods['_zerorpc_ping'] = lambda: ['pong', self._name]
100-
self._methods['_zerorpc_help'] = lambda m: self._methods[m].__doc__
110+
self._methods['_zerorpc_help'] = lambda m: self._get_method(m).__doc__
101111
self._methods['_zerorpc_args'] = \
102-
lambda m: self._methods[m]._zerorpc_args()
112+
lambda m: self._get_method(m)._zerorpc_args()
103113
self._methods['_zerorpc_inspect'] = self._zerorpc_inspect
104114

105-
def __call__(self, method, *args):
106-
if method not in self._methods:
115+
def _get_method(self, method):
116+
f = self._methods.get(method, None)
117+
if f is None:
118+
wilcard = self._methods.get('*', None)
119+
if wilcard is not None:
120+
def f(*args, **kargs):
121+
return wilcard(method, *args, **kargs)
122+
if not isinstance(f, DecoratorBase):
123+
f = rep(f)
124+
if f is None:
107125
raise NameError(method)
108-
return self._methods[method](*args)
126+
return f
127+
128+
def __call__(self, method, *args):
129+
return self._get_method(method)(*args)
109130

110131
def _print_traceback(self, protocol_v1):
111132
exc_type, exc_value, exc_traceback = sys.exc_info()
@@ -132,9 +153,8 @@ def _async_task(self, initial_event):
132153
event = bufchan.recv()
133154
try:
134155
self._context.middleware_load_task_context(event.header)
135-
functor = self._methods.get(event.name, None)
136-
if functor is None:
137-
raise NameError(event.name)
156+
functor = self._get_method(event.name)
157+
print type(functor), functor
138158
functor.pattern.process_call(self._context, bufchan, event, functor)
139159
except LostRemote:
140160
self._print_traceback(protocol_v1)

0 commit comments

Comments
 (0)