blob: 4a143b03e676b27fbba0d9cb0105d34df3ae2dd3 [file] [log] [blame]
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001"""Response classes used by urllib.
2
3The base class, addbase, defines a minimal file-like interface,
4including read() and readline(). The typical response object is an
5addinfourl instance, which defines an info() method that returns
6headers and a geturl() method that returns the url.
7"""
8
Senthil Kumaran6117e5d2014-04-20 09:41:29 -07009import tempfile
10
11__all__ = ['addbase', 'addclosehook', 'addinfo', 'addinfourl']
12
13
14class addbase(tempfile._TemporaryFileWrapper):
15 """Base class for addinfo and addclosehook. Is a good idea for garbage collection."""
Jeremy Hylton1afc1692008-06-18 20:49:58 +000016
17 # XXX Add a method to expose the timeout on the underlying socket?
18
19 def __init__(self, fp):
Senthil Kumaran6117e5d2014-04-20 09:41:29 -070020 super(addbase, self).__init__(fp, '<urllib response>', delete=False)
21 # Keep reference around as this was part of the original API.
Jeremy Hylton1afc1692008-06-18 20:49:58 +000022 self.fp = fp
Jeremy Hylton1afc1692008-06-18 20:49:58 +000023
24 def __repr__(self):
25 return '<%s at %r whose fp = %r>' % (self.__class__.__name__,
Senthil Kumaran6117e5d2014-04-20 09:41:29 -070026 id(self), self.file)
Jeremy Hylton1afc1692008-06-18 20:49:58 +000027
Jeremy Hyltonb476d592009-03-26 21:34:20 +000028 def __enter__(self):
Senthil Kumaran6117e5d2014-04-20 09:41:29 -070029 if self.fp.closed:
Jeremy Hyltonb476d592009-03-26 21:34:20 +000030 raise ValueError("I/O operation on closed file")
31 return self
32
33 def __exit__(self, type, value, traceback):
34 self.close()
35
Senthil Kumaran6117e5d2014-04-20 09:41:29 -070036
Jeremy Hylton1afc1692008-06-18 20:49:58 +000037class addclosehook(addbase):
38 """Class to add a close hook to an open file."""
39
40 def __init__(self, fp, closehook, *hookargs):
Senthil Kumaran6117e5d2014-04-20 09:41:29 -070041 super(addclosehook, self).__init__(fp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +000042 self.closehook = closehook
43 self.hookargs = hookargs
44
45 def close(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +000046 if self.closehook:
47 self.closehook(*self.hookargs)
48 self.closehook = None
49 self.hookargs = None
Senthil Kumaran6117e5d2014-04-20 09:41:29 -070050 super(addclosehook, self).close()
51
Jeremy Hylton1afc1692008-06-18 20:49:58 +000052
53class addinfo(addbase):
54 """class to add an info() method to an open file."""
55
56 def __init__(self, fp, headers):
Senthil Kumaran6117e5d2014-04-20 09:41:29 -070057 super(addinfo, self).__init__(fp)
Jeremy Hylton1afc1692008-06-18 20:49:58 +000058 self.headers = headers
59
60 def info(self):
61 return self.headers
62
Senthil Kumaran6117e5d2014-04-20 09:41:29 -070063
64class addinfourl(addinfo):
Jeremy Hylton1afc1692008-06-18 20:49:58 +000065 """class to add info() and geturl() methods to an open file."""
66
67 def __init__(self, fp, headers, url, code=None):
Senthil Kumaran6117e5d2014-04-20 09:41:29 -070068 super(addinfourl, self).__init__(fp, headers)
Jeremy Hylton1afc1692008-06-18 20:49:58 +000069 self.url = url
70 self.code = code
71
Jeremy Hylton1afc1692008-06-18 20:49:58 +000072 def getcode(self):
73 return self.code
74
75 def geturl(self):
76 return self.url