Package gofer :: Package rmi :: Module criteria
[hide private]
[frames] | no frames]

Source Code for Module gofer.rmi.criteria

  1  # Copyright (c) 2012 Red Hat, Inc. 
  2  # 
  3  # This software is licensed to you under the GNU General Public 
  4  # License as published by the Free Software Foundation; either version 
  5  # 2 of the License (GPLv2) or (at your option) any later version. 
  6  # There is NO WARRANTY for this software, express or implied, 
  7  # including the implied warranties of MERCHANTABILITY, 
  8  # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should 
  9  # have received a copy of GPLv2 along with this software; if not, see 
 10  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 11   
 12   
13 -class InvalidOperator(Exception):
14 pass
15 16
17 -class Criteria:
18 """ 19 The criteria used to match on an RMI locator. 20 """ 21
22 - def __init__(self, criteria):
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
40 -class Equal(Criteria):
41
42 - def match(self, locator):
43 return locator == self.criteria
44 45
46 -class NotEqual(Criteria):
47
48 - def match(self, locator):
49 return locator != self.criteria
50 51
52 -class Greater(Criteria):
53
54 - def match(self, locator):
55 return locator > self.criteria
56 57
58 -class Less(Criteria):
59
60 - def match(self, locator):
61 return locator < self.criteria
62 63
64 -class In(Criteria):
65
66 - def match(self, locator):
67 return locator in self.criteria
68 69
70 -class And(Criteria):
71
72 - def match(self, locator):
73 left, right = self.criteria 74 return left.match(locator) and right.match(locator)
75 76
77 -class Or(Criteria):
78
79 - def match(self, locator):
80 left, right = self.criteria 81 return left.match(locator) or right.match(locator)
82 83
84 -class Builder:
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
127 - def _resolve(self, object):
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
138 - def _criteria(self, object):
139 if isinstance(object, dict): 140 for k in self.METHODS: 141 if k in object: 142 return True 143 return False
144