1 from types import StringTypes
9 import datetime # Jordan
10 #from manifold.util.parameter import Parameter, Mixed, python_type
11 from manifold.util.predicate import Predicate, eq
12 from itertools import ifilter
16 A filter is a set of predicates
24 f.add(Predicate(*element))
26 print "Error in setting Filter from list", e
33 for key, value in d.items():
34 if key[0] in Predicate.operators.keys():
35 f.add(Predicate(key[1:], key[0], value))
37 f.add(Predicate(key, '=', value))
40 def filter_by(self, predicate):
45 return '<Filter: %s>' % ' AND '.join([str(pred) for pred in self])
51 return tuple([hash(pred) for pred in self])
54 return hash(self.__key())
56 def __additem__(self, value):
57 if value.__class__ != Predicate:
58 raise TypeError("Element of class Predicate expected, received %s" % value.__class__.__name__)
59 set.__additem__(self, value)
62 return set([x.key for x in self])
70 def has_op(self, key, op):
72 if x.key == key and x.op == op:
76 def has_eq(self, key):
77 return self.has_op(key, eq)
86 def delete(self, key):
94 #self = filter(lambda x: x.key != key, self)
96 def get_op(self, key, op):
98 if x.key == key and x.op == op:
102 def get_eq(self, key):
103 return self.get_op(key, eq)
105 def set_op(self, key, op, value):
107 if x.key == key and x.op == op:
112 def set_eq(self, key, value):
113 return self.set_op(key, eq, value)
115 def get_predicates(self, key):
116 # XXX Would deserve returning a filter (cf usage in SFA gateway)
123 # def filter(self, dic):
124 # # We go through every filter sequentially
125 # for predicate in self:
126 # print "predicate", predicate
127 # dic = predicate.filter(dic)
130 def match(self, dic):
131 for predicate in self:
132 if not predicate.match(dic, ignore_missing=True):
144 return [list(pred.get_str_tuple()) for pred in self]
146 #class OldFilter(Parameter, dict):
148 # A type of parameter that represents a filter on one or more
149 # columns of a database table.
150 # Special features provide support for negation, upper and lower bounds,
151 # as well as sorting and clipping.
154 # fields should be a dictionary of field names and types.
155 # As of PLCAPI-4.3-26, we provide support for filtering on
156 # sequence types as well, with the special '&' and '|' modifiers.
157 # example : fields = {'node_id': Parameter(int, "Node identifier"),
158 # 'hostname': Parameter(int, "Fully qualified hostname", max = 255),
162 # filter should be a dictionary of field names and values
163 # representing the criteria for filtering.
164 # example : filter = { 'hostname' : '*.edu' , site_id : [34,54] }
165 # Whether the filter represents an intersection (AND) or a union (OR)
166 # of these criteria is determined by the join_with argument
167 # provided to the sql method below
171 # * a field starting with '&' or '|' should refer to a sequence type
172 # the semantic is then that the object value (expected to be a list)
173 # should contain all (&) or any (|) value specified in the corresponding
174 # filter value. See other examples below.
175 # example : filter = { '|role_ids' : [ 20, 40 ] }
176 # example : filter = { '|roles' : ['tech', 'pi'] }
177 # example : filter = { '&roles' : ['admin', 'tech'] }
178 # example : filter = { '&roles' : 'tech' }
180 # * a field starting with the ~ character means negation.
181 # example : filter = { '~peer_id' : None }
183 # * a field starting with < [ ] or > means lower than or greater than
184 # < > uses strict comparison
185 # [ ] is for using <= or >= instead
186 # example : filter = { ']event_id' : 2305 }
187 # example : filter = { '>time' : 1178531418 }
188 # in this example the integer value denotes a unix timestamp
190 # * if a value is a sequence type, then it should represent
191 # a list of possible values for that field
192 # example : filter = { 'node_id' : [12,34,56] }
194 # * a (string) value containing either a * or a % character is
195 # treated as a (sql) pattern; * are replaced with % that is the
196 # SQL wildcard character.
197 # example : filter = { 'hostname' : '*.jp' }
199 # * the filter's keys starting with '-' are special and relate to sorting and clipping
200 # * '-SORT' : a field name, or an ordered list of field names that are used for sorting
201 # these fields may start with + (default) or - for denoting increasing or decreasing order
202 # example : filter = { '-SORT' : [ '+node_id', '-hostname' ] }
203 # * '-OFFSET' : the number of first rows to be ommitted
204 # * '-LIMIT' : the amount of rows to be returned
205 # example : filter = { '-OFFSET' : 100, '-LIMIT':25}
207 # Here are a few realistic examples
209 # GetNodes ( { 'node_type' : 'regular' , 'hostname' : '*.edu' , '-SORT' : 'hostname' , '-OFFSET' : 30 , '-LIMIT' : 25 } )
210 # would return regular (usual) nodes matching '*.edu' in alphabetical order from 31th to 55th
212 # GetPersons ( { '|role_ids' : [ 20 , 40] } )
213 # would return all persons that have either pi (20) or tech (40) roles
215 # GetPersons ( { '&role_ids' : 10 } )
216 # GetPersons ( { '&role_ids' : 10 } )
217 # GetPersons ( { '|role_ids' : [ 10 ] } )
218 # GetPersons ( { '|role_ids' : [ 10 ] } )
219 # all 4 forms are equivalent and would return all admin users in the system
222 # def __init__(self, fields = {}, filter = {}, doc = "Attribute filter"):
223 # # Store the filter in our dict instance
224 # dict.__init__(self, filter)
226 # # Declare ourselves as a type of parameter that can take
227 # # either a value or a list of values for each of the specified
229 # self.fields = dict ( [ ( field, Mixed (expected, [expected]))
230 # for (field,expected) in fields.iteritems() ] )
232 # # Null filter means no filter
233 # Parameter.__init__(self, self.fields, doc = doc, nullok = True)
235 # def sql(self, api, join_with = "AND"):
237 # Returns a SQL conditional that represents this filter.
240 # # So that we always return something
241 # if join_with == "AND":
242 # conditionals = ["True"]
243 # elif join_with == "OR":
244 # conditionals = ["False"]
246 # assert join_with in ("AND", "OR")
252 # for field, value in self.iteritems():
253 # # handle negation, numeric comparisons
254 # # simple, 1-depth only mechanism
256 # modifiers={'~' : False,
257 # '<' : False, '>' : False,
258 # '[' : False, ']' : False,
260 # '&' : False, '|' : False,
263 # def check_modifiers(field):
264 # if field[0] in modifiers.keys():
265 # modifiers[field[0]] = True
267 # return check_modifiers(field)
269 # field = check_modifiers(field)
272 # if not modifiers['-']:
273 # if field not in self.fields:
274 # raise PLCInvalidArgument, "Invalid filter field '%s'" % field
276 # # handling array fileds always as compound values
277 # if modifiers['&'] or modifiers['|']:
278 # if not isinstance(value, (list, tuple, set)):
281 # if isinstance(value, (list, tuple, set)):
282 # # handling filters like '~slice_id':[]
283 # # this should return true, as it's the opposite of 'slice_id':[] which is false
284 # # prior to this fix, 'slice_id':[] would have returned ``slice_id IN (NULL) '' which is unknown
285 # # so it worked by coincidence, but the negation '~slice_ids':[] would return false too
287 # if modifiers['&'] or modifiers['|']:
295 # value = map(str, map(api.db.quote, value))
298 # value = "ARRAY[%s]" % ", ".join(value)
299 # elif modifiers['|']:
301 # value = "ARRAY[%s]" % ", ".join(value)
304 # value = "(%s)" % ", ".join(value)
309 # elif isinstance(value, StringTypes) and \
310 # (value.find("*") > -1 or value.find("%") > -1):
312 # # insert *** in pattern instead of either * or %
313 # # we dont use % as requests are likely to %-expansion later on
314 # # actual replacement to % done in PostgreSQL.py
315 # value = value.replace ('*','***')
316 # value = value.replace ('%','***')
317 # value = str(api.db.quote(value))
329 # # value = str(api.db.quote(value))
331 # if isinstance(value, StringTypes) and value[-2:] != "()": # XXX
332 # value = str(api.db.quote(value))
333 # if isinstance(value, datetime.datetime):
334 # value = str(api.db.quote(str(value)))
337 # # field = "%s.%s" % (prefix,field)
339 # clause = "\"%s\" %s %s" % (field, operator, value)
341 # clause = "%s %s %s" % (field, operator, value)
344 # clause = " ( NOT %s ) " % (clause)
346 # conditionals.append(clause)
347 # # sorting and clipping
349 # if field not in ('SORT','OFFSET','LIMIT'):
350 # raise PLCInvalidArgument, "Invalid filter, unknown sort and clip field %r"%field
352 # if field == 'SORT':
353 # if not isinstance(value,(list,tuple,set)):
355 # for field in value:
357 # if field[0] == '+':
359 # elif field[0] == '-':
362 # if field not in self.fields:
363 # raise PLCInvalidArgument, "Invalid field %r in SORT filter"%field
364 # sorts.append("%s %s"%(field,order))
366 # elif field == 'OFFSET':
367 # clips.append("OFFSET %d"%value)
368 # # clipping continued
369 # elif field == 'LIMIT' :
370 # clips.append("LIMIT %d"%value)
372 # where_part = (" %s " % join_with).join(conditionals)
375 # clip_part += " ORDER BY " + ",".join(sorts)
377 # clip_part += " " + " ".join(clips)
378 ## print 'where_part=',where_part,'clip_part',clip_part
379 # return (where_part,clip_part)