blob: 77e1fbade97ac478287c6ccbc0fd6ca063b2c076 [file] [log] [blame]
mbligh2b672532007-11-05 19:24:51 +00001"""
2This library provides a bunch of miscellaneous parameter parsing,
3sql generating and list cleanup library functions that are used
4by both the reporting cli and web interface.
5"""
6
7import sys, os, re
8
mbligh6f075f02008-01-25 16:36:16 +00009tko = os.path.dirname(__file__)
mbligh2b672532007-11-05 19:24:51 +000010sys.path.insert(0, tko)
11
12import display, frontend, db
13
14db = db.db()
15
mbligh12eebfa2008-01-03 02:01:53 +000016def dprint(str):
jadmanski0afbb632008-06-06 21:10:57 +000017 pass
18 #print "! %s<br>" % str
mbligh12eebfa2008-01-03 02:01:53 +000019
20def parse_scrub_and_gen_condition(condition, valid_field_dict):
jadmanski0afbb632008-06-06 21:10:57 +000021 me = parse_scrub_and_gen_condition # shorten the name
22 compare_ops = {'=':'=', '<>':'<>', '==':'=', '!=':'<>', '>':'>',
23 '<':'<', '>=':'>=', '<=':'<=', '~':'LIKE', '#':'REGEXP'}
mbligh12eebfa2008-01-03 02:01:53 +000024
jadmanski0afbb632008-06-06 21:10:57 +000025 # strip white space
26 condition = condition.strip()
mbligh12eebfa2008-01-03 02:01:53 +000027
jadmanski0afbb632008-06-06 21:10:57 +000028 # ()'s
29 #match = re.match(r'^[(](.+)[)]$', condition)
30 #if match:
31 # dprint("Matched () on %s" % condition)
32 # depth = 0
33 # for c in match.group(1):
34 # if c == '(': depth += 1
35 # if c == ')': depth -= 1
36 # if depth < 0: break
37 # dprint("Depth is %d" % depth)
38 # if depth == 0:
39 # dprint("Match...stripping ()'s")
40 # return me(match.group(1), valid_field_dict)
mbligh12eebfa2008-01-03 02:01:53 +000041
jadmanski0afbb632008-06-06 21:10:57 +000042 # OR
43 match = re.match(r'^(.+)[|](.+)$', condition)
44 if match:
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),
49 a_values + b_values)
mbligh12eebfa2008-01-03 02:01:53 +000050
jadmanski0afbb632008-06-06 21:10:57 +000051 # AND
52 match = re.match(r'^(.+)[&](.+)$', condition)
53 if match:
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),
58 a_values + b_values)
mbligh12eebfa2008-01-03 02:01:53 +000059
jadmanski0afbb632008-06-06 21:10:57 +000060 # NOT
61 #match = re.match(r'^[!](.+)$', condition)
62 #if match:
63 # dprint("Matched ! on %s" % condition)
64 # (sql, values) = me(match.group(1), valid_field_dict)
65 # return (" NOT (%s) " % (sql,), values)
mbligh12eebfa2008-01-03 02:01:53 +000066
jadmanski0afbb632008-06-06 21:10:57 +000067 # '<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)
74 if match:
75 field = valid_field_dict[match.group(1)]
76 op = compare_ops[match.group(2)]
77 if match.group(5):
78 val = match.group(4).replace("''", "'")
79 elif match.group(7):
80 val = match.group(6).replace('""', '"')
81 elif match.group(8):
82 val = match.group(8)
83 else:
84 raise "Internal error"
85 return ("%s %s %%s" % (field, op), [val])
mbligh12eebfa2008-01-03 02:01:53 +000086
mbligh12eebfa2008-01-03 02:01:53 +000087
jadmanski0afbb632008-06-06 21:10:57 +000088 raise "Could not parse '%s' (%s)" % (condition, regex)