<s> """<STR_LIT>""" <EOL> import random <EOL> import struct <EOL> import bson <EOL> from bson . son import SON <EOL> try : <EOL> from pymongo import _cbson <EOL> _use_c = True <EOL> except ImportError : <EOL> _use_c = False <EOL> from pymongo . errors import InvalidOperation <EOL> __ZERO = "<STR_LIT>" <EOL> def __last_error ( args ) : <EOL> """<STR_LIT>""" <EOL> cmd = SON ( [ ( "<STR_LIT>" , <NUM_LIT:1> ) ] ) <EOL> cmd . update ( args ) <EOL> return query ( <NUM_LIT:0> , "<STR_LIT>" , <NUM_LIT:0> , - <NUM_LIT:1> , cmd ) <EOL> def __pack_message ( operation , data ) : <EOL> """<STR_LIT>""" <EOL> request_id = random . randint ( - <NUM_LIT:2> ** <NUM_LIT> - <NUM_LIT:1> , <NUM_LIT:2> ** <NUM_LIT> ) <EOL> message = struct . pack ( "<STR_LIT>" , <NUM_LIT:16> + len ( data ) ) <EOL> message += struct . pack ( "<STR_LIT>" , request_id ) <EOL> message += __ZERO <EOL> message += struct . pack ( "<STR_LIT>" , operation ) <EOL> return ( request_id , message + data ) <EOL> def insert ( collection_name , docs , check_keys , safe , last_error_args ) : <EOL> """<STR_LIT>""" <EOL> data = __ZERO <EOL> data += bson . _make_c_string ( collection_name ) <EOL> bson_data = "<STR_LIT>" . join ( [ bson . BSON . encode ( doc , check_keys ) for doc in docs ] ) <EOL> if not bson_data : <EOL> raise InvalidOperation ( "<STR_LIT>" ) <EOL> data += bson_data <EOL> if safe : <EOL> ( _ , insert_message ) = __pack_message ( <NUM_LIT> , data ) <EOL> ( request_id , error_message ) = __last_error ( last_error_args ) <EOL> return ( request_id , insert_message + error_message ) <EOL> else : <EOL> return __pack_message ( <NUM_LIT> , data ) <EOL> if _use_c : <EOL> insert = _cbson . _insert_message <EOL> def update ( collection_name , upsert , multi , spec , doc , safe , last_error_args ) : <EOL> """<STR_LIT>""" <EOL> options = <NUM_LIT:0> <EOL> if upsert : <EOL> options += <NUM_LIT:1> <EOL> if multi : <EOL> options += <NUM_LIT:2> <EOL> data = __ZERO <EOL> data += bson . _make_c_string ( collection_name ) <EOL> data += struct . pack ( "<STR_LIT>" , options ) <EOL> data += bson . BSON . encode ( spec ) <EOL> data += bson . BSON . encode ( doc ) <EOL> if safe : <EOL> ( _ , update_message ) = __pack_message ( <NUM_LIT> , data ) <EOL> ( request_id , error_message ) = __last_error ( last_error_args ) <EOL> return ( request_id , update_message + error_message ) <EOL> else : <EOL> return __pack_message ( <NUM_LIT> , data ) <EOL> if _use_c : <EOL> update = _cbson . _update_message <EOL> def query ( options , collection_name , <EOL> num_to_skip , num_to_return , query , field_selector = None ) : <EOL> """<STR_LIT>""" <EOL> data = struct . pack ( "<STR_LIT>" , options ) <EOL> data += bson . _make_c_string ( collection_name ) <EOL> data += struct . pack ( "<STR_LIT>" , num_to_skip ) <EOL> data += struct . pack ( "<STR_LIT>" , num_to_return ) <EOL> data += bson . BSON . encode ( query ) <EOL> if field_selector is not None : <EOL> data += bson . BSON . encode ( field_selector ) <EOL> return __pack_message ( <NUM_LIT> , data ) <EOL> if _use_c : <EOL> query = _cbson . _query_message <EOL> def get_more ( collection_name , num_to_return , cursor_id ) : <EOL> """<STR_LIT>""" <EOL> data = __ZERO <EOL> data += bson . _make_c_string ( collection_name ) <EOL> data += struct . pack ( "<STR_LIT>" , num_to_return ) <EOL> data += struct . pack ( "<STR_LIT>" , cursor_id ) <EOL> return __pack_message ( <NUM_LIT> , data ) <EOL> if _use_c : <EOL> get_more = _cbson . _get_more_message <EOL> def delete ( collection_name , spec , safe , last_error_args ) : <EOL> """<STR_LIT>""" <EOL> data = __ZERO <EOL> data += bson . _make_c_string ( collection_name ) <EOL> data += __ZERO <EOL> data += bson . BSON . encode ( spec ) <EOL> if safe : <EOL> ( _ , remove_message ) = __pack_message ( <NUM_LIT> , data ) <EOL> ( request_id , error_message ) = __last_error ( last_error_args ) <EOL> return ( request_id , remove_message + error_message ) <EOL> else : <EOL> return __pack_message ( <NUM_LIT> , data ) <EOL> def kill_cursors ( cursor_ids ) : <EOL> """<STR_LIT>""" <EOL> data = __ZERO <EOL> data += struct . pack ( "<STR_LIT>" , len ( cursor_ids ) ) <EOL> for cursor_id in cursor_ids : <EOL> data += struct . pack ( "<STR_LIT>" , cursor_id ) <EOL> return __pack_message ( <NUM_LIT> , data ) </s>
