mbligh | 2b67253 | 2007-11-05 19:24:51 +0000 | [diff] [blame] | 1 | """ |
| 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. |
| 5 | """ |
| 6 | |
| 7 | import sys, os, re |
| 8 | |
mbligh | 6f075f0 | 2008-01-25 16:36:16 +0000 | [diff] [blame] | 9 | tko = os.path.dirname(__file__) |
mbligh | 2b67253 | 2007-11-05 19:24:51 +0000 | [diff] [blame] | 10 | sys.path.insert(0, tko) |
| 11 | |
| 12 | import display, frontend, db |
| 13 | |
| 14 | db = db.db() |
| 15 | |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 16 | def dprint(str): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 17 | pass |
| 18 | #print "! %s<br>" % str |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 19 | |
| 20 | def parse_scrub_and_gen_condition(condition, valid_field_dict): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 21 | me = parse_scrub_and_gen_condition # shorten the name |
| 22 | compare_ops = {'=':'=', '<>':'<>', '==':'=', '!=':'<>', '>':'>', |
| 23 | '<':'<', '>=':'>=', '<=':'<=', '~':'LIKE', '#':'REGEXP'} |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 24 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 25 | # strip white space |
| 26 | condition = condition.strip() |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 27 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 28 | # ()'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) |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 41 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 42 | # 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) |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 50 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 51 | # 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) |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 59 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 60 | # 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) |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 66 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 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) |
| 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]) |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 86 | |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 87 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 88 | raise "Could not parse '%s' (%s)" % (condition, regex) |