o Sby1@sdZddlmZddlmZmZmZmZmZm Z m Z ddl m Z ddl mZmZddlmZmZmZddlmZmZmZddlmZdd lmZmZerXdd lmZdd lm Z Gd d d eeZ!Gddde!eeZ"dS)z4CommandCursor class to iterate over command results.)deque) TYPE_CHECKINGAnyGenericIteratorMappingNoReturnOptional)&_convert_raw_document_lists_to_streams)_CURSOR_CLOSED_ERRORS_SocketManager)ConnectionFailureInvalidOperationOperationFailure)_CursorAddress_GetMore_RawBatchGetMore)PinnedResponse)_Address _DocumentType) ClientSession) Collectionc@sreZdZdZeZ     d>dddeeefde e d e d e e d e d d e deddfddZ d?ddZd@ddZddZd?ddZd e ddfddZddZedd Zd!d"Zd#d$Z dAd%d&Zd'd(Zede fd)d*Zede fd+d,Zede e fd-d.Zede d fd/d0Zdeefd1d2Z defd3d4Z!e!Z"d5d6Z#dBd7d8Z$d9ed:ed;eddfd= 0)r6r7r8 ValueErrorr.)r:rr;r;r<rms zCommandCursor.batch_sizecCst|jdkS)zUReturns `True` if the cursor has documents remaining from the previous batch.r)lenr*rBr;r;r< _has_nextszCommandCursor._has_nextcC|jS)zcRetrieve the postBatchResumeToken from the response to a changeStream aggregate or getMore.)r,rBr;r;r<_post_batch_resume_tokensz&CommandCursor._post_batch_resume_tokencCsV|jjj}||js dS|js)|t|d}|jdkr$| dS||_dSdS)NFr) r(rErFZ_should_pin_cursorr0r'Z pin_cursorr r)rJ)r:Z sock_inforFZsock_mgrr;r;r<_maybe_pin_connections      z#CommandCursor._maybe_pin_connectionc Cs |jjj}z |j||j|jd}Wn2ty*}z |jtvr!d|_ | d}~wt y8d|_ | t yC| wt |trT|jsTt|j|j|_|jrn|jdd}|d}|d|_|d|_n|j}|jj|_|jdkr| t||_dS) z/Send a getmore message and handle the response.)rTNrcursorZ nextBatchr%r#)r(rErFZ_run_operation_unpack_responser-rcoder r2rJr Exceptionr6rr'r Z socket_infoZ more_to_comeZ from_commanddocsr+r,r)datarHrr*)r:Z operationrFresponseexcrTZ documentsr;r;r<Z__send_messages@           zCommandCursor.__send_messagecCs|||||Sr?)Zunpack_response)r:rZrH codec_options user_fieldslegacy_responser;r;r<rUszCommandCursor._unpack_responsecCst|js|jr t|jS|jr?|jdd\}}|j|j}| | |||j |j|jj ||j |jjj|j|jd|j n|dt|jS)aRefreshes the cursor with more data from the server. Returns the length of self.__data after refresh. Will exit early if self.__data is already non-empty. Raises OperationFailure when the cursor cannot be refreshed due to an error on the query. .rLFT)rOr*r2r)r5splitr(Z_read_preference_forr_CommandCursor__send_message_getmore_classr.r\r0rErFr/r'r3rA)r:ZdbnameZcollnameZ read_prefr;r;r<_refreshs.   zCommandCursor._refreshcCstt|jp |j S)aDoes this cursor have the potential to return more data? Even if :attr:`alive` is ``True``, :meth:`next` can raise :exc:`StopIteration`. Best to use a for loop:: for doc in collection.aggregate(pipeline): print(doc) .. note:: :attr:`alive` can be True while iterating a cursor from a failed server. In this case :attr:`alive` will return False after :meth:`next` fails to retrieve the next batch of results from the server. )boolrOr*r2rBr;r;r<aliveszCommandCursor.alivecCrQ)zReturns the id of the cursor.)r)rBr;r;r<rHszCommandCursor.cursor_idcCrQ)zUThe (host, port) of the server used, or None. .. versionadded:: 3.0 )r-rBr;r;r<rszCommandCursor.addresscCs|jr|jSdS)zmThe cursor's :class:`~pymongo.client_session.ClientSession`, or None. .. versionadded:: 3.6 N)r1r0rBr;r;r<r szCommandCursor.sessioncC|Sr?r;rBr;r;r<__iter__zCommandCursor.__iter__cCs&|jr|d}|dur|S|jst)zAdvance the cursor.TN)re _try_next StopIteration)r:docr;r;r<nexts  zCommandCursor.nextcCs4t|js|js|r|t|jr|jSdS)zr?)rJ)r:rorprqr;r;r<__exit__0rDzCommandCursor.__exit__rNNFN)r"N)FNF)r"rK)&__name__ __module__ __qualname____doc__rrbrstrrr rr7rdr=rCrAr4rJrrPpropertyrRrSrarUrcrerHrrrrrgrl__next__rirnrrr;r;r;r<r sl   %   ) "   rcseZdZeZ     ddddeeefdee de d ee d ed d e d eddffdd Z dddZ de defddZZS)RawBatchCommandCursorrNFrrrrrrrrr r!r"c s0|drJtt|||||||||dS)aLCreate a new cursor / iterator over raw batches of BSON data. Should not be called directly by application developers - see :meth:`~pymongo.collection.Collection.aggregate_raw_batches` instead. .. seealso:: The MongoDB documentation on `cursors `_. r$N)r+superr|r=r9 __class__r;r<r=7s zRawBatchCommandCursor.__init__cCs"|j||d}|st|d|S)N)r]r) raw_responser )r:rZrHr\r]r^rr;r;r<rUVs z&RawBatchCommandCursor._unpack_responseindexcCstd)Nz)Cannot call __getitem__ on RawBatchCursor)r)r:rr;r;r< __getitem__`sz!RawBatchCommandCursor.__getitem__rsrt)rurvrwrrbrryrr rr7rdr=rUrr __classcell__r;r;r~r<r|4s:     r|N)#rx collectionsrtypingrrrrrrr Zbsonr Zpymongo.cursorr r Zpymongo.errorsr rrZpymongo.messagerrrZpymongo.responserZpymongo.typingsrrZpymongo.client_sessionrZpymongo.collectionrrr|r;r;r;r<s  $