| 404 | self.process_request = {} |
| 405 | |
| 406 | def add_handler(self, handler): |
| 407 | if not hasattr(handler, "add_parent"): |
| 408 | raise TypeError("expected BaseHandler instance, got %r" % |
| 409 | type(handler)) |
| 410 | |
| 411 | added = False |
| 412 | for meth in dir(handler): |
| 413 | if meth in ["redirect_request", "do_open", "proxy_open"]: |
| 414 | # oops, coincidental match |
| 415 | continue |
| 416 | |
| 417 | i = meth.find("_") |
| 418 | if i < 1: |
| 419 | continue |
| 420 | protocol = meth[:i] |
| 421 | condition = meth[i+1:] |
| 422 | |
| 423 | if condition.startswith("error"): |
| 424 | j = condition.find("_") + i + 1 |
| 425 | kind = meth[j+1:] |
| 426 | try: |
| 427 | kind = int(kind) |
| 428 | except ValueError: |
| 429 | pass |
| 430 | lookup = self.handle_error.get(protocol, {}) |
| 431 | self.handle_error[protocol] = lookup |
| 432 | elif condition == "open": |
| 433 | kind = protocol |
| 434 | lookup = self.handle_open |
| 435 | elif condition == "response": |
| 436 | kind = protocol |
| 437 | lookup = self.process_response |
| 438 | elif condition == "request": |
| 439 | kind = protocol |
| 440 | lookup = self.process_request |
| 441 | else: |
| 442 | continue |
| 443 | |
| 444 | handlers = lookup.setdefault(kind, []) |
| 445 | if handlers: |
| 446 | bisect.insort(handlers, handler) |
| 447 | else: |
| 448 | handlers.append(handler) |
| 449 | added = True |
| 450 | |
| 451 | if added: |
| 452 | bisect.insort(self.handlers, handler) |
| 453 | handler.add_parent(self) |
| 454 | |
| 455 | def close(self): |
| 456 | # Only exists for backwards compatibility. |