1
2
3
4
5
6
7
8
9
10
11
12
15
16
18 """
19 The criteria used to match on an RMI locator.
20 """
21
23 """
24 @param criteria: The data used for matching.
25 @type criteria: object
26 """
27 self.criteria = criteria
28
29 - def match(self, locator):
30 """
31 Match on the specified criteria.
32 @param locator: RMI request locator information.
33 @type locator: object
34 @return: True on match.
35 @rtype: bool
36 """
37 raise NotImplementedError()
38
39
41
42 - def match(self, locator):
44
45
47
48 - def match(self, locator):
50
51
53
54 - def match(self, locator):
56
57
58 -class Less(Criteria):
59
60 - def match(self, locator):
62
63
65
66 - def match(self, locator):
68
69
71
72 - def match(self, locator):
75
76
78
79 - def match(self, locator):
82
83
85 """
86 Build a criteria object graph based on dictionary representations.
87 These representations can be nested.
88 Examples:
89 {'eq':10}
90 {'neq':10}
91 {'in':[1,2]}
92 {'gt':10}
93 {'lt':10}
94 {'and':({'gt':1},{'lt':10})}
95 {'or':({'eq':10},{'in':[1,2]})}
96 {'or':({'eq':10},{'or':({'eq':1},{'eq':2})}
97 """
98
99 METHODS = {
100 'eq':Equal,
101 'neq':NotEqual,
102 'in':In,
103 'gt':Greater,
104 'lt':Less,
105 'and':And,
106 'or':Or,
107 }
108
109 - def build(self, criteria):
110 """
111 Build a L{Criteria} object based on the specified
112 dict representation.
113 @param criteria: The criteria to build.
114 @type criteria: dict
115 @rtype: L{Criteria}
116 @raise Exception, on invalid criteria.
117 """
118 for k, v in criteria.items():
119 if self._criteria(v):
120 v = self._resolve(v)
121 m = self.METHODS.get(k)
122 if m:
123 return m(self._resolve(v))
124 else:
125 raise InvalidOperator, '%s not supported' % k
126
128 if self._criteria(object):
129 return self.build(object)
130 if isinstance(object, (list,tuple)) and len(object) == 2:
131 left, right = object
132 if self._criteria(left) and self._criteria(right):
133 left = self.build(left)
134 right = self.build(right)
135 return (left, right)
136 return object
137
139 if isinstance(object, dict):
140 for k in self.METHODS:
141 if k in object:
142 return True
143 return False
144