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