Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """Web tree checker. |
| 4 | |
| 5 | This utility is handy to check a subweb of the world-wide web for |
| 6 | errors. A subweb is specified by giving one or more ``root URLs''; a |
| 7 | page belongs to the subweb if one of the root URLs is an initial |
| 8 | prefix of it. |
| 9 | |
| 10 | File URL extension: |
| 11 | |
| 12 | In order to easy the checking of subwebs via the local file system, |
| 13 | the interpretation of ``file:'' URLs is extended to mimic the behavior |
| 14 | of your average HTTP daemon: if a directory pathname is given, the |
| 15 | file index.html in that directory is returned if it exists, otherwise |
| 16 | a directory listing is returned. Now, you can point webchecker to the |
| 17 | document tree in the local file system of your HTTP daemon, and have |
| 18 | most of it checked. In fact the default works this way if your local |
| 19 | web tree is located at /usr/local/etc/httpd/htdpcs (the default for |
| 20 | the NCSA HTTP daemon and probably others). |
| 21 | |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 22 | Report printed: |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 24 | When done, it reports pages with bad links within the subweb. When |
| 25 | interrupted, it reports for the pages that it has checked already. |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 26 | |
| 27 | In verbose mode, additional messages are printed during the |
| 28 | information gathering phase. By default, it prints a summary of its |
| 29 | work status every 50 URLs (adjustable with the -r option), and it |
| 30 | reports errors as they are encountered. Use the -q option to disable |
| 31 | this output. |
| 32 | |
| 33 | Checkpoint feature: |
| 34 | |
| 35 | Whether interrupted or not, it dumps its state (a Python pickle) to a |
| 36 | checkpoint file and the -R option allows it to restart from the |
| 37 | checkpoint (assuming that the pages on the subweb that were already |
| 38 | processed haven't changed). Even when it has run till completion, -R |
| 39 | can still be useful -- it will print the reports again, and -Rq prints |
| 40 | the errors only. In this case, the checkpoint file is not written |
| 41 | again. The checkpoint file can be set with the -d option. |
| 42 | |
| 43 | The checkpoint file is written as a Python pickle. Remember that |
| 44 | Python's pickle module is currently quite slow. Give it the time it |
| 45 | needs to load and save the checkpoint file. When interrupted while |
| 46 | writing the checkpoint file, the old checkpoint file is not |
| 47 | overwritten, but all work done in the current run is lost. |
| 48 | |
| 49 | Miscellaneous: |
| 50 | |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 51 | - You may find the (Tk-based) GUI version easier to use. See wcgui.py. |
| 52 | |
Guido van Rossum | 3edbb35 | 1997-01-30 03:19:41 +0000 | [diff] [blame] | 53 | - Webchecker honors the "robots.txt" convention. Thanks to Skip |
| 54 | Montanaro for his robotparser.py module (included in this directory)! |
| 55 | The agent name is hardwired to "webchecker". URLs that are disallowed |
| 56 | by the robots.txt file are reported as external URLs. |
| 57 | |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 58 | - Because the SGML parser is a bit slow, very large SGML files are |
Guido van Rossum | 3edbb35 | 1997-01-30 03:19:41 +0000 | [diff] [blame] | 59 | skipped. The size limit can be set with the -m option. |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 61 | - When the server or protocol does not tell us a file's type, we guess |
| 62 | it based on the URL's suffix. The mimetypes.py module (also in this |
| 63 | directory) has a built-in table mapping most currently known suffixes, |
| 64 | and in addition attempts to read the mime.types configuration files in |
| 65 | the default locations of Netscape and the NCSA HTTP daemon. |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 67 | - We follows links indicated by <A>, <FRAME> and <IMG> tags. We also |
| 68 | honor the <BASE> tag. |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 70 | - Checking external links is now done by default; use -x to *disable* |
| 71 | this feature. External links are now checked during normal |
| 72 | processing. (XXX The status of a checked link could be categorized |
| 73 | better. Later...) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 74 | |
| 75 | |
| 76 | Usage: webchecker.py [option] ... [rooturl] ... |
| 77 | |
| 78 | Options: |
| 79 | |
| 80 | -R -- restart from checkpoint file |
| 81 | -d file -- checkpoint filename (default %(DUMPFILE)s) |
| 82 | -m bytes -- skip HTML pages larger than this size (default %(MAXPAGE)d) |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 83 | -n -- reports only, no checking (use with -R) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 84 | -q -- quiet operation (also suppresses external links report) |
| 85 | -r number -- number of links processed per round (default %(ROUNDSIZE)d) |
| 86 | -v -- verbose operation; repeating -v will increase verbosity |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 87 | -x -- don't check external links (these are often slow to check) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 88 | |
| 89 | Arguments: |
| 90 | |
| 91 | rooturl -- URL to start checking |
| 92 | (default %(DEFROOT)s) |
| 93 | |
| 94 | """ |
| 95 | |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 97 | __version__ = "$Revision$" |
Guido van Rossum | 325a64f | 1997-01-30 03:30:20 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 99 | |
| 100 | import sys |
| 101 | import os |
| 102 | from types import * |
| 103 | import string |
| 104 | import StringIO |
| 105 | import getopt |
| 106 | import pickle |
| 107 | |
| 108 | import urllib |
| 109 | import urlparse |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 110 | import sgmllib |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 111 | |
| 112 | import mimetypes |
Guido van Rossum | 3edbb35 | 1997-01-30 03:19:41 +0000 | [diff] [blame] | 113 | import robotparser |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 115 | # Extract real version number if necessary |
| 116 | if __version__[0] == '$': |
| 117 | _v = string.split(__version__) |
| 118 | if len(_v) == 3: |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 119 | __version__ = _v[1] |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 121 | |
| 122 | # Tunable parameters |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 123 | DEFROOT = "file:/usr/local/etc/httpd/htdocs/" # Default root URL |
| 124 | CHECKEXT = 1 # Check external references (1 deep) |
| 125 | VERBOSE = 1 # Verbosity level (0-3) |
| 126 | MAXPAGE = 150000 # Ignore files bigger than this |
| 127 | ROUNDSIZE = 50 # Number of links processed per round |
| 128 | DUMPFILE = "@webchecker.pickle" # Pickled checkpoint |
| 129 | AGENTNAME = "webchecker" # Agent name for robots.txt parser |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 130 | |
| 131 | |
| 132 | # Global variables |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 133 | |
| 134 | |
| 135 | def main(): |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 136 | checkext = CHECKEXT |
| 137 | verbose = VERBOSE |
| 138 | maxpage = MAXPAGE |
| 139 | roundsize = ROUNDSIZE |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 140 | dumpfile = DUMPFILE |
| 141 | restart = 0 |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 142 | norun = 0 |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 143 | |
| 144 | try: |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 145 | opts, args = getopt.getopt(sys.argv[1:], 'Rd:m:nqr:vx') |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 146 | except getopt.error, msg: |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 147 | sys.stdout = sys.stderr |
| 148 | print msg |
| 149 | print __doc__%globals() |
| 150 | sys.exit(2) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 151 | for o, a in opts: |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 152 | if o == '-R': |
| 153 | restart = 1 |
| 154 | if o == '-d': |
| 155 | dumpfile = a |
| 156 | if o == '-m': |
| 157 | maxpage = string.atoi(a) |
| 158 | if o == '-n': |
| 159 | norun = 1 |
| 160 | if o == '-q': |
| 161 | verbose = 0 |
| 162 | if o == '-r': |
| 163 | roundsize = string.atoi(a) |
| 164 | if o == '-v': |
| 165 | verbose = verbose + 1 |
| 166 | if o == '-x': |
| 167 | checkext = not checkext |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 169 | if verbose > 0: |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 170 | print AGENTNAME, "version", __version__ |
Guido van Rossum | 325a64f | 1997-01-30 03:30:20 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 172 | if restart: |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 173 | c = load_pickle(dumpfile=dumpfile, verbose=verbose) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 174 | else: |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 175 | c = Checker() |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 176 | |
| 177 | c.setflags(checkext=checkext, verbose=verbose, |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 178 | maxpage=maxpage, roundsize=roundsize) |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 179 | |
| 180 | if not restart and not args: |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 181 | args.append(DEFROOT) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 182 | |
| 183 | for arg in args: |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 184 | c.addroot(arg) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 185 | |
Guido van Rossum | bee6453 | 1998-04-27 19:35:15 +0000 | [diff] [blame] | 186 | try: |
| 187 | |
| 188 | if not norun: |
| 189 | try: |
| 190 | c.run() |
| 191 | except KeyboardInterrupt: |
| 192 | if verbose > 0: |
| 193 | print "[run interrupted]" |
| 194 | |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 195 | try: |
Guido van Rossum | bee6453 | 1998-04-27 19:35:15 +0000 | [diff] [blame] | 196 | c.report() |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 197 | except KeyboardInterrupt: |
| 198 | if verbose > 0: |
Guido van Rossum | bee6453 | 1998-04-27 19:35:15 +0000 | [diff] [blame] | 199 | print "[report interrupted]" |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 200 | |
Guido van Rossum | bee6453 | 1998-04-27 19:35:15 +0000 | [diff] [blame] | 201 | finally: |
| 202 | if c.save_pickle(dumpfile): |
| 203 | if dumpfile == DUMPFILE: |
| 204 | print "Use ``%s -R'' to restart." % sys.argv[0] |
| 205 | else: |
| 206 | print "Use ``%s -R -d %s'' to restart." % (sys.argv[0], |
| 207 | dumpfile) |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 208 | |
| 209 | |
| 210 | def load_pickle(dumpfile=DUMPFILE, verbose=VERBOSE): |
| 211 | if verbose > 0: |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 212 | print "Loading checkpoint from %s ..." % dumpfile |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 213 | f = open(dumpfile, "rb") |
| 214 | c = pickle.load(f) |
| 215 | f.close() |
| 216 | if verbose > 0: |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 217 | print "Done." |
| 218 | print "Root:", string.join(c.roots, "\n ") |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 219 | return c |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 220 | |
| 221 | |
| 222 | class Checker: |
| 223 | |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 224 | checkext = CHECKEXT |
| 225 | verbose = VERBOSE |
| 226 | maxpage = MAXPAGE |
| 227 | roundsize = ROUNDSIZE |
| 228 | |
| 229 | validflags = tuple(dir()) |
| 230 | |
| 231 | def __init__(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 232 | self.reset() |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 233 | |
| 234 | def setflags(self, **kw): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 235 | for key in kw.keys(): |
| 236 | if key not in self.validflags: |
| 237 | raise NameError, "invalid keyword argument: %s" % str(key) |
| 238 | for key, value in kw.items(): |
| 239 | setattr(self, key, value) |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 240 | |
| 241 | def reset(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 242 | self.roots = [] |
| 243 | self.todo = {} |
| 244 | self.done = {} |
| 245 | self.bad = {} |
| 246 | self.round = 0 |
| 247 | # The following are not pickled: |
| 248 | self.robots = {} |
| 249 | self.errors = {} |
| 250 | self.urlopener = MyURLopener() |
| 251 | self.changed = 0 |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 252 | |
| 253 | def note(self, level, format, *args): |
| 254 | if self.verbose > level: |
| 255 | if args: |
| 256 | format = format%args |
| 257 | self.message(format) |
| 258 | |
| 259 | def message(self, format, *args): |
| 260 | if args: |
| 261 | format = format%args |
| 262 | print format |
Guido van Rossum | 3edbb35 | 1997-01-30 03:19:41 +0000 | [diff] [blame] | 263 | |
| 264 | def __getstate__(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 265 | return (self.roots, self.todo, self.done, self.bad, self.round) |
Guido van Rossum | 3edbb35 | 1997-01-30 03:19:41 +0000 | [diff] [blame] | 266 | |
| 267 | def __setstate__(self, state): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 268 | self.reset() |
| 269 | (self.roots, self.todo, self.done, self.bad, self.round) = state |
| 270 | for root in self.roots: |
| 271 | self.addrobot(root) |
| 272 | for url in self.bad.keys(): |
| 273 | self.markerror(url) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 274 | |
| 275 | def addroot(self, root): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 276 | if root not in self.roots: |
| 277 | troot = root |
| 278 | scheme, netloc, path, params, query, fragment = \ |
| 279 | urlparse.urlparse(root) |
| 280 | i = string.rfind(path, "/") + 1 |
| 281 | if 0 < i < len(path): |
| 282 | path = path[:i] |
| 283 | troot = urlparse.urlunparse((scheme, netloc, path, |
| 284 | params, query, fragment)) |
| 285 | self.roots.append(troot) |
| 286 | self.addrobot(root) |
| 287 | self.newlink(root, ("<root>", root)) |
Guido van Rossum | 3edbb35 | 1997-01-30 03:19:41 +0000 | [diff] [blame] | 288 | |
| 289 | def addrobot(self, root): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 290 | root = urlparse.urljoin(root, "/") |
| 291 | if self.robots.has_key(root): return |
| 292 | url = urlparse.urljoin(root, "/robots.txt") |
| 293 | self.robots[root] = rp = robotparser.RobotFileParser() |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 294 | self.note(2, "Parsing %s", url) |
| 295 | rp.debug = self.verbose > 3 |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 296 | rp.set_url(url) |
| 297 | try: |
| 298 | rp.read() |
| 299 | except IOError, msg: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 300 | self.note(1, "I/O error parsing %s: %s", url, msg) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 301 | |
| 302 | def run(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 303 | while self.todo: |
| 304 | self.round = self.round + 1 |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 305 | self.note(0, "\nRound %d (%s)\n", self.round, self.status()) |
Guido van Rossum | 6eb9d32 | 1998-06-15 12:33:02 +0000 | [diff] [blame] | 306 | urls = self.todo.keys() |
| 307 | urls.sort() |
| 308 | del urls[self.roundsize:] |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 309 | for url in urls: |
| 310 | self.dopage(url) |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 311 | |
| 312 | def status(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 313 | return "%d total, %d to do, %d done, %d bad" % ( |
| 314 | len(self.todo)+len(self.done), |
| 315 | len(self.todo), len(self.done), |
| 316 | len(self.bad)) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 317 | |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 318 | def report(self): |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 319 | self.message("") |
| 320 | if not self.todo: s = "Final" |
| 321 | else: s = "Interim" |
| 322 | self.message("%s Report (%s)", s, self.status()) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 323 | self.report_errors() |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 325 | def report_errors(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 326 | if not self.bad: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 327 | self.message("\nNo errors") |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 328 | return |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 329 | self.message("\nError Report:") |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 330 | sources = self.errors.keys() |
| 331 | sources.sort() |
| 332 | for source in sources: |
| 333 | triples = self.errors[source] |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 334 | self.message("") |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 335 | if len(triples) > 1: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 336 | self.message("%d Errors in %s", len(triples), source) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 337 | else: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 338 | self.message("Error in %s", source) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 339 | for url, rawlink, msg in triples: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 340 | if rawlink != url: s = " (%s)" % rawlink |
| 341 | else: s = "" |
| 342 | self.message(" HREF %s%s\n msg %s", url, s, msg) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 343 | |
| 344 | def dopage(self, url): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 345 | if self.verbose > 1: |
| 346 | if self.verbose > 2: |
| 347 | self.show("Check ", url, " from", self.todo[url]) |
| 348 | else: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 349 | self.message("Check %s", url) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 350 | page = self.getpage(url) |
| 351 | if page: |
| 352 | for info in page.getlinkinfos(): |
| 353 | link, rawlink = info |
| 354 | origin = url, rawlink |
| 355 | self.newlink(link, origin) |
| 356 | self.markdone(url) |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 357 | |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 358 | def newlink(self, url, origin): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 359 | if self.done.has_key(url): |
| 360 | self.newdonelink(url, origin) |
| 361 | else: |
| 362 | self.newtodolink(url, origin) |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 363 | |
| 364 | def newdonelink(self, url, origin): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 365 | self.done[url].append(origin) |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 366 | self.note(3, " Done link %s", url) |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 367 | |
| 368 | def newtodolink(self, url, origin): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 369 | if self.todo.has_key(url): |
| 370 | self.todo[url].append(origin) |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 371 | self.note(3, " Seen todo link %s", url) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 372 | else: |
| 373 | self.todo[url] = [origin] |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 374 | self.note(3, " New todo link %s", url) |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 375 | |
| 376 | def markdone(self, url): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 377 | self.done[url] = self.todo[url] |
| 378 | del self.todo[url] |
| 379 | self.changed = 1 |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 380 | |
| 381 | def inroots(self, url): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 382 | for root in self.roots: |
| 383 | if url[:len(root)] == root: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 384 | return self.isallowed(root, url) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 385 | return 0 |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 386 | |
| 387 | def isallowed(self, root, url): |
| 388 | root = urlparse.urljoin(root, "/") |
| 389 | return self.robots[root].can_fetch(AGENTNAME, url) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 390 | |
| 391 | def getpage(self, url): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 392 | if url[:7] == 'mailto:' or url[:5] == 'news:': |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 393 | self.note(1, " Not checking mailto/news URL") |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 394 | return None |
| 395 | isint = self.inroots(url) |
| 396 | if not isint: |
| 397 | if not self.checkext: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 398 | self.note(1, " Not checking ext link") |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 399 | return None |
| 400 | f = self.openpage(url) |
| 401 | if f: |
| 402 | self.safeclose(f) |
| 403 | return None |
| 404 | text, nurl = self.readhtml(url) |
| 405 | if nurl != url: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 406 | self.note(1, " Redirected to %s", nurl) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 407 | url = nurl |
| 408 | if text: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 409 | return Page(text, url, maxpage=self.maxpage, checker=self) |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 410 | |
| 411 | def readhtml(self, url): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 412 | text = None |
| 413 | f, url = self.openhtml(url) |
| 414 | if f: |
| 415 | text = f.read() |
| 416 | f.close() |
| 417 | return text, url |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 418 | |
| 419 | def openhtml(self, url): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 420 | f = self.openpage(url) |
| 421 | if f: |
| 422 | url = f.geturl() |
| 423 | info = f.info() |
| 424 | if not self.checkforhtml(info, url): |
| 425 | self.safeclose(f) |
| 426 | f = None |
| 427 | return f, url |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 428 | |
| 429 | def openpage(self, url): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 430 | try: |
| 431 | return self.urlopener.open(url) |
| 432 | except IOError, msg: |
| 433 | msg = self.sanitize(msg) |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 434 | self.note(0, "Error %s", msg) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 435 | if self.verbose > 0: |
| 436 | self.show(" HREF ", url, " from", self.todo[url]) |
| 437 | self.setbad(url, msg) |
| 438 | return None |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 439 | |
| 440 | def checkforhtml(self, info, url): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 441 | if info.has_key('content-type'): |
| 442 | ctype = string.lower(info['content-type']) |
| 443 | else: |
| 444 | if url[-1:] == "/": |
| 445 | return 1 |
| 446 | ctype, encoding = mimetypes.guess_type(url) |
| 447 | if ctype == 'text/html': |
| 448 | return 1 |
| 449 | else: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 450 | self.note(1, " Not HTML, mime type %s", ctype) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 451 | return 0 |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 452 | |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 453 | def setgood(self, url): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 454 | if self.bad.has_key(url): |
| 455 | del self.bad[url] |
| 456 | self.changed = 1 |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 457 | self.note(0, "(Clear previously seen error)") |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 458 | |
| 459 | def setbad(self, url, msg): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 460 | if self.bad.has_key(url) and self.bad[url] == msg: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 461 | self.note(0, "(Seen this error before)") |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 462 | return |
| 463 | self.bad[url] = msg |
| 464 | self.changed = 1 |
| 465 | self.markerror(url) |
| 466 | |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 467 | def markerror(self, url): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 468 | try: |
| 469 | origins = self.todo[url] |
| 470 | except KeyError: |
| 471 | origins = self.done[url] |
| 472 | for source, rawlink in origins: |
| 473 | triple = url, rawlink, self.bad[url] |
| 474 | self.seterror(source, triple) |
Guido van Rossum | af310c1 | 1997-02-02 23:30:32 +0000 | [diff] [blame] | 475 | |
| 476 | def seterror(self, url, triple): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 477 | try: |
| 478 | self.errors[url].append(triple) |
| 479 | except KeyError: |
| 480 | self.errors[url] = [triple] |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 481 | |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 482 | # The following used to be toplevel functions; they have been |
| 483 | # changed into methods so they can be overridden in subclasses. |
| 484 | |
| 485 | def show(self, p1, link, p2, origins): |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 486 | self.message("%s %s", p1, link) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 487 | i = 0 |
| 488 | for source, rawlink in origins: |
| 489 | i = i+1 |
| 490 | if i == 2: |
| 491 | p2 = ' '*len(p2) |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 492 | if rawlink != link: s = " (%s)" % rawlink |
| 493 | else: s = "" |
| 494 | self.message("%s %s%s", p2, source, s) |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 495 | |
| 496 | def sanitize(self, msg): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 497 | if isinstance(IOError, ClassType) and isinstance(msg, IOError): |
| 498 | # Do the other branch recursively |
| 499 | msg.args = self.sanitize(msg.args) |
| 500 | elif isinstance(msg, TupleType): |
| 501 | if len(msg) >= 4 and msg[0] == 'http error' and \ |
| 502 | isinstance(msg[3], InstanceType): |
| 503 | # Remove the Message instance -- it may contain |
| 504 | # a file object which prevents pickling. |
| 505 | msg = msg[:3] + msg[4:] |
| 506 | return msg |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 507 | |
| 508 | def safeclose(self, f): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 509 | try: |
| 510 | url = f.geturl() |
| 511 | except AttributeError: |
| 512 | pass |
| 513 | else: |
| 514 | if url[:4] == 'ftp:' or url[:7] == 'file://': |
| 515 | # Apparently ftp connections don't like to be closed |
| 516 | # prematurely... |
| 517 | text = f.read() |
| 518 | f.close() |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 519 | |
| 520 | def save_pickle(self, dumpfile=DUMPFILE): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 521 | if not self.changed: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 522 | self.note(0, "\nNo need to save checkpoint") |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 523 | elif not dumpfile: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 524 | self.note(0, "No dumpfile, won't save checkpoint") |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 525 | else: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 526 | self.note(0, "\nSaving checkpoint to %s ...", dumpfile) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 527 | newfile = dumpfile + ".new" |
| 528 | f = open(newfile, "wb") |
| 529 | pickle.dump(self, f) |
| 530 | f.close() |
| 531 | try: |
| 532 | os.unlink(dumpfile) |
| 533 | except os.error: |
| 534 | pass |
| 535 | os.rename(newfile, dumpfile) |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 536 | self.note(0, "Done.") |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 537 | return 1 |
Guido van Rossum | 00756bd | 1998-02-21 20:02:09 +0000 | [diff] [blame] | 538 | |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 539 | |
| 540 | class Page: |
| 541 | |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 542 | def __init__(self, text, url, verbose=VERBOSE, maxpage=MAXPAGE, checker=None): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 543 | self.text = text |
| 544 | self.url = url |
| 545 | self.verbose = verbose |
| 546 | self.maxpage = maxpage |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 547 | self.checker = checker |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 548 | |
Guido van Rossum | a42c1ee | 1998-08-06 21:31:13 +0000 | [diff] [blame] | 549 | def note(self, level, msg, *args): |
| 550 | if self.checker: |
| 551 | apply(self.checker.note, (level, msg) + args) |
| 552 | else: |
| 553 | if self.verbose >= level: |
| 554 | if args: |
| 555 | msg = msg%args |
| 556 | print msg |
| 557 | |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 558 | def getlinkinfos(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 559 | size = len(self.text) |
| 560 | if size > self.maxpage: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 561 | self.note(0, "Skip huge file %s (%.0f Kbytes)", self.url, (size*0.001)) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 562 | return [] |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 563 | self.checker.note(2, " Parsing %s (%d bytes)", self.url, size) |
| 564 | parser = MyHTMLParser(verbose=self.verbose, checker=self.checker) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 565 | parser.feed(self.text) |
| 566 | parser.close() |
| 567 | rawlinks = parser.getlinks() |
| 568 | base = urlparse.urljoin(self.url, parser.getbase() or "") |
| 569 | infos = [] |
| 570 | for rawlink in rawlinks: |
| 571 | t = urlparse.urlparse(rawlink) |
| 572 | t = t[:-1] + ('',) |
| 573 | rawlink = urlparse.urlunparse(t) |
| 574 | link = urlparse.urljoin(base, rawlink) |
| 575 | infos.append((link, rawlink)) |
| 576 | return infos |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 577 | |
| 578 | |
| 579 | class MyStringIO(StringIO.StringIO): |
| 580 | |
| 581 | def __init__(self, url, info): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 582 | self.__url = url |
| 583 | self.__info = info |
| 584 | StringIO.StringIO.__init__(self) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 585 | |
| 586 | def info(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 587 | return self.__info |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 588 | |
| 589 | def geturl(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 590 | return self.__url |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 591 | |
| 592 | |
| 593 | class MyURLopener(urllib.FancyURLopener): |
| 594 | |
| 595 | http_error_default = urllib.URLopener.http_error_default |
| 596 | |
Guido van Rossum | c59a5d4 | 1997-01-30 06:04:00 +0000 | [diff] [blame] | 597 | def __init__(*args): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 598 | self = args[0] |
| 599 | apply(urllib.FancyURLopener.__init__, args) |
| 600 | self.addheaders = [ |
| 601 | ('User-agent', 'Python-webchecker/%s' % __version__), |
| 602 | ] |
Guido van Rossum | 89efda3 | 1997-05-07 15:00:56 +0000 | [diff] [blame] | 603 | |
| 604 | def http_error_401(self, url, fp, errcode, errmsg, headers): |
| 605 | return None |
Guido van Rossum | c59a5d4 | 1997-01-30 06:04:00 +0000 | [diff] [blame] | 606 | |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 607 | def open_file(self, url): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 608 | path = urllib.url2pathname(urllib.unquote(url)) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 609 | if os.path.isdir(path): |
Guido van Rossum | 0ec1493 | 1999-04-26 23:11:46 +0000 | [diff] [blame^] | 610 | if path[-1] != os.sep: |
| 611 | url = url + '/' |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 612 | indexpath = os.path.join(path, "index.html") |
| 613 | if os.path.exists(indexpath): |
| 614 | return self.open_file(url + "index.html") |
| 615 | try: |
| 616 | names = os.listdir(path) |
| 617 | except os.error, msg: |
| 618 | raise IOError, msg, sys.exc_traceback |
| 619 | names.sort() |
| 620 | s = MyStringIO("file:"+url, {'content-type': 'text/html'}) |
| 621 | s.write('<BASE HREF="file:%s">\n' % |
| 622 | urllib.quote(os.path.join(path, ""))) |
| 623 | for name in names: |
| 624 | q = urllib.quote(name) |
| 625 | s.write('<A HREF="%s">%s</A>\n' % (q, q)) |
| 626 | s.seek(0) |
| 627 | return s |
Guido van Rossum | 0ec1493 | 1999-04-26 23:11:46 +0000 | [diff] [blame^] | 628 | return urllib.FancyURLopener.open_file(self, url) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 629 | |
| 630 | |
Guido van Rossum | e5605ba | 1997-01-31 14:43:15 +0000 | [diff] [blame] | 631 | class MyHTMLParser(sgmllib.SGMLParser): |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 632 | |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 633 | def __init__(self, verbose=VERBOSE, checker=None): |
| 634 | self.myverbose = verbose # now unused |
| 635 | self.checker = checker |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 636 | self.base = None |
| 637 | self.links = {} |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 638 | sgmllib.SGMLParser.__init__(self) |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 639 | |
| 640 | def start_a(self, attributes): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 641 | self.link_attr(attributes, 'href') |
Guido van Rossum | 6133ec6 | 1997-02-01 05:16:08 +0000 | [diff] [blame] | 642 | |
| 643 | def end_a(self): pass |
| 644 | |
Guido van Rossum | 2237b73 | 1997-10-06 18:54:01 +0000 | [diff] [blame] | 645 | def do_area(self, attributes): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 646 | self.link_attr(attributes, 'href') |
Guido van Rossum | 2237b73 | 1997-10-06 18:54:01 +0000 | [diff] [blame] | 647 | |
Guido van Rossum | 6133ec6 | 1997-02-01 05:16:08 +0000 | [diff] [blame] | 648 | def do_img(self, attributes): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 649 | self.link_attr(attributes, 'src', 'lowsrc') |
Guido van Rossum | 6133ec6 | 1997-02-01 05:16:08 +0000 | [diff] [blame] | 650 | |
| 651 | def do_frame(self, attributes): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 652 | self.link_attr(attributes, 'src') |
Guido van Rossum | 6133ec6 | 1997-02-01 05:16:08 +0000 | [diff] [blame] | 653 | |
| 654 | def link_attr(self, attributes, *args): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 655 | for name, value in attributes: |
| 656 | if name in args: |
| 657 | if value: value = string.strip(value) |
| 658 | if value: self.links[value] = None |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 659 | |
| 660 | def do_base(self, attributes): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 661 | for name, value in attributes: |
| 662 | if name == 'href': |
| 663 | if value: value = string.strip(value) |
| 664 | if value: |
Guido van Rossum | 125700a | 1998-07-08 03:04:39 +0000 | [diff] [blame] | 665 | if self.checker: |
| 666 | self.checker.note(1, " Base %s", value) |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 667 | self.base = value |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 668 | |
| 669 | def getlinks(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 670 | return self.links.keys() |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 671 | |
| 672 | def getbase(self): |
Guido van Rossum | 986abac | 1998-04-06 14:29:28 +0000 | [diff] [blame] | 673 | return self.base |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 674 | |
| 675 | |
Guido van Rossum | 272b37d | 1997-01-30 02:44:48 +0000 | [diff] [blame] | 676 | if __name__ == '__main__': |
| 677 | main() |