mbligh | 2b67253 | 2007-11-05 19:24:51 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | """ |
| 3 | This library provides a bunch of miscellaneous parameter parsing, |
| 4 | sql generating and list cleanup library functions that are used |
| 5 | by both the reporting cli and web interface. |
| 6 | """ |
| 7 | |
| 8 | import sys, os, re |
| 9 | |
mbligh | 6f075f0 | 2008-01-25 16:36:16 +0000 | [diff] [blame] | 10 | tko = os.path.dirname(__file__) |
mbligh | 2b67253 | 2007-11-05 19:24:51 +0000 | [diff] [blame] | 11 | sys.path.insert(0, tko) |
| 12 | |
| 13 | import display, frontend, db |
| 14 | |
| 15 | db = db.db() |
| 16 | |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 17 | def dprint(str): |
| 18 | pass |
| 19 | #print "! %s<br>" % str |
| 20 | |
| 21 | def parse_scrub_and_gen_condition(condition, valid_field_dict): |
| 22 | me = parse_scrub_and_gen_condition # shorten the name |
| 23 | compare_ops = {'=':'=', '<>':'<>', '==':'=', '!=':'<>', '>':'>', |
| 24 | '<':'<', '>=':'>=', '<=':'<=', '~':'LIKE', '#':'REGEXP'} |
| 25 | |
| 26 | # strip white space |
| 27 | condition = condition.strip() |
| 28 | |
| 29 | # ()'s |
| 30 | #match = re.match(r'^[(](.+)[)]$', condition) |
| 31 | #if match: |
| 32 | # dprint("Matched () on %s" % condition) |
| 33 | # depth = 0 |
| 34 | # for c in match.group(1): |
| 35 | # if c == '(': depth += 1 |
| 36 | # if c == ')': depth -= 1 |
| 37 | # if depth < 0: break |
| 38 | # dprint("Depth is %d" % depth) |
| 39 | # if depth == 0: |
| 40 | # dprint("Match...stripping ()'s") |
| 41 | # return me(match.group(1), valid_field_dict) |
| 42 | |
| 43 | # OR |
| 44 | match = re.match(r'^(.+)[|](.+)$', condition) |
| 45 | if match: |
| 46 | dprint("Matched | on %s" % condition) |
| 47 | (a_sql, a_values) = me(match.group(1), valid_field_dict) |
| 48 | (b_sql, b_values) = me(match.group(2), valid_field_dict) |
| 49 | return (" (%s) OR (%s) " % (a_sql, b_sql), |
| 50 | a_values + b_values) |
| 51 | |
| 52 | # AND |
| 53 | match = re.match(r'^(.+)[&](.+)$', condition) |
| 54 | if match: |
| 55 | dprint("Matched & on %s" % condition) |
| 56 | (a_sql, a_values) = me(match.group(1), valid_field_dict) |
| 57 | (b_sql, b_values) = me(match.group(2), valid_field_dict) |
| 58 | return (" (%s) AND (%s) " % (a_sql, b_sql), |
| 59 | a_values + b_values) |
| 60 | |
| 61 | # NOT |
| 62 | #match = re.match(r'^[!](.+)$', condition) |
| 63 | #if match: |
| 64 | # dprint("Matched ! on %s" % condition) |
| 65 | # (sql, values) = me(match.group(1), valid_field_dict) |
| 66 | # return (" NOT (%s) " % (sql,), values) |
| 67 | |
| 68 | # '<field> <op> <value>' where value can be quoted |
| 69 | # double quotes are escaped....i.e. '''' is the same as "'" |
| 70 | regex = r'^(%s)[ \t]*(%s)[ \t]*' + \ |
| 71 | r'(\'((\'\'|[^\'])*)\'|"((""|[^"])*)"|([^\'"].*))$' |
| 72 | regex = regex % ('|'.join(valid_field_dict.keys()), |
| 73 | '|'.join(compare_ops.keys())) |
| 74 | match = re.match(regex, condition) |
| 75 | if match: |
| 76 | field = valid_field_dict[match.group(1)] |
| 77 | op = compare_ops[match.group(2)] |
| 78 | if match.group(5): |
| 79 | val = match.group(4).replace("''", "'") |
| 80 | elif match.group(7): |
| 81 | val = match.group(6).replace('""', '"') |
| 82 | elif match.group(8): |
| 83 | val = match.group(8) |
| 84 | else: |
| 85 | raise "Internal error" |
| 86 | return ("%s %s %%s" % (field, op), [val]) |
| 87 | |
| 88 | |
| 89 | raise "Could not parse '%s' (%s)" % (condition, regex) |
| 90 | |