| 1 | """ |
|---|
| 2 | Extension functions to discard all moderated messages in a SourceForge-based |
|---|
| 3 | mailman queue. |
|---|
| 4 | |
|---|
| 5 | (Currently there is no way to do this without manually selecting 'discard' |
|---|
| 6 | for each and every message.) |
|---|
| 7 | """ |
|---|
| 8 | |
|---|
| 9 | import twill, twill.utils |
|---|
| 10 | import re |
|---|
| 11 | |
|---|
| 12 | # export: |
|---|
| 13 | __all__ = ['discard_all_messages', |
|---|
| 14 | 'exit_if_empty' |
|---|
| 15 | ] |
|---|
| 16 | |
|---|
| 17 | def exit_if_empty(): |
|---|
| 18 | """ |
|---|
| 19 | >> exit_if_empty |
|---|
| 20 | |
|---|
| 21 | Exit the script currently running, if there are no deferred messages |
|---|
| 22 | on the current page. |
|---|
| 23 | """ |
|---|
| 24 | state = twill.get_browser() |
|---|
| 25 | form = state.get_form("1") |
|---|
| 26 | if not form: |
|---|
| 27 | print "No messages; exiting." |
|---|
| 28 | raise SystemExit |
|---|
| 29 | |
|---|
| 30 | def discard_all_messages(): |
|---|
| 31 | """ |
|---|
| 32 | >> discard_all_messages |
|---|
| 33 | |
|---|
| 34 | Set all buttons to "discard". |
|---|
| 35 | """ |
|---|
| 36 | _formvalue_by_regexp_setall("1", "^\d+$", "3") |
|---|
| 37 | |
|---|
| 38 | ### utility functions |
|---|
| 39 | |
|---|
| 40 | def _formvalue_by_regexp_setall(formname, fieldname, value): |
|---|
| 41 | state = twill.get_browser() |
|---|
| 42 | |
|---|
| 43 | form = state.get_form(formname) |
|---|
| 44 | if not form: |
|---|
| 45 | print 'no such form', formname |
|---|
| 46 | return |
|---|
| 47 | |
|---|
| 48 | regexp = re.compile(fieldname) |
|---|
| 49 | |
|---|
| 50 | matches = [ ctl for ctl in form.controls if regexp.search(str(ctl.name)) ] |
|---|
| 51 | |
|---|
| 52 | if matches: |
|---|
| 53 | print '-- matches %d' % (len(matches),) |
|---|
| 54 | |
|---|
| 55 | n = 0 |
|---|
| 56 | for control in matches: |
|---|
| 57 | state.clicked(form, control) |
|---|
| 58 | if control.readonly: |
|---|
| 59 | continue |
|---|
| 60 | |
|---|
| 61 | n += 1 |
|---|
| 62 | twill.utils.set_form_control_value(control, value) |
|---|
| 63 | |
|---|
| 64 | print 'set %d values total' % (n,) |
|---|