enhance select command, add condition command
This commit is contained in:
parent
65bcd201e0
commit
13a1f2a8a5
2 changed files with 67 additions and 25 deletions
|
|
@ -68,27 +68,27 @@ class Results(object):
|
|||
|
||||
|
||||
class ResultsCondition(object):
|
||||
condition_str = None
|
||||
|
||||
def __init__(self, condition_str):
|
||||
condition_str = condition_str.replace('OR', 'or') \
|
||||
.replace('AND', 'and') \
|
||||
.replace('NOT', 'not')
|
||||
or_list = []
|
||||
try:
|
||||
for _or in condition_str.split('or'):
|
||||
and_dict = {}
|
||||
for _and in _or.split('and'):
|
||||
if '!=' in _and:
|
||||
k, v = _and.split('!=')
|
||||
k += '!'
|
||||
elif '=' in _and:
|
||||
k, v = _and.split('=')
|
||||
else:
|
||||
raise ResultsConditionException(u'Could not find = or != operator in sub-expression "%s"' % _and)
|
||||
and_dict[k] = v
|
||||
or_list.append(and_dict)
|
||||
except:
|
||||
raise ResultsConditionException(u'Could not parse condition expression "%s"' % condition_str)
|
||||
for _or in condition_str.split('or'):
|
||||
and_dict = {}
|
||||
for _and in _or.split('and'):
|
||||
if '!=' in _and:
|
||||
k, v = _and.split('!=')
|
||||
k += '!'
|
||||
elif '=' in _and:
|
||||
k, v = _and.split('=')
|
||||
else:
|
||||
raise ResultsConditionException(u'Could not find = or != operator in sub-expression "%s"' % _and)
|
||||
and_dict[k.strip()] = v.strip()
|
||||
or_list.append(and_dict)
|
||||
self.condition = or_list
|
||||
self.condition_str = condition_str
|
||||
|
||||
def is_valid(self, d):
|
||||
for _or in self.condition:
|
||||
|
|
@ -109,6 +109,12 @@ class ResultsCondition(object):
|
|||
raise ResultsConditionException(u'Field "%s" is not valid.' % k)
|
||||
return True
|
||||
|
||||
def __str__(self):
|
||||
return unicode(self).encode('utf-8')
|
||||
|
||||
def __unicode__(self):
|
||||
return self.condition_str
|
||||
|
||||
|
||||
class ResultsConditionException(Exception):
|
||||
pass
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue