1 | from django.conf import settings
|
---|
2 |
|
---|
3 | class Dispatcher:
|
---|
4 | """
|
---|
5 | This class encapsulates a dispatcher. This is used to build
|
---|
6 | the real table needed for the RPC method dispatch. An instance
|
---|
7 | of this class is returned by the dispatch and include functions.
|
---|
8 | """
|
---|
9 | def __init__(self, base, module=None, entries=None):
|
---|
10 | self.dispatch = {}
|
---|
11 | if module is None and entries is None:
|
---|
12 | raise ValueError('either module or entries needs to be specified')
|
---|
13 | if entries is None:
|
---|
14 | module = __import__(module, '', '', [''])
|
---|
15 | self.dispatch.update(module.rpccalls.dispatch)
|
---|
16 | else:
|
---|
17 | for el in entries:
|
---|
18 | if type(el) == tuple:
|
---|
19 | (fn, fc) = el
|
---|
20 | if base: fc = '%s.%s' % (base, fc)
|
---|
21 | p = fc.rfind('.')
|
---|
22 | modname = fc[:p]
|
---|
23 | funcname = fc[p+1:]
|
---|
24 | self.dispatch[fn] = getattr(__import__(modname, '', '', ['']), funcname)
|
---|
25 | elif isinstance(el, Dispatcher):
|
---|
26 | self.dispatch.update(el.dispatch)
|
---|
27 |
|
---|
28 | def find(self, function):
|
---|
29 | """
|
---|
30 | This method searches for a matching function definition.
|
---|
31 | """
|
---|
32 | return self.dispatch.get(function, None)
|
---|
33 |
|
---|
34 | def dispatch(base, *entries):
|
---|
35 | return Dispatcher(base, entries=entries)
|
---|
36 |
|
---|
37 | def include(module):
|
---|
38 | return Dispatcher('', module=module)
|
---|
39 |
|
---|
40 | dispatch_table = None
|
---|
41 | def dispatcher():
|
---|
42 | global dispatch_table
|
---|
43 | if dispatch_table is None:
|
---|
44 | dispatch_table = include(settings.ROOT_RPCCONF)
|
---|
45 | return dispatch_table
|
---|
46 |
|
---|
47 | if __name__ == '__main__':
|
---|
48 | print dispatcher().dispatch
|
---|