| 1 | """ |
|---|
| 2 | sureshvv's extension for slicing and dicing variables with regular expressions. |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | import re |
|---|
| 6 | from twill.namespaces import get_twill_glocals |
|---|
| 7 | from twill.commands import browser |
|---|
| 8 | |
|---|
| 9 | def showvar(which): |
|---|
| 10 | """ |
|---|
| 11 | >> showvar var |
|---|
| 12 | |
|---|
| 13 | Shows the value of the variable 'var'. |
|---|
| 14 | """ |
|---|
| 15 | global_dict, local_dict = get_twill_glocals() |
|---|
| 16 | |
|---|
| 17 | d = global_dict.copy() |
|---|
| 18 | d.update(local_dict) |
|---|
| 19 | |
|---|
| 20 | print d.get(str(which)) |
|---|
| 21 | |
|---|
| 22 | def split(what): |
|---|
| 23 | """ |
|---|
| 24 | >> split <regexp> |
|---|
| 25 | |
|---|
| 26 | Sets __matchlist__ to re.split(regexp, page). |
|---|
| 27 | """ |
|---|
| 28 | page = browser.get_html() |
|---|
| 29 | |
|---|
| 30 | m = re.split(what, page) |
|---|
| 31 | |
|---|
| 32 | global_dict, local_dict = get_twill_glocals() |
|---|
| 33 | local_dict['__matchlist__'] = m |
|---|
| 34 | |
|---|
| 35 | def findall(what): |
|---|
| 36 | """ |
|---|
| 37 | >> findall <regexp> |
|---|
| 38 | |
|---|
| 39 | Sets __matchlist__ to re.findall(regexp, page). |
|---|
| 40 | """ |
|---|
| 41 | page = browser.get_html() |
|---|
| 42 | |
|---|
| 43 | regexp = re.compile(what, re.DOTALL) |
|---|
| 44 | m = regexp.findall(page) |
|---|
| 45 | |
|---|
| 46 | global_dict, local_dict = get_twill_glocals() |
|---|
| 47 | local_dict['__matchlist__'] = m |
|---|
| 48 | |
|---|
| 49 | def getmatch(where, what): |
|---|
| 50 | """ |
|---|
| 51 | >> getmatch into_var expression |
|---|
| 52 | |
|---|
| 53 | Evaluates an expression against __match__ and puts it into 'into_var'. |
|---|
| 54 | """ |
|---|
| 55 | global_dict, local_dict = get_twill_glocals() |
|---|
| 56 | match = local_dict['__match__'] |
|---|
| 57 | local_dict[where] = _do_eval(match, what) |
|---|
| 58 | |
|---|
| 59 | def setmatch(what): |
|---|
| 60 | """ |
|---|
| 61 | >> setmatch expression |
|---|
| 62 | |
|---|
| 63 | Sets each element __matchlist__ to eval(expression); 'm' is set |
|---|
| 64 | to each element of __matchlist__ prior to processing. |
|---|
| 65 | """ |
|---|
| 66 | global_dict, local_dict = get_twill_glocals() |
|---|
| 67 | |
|---|
| 68 | match = local_dict['__matchlist__'] |
|---|
| 69 | if isinstance(match, str): # convert to list |
|---|
| 70 | match = [ match, ] |
|---|
| 71 | |
|---|
| 72 | new_match = [ _do_eval(m, what) for m in match ] |
|---|
| 73 | local_dict['__matchlist__'] = new_match |
|---|
| 74 | |
|---|
| 75 | def _do_eval(match, exp): |
|---|
| 76 | """ |
|---|
| 77 | Used internally to evaluate an expresseion |
|---|
| 78 | """ |
|---|
| 79 | return eval(exp, globals(), {'m': match}) |
|---|
| 80 | |
|---|
| 81 | def popmatch(which): |
|---|
| 82 | """ |
|---|
| 83 | >> popmatch index |
|---|
| 84 | |
|---|
| 85 | Pops __matchlist__[i] into __match__. |
|---|
| 86 | """ |
|---|
| 87 | global_dict, local_dict = get_twill_glocals() |
|---|
| 88 | |
|---|
| 89 | matchlist = local_dict['__matchlist__'] |
|---|
| 90 | match = matchlist.pop(int(which)) |
|---|
| 91 | local_dict['__match__'] = match |
|---|