<s> """<STR_LIT>""" <EOL> import copy , mimetools <EOL> from cStringIO import StringIO <EOL> import urllib2 <EOL> def len_of_seekable ( file_ ) : <EOL> pos = file_ . tell ( ) <EOL> file_ . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> try : <EOL> return file_ . tell ( ) <EOL> finally : <EOL> file_ . seek ( pos ) <EOL> class seek_wrapper : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , wrapped ) : <EOL> self . wrapped = wrapped <EOL> self . __read_complete_state = [ False ] <EOL> self . __is_closed_state = [ False ] <EOL> self . __have_readline = hasattr ( self . wrapped , "<STR_LIT>" ) <EOL> self . __cache = StringIO ( ) <EOL> self . __pos = <NUM_LIT:0> <EOL> def invariant ( self ) : <EOL> return self . wrapped . tell ( ) == len ( self . __cache . getvalue ( ) ) <EOL> def close ( self ) : <EOL> self . wrapped . close ( ) <EOL> self . is_closed = True <EOL> def __getattr__ ( self , name ) : <EOL> if name == "<STR_LIT>" : <EOL> return self . __is_closed_state [ <NUM_LIT:0> ] <EOL> elif name == "<STR_LIT>" : <EOL> return self . __read_complete_state [ <NUM_LIT:0> ] <EOL> wrapped = self . __dict__ . get ( "<STR_LIT>" ) <EOL> if wrapped : <EOL> return getattr ( wrapped , name ) <EOL> return getattr ( self . __class__ , name ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if name == "<STR_LIT>" : <EOL> self . __is_closed_state [ <NUM_LIT:0> ] = bool ( value ) <EOL> elif name == "<STR_LIT>" : <EOL> if not self . is_closed : <EOL> self . __read_complete_state [ <NUM_LIT:0> ] = bool ( value ) <EOL> else : <EOL> self . __dict__ [ name ] = value <EOL> def seek ( self , offset , whence = <NUM_LIT:0> ) : <EOL> assert whence in [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> if whence == <NUM_LIT:2> : <EOL> if offset < <NUM_LIT:0> : raise ValueError ( "<STR_LIT>" ) <EOL> to_read = None <EOL> else : <EOL> if whence == <NUM_LIT:0> : <EOL> if offset < <NUM_LIT:0> : raise ValueError ( "<STR_LIT>" ) <EOL> dest = offset <EOL> else : <EOL> pos = self . __pos <EOL> if pos < offset : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> dest = pos + offset <EOL> end = len_of_seekable ( self . __cache ) <EOL> to_read = dest - end <EOL> if to_read < <NUM_LIT:0> : <EOL> to_read = <NUM_LIT:0> <EOL> if to_read != <NUM_LIT:0> : <EOL> self . __cache . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> if to_read is None : <EOL> assert whence == <NUM_LIT:2> <EOL> self . __cache . write ( self . wrapped . read ( ) ) <EOL> self . read_complete = True <EOL> self . __pos = self . __cache . tell ( ) - offset <EOL> else : <EOL> data = self . wrapped . read ( to_read ) <EOL> if not data : <EOL> self . read_complete = True <EOL> else : <EOL> self . __cache . write ( data ) <EOL> self . __pos = dest <EOL> else : <EOL> self . __pos = dest <EOL> def tell ( self ) : <EOL> return self . __pos <EOL> def __copy__ ( self ) : <EOL> cpy = self . __class__ ( self . wrapped ) <EOL> cpy . __cache = self . __cache <EOL> cpy . __read_complete_state = self . __read_complete_state <EOL> cpy . __is_closed_state = self . __is_closed_state <EOL> return cpy <EOL> def get_data ( self ) : <EOL> pos = self . __pos <EOL> try : <EOL> self . seek ( <NUM_LIT:0> ) <EOL> return self . read ( - <NUM_LIT:1> ) <EOL> finally : <EOL> self . __pos = pos <EOL> def read ( self , size = - <NUM_LIT:1> ) : <EOL> pos = self . __pos <EOL> end = len_of_seekable ( self . __cache ) <EOL> available = end - pos <EOL> if size <= available and size != - <NUM_LIT:1> : <EOL> self . __cache . seek ( pos ) <EOL> self . __pos = pos + size <EOL> return self . __cache . read ( size ) <EOL> self . __cache . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> if size == - <NUM_LIT:1> : <EOL> self . __cache . write ( self . wrapped . read ( ) ) <EOL> self . read_complete = True <EOL> else : <EOL> to_read = size - available <EOL> assert to_read > <NUM_LIT:0> <EOL> data = self . wrapped . read ( to_read ) <EOL> if not data : <EOL> self . read_complete = True <EOL> else : <EOL> self . __cache . write ( data ) <EOL> self . __cache . seek ( pos ) <EOL> data = self . __cache . read ( size ) <EOL> self . __pos = self . __cache . tell ( ) <EOL> assert self . __pos == pos + len ( data ) <EOL> return data <EOL> def readline ( self , size = - <NUM_LIT:1> ) : <EOL> if not self . __have_readline : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> pos = self . __pos <EOL> self . __cache . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> data = self . wrapped . readline ( ) <EOL> if not data : <EOL> self . read_complete = True <EOL> else : <EOL> self . __cache . write ( data ) <EOL> self . __cache . seek ( pos ) <EOL> data = self . __cache . readline ( ) <EOL> if size != - <NUM_LIT:1> : <EOL> r = data [ : size ] <EOL> self . __pos = pos + size <EOL> else : <EOL> r = data <EOL> self . __pos = pos + len ( data ) <EOL> return r <EOL> def readlines ( self , sizehint = - <NUM_LIT:1> ) : <EOL> pos = self . __pos <EOL> self . __cache . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> self . __cache . write ( self . wrapped . read ( ) ) <EOL> self . read_complete = True <EOL> self . __cache . seek ( pos ) <EOL> data = self . __cache . readlines ( sizehint ) <EOL> self . __pos = self . __cache . tell ( ) <EOL> return data <EOL> def __iter__ ( self ) : return self <EOL> def next ( self ) : <EOL> line = self . readline ( ) <EOL> if line == "<STR_LIT>" : raise StopIteration <EOL> return line <EOL> xreadlines = __iter__ <EOL> def __repr__ ( self ) : <EOL> return ( "<STR_LIT>" % <EOL> ( self . __class__ . __name__ , hex ( abs ( id ( self ) ) ) , self . wrapped ) ) <EOL> class response_seek_wrapper ( seek_wrapper ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , wrapped ) : <EOL> seek_wrapper . __init__ ( self , wrapped ) <EOL> self . _headers = self . wrapped . info ( ) <EOL> def __copy__ ( self ) : <EOL> cpy = seek_wrapper . __copy__ ( self ) <EOL> cpy . _headers = copy . copy ( self . info ( ) ) <EOL> return cpy <EOL> def info ( self ) : <EOL> return self . _headers <EOL> def geturl ( self ) : <EOL> return self . wrapped . geturl ( ) <EOL> def set_data ( self , data ) : <EOL> self . seek ( <NUM_LIT:0> ) <EOL> self . read ( ) <EOL> self . close ( ) <EOL> cache = self . _seek_wrapper__cache = StringIO ( ) <EOL> cache . write ( data ) <EOL> self . seek ( <NUM_LIT:0> ) <EOL> class eoffile : <EOL> def read ( self , size = - <NUM_LIT:1> ) : return "<STR_LIT>" <EOL> def readline ( self , size = - <NUM_LIT:1> ) : return "<STR_LIT>" <EOL> def __iter__ ( self ) : return self <EOL> def next ( self ) : return "<STR_LIT>" <EOL> def close ( self ) : pass <EOL> class eofresponse ( eoffile ) : <EOL> def __init__ ( self , url , headers , code , msg ) : <EOL> self . _url = url <EOL> self . _headers = headers <EOL> self . code = code <EOL> self . msg = msg <EOL> def geturl ( self ) : return self . _url <EOL> def info ( self ) : return self . _headers <EOL> class closeable_response : <EOL> """<STR_LIT>""" <EOL> closeable_response = None <EOL> def __init__ ( self , fp , headers , url , code , msg ) : <EOL> self . _set_fp ( fp ) <EOL> self . _headers = headers <EOL> self . _url = url <EOL> self . code = code <EOL> self . msg = msg <EOL> def _set_fp ( self , fp ) : <EOL> self . fp = fp <EOL> self . read = self . fp . read <EOL> self . readline = self . fp . readline <EOL> if hasattr ( self . fp , "<STR_LIT>" ) : self . readlines = self . fp . readlines <EOL> if hasattr ( self . fp , "<STR_LIT>" ) : <EOL> self . fileno = self . fp . fileno <EOL> else : <EOL> self . fileno = lambda : None <EOL> self . __iter__ = self . fp . __iter__ <EOL> self . next = self . fp . next <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , hex ( abs ( id ( self ) ) ) , self . fp ) <EOL> def info ( self ) : <EOL> return self . _headers <EOL> def geturl ( self ) : <EOL> return self . _url <EOL> def close ( self ) : <EOL> wrapped = self . fp <EOL> wrapped . close ( ) <EOL> new_wrapped = eofresponse ( <EOL> self . _url , self . _headers , self . code , self . msg ) <EOL> self . _set_fp ( new_wrapped ) <EOL> def __getstate__ ( self ) : <EOL> state = self . __dict__ . copy ( ) <EOL> new_wrapped = eofresponse ( <EOL> self . _url , self . _headers , self . code , self . msg ) <EOL> state [ "<STR_LIT>" ] = new_wrapped <EOL> return state <EOL> def test_response ( data = '<STR_LIT>' , headers = [ ] , <EOL> url = "<STR_LIT>" , code = <NUM_LIT:200> , msg = "<STR_LIT:OK>" ) : <EOL> return make_response ( data , headers , url , code , msg ) <EOL> def test_html_response ( data = '<STR_LIT>' , headers = [ ] , <EOL> url = "<STR_LIT>" , code = <NUM_LIT:200> , msg = "<STR_LIT:OK>" ) : <EOL> headers += [ ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> return make_response ( data , headers , url , code , msg ) <EOL> def make_response ( data , headers , url , code , msg ) : <EOL> """<STR_LIT>""" <EOL> mime_headers = make_headers ( headers ) <EOL> r = closeable_response ( StringIO ( data ) , mime_headers , url , code , msg ) <EOL> return response_seek_wrapper ( r ) <EOL> def make_headers ( headers ) : <EOL> """<STR_LIT>""" <EOL> hdr_text = [ ] <EOL> for name_value in headers : <EOL> hdr_text . append ( "<STR_LIT>" % name_value ) <EOL> return mimetools . Message ( StringIO ( "<STR_LIT:\n>" . join ( hdr_text ) ) ) <EOL> def get_seek_wrapper_class ( response ) : <EOL> if ( isinstance ( response , urllib2 . HTTPError ) and <EOL> not hasattr ( response , "<STR_LIT>" ) ) : <EOL> if response . __class__ . __module__ == "<STR_LIT>" : <EOL> exc_class_name = response . __class__ . __name__ <EOL> else : <EOL> exc_class_name = "<STR_LIT>" % ( <EOL> response . __class__ . __module__ , response . __class__ . __name__ ) <EOL> class httperror_seek_wrapper ( response_seek_wrapper , response . __class__ ) : <EOL> _exc_class_name = exc_class_name <EOL> def __init__ ( self , wrapped ) : <EOL> response_seek_wrapper . __init__ ( self , wrapped ) <EOL> self . hdrs = wrapped . info ( ) <EOL> self . filename = wrapped . geturl ( ) <EOL> def __repr__ ( self ) : <EOL> return ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , self . _exc_class_name , <EOL> hex ( abs ( id ( self ) ) ) , self . wrapped ) <EOL> ) <EOL> wrapper_class = httperror_seek_wrapper <EOL> else : <EOL> wrapper_class = response_seek_wrapper <EOL> return wrapper_class <EOL> def seek_wrapped_response ( response ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( response , "<STR_LIT>" ) : <EOL> wrapper_class = get_seek_wrapper_class ( response ) <EOL> response = wrapper_class ( response ) <EOL> assert hasattr ( response , "<STR_LIT>" ) <EOL> return response <EOL> def upgrade_response ( response ) : <EOL> """<STR_LIT>""" <EOL> wrapper_class = get_seek_wrapper_class ( response ) <EOL> if hasattr ( response , "<STR_LIT>" ) : <EOL> if not hasattr ( response , "<STR_LIT>" ) : <EOL> response = wrapper_class ( response ) <EOL> assert hasattr ( response , "<STR_LIT>" ) <EOL> return copy . copy ( response ) <EOL> try : <EOL> code = response . code <EOL> except AttributeError : <EOL> code = None <EOL> try : <EOL> msg = response . msg <EOL> except AttributeError : <EOL> msg = None <EOL> data = None <EOL> get_data = getattr ( response , "<STR_LIT>" , None ) <EOL> if get_data : <EOL> data = get_data ( ) <EOL> response = closeable_response ( <EOL> response . fp , response . info ( ) , response . geturl ( ) , code , msg ) <EOL> response = wrapper_class ( response ) <EOL> if data : <EOL> response . set_data ( data ) <EOL> return response </s>
