[3] | 1 | """ |
---|
| 2 | A simple set of extensions to manage post-load requirements for pages. |
---|
| 3 | |
---|
| 4 | Commands: |
---|
| 5 | |
---|
| 6 | require -- turn on post-load requirements; either 'success' or |
---|
| 7 | 'links_ok'. |
---|
| 8 | |
---|
| 9 | no_require -- turn off requirements. |
---|
| 10 | |
---|
| 11 | skip_require -- for the next page visit, skip requirements processing. |
---|
| 12 | |
---|
| 13 | flush_visited -- flush the list of already visited pages |
---|
| 14 | (for links checking) |
---|
| 15 | """ |
---|
| 16 | |
---|
| 17 | __all__ = ['require', 'skip_require', 'flush_visited', 'no_require'] |
---|
| 18 | |
---|
| 19 | DEBUG=False |
---|
| 20 | |
---|
| 21 | ### |
---|
| 22 | |
---|
| 23 | _requirements = [] # what requirements to satisfy |
---|
| 24 | |
---|
| 25 | ignore_once = False # reset after each hook call |
---|
| 26 | ignore_always = False # never reset |
---|
| 27 | links_visited = {} # list of known good links, for |
---|
| 28 | # link checking. |
---|
| 29 | |
---|
| 30 | def _require_post_load_hook(action, *args, **kwargs): |
---|
| 31 | """ |
---|
| 32 | post-load hook function to be called after each page is loaded. |
---|
| 33 | |
---|
| 34 | See TwillBrowser._journey() for more information. |
---|
| 35 | """ |
---|
| 36 | if action == 'back': # do nothing on a 'back' |
---|
| 37 | return |
---|
| 38 | |
---|
| 39 | from twill import commands |
---|
| 40 | OUT=commands.OUT |
---|
| 41 | |
---|
| 42 | global ignore_once |
---|
| 43 | global ignore_always |
---|
| 44 | |
---|
| 45 | if ignore_once or ignore_always: |
---|
| 46 | ignore_once = False |
---|
| 47 | return |
---|
| 48 | |
---|
| 49 | for what in _requirements: |
---|
| 50 | |
---|
| 51 | #### |
---|
| 52 | #### |
---|
| 53 | #### |
---|
| 54 | |
---|
| 55 | if what == 'success': |
---|
| 56 | if DEBUG: |
---|
| 57 | print>>OUT, 'REQUIRING success' |
---|
| 58 | commands.code(200) |
---|
| 59 | |
---|
| 60 | #### |
---|
| 61 | #### |
---|
| 62 | #### |
---|
| 63 | |
---|
| 64 | elif what == 'links_ok': |
---|
| 65 | from check_links import check_links |
---|
| 66 | |
---|
| 67 | ignore_always = True |
---|
| 68 | if DEBUG: |
---|
| 69 | print>>OUT, 'REQUIRING functioning links' |
---|
| 70 | print>>OUT, '(already visited:)' |
---|
| 71 | print "\n\t".join(links_visited.keys()) |
---|
| 72 | |
---|
| 73 | try: |
---|
| 74 | check_links(visited=links_visited) |
---|
| 75 | finally: |
---|
| 76 | ignore_always = False |
---|
| 77 | |
---|
| 78 | ####### |
---|
| 79 | |
---|
| 80 | # |
---|
| 81 | # twill command-line functions. |
---|
| 82 | # |
---|
| 83 | |
---|
| 84 | def skip_require(): |
---|
| 85 | """ |
---|
| 86 | >> skip_require |
---|
| 87 | |
---|
| 88 | Skip the post-page-load requirements. |
---|
| 89 | """ |
---|
| 90 | global ignore_once |
---|
| 91 | ignore_once = True |
---|
| 92 | |
---|
| 93 | def require(what): |
---|
| 94 | """ |
---|
| 95 | >> require <what> |
---|
| 96 | |
---|
| 97 | After each page is loaded, require that 'what' be satisfied. 'what' |
---|
| 98 | can be: |
---|
| 99 | * 'success' -- HTTP return code is 200 |
---|
| 100 | * 'links_ok' -- all of the links on the page load OK (see 'check_links' |
---|
| 101 | extension module) |
---|
| 102 | """ |
---|
| 103 | global _requirements |
---|
| 104 | from twill import commands |
---|
| 105 | |
---|
| 106 | # |
---|
| 107 | # install the post-load hook function. |
---|
| 108 | # |
---|
| 109 | |
---|
| 110 | if _require_post_load_hook not in commands.browser._post_load_hooks: |
---|
| 111 | if DEBUG: |
---|
| 112 | print>>commands.OUT, 'INSTALLING POST-LOAD HOOK' |
---|
| 113 | commands.browser._post_load_hooks.append(_require_post_load_hook) |
---|
| 114 | |
---|
| 115 | # |
---|
| 116 | # add the requirement. |
---|
| 117 | # |
---|
| 118 | |
---|
| 119 | if what not in _requirements: |
---|
| 120 | if DEBUG: |
---|
| 121 | print>>commands.OUT, 'Adding requirement', what |
---|
| 122 | _requirements.append(what) |
---|
| 123 | |
---|
| 124 | def no_require(): |
---|
| 125 | """ |
---|
| 126 | >> no_require |
---|
| 127 | |
---|
| 128 | Remove all post-load requirements. |
---|
| 129 | """ |
---|
| 130 | from twill import commands |
---|
| 131 | |
---|
| 132 | l = commands.browser._post_load_hooks |
---|
| 133 | l = [ fn for fn in l if fn != _require_post_load_hook ] |
---|
| 134 | commands.browser._post_load_hooks = l |
---|
| 135 | |
---|
| 136 | global _requirements |
---|
| 137 | _requirements = [] |
---|
| 138 | |
---|
| 139 | def flush_visited(): |
---|
| 140 | """ |
---|
| 141 | >> flush_visited |
---|
| 142 | |
---|
| 143 | Flush the list of pages successfully visited already. |
---|
| 144 | """ |
---|
| 145 | global links_visited |
---|
| 146 | links_visited.clear() |
---|