<s> """<STR_LIT>""" <EOL> import random <EOL> import struct <EOL> import bson <EOL> import pymongo <EOL> from bson . binary import OLD_UUID_SUBTYPE <EOL> from bson . son import SON <EOL> from pymongo . errors import ( AutoReconnect , <EOL> CursorNotFound , <EOL> DuplicateKeyError , <EOL> OperationFailure , <EOL> ExecutionTimeout , <EOL> WTimeoutError ) <EOL> def _index_list ( key_or_list , direction = None ) : <EOL> """<STR_LIT>""" <EOL> if direction is not None : <EOL> return [ ( key_or_list , direction ) ] <EOL> else : <EOL> if isinstance ( key_or_list , basestring ) : <EOL> return [ ( key_or_list , pymongo . ASCENDING ) ] <EOL> elif not isinstance ( key_or_list , ( list , tuple ) ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return key_or_list <EOL> def _index_document ( index_list ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( index_list , dict ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % list ( index_list . iteritems ( ) ) ) <EOL> elif not isinstance ( index_list , ( list , tuple ) ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" + repr ( index_list ) ) <EOL> if not len ( index_list ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> index = SON ( ) <EOL> for ( key , value ) in index_list : <EOL> if not isinstance ( key , basestring ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not isinstance ( value , ( basestring , int , dict ) ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> index [ key ] = value <EOL> return index <EOL> def _unpack_response ( response , cursor_id = None , as_class = dict , <EOL> tz_aware = False , uuid_subtype = OLD_UUID_SUBTYPE , <EOL> compile_re = True ) : <EOL> """<STR_LIT>""" <EOL> response_flag = struct . unpack ( "<STR_LIT>" , response [ : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] <EOL> if response_flag & <NUM_LIT:1> : <EOL> assert cursor_id is not None <EOL> raise CursorNotFound ( "<STR_LIT>" % <EOL> cursor_id ) <EOL> elif response_flag & <NUM_LIT:2> : <EOL> error_object = bson . BSON ( response [ <NUM_LIT:20> : ] ) . decode ( ) <EOL> if error_object [ "<STR_LIT>" ] . startswith ( "<STR_LIT>" ) : <EOL> raise AutoReconnect ( error_object [ "<STR_LIT>" ] ) <EOL> elif error_object . get ( "<STR_LIT:code>" ) == <NUM_LIT:50> : <EOL> raise ExecutionTimeout ( error_object . get ( "<STR_LIT>" ) , <EOL> error_object . get ( "<STR_LIT:code>" ) , <EOL> error_object ) <EOL> raise OperationFailure ( "<STR_LIT>" % <EOL> error_object . get ( "<STR_LIT>" ) , <EOL> error_object . get ( "<STR_LIT:code>" ) , <EOL> error_object ) <EOL> result = { } <EOL> result [ "<STR_LIT>" ] = struct . unpack ( "<STR_LIT>" , response [ <NUM_LIT:4> : <NUM_LIT:12> ] ) [ <NUM_LIT:0> ] <EOL> result [ "<STR_LIT>" ] = struct . unpack ( "<STR_LIT>" , response [ <NUM_LIT:12> : <NUM_LIT:16> ] ) [ <NUM_LIT:0> ] <EOL> result [ "<STR_LIT>" ] = struct . unpack ( "<STR_LIT>" , response [ <NUM_LIT:16> : <NUM_LIT:20> ] ) [ <NUM_LIT:0> ] <EOL> result [ "<STR_LIT:data>" ] = bson . decode_all ( response [ <NUM_LIT:20> : ] , <EOL> as_class , tz_aware , uuid_subtype , <EOL> compile_re ) <EOL> assert len ( result [ "<STR_LIT:data>" ] ) == result [ "<STR_LIT>" ] <EOL> return result <EOL> def _check_command_response ( response , reset , msg = None , allowable_errors = None ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" not in response : <EOL> raise OperationFailure ( response . get ( "<STR_LIT>" ) , <EOL> response . get ( "<STR_LIT:code>" ) , <EOL> response ) <EOL> if response . get ( "<STR_LIT>" , False ) : <EOL> raise WTimeoutError ( response . get ( "<STR_LIT>" , response . get ( "<STR_LIT>" ) ) , <EOL> response . get ( "<STR_LIT:code>" ) , <EOL> response ) <EOL> if not response [ "<STR_LIT>" ] : <EOL> details = response <EOL> if "<STR_LIT>" in response : <EOL> for shard in response [ "<STR_LIT>" ] . itervalues ( ) : <EOL> if not shard . get ( "<STR_LIT>" ) : <EOL> details = shard <EOL> break <EOL> errmsg = details [ "<STR_LIT>" ] <EOL> if allowable_errors is None or errmsg not in allowable_errors : <EOL> if ( errmsg . startswith ( "<STR_LIT>" ) <EOL> or errmsg . startswith ( "<STR_LIT>" ) ) : <EOL> if reset is not None : <EOL> reset ( ) <EOL> raise AutoReconnect ( errmsg ) <EOL> if errmsg == "<STR_LIT>" : <EOL> errmsg = ( "<STR_LIT>" % <EOL> details . get ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> raise OperationFailure ( errmsg , <EOL> details . get ( "<STR_LIT>" ) , <EOL> response ) <EOL> code = details . get ( "<STR_LIT:code>" ) <EOL> if code in ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) : <EOL> raise DuplicateKeyError ( errmsg , code , response ) <EOL> elif code == <NUM_LIT:50> : <EOL> raise ExecutionTimeout ( errmsg , code , response ) <EOL> msg = msg or "<STR_LIT:%s>" <EOL> raise OperationFailure ( msg % errmsg , code , response ) <EOL> def _check_write_command_response ( results ) : <EOL> """<STR_LIT>""" <EOL> errors = [ res for res in results <EOL> if "<STR_LIT>" in res [ <NUM_LIT:1> ] or "<STR_LIT>" in res [ <NUM_LIT:1> ] ] <EOL> if errors : <EOL> offset , result = errors [ - <NUM_LIT:1> ] <EOL> write_errors = result . get ( "<STR_LIT>" ) <EOL> if write_errors : <EOL> error = write_errors [ - <NUM_LIT:1> ] <EOL> error [ "<STR_LIT:index>" ] += offset <EOL> if error . get ( "<STR_LIT:code>" ) == <NUM_LIT> : <EOL> raise DuplicateKeyError ( error . get ( "<STR_LIT>" ) , <NUM_LIT> , error ) <EOL> else : <EOL> error = result [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in error and error [ "<STR_LIT>" ] . get ( '<STR_LIT>' ) : <EOL> raise WTimeoutError ( error . get ( "<STR_LIT>" ) , <EOL> error . get ( "<STR_LIT:code>" ) , error ) <EOL> raise OperationFailure ( error . get ( "<STR_LIT>" ) , error . get ( "<STR_LIT:code>" ) , error ) <EOL> def _fields_list_to_dict ( fields ) : <EOL> """<STR_LIT>""" <EOL> as_dict = { } <EOL> for field in fields : <EOL> if not isinstance ( field , basestring ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( basestring . __name__ , ) ) <EOL> as_dict [ field ] = <NUM_LIT:1> <EOL> return as_dict <EOL> def shuffled ( sequence ) : <EOL> """<STR_LIT>""" <EOL> out = list ( sequence ) <EOL> random . shuffle ( out ) <EOL> return out </s>
