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
52
53
54 -class true(selector):
59
65
66
71
72
77
78
81 self.l = l
82 self.r = r
83 self.op = op
84
86 return (self.op)(x, self.l, self.r)
87
88
90 def __init__(self, l):
91 self.l = l
92
93 def accept(self, x):
94 return not self.l(x)
95
96
97
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[.]?$')
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
132 return (self.op)(item)
133
134
135
154
155
156
157 if __name__ == '__main__':
158 test()
159