| 1 | n/a | """Response classes used by urllib. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | The base class, addbase, defines a minimal file-like interface, |
|---|
| 4 | n/a | including read() and readline(). The typical response object is an |
|---|
| 5 | n/a | addinfourl instance, which defines an info() method that returns |
|---|
| 6 | n/a | headers and a geturl() method that returns the url. |
|---|
| 7 | n/a | """ |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | import tempfile |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | __all__ = ['addbase', 'addclosehook', 'addinfo', 'addinfourl'] |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | class addbase(tempfile._TemporaryFileWrapper): |
|---|
| 15 | n/a | """Base class for addinfo and addclosehook. Is a good idea for garbage collection.""" |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | # XXX Add a method to expose the timeout on the underlying socket? |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def __init__(self, fp): |
|---|
| 20 | n/a | super(addbase, self).__init__(fp, '<urllib response>', delete=False) |
|---|
| 21 | n/a | # Keep reference around as this was part of the original API. |
|---|
| 22 | n/a | self.fp = fp |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | def __repr__(self): |
|---|
| 25 | n/a | return '<%s at %r whose fp = %r>' % (self.__class__.__name__, |
|---|
| 26 | n/a | id(self), self.file) |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | def __enter__(self): |
|---|
| 29 | n/a | if self.fp.closed: |
|---|
| 30 | n/a | raise ValueError("I/O operation on closed file") |
|---|
| 31 | n/a | return self |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | def __exit__(self, type, value, traceback): |
|---|
| 34 | n/a | self.close() |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | class addclosehook(addbase): |
|---|
| 38 | n/a | """Class to add a close hook to an open file.""" |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | def __init__(self, fp, closehook, *hookargs): |
|---|
| 41 | n/a | super(addclosehook, self).__init__(fp) |
|---|
| 42 | n/a | self.closehook = closehook |
|---|
| 43 | n/a | self.hookargs = hookargs |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | def close(self): |
|---|
| 46 | n/a | try: |
|---|
| 47 | n/a | closehook = self.closehook |
|---|
| 48 | n/a | hookargs = self.hookargs |
|---|
| 49 | n/a | if closehook: |
|---|
| 50 | n/a | self.closehook = None |
|---|
| 51 | n/a | self.hookargs = None |
|---|
| 52 | n/a | closehook(*hookargs) |
|---|
| 53 | n/a | finally: |
|---|
| 54 | n/a | super(addclosehook, self).close() |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | class addinfo(addbase): |
|---|
| 58 | n/a | """class to add an info() method to an open file.""" |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | def __init__(self, fp, headers): |
|---|
| 61 | n/a | super(addinfo, self).__init__(fp) |
|---|
| 62 | n/a | self.headers = headers |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | def info(self): |
|---|
| 65 | n/a | return self.headers |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | class addinfourl(addinfo): |
|---|
| 69 | n/a | """class to add info() and geturl() methods to an open file.""" |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | def __init__(self, fp, headers, url, code=None): |
|---|
| 72 | n/a | super(addinfourl, self).__init__(fp, headers) |
|---|
| 73 | n/a | self.url = url |
|---|
| 74 | n/a | self.code = code |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | def getcode(self): |
|---|
| 77 | n/a | return self.code |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def geturl(self): |
|---|
| 80 | n/a | return self.url |
|---|