Package gmisclib :: Module g_selector
[frames] | no frames]

Source Code for Module gmisclib.g_selector

  1  import re 
  2  import die 
  3   
  4  """This selects dictionaries (i.e. sets of attribute=value pairs), 
  5  to see if they meet specified criteria. 
  6  """ 
  7   
  8   
9 -class selector:
10 - def accept(self, item):
11 return self.x.accept(item)
12
13 - def __init__(self):
14 self.x = true()
15
16 - def and_another(self, txt):
17 if isinstance(self.x, true): 18 self.x = one_selector(txt) # Leaf node 19 elif isinstance(self.x, false): 20 pass 21 else: 22 self.x = selector_op(self.x, _and, one_selector(txt) )
23 24 select = and_another 25
26 - def or_another(self, txt):
27 if isinstance(self.x, false): 28 self.x = one_selector(txt) # Leaf node 29 elif isinstance(self.x, true): 30 pass 31 else: 32 self.x = selector_op(self.x, _or, one_selector(txt) )
33
34 - def And(self, s):
35 self.x = selector_op(self.x, _and, s)
36
37 - def Or(self, s):
38 self.x = selector_op(self.x, _or, s)
39
40 - def Not(self):
41 self.x = selector_not(self.x)
42
43 - def leaf(self, txt):
44 self.x = one_selector(txt)
45
46 - def filterlist(self, x):
47 o = [] 48 for datum in x: 49 if self.accept(datum): 50 o.append( datum ) 51 return o
52 53
54 -class true(selector):
55 - def __init__(self):
56 pass
57 - def accept(self, item):
58 return True
59
60 -class false(selector):
61 - def __init__(self):
62 pass
63 - def accept(self, item):
64 return False
65 66
67 -def _and(x, l, r):
68 if l.accept(x): 69 return r.accept(x) 70 return False
71 72
73 -def _or(x, l, r):
74 if l.accept(x): 75 return True 76 return r.accept(x)
77 78
79 -class selector_op(selector):
80 - def __init__(self, l, op, r):
81 self.l = l 82 self.r = r 83 self.op = op
84
85 - def accept(self, x):
86 return (self.op)(x, self.l, self.r)
87 88
89 -def selector_not(selector):
90 def __init__(self, l): 91 self.l = l
92 93 def accept(self, x): 94 return not self.l(x) 95 96 97
98 -class one_selector(selector):
99 _eq = re.compile('^(.*)[.]eq[.](.*)$') 100 _eqn = re.compile('^(.*)==(.*)$') 101 _ne = re.compile('^(.*)[.]ne[.](.*)$') 102 _nen = re.compile('^(.*)!=(.*)$') 103 _ew = re.compile('^(.*)[.]endswith[.](.*)$') 104 _sw = re.compile('^(.*)[.]startswith[.](.*)$') 105 _ex = re.compile('^(.*)[.]exists[.]?$')
106 - def __init__(self, txt):
107 if self._eq.match(txt): 108 m = self._eq.match(txt) 109 self.op = lambda t, k=m.group(1), v=m.group(2): t[k]==v 110 elif self._ne.match(txt): 111 m = self._ne.match(txt) 112 self.op = lambda t, k=m.group(1), v=m.group(2): t[k]!=v 113 elif self._eqn.match(txt): 114 m = self._eqn.match(txt) 115 self.op = lambda t, k=m.group(1), v=float(m.group(2)): float(t[k])==float(v) 116 elif self._nen.match(txt): 117 m = self._nen.match(txt) 118 self.op = lambda t, k=m.group(1), v=float(m.group(2)): float(t[k])!=float(v) 119 elif self._ew.match(txt): 120 m = self._ew.match(txt) 121 self.op = lambda t, k=m.group(1), v=m.group(2): t[k].endswith(v) 122 elif self._sw.match(txt): 123 m = self._sw.match(txt) 124 self.op = lambda t, k=m.group(1), v=m.group(2): t[k].startswith(v) 125 elif self._ex.match(txt): 126 m = self._ex.match(txt) 127 self.op = lambda t, k=m.group(1): t.has_key(k) 128 else: 129 die.die("Unrecognized operator: %s" % txt)
130
131 - def accept(self, item):
132 return (self.op)(item)
133 134 135
136 -def test():
137 s = selector() 138 s.select('a.eq.3') 139 assert s.accept({'a': '3'}) 140 assert not s.accept({'a': '4'}) 141 s = selector() 142 s.select('a.eq.3') 143 s.select('b.ne.5') 144 assert s.accept({'a': '3', 'b': '4'}) 145 assert s.accept({'a': '3', 'b': 'k'}) 146 assert not s.accept({'a': '3', 'b': '5'}) 147 assert not s.accept({'a': '2', 'b': '6'}) 148 assert not s.accept({'a': '2', 'b': '5'}) 149 s.or_another('a.eq.2') 150 assert s.accept({'a': '2', 'b': '5'}) 151 s = selector() 152 s.select('a.endswith.2') 153 assert s.accept({'a': '2342'})
154 155 156 157 if __name__ == '__main__': 158 test() 159