<s> """<STR_LIT>""" <EOL> from email . encoders import encode_noop <EOL> from email . generator import Generator <EOL> from email . mime . application import MIMEApplication <EOL> from email . mime . multipart import MIMEMultipart <EOL> from email . parser import Parser <EOL> import io <EOL> import json <EOL> import httplib2 <EOL> import six <EOL> from gcloud . exceptions import make_exception <EOL> from gcloud . storage . connection import Connection <EOL> class MIMEApplicationHTTP ( MIMEApplication ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , method , uri , headers , body ) : <EOL> if isinstance ( body , dict ) : <EOL> body = json . dumps ( body ) <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> headers [ '<STR_LIT>' ] = len ( body ) <EOL> if body is None : <EOL> body = '<STR_LIT>' <EOL> lines = [ '<STR_LIT>' % ( method , uri ) ] <EOL> lines . extend ( [ '<STR_LIT>' % ( key , value ) <EOL> for key , value in sorted ( headers . items ( ) ) ] ) <EOL> lines . append ( '<STR_LIT>' ) <EOL> lines . append ( body ) <EOL> payload = '<STR_LIT:\r\n>' . join ( lines ) <EOL> if six . PY2 : <EOL> MIMEApplication . __init__ ( self , payload , '<STR_LIT:http>' , encode_noop ) <EOL> else : <EOL> super_init = super ( MIMEApplicationHTTP , self ) . __init__ <EOL> super_init ( payload , '<STR_LIT:http>' , encode_noop ) <EOL> class NoContent ( object ) : <EOL> """<STR_LIT>""" <EOL> status = <NUM_LIT> <EOL> class _FutureDict ( object ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def get ( key , default = None ) : <EOL> """<STR_LIT>""" <EOL> raise KeyError ( '<STR_LIT>' % ( <EOL> key , default ) ) <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> raise KeyError ( '<STR_LIT>' % ( key , ) ) <EOL> def __setitem__ ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> raise KeyError ( '<STR_LIT>' % ( key , value ) ) <EOL> class Batch ( Connection ) : <EOL> """<STR_LIT>""" <EOL> _MAX_BATCH_SIZE = <NUM_LIT:1000> <EOL> def __init__ ( self , client ) : <EOL> super ( Batch , self ) . __init__ ( ) <EOL> self . _client = client <EOL> self . _requests = [ ] <EOL> self . _target_objects = [ ] <EOL> def _do_request ( self , method , url , headers , data , target_object ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . _requests ) >= self . _MAX_BATCH_SIZE : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . _MAX_BATCH_SIZE ) <EOL> self . _requests . append ( ( method , url , headers , data ) ) <EOL> result = _FutureDict ( ) <EOL> self . _target_objects . append ( target_object ) <EOL> if target_object is not None : <EOL> target_object . _properties = result <EOL> return NoContent ( ) , result <EOL> def _prepare_batch_request ( self ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . _requests ) == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> multi = MIMEMultipart ( ) <EOL> for method , uri , headers , body in self . _requests : <EOL> subrequest = MIMEApplicationHTTP ( method , uri , headers , body ) <EOL> multi . attach ( subrequest ) <EOL> if six . PY3 : <EOL> buf = io . StringIO ( ) <EOL> else : <EOL> buf = io . BytesIO ( ) <EOL> generator = Generator ( buf , False , <NUM_LIT:0> ) <EOL> generator . flatten ( multi ) <EOL> payload = buf . getvalue ( ) <EOL> _ , body = payload . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> return dict ( multi . _headers ) , body <EOL> def _finish_futures ( self , responses ) : <EOL> """<STR_LIT>""" <EOL> exception_args = None <EOL> if len ( self . _target_objects ) != len ( responses ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> for target_object , sub_response in zip ( self . _target_objects , <EOL> responses ) : <EOL> resp_headers , sub_payload = sub_response <EOL> if not <NUM_LIT:200> <= resp_headers . status < <NUM_LIT> : <EOL> exception_args = exception_args or ( resp_headers , <EOL> sub_payload ) <EOL> elif target_object is not None : <EOL> target_object . _properties = sub_payload <EOL> if exception_args is not None : <EOL> raise make_exception ( * exception_args ) <EOL> def finish ( self ) : <EOL> """<STR_LIT>""" <EOL> headers , body = self . _prepare_batch_request ( ) <EOL> url = '<STR_LIT>' % self . API_BASE_URL <EOL> response , content = self . _client . _connection . _make_request ( <EOL> '<STR_LIT:POST>' , url , data = body , headers = headers ) <EOL> responses = list ( _unpack_batch_response ( response , content ) ) <EOL> self . _finish_futures ( responses ) <EOL> return responses <EOL> def current ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . current_batch <EOL> def __enter__ ( self ) : <EOL> self . _client . _push_batch ( self ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> try : <EOL> if exc_type is None : <EOL> self . finish ( ) <EOL> finally : <EOL> self . _client . _pop_batch ( ) <EOL> def _generate_faux_mime_message ( parser , response , content ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( content , six . binary_type ) : <EOL> content = content . encode ( '<STR_LIT:utf-8>' ) <EOL> content_type = response [ '<STR_LIT>' ] <EOL> if not isinstance ( content_type , six . binary_type ) : <EOL> content_type = content_type . encode ( '<STR_LIT:utf-8>' ) <EOL> faux_message = b'<STR_LIT>' . join ( [ <EOL> b'<STR_LIT>' , <EOL> content_type , <EOL> b'<STR_LIT>' , <EOL> content , <EOL> ] ) <EOL> if six . PY2 : <EOL> return parser . parsestr ( faux_message ) <EOL> else : <EOL> return parser . parsestr ( faux_message . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def _unpack_batch_response ( response , content ) : <EOL> """<STR_LIT>""" <EOL> parser = Parser ( ) <EOL> message = _generate_faux_mime_message ( parser , response , content ) <EOL> if not isinstance ( message . _payload , list ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> for subrequest in message . _payload : <EOL> status_line , rest = subrequest . _payload . split ( '<STR_LIT:\n>' , <NUM_LIT:1> ) <EOL> _ , status , _ = status_line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:2> ) <EOL> sub_message = parser . parsestr ( rest ) <EOL> payload = sub_message . _payload <EOL> ctype = sub_message [ '<STR_LIT:Content-Type>' ] <EOL> msg_headers = dict ( sub_message . _headers ) <EOL> msg_headers [ '<STR_LIT:status>' ] = status <EOL> headers = httplib2 . Response ( msg_headers ) <EOL> if ctype and ctype . startswith ( '<STR_LIT:application/json>' ) : <EOL> payload = json . loads ( payload ) <EOL> yield headers , payload </s>
