layer filter
import redef filter_record(filt, record):
if isinstance(filt, dict):
if 'and' in filt:
res = True
for sfilt in filt['and']:
res = res and filter_record(sfilt, record)
elif 'or' in filt:
res = False
for sfilt in filt['or']:
res = res or filter_record(sfilt, record)
elif isinstance(filt, list):
res = filter_single(filt, record)
return resdef filter_single(filt, record):
key, comp, val = filt
prop = record[key]
comp = comp.lower().split(' ')
if 'in' in comp:
res = prop in val
elif 'like' in comp:
res = re.search('^' + _escape_regex(val).replace('%', '.*') + '$', prop) is not None
elif 'matches' in comp:
res = re.search(val, prop) is not None
elif 'is' in comp or '=' in comp:
res = prop == val
elif 'greater' in comp or ('>' in comp):
res = prop > val
elif 'less' in comp or '<' in comp:
res = prop < val
if 'not' in comp:
return not res
else:
return resdef _escape_regex(s):
chars = ('.', '*', '?', '+', '(', ')', '[', ']', '-')
for c in chars:
s = s.replace(c, '\\' + c)
return s