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