<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> import StringIO <EOL> import copy <EOL> import gzip <EOL> import httplib2 <EOL> import mimeparse <EOL> import mimetypes <EOL> import os <EOL> import urllib <EOL> import urlparse <EOL> import uuid <EOL> from email . mime . multipart import MIMEMultipart <EOL> from email . mime . nonmultipart import MIMENonMultipart <EOL> from email . parser import FeedParser <EOL> from errors import BatchError <EOL> from errors import HttpError <EOL> from errors import ResumableUploadError <EOL> from errors import UnexpectedBodyError <EOL> from errors import UnexpectedMethodError <EOL> from model import JsonModel <EOL> from oauth2client . anyjson import simplejson <EOL> class MediaUploadProgress ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resumable_progress , total_size ) : <EOL> """<STR_LIT>""" <EOL> self . resumable_progress = resumable_progress <EOL> self . total_size = total_size <EOL> def progress ( self ) : <EOL> """<STR_LIT>""" <EOL> return float ( self . resumable_progress ) / float ( self . total_size ) <EOL> class MediaUpload ( object ) : <EOL> """<STR_LIT>""" <EOL> def getbytes ( self , begin , end ) : <EOL> raise NotImplementedError ( ) <EOL> def size ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def chunksize ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def mimetype ( self ) : <EOL> return '<STR_LIT>' <EOL> def resumable ( self ) : <EOL> return False <EOL> def _to_json ( self , strip = None ) : <EOL> """<STR_LIT>""" <EOL> t = type ( self ) <EOL> d = copy . copy ( self . __dict__ ) <EOL> if strip is not None : <EOL> for member in strip : <EOL> del d [ member ] <EOL> d [ '<STR_LIT>' ] = t . __name__ <EOL> d [ '<STR_LIT>' ] = t . __module__ <EOL> return simplejson . dumps ( d ) <EOL> def to_json ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _to_json ( ) <EOL> @ classmethod <EOL> def new_from_json ( cls , s ) : <EOL> """<STR_LIT>""" <EOL> data = simplejson . loads ( s ) <EOL> module = data [ '<STR_LIT>' ] <EOL> m = __import__ ( module , fromlist = module . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) <EOL> kls = getattr ( m , data [ '<STR_LIT>' ] ) <EOL> from_json = getattr ( kls , '<STR_LIT>' ) <EOL> return from_json ( s ) <EOL> class MediaFileUpload ( MediaUpload ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename , mimetype = None , chunksize = <NUM_LIT> * <NUM_LIT> , resumable = False ) : <EOL> """<STR_LIT>""" <EOL> self . _filename = filename <EOL> self . _size = os . path . getsize ( filename ) <EOL> self . _fd = None <EOL> if mimetype is None : <EOL> ( mimetype , encoding ) = mimetypes . guess_type ( filename ) <EOL> self . _mimetype = mimetype <EOL> self . _chunksize = chunksize <EOL> self . _resumable = resumable <EOL> def mimetype ( self ) : <EOL> return self . _mimetype <EOL> def size ( self ) : <EOL> return self . _size <EOL> def chunksize ( self ) : <EOL> return self . _chunksize <EOL> def resumable ( self ) : <EOL> return self . _resumable <EOL> def getbytes ( self , begin , length ) : <EOL> """<STR_LIT>""" <EOL> if self . _fd is None : <EOL> self . _fd = open ( self . _filename , '<STR_LIT:rb>' ) <EOL> self . _fd . seek ( begin ) <EOL> return self . _fd . read ( length ) <EOL> def to_json ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _to_json ( [ '<STR_LIT>' ] ) <EOL> @ staticmethod <EOL> def from_json ( s ) : <EOL> d = simplejson . loads ( s ) <EOL> return MediaFileUpload ( <EOL> d [ '<STR_LIT>' ] , d [ '<STR_LIT>' ] , d [ '<STR_LIT>' ] , d [ '<STR_LIT>' ] ) <EOL> class HttpRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , http , postproc , uri , <EOL> method = '<STR_LIT:GET>' , <EOL> body = None , <EOL> headers = None , <EOL> methodId = None , <EOL> resumable = None ) : <EOL> """<STR_LIT>""" <EOL> self . uri = uri <EOL> self . method = method <EOL> self . body = body <EOL> self . headers = headers or { } <EOL> self . methodId = methodId <EOL> self . http = http <EOL> self . postproc = postproc <EOL> self . resumable = resumable <EOL> major , minor , params = mimeparse . parse_mime_type ( <EOL> headers . get ( '<STR_LIT>' , '<STR_LIT:application/json>' ) ) <EOL> self . body_size = len ( self . body or '<STR_LIT>' ) <EOL> self . resumable_uri = None <EOL> self . resumable_progress = <NUM_LIT:0> <EOL> def execute ( self , http = None ) : <EOL> """<STR_LIT>""" <EOL> if http is None : <EOL> http = self . http <EOL> if self . resumable : <EOL> body = None <EOL> while body is None : <EOL> _ , body = self . next_chunk ( http ) <EOL> return body <EOL> else : <EOL> resp , content = http . request ( self . uri , self . method , <EOL> body = self . body , <EOL> headers = self . headers ) <EOL> if resp . status >= <NUM_LIT> : <EOL> raise HttpError ( resp , content , self . uri ) <EOL> return self . postproc ( resp , content ) <EOL> def next_chunk ( self , http = None ) : <EOL> """<STR_LIT>""" <EOL> if http is None : <EOL> http = self . http <EOL> if self . resumable_uri is None : <EOL> start_headers = copy . copy ( self . headers ) <EOL> start_headers [ '<STR_LIT>' ] = self . resumable . mimetype ( ) <EOL> start_headers [ '<STR_LIT>' ] = str ( self . resumable . size ( ) ) <EOL> start_headers [ '<STR_LIT>' ] = str ( self . body_size ) <EOL> resp , content = http . request ( self . uri , self . method , <EOL> body = self . body , <EOL> headers = start_headers ) <EOL> if resp . status == <NUM_LIT:200> and '<STR_LIT:location>' in resp : <EOL> self . resumable_uri = resp [ '<STR_LIT:location>' ] <EOL> else : <EOL> raise ResumableUploadError ( "<STR_LIT>" ) <EOL> data = self . resumable . getbytes ( self . resumable_progress , <EOL> self . resumable . chunksize ( ) ) <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' % ( <EOL> self . resumable_progress , self . resumable_progress + len ( data ) - <NUM_LIT:1> , <EOL> self . resumable . size ( ) ) , <EOL> } <EOL> resp , content = http . request ( self . resumable_uri , '<STR_LIT>' , <EOL> body = data , <EOL> headers = headers ) <EOL> if resp . status in [ <NUM_LIT:200> , <NUM_LIT> ] : <EOL> return None , self . postproc ( resp , content ) <EOL> elif resp . status == <NUM_LIT> : <EOL> self . resumable_progress = int ( resp [ '<STR_LIT>' ] . split ( '<STR_LIT:->' ) [ <NUM_LIT:1> ] ) + <NUM_LIT:1> <EOL> if '<STR_LIT:location>' in resp : <EOL> self . resumable_uri = resp [ '<STR_LIT:location>' ] <EOL> else : <EOL> raise HttpError ( resp , content , self . uri ) <EOL> return ( MediaUploadProgress ( self . resumable_progress , self . resumable . size ( ) ) , <EOL> None ) <EOL> def to_json ( self ) : <EOL> """<STR_LIT>""" <EOL> d = copy . copy ( self . __dict__ ) <EOL> if d [ '<STR_LIT>' ] is not None : <EOL> d [ '<STR_LIT>' ] = self . resumable . to_json ( ) <EOL> del d [ '<STR_LIT:http>' ] <EOL> del d [ '<STR_LIT>' ] <EOL> return simplejson . dumps ( d ) <EOL> @ staticmethod <EOL> def from_json ( s , http , postproc ) : <EOL> """<STR_LIT>""" <EOL> d = simplejson . loads ( s ) <EOL> if d [ '<STR_LIT>' ] is not None : <EOL> d [ '<STR_LIT>' ] = MediaUpload . new_from_json ( d [ '<STR_LIT>' ] ) <EOL> return HttpRequest ( <EOL> http , <EOL> postproc , <EOL> uri = d [ '<STR_LIT>' ] , <EOL> method = d [ '<STR_LIT>' ] , <EOL> body = d [ '<STR_LIT:body>' ] , <EOL> headers = d [ '<STR_LIT>' ] , <EOL> methodId = d [ '<STR_LIT>' ] , <EOL> resumable = d [ '<STR_LIT>' ] ) <EOL> class BatchHttpRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , callback = None , batch_uri = None ) : <EOL> """<STR_LIT>""" <EOL> if batch_uri is None : <EOL> batch_uri = '<STR_LIT>' <EOL> self . _batch_uri = batch_uri <EOL> self . _callback = callback <EOL> self . _requests = { } <EOL> self . _order = [ ] <EOL> self . _last_auto_id = <NUM_LIT:0> <EOL> self . _base_id = None <EOL> def _id_to_header ( self , id_ ) : <EOL> """<STR_LIT>""" <EOL> if self . _base_id is None : <EOL> self . _base_id = uuid . uuid4 ( ) <EOL> return '<STR_LIT>' % ( self . _base_id , urllib . quote ( id_ ) ) <EOL> def _header_to_id ( self , header ) : <EOL> """<STR_LIT>""" <EOL> if header [ <NUM_LIT:0> ] != '<STR_LIT:<>' or header [ - <NUM_LIT:1> ] != '<STR_LIT:>>' : <EOL> raise BatchError ( "<STR_LIT>" % header ) <EOL> if '<STR_LIT:+>' not in header : <EOL> raise BatchError ( "<STR_LIT>" % header ) <EOL> base , id_ = header [ <NUM_LIT:1> : - <NUM_LIT:1> ] . rsplit ( '<STR_LIT:+>' , <NUM_LIT:1> ) <EOL> return urllib . unquote ( id_ ) <EOL> def _serialize_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> parsed = urlparse . urlparse ( request . uri ) <EOL> request_line = urlparse . urlunparse ( <EOL> ( None , None , parsed . path , parsed . params , parsed . query , None ) <EOL> ) <EOL> status_line = request . method + '<STR_LIT:U+0020>' + request_line + '<STR_LIT>' <EOL> major , minor = request . headers . get ( '<STR_LIT>' , '<STR_LIT:application/json>' ) . split ( '<STR_LIT:/>' ) <EOL> msg = MIMENonMultipart ( major , minor ) <EOL> headers = request . headers . copy ( ) <EOL> if '<STR_LIT>' in headers : <EOL> del headers [ '<STR_LIT>' ] <EOL> for key , value in headers . iteritems ( ) : <EOL> msg [ key ] = value <EOL> msg [ '<STR_LIT>' ] = parsed . netloc <EOL> msg . set_unixfrom ( None ) <EOL> if request . body is not None : <EOL> msg . set_payload ( request . body ) <EOL> msg [ '<STR_LIT>' ] = str ( len ( request . body ) ) <EOL> body = msg . as_string ( False ) <EOL> if request . body is None : <EOL> body = body [ : - <NUM_LIT:2> ] <EOL> return status_line . encode ( '<STR_LIT:utf-8>' ) + body <EOL> def _deserialize_response ( self , payload ) : <EOL> """<STR_LIT>""" <EOL> status_line , payload = payload . split ( '<STR_LIT:\n>' , <NUM_LIT:1> ) <EOL> protocol , status , reason = status_line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:2> ) <EOL> parser = FeedParser ( ) <EOL> parser . feed ( payload ) <EOL> msg = parser . close ( ) <EOL> msg [ '<STR_LIT:status>' ] = status <EOL> resp = httplib2 . Response ( msg ) <EOL> resp . reason = reason <EOL> resp . version = int ( protocol . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . replace ( '<STR_LIT:.>' , '<STR_LIT>' ) ) <EOL> content = payload . split ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> return resp , content <EOL> def _new_id ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _last_auto_id += <NUM_LIT:1> <EOL> while str ( self . _last_auto_id ) in self . _requests : <EOL> self . _last_auto_id += <NUM_LIT:1> <EOL> return str ( self . _last_auto_id ) <EOL> def add ( self , request , callback = None , request_id = None ) : <EOL> """<STR_LIT>""" <EOL> if request_id is None : <EOL> request_id = self . _new_id ( ) <EOL> if request . resumable is not None : <EOL> raise BatchError ( "<STR_LIT>" ) <EOL> if request_id in self . _requests : <EOL> raise KeyError ( "<STR_LIT>" % request_id ) <EOL> self . _requests [ request_id ] = ( request , callback ) <EOL> self . _order . append ( request_id ) <EOL> def execute ( self , http = None ) : <EOL> """<STR_LIT>""" <EOL> if http is None : <EOL> for request_id in self . _order : <EOL> request , callback = self . _requests [ request_id ] <EOL> if request is not None : <EOL> http = request . http <EOL> break <EOL> if http is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> msgRoot = MIMEMultipart ( '<STR_LIT>' ) <EOL> setattr ( msgRoot , '<STR_LIT>' , lambda self : None ) <EOL> for request_id in self . _order : <EOL> request , callback = self . _requests [ request_id ] <EOL> msg = MIMENonMultipart ( '<STR_LIT>' , '<STR_LIT:http>' ) <EOL> msg [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> msg [ '<STR_LIT>' ] = self . _id_to_header ( request_id ) <EOL> body = self . _serialize_request ( request ) <EOL> msg . set_payload ( body ) <EOL> msgRoot . attach ( msg ) <EOL> body = msgRoot . as_string ( ) <EOL> headers = { } <EOL> headers [ '<STR_LIT>' ] = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % msgRoot . get_boundary ( ) <EOL> resp , content = http . request ( self . _batch_uri , '<STR_LIT:POST>' , body = body , <EOL> headers = headers ) <EOL> if resp . status >= <NUM_LIT> : <EOL> raise HttpError ( resp , content , self . _batch_uri ) <EOL> boundary , _ = content . split ( None , <NUM_LIT:1> ) <EOL> header = '<STR_LIT>' % resp [ '<STR_LIT>' ] <EOL> for_parser = header + content <EOL> parser = FeedParser ( ) <EOL> parser . feed ( for_parser ) <EOL> respRoot = parser . close ( ) <EOL> if not respRoot . is_multipart ( ) : <EOL> raise BatchError ( "<STR_LIT>" , resp , <EOL> content ) <EOL> parts = respRoot . get_payload ( ) <EOL> for part in parts : <EOL> request_id = self . _header_to_id ( part [ '<STR_LIT>' ] ) <EOL> headers , content = self . _deserialize_response ( part . get_payload ( ) ) <EOL> if content [ <NUM_LIT:0> ] != '<STR_LIT:{>' : <EOL> gzipped_content = content <EOL> content = gzip . GzipFile ( <EOL> fileobj = StringIO . StringIO ( gzipped_content ) ) . read ( ) <EOL> request , cb = self . _requests [ request_id ] <EOL> postproc = request . postproc <EOL> response = postproc ( resp , content ) <EOL> if cb is not None : <EOL> cb ( request_id , response ) <EOL> if self . _callback is not None : <EOL> self . _callback ( request_id , response ) <EOL> class HttpRequestMock ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , resp , content , postproc ) : <EOL> """<STR_LIT>""" <EOL> self . resp = resp <EOL> self . content = content <EOL> self . postproc = postproc <EOL> if resp is None : <EOL> self . resp = httplib2 . Response ( { '<STR_LIT:status>' : <NUM_LIT:200> , '<STR_LIT>' : '<STR_LIT:OK>' } ) <EOL> if '<STR_LIT>' in self . resp : <EOL> self . resp . reason = self . resp [ '<STR_LIT>' ] <EOL> def execute ( self , http = None ) : <EOL> """<STR_LIT>""" <EOL> return self . postproc ( self . resp , self . content ) <EOL> class RequestMockBuilder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , responses , check_unexpected = False ) : <EOL> """<STR_LIT>""" <EOL> self . responses = responses <EOL> self . check_unexpected = check_unexpected <EOL> def __call__ ( self , http , postproc , uri , method = '<STR_LIT:GET>' , body = None , <EOL> headers = None , methodId = None , resumable = None ) : <EOL> """<STR_LIT>""" <EOL> if methodId in self . responses : <EOL> response = self . responses [ methodId ] <EOL> resp , content = response [ : <NUM_LIT:2> ] <EOL> if len ( response ) > <NUM_LIT:2> : <EOL> expected_body = response [ <NUM_LIT:2> ] <EOL> if bool ( expected_body ) != bool ( body ) : <EOL> raise UnexpectedBodyError ( expected_body , body ) <EOL> if isinstance ( expected_body , str ) : <EOL> expected_body = simplejson . loads ( expected_body ) <EOL> body = simplejson . loads ( body ) <EOL> if body != expected_body : <EOL> raise UnexpectedBodyError ( expected_body , body ) <EOL> return HttpRequestMock ( resp , content , postproc ) <EOL> elif self . check_unexpected : <EOL> raise UnexpectedMethodError ( methodId ) <EOL> else : <EOL> model = JsonModel ( False ) <EOL> return HttpRequestMock ( None , '<STR_LIT:{}>' , model . response ) <EOL> class HttpMock ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> headers = { '<STR_LIT:status>' : '<STR_LIT>' } <EOL> f = file ( filename , '<STR_LIT:r>' ) <EOL> self . data = f . read ( ) <EOL> f . close ( ) <EOL> self . headers = headers <EOL> def request ( self , uri , <EOL> method = '<STR_LIT:GET>' , <EOL> body = None , <EOL> headers = None , <EOL> redirections = <NUM_LIT:1> , <EOL> connection_type = None ) : <EOL> return httplib2 . Response ( self . headers ) , self . data <EOL> class HttpMockSequence ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , iterable ) : <EOL> """<STR_LIT>""" <EOL> self . _iterable = iterable <EOL> def request ( self , uri , <EOL> method = '<STR_LIT:GET>' , <EOL> body = None , <EOL> headers = None , <EOL> redirections = <NUM_LIT:1> , <EOL> connection_type = None ) : <EOL> resp , content = self . _iterable . pop ( <NUM_LIT:0> ) <EOL> if content == '<STR_LIT>' : <EOL> content = headers <EOL> elif content == '<STR_LIT>' : <EOL> content = simplejson . dumps ( headers ) <EOL> elif content == '<STR_LIT>' : <EOL> content = body <EOL> elif content == '<STR_LIT>' : <EOL> content = uri <EOL> return httplib2 . Response ( resp ) , content <EOL> def set_user_agent ( http , user_agent ) : <EOL> """<STR_LIT>""" <EOL> request_orig = http . request <EOL> def new_request ( uri , method = '<STR_LIT:GET>' , body = None , headers = None , <EOL> redirections = httplib2 . DEFAULT_MAX_REDIRECTS , <EOL> connection_type = None ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> headers = { } <EOL> if '<STR_LIT>' in headers : <EOL> headers [ '<STR_LIT>' ] = user_agent + '<STR_LIT:U+0020>' + headers [ '<STR_LIT>' ] <EOL> else : <EOL> headers [ '<STR_LIT>' ] = user_agent <EOL> resp , content = request_orig ( uri , method , body , headers , <EOL> redirections , connection_type ) <EOL> return resp , content <EOL> http . request = new_request <EOL> return http <EOL> def tunnel_patch ( http ) : <EOL> """<STR_LIT>""" <EOL> request_orig = http . request <EOL> def new_request ( uri , method = '<STR_LIT:GET>' , body = None , headers = None , <EOL> redirections = httplib2 . DEFAULT_MAX_REDIRECTS , <EOL> connection_type = None ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> headers = { } <EOL> if method == '<STR_LIT>' : <EOL> if '<STR_LIT>' in headers . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> logging . warning ( <EOL> '<STR_LIT>' ) <EOL> headers [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> method = '<STR_LIT:POST>' <EOL> resp , content = request_orig ( uri , method , body , headers , <EOL> redirections , connection_type ) <EOL> return resp , content <EOL> http . request = new_request <EOL> return http </s>
