2 This library provides a bunch of miscellaneous parameter parsing,
3 sql generating and list cleanup library functions that are used
4 by both the reporting cli and web interface.
9 tko
= os
.path
.dirname(__file__
)
10 sys
.path
.insert(0, tko
)
12 import display
, frontend
, db
18 #print "! %s<br>" % str
20 def parse_scrub_and_gen_condition(condition
, valid_field_dict
):
21 me
= parse_scrub_and_gen_condition
# shorten the name
22 compare_ops
= {'=':'=', '<>':'<>', '==':'=', '!=':'<>', '>':'>',
23 '<':'<', '>=':'>=', '<=':'<=', '~':'LIKE', '#':'REGEXP'}
26 condition
= condition
.strip()
29 #match = re.match(r'^[(](.+)[)]$', condition)
31 # dprint("Matched () on %s" % condition)
33 # for c in match.group(1):
34 # if c == '(': depth += 1
35 # if c == ')': depth -= 1
37 # dprint("Depth is %d" % depth)
39 # dprint("Match...stripping ()'s")
40 # return me(match.group(1), valid_field_dict)
43 match
= re
.match(r
'^(.+)[|](.+)$', condition
)
45 dprint("Matched | on %s" % condition
)
46 (a_sql
, a_values
) = me(match
.group(1), valid_field_dict
)
47 (b_sql
, b_values
) = me(match
.group(2), valid_field_dict
)
48 return (" (%s) OR (%s) " % (a_sql
, b_sql
),
52 match
= re
.match(r
'^(.+)[&](.+)$', condition
)
54 dprint("Matched & on %s" % condition
)
55 (a_sql
, a_values
) = me(match
.group(1), valid_field_dict
)
56 (b_sql
, b_values
) = me(match
.group(2), valid_field_dict
)
57 return (" (%s) AND (%s) " % (a_sql
, b_sql
),
61 #match = re.match(r'^[!](.+)$', condition)
63 # dprint("Matched ! on %s" % condition)
64 # (sql, values) = me(match.group(1), valid_field_dict)
65 # return (" NOT (%s) " % (sql,), values)
67 # '<field> <op> <value>' where value can be quoted
68 # double quotes are escaped....i.e. '''' is the same as "'"
69 regex
= r
'^(%s)[ \t]*(%s)[ \t]*' + \
70 r
'(\'((\'\'|
[^
\'])*)\'|
"((""|[^"])*)"|([^\'"].*))$
'
71 regex = regex % ('|
'.join(valid_field_dict.keys()),
72 '|
'.join(compare_ops.keys()))
73 match = re.match(regex, condition)
75 field = valid_field_dict[match.group(1)]
76 op = compare_ops[match.group(2)]
78 val = match.group(4).replace("''", "'")
80 val = match.group(6).replace('""', '"')
84 raise "Internal error"
85 return ("%s %s %%s" % (field, op), [val])
88 raise "Could not parse '%s' (%s)" % (condition, regex)