<s> """<STR_LIT>""" <EOL> import datetime <EOL> from collections import deque <EOL> from bson . py3compat import integer_types <EOL> from pymongo import helpers <EOL> from pymongo . errors import AutoReconnect , NotMasterError , OperationFailure <EOL> from pymongo . message import _CursorAddress , _GetMore , _convert_exception <EOL> class CommandCursor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , collection , cursor_info , address , retrieved = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . __collection = collection <EOL> self . __id = cursor_info [ '<STR_LIT:id>' ] <EOL> self . __address = address <EOL> self . __data = deque ( cursor_info [ '<STR_LIT>' ] ) <EOL> self . __retrieved = retrieved <EOL> self . __batch_size = <NUM_LIT:0> <EOL> self . __killed = ( self . __id == <NUM_LIT:0> ) <EOL> if "<STR_LIT>" in cursor_info : <EOL> self . __ns = cursor_info [ "<STR_LIT>" ] <EOL> else : <EOL> self . __ns = collection . full_name <EOL> def __del__ ( self ) : <EOL> if self . __id and not self . __killed : <EOL> self . __die ( ) <EOL> def __die ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __id and not self . __killed : <EOL> self . __collection . database . client . close_cursor ( <EOL> self . __id , _CursorAddress ( self . __address , self . __ns ) ) <EOL> self . __killed = True <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __die ( ) <EOL> def batch_size ( self , batch_size ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( batch_size , integer_types ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if batch_size < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . __batch_size = batch_size == <NUM_LIT:1> and <NUM_LIT:2> or batch_size <EOL> return self <EOL> def __send_message ( self , operation ) : <EOL> """<STR_LIT>""" <EOL> client = self . __collection . database . client <EOL> listeners = client . _event_listeners <EOL> publish = listeners . enabled_for_commands <EOL> try : <EOL> response = client . _send_message_with_response ( <EOL> operation , address = self . __address ) <EOL> except AutoReconnect : <EOL> self . __killed = True <EOL> raise <EOL> cmd_duration = response . duration <EOL> rqst_id = response . request_id <EOL> from_command = response . from_command <EOL> if publish : <EOL> start = datetime . datetime . now ( ) <EOL> try : <EOL> doc = helpers . _unpack_response ( response . data , <EOL> self . __id , <EOL> self . __collection . codec_options ) <EOL> if from_command : <EOL> helpers . _check_command_response ( doc [ '<STR_LIT:data>' ] [ <NUM_LIT:0> ] ) <EOL> except OperationFailure as exc : <EOL> self . __killed = True <EOL> if publish : <EOL> duration = ( datetime . datetime . now ( ) - start ) + cmd_duration <EOL> listeners . publish_command_failure ( <EOL> duration , exc . details , "<STR_LIT>" , rqst_id , self . __address ) <EOL> raise <EOL> except NotMasterError as exc : <EOL> self . __killed = True <EOL> if publish : <EOL> duration = ( datetime . datetime . now ( ) - start ) + cmd_duration <EOL> listeners . publish_command_failure ( <EOL> duration , exc . details , "<STR_LIT>" , rqst_id , self . __address ) <EOL> client . _reset_server_and_request_check ( self . address ) <EOL> raise <EOL> except Exception as exc : <EOL> if publish : <EOL> duration = ( datetime . datetime . now ( ) - start ) + cmd_duration <EOL> listeners . publish_command_failure ( <EOL> duration , _convert_exception ( exc ) , "<STR_LIT>" , rqst_id , <EOL> self . __address ) <EOL> raise <EOL> if from_command : <EOL> cursor = doc [ '<STR_LIT:data>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> documents = cursor [ '<STR_LIT>' ] <EOL> self . __id = cursor [ '<STR_LIT:id>' ] <EOL> self . __retrieved += len ( documents ) <EOL> else : <EOL> documents = doc [ "<STR_LIT:data>" ] <EOL> self . __id = doc [ "<STR_LIT>" ] <EOL> self . __retrieved += doc [ "<STR_LIT>" ] <EOL> if publish : <EOL> duration = ( datetime . datetime . now ( ) - start ) + cmd_duration <EOL> res = { "<STR_LIT>" : { "<STR_LIT:id>" : self . __id , <EOL> "<STR_LIT>" : self . __collection . full_name , <EOL> "<STR_LIT>" : documents } , <EOL> "<STR_LIT>" : <NUM_LIT:1> } <EOL> listeners . publish_command_success ( <EOL> duration , res , "<STR_LIT>" , rqst_id , self . __address ) <EOL> if self . __id == <NUM_LIT:0> : <EOL> self . __killed = True <EOL> self . __data = deque ( documents ) <EOL> def _refresh ( self ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . __data ) or self . __killed : <EOL> return len ( self . __data ) <EOL> if self . __id : <EOL> dbname , collname = self . __ns . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> self . __send_message ( <EOL> _GetMore ( dbname , <EOL> collname , <EOL> self . __batch_size , <EOL> self . __id , <EOL> self . __collection . codec_options ) ) <EOL> else : <EOL> self . __killed = True <EOL> return len ( self . __data ) <EOL> @ property <EOL> def alive ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( len ( self . __data ) or ( not self . __killed ) ) <EOL> @ property <EOL> def cursor_id ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __id <EOL> @ property <EOL> def address ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __address <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . __data ) or self . _refresh ( ) : <EOL> coll = self . __collection <EOL> return coll . database . _fix_outgoing ( self . __data . popleft ( ) , coll ) <EOL> else : <EOL> raise StopIteration <EOL> __next__ = next <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> self . __die ( ) </s>
