1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """
21 Simple component interface injection and component repository querying
22 mechanism.
23 """
24
25 import itertools
26 ichain = itertools.chain.from_iterable
27 import logging
28
29 log = logging.getLogger('kenozooid.component')
30
31
32 _registry = {}
33
35 """
36 Class decorator to declare interface implementation.
37
38 Injection parameters can be used to query for classes implementing an
39 interface and having appropriate values.
40
41 :Parameters:
42 iface
43 Interface to inject.
44 params
45 Injection parameters.
46 """
47 def f(cls):
48 log.debug('inject interface %s for class %s with params %s' \
49 % (iface.__name__, cls.__name__, params))
50
51 if iface not in _registry:
52 _registry[iface] = []
53 _registry[iface].append((cls, params))
54
55 return cls
56
57 return f
58
59
61 """
62 Check if values stored in two dictionaries are equal for all keys
63 stored in first dictionary.
64
65 :Parameters:
66 p1
67 First dictionary.
68 p2
69 Second dictionary.
70 """
71 keys = set(p2.keys())
72 return all(k in keys and p1[k] == p2[k] for k in p1.keys())
73
74
75 -def query(iface=None, **params):
87
88
90 """
91 Get interface injection parameters for component realized with
92 specified class.
93
94 :Parameters:
95 cls
96 Class realizing component.
97 """
98 for c, p in ichain(_registry.values()):
99 if c == cls:
100 return p
101
102
103
104