# Copyright 2009-2010 10gen, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tools for creating `messages `_ to be sent to MongoDB. .. note:: This module is for internal use and is generally not needed by application developers. .. versionadded:: 1.1.2 """ import random import struct import bson from bson.binary import OLD_UUID_SUBTYPE from bson.son import SON try: from pymongo import _cmessage _use_c = True except ImportError: _use_c = False from pymongo.errors import InvalidOperation __ZERO = "\x00\x00\x00\x00" MAX_INT32 = 2147483647 MIN_INT32 = -2147483648 def __last_error(args): """Data to send to do a lastError. """ cmd = SON([("getlasterror", 1)]) cmd.update(args) return query(0, "admin.$cmd", 0, -1, cmd) def __pack_message(operation, data): """Takes message data and adds a message header based on the operation. Returns the resultant message string. """ request_id = random.randint(MIN_INT32, MAX_INT32) message = struct.pack("