main
Last change
on this file was
42bd667,
checked in by David Fuertes <dfuertes@…>, 4 years ago
|
Historial Limpio
|
-
Property mode set to
100755
|
File size:
652 bytes
|
Line | |
---|
1 | class Dispatcher(object): |
---|
2 | namespace = "dispatcher" |
---|
3 | |
---|
4 | def __init__(self, namespace=None): |
---|
5 | self._registry_ = {} |
---|
6 | if namespace: |
---|
7 | self.namespace = namespace |
---|
8 | |
---|
9 | def register_for(self, target): |
---|
10 | def wrap(dispatch_class): |
---|
11 | self._registry_[target] = dispatch_class |
---|
12 | return dispatch_class |
---|
13 | |
---|
14 | return wrap |
---|
15 | |
---|
16 | def get_for(self, obj): |
---|
17 | targets = type(obj).__mro__ |
---|
18 | for target in targets: |
---|
19 | if target in self._registry_: |
---|
20 | return self._registry_[target](obj) |
---|
21 | else: |
---|
22 | raise ValueError("no %s found for object: %s" % (self.namespace, obj)) |
---|
Note: See
TracBrowser
for help on using the repository browser.