
    h</                     h   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZmZ ddlmZmZmZmZmZmZ dd	lmZmZ dd
lmZ ddlmZ  ej>                  d      j@                  Z!	 	 	 	 	 	 	 	 	 	 	 ddZ" ej>                  d      j@                  Z#efdZ$dZ%d Z&esd Z'yd Z'y)z&Internal network layer helper methods.    N)_decode_all_selective)PY3)helpersmessage)MAX_MESSAGE_SIZE)
decompress_NO_COMPRESSION)AutoReconnectNotMasterErrorOperationFailureProtocolErrorNetworkTimeout_OperationCancelled)_UNPACK_REPLY_OpMsg)time)_errno_from_exceptionz<iiiic                    t        t        |            }|dz   }|rdnd}|}|r|st        j                  ||      }|rl|r|j                  s^|j
                  r|j                  |d<   |rA|j                  j                  r+|j                  |j                  |j                  di       d<   |||d<   |duxr |j                  }|rt        j                  j                         }|r|j                         t        v rd}|rD|j                   r8|j                   j"                  s"|j                   j%                  ||||      x}}d}|rn|rt&        j(                  nd}||rt&        j*                  ndz  }t        j,                  ||||||||	      \  }}}} |r@|>| |kD  r9t        j.                  |||       n!t        j0                  ||dd
|d|||	      \  }}}|>||t        j2                  z   kD  r(t        j.                  |||t        j2                  z          |rSt        j                  j                         z
  }!|j5                  ||||       t        j                  j                         }	 | j6                  j9                  |       |r	|rd}"ddi}#nnt;        | |      }"|"j<                  | _        |"j?                  ||      }$|$d   }#|r|jA                  |#|       |	r#tC        jD                  |#| jF                  |
|       |r9t        j                  j                         z
  !z   }&|jW                  |&|#|||       |rG|j                   r;|"r9|j                   jY                  |"j[                               }(t]        |(||      d   }#|#S # tH        $ ry}%|rqt        j                  j                         z
  !z   }&tK        |%tL        tN        f      r|%jP                  }'nt        jR                  |%      }'|jU                  |&|'|||        d}%~%ww xY w)a\  Execute a command over the socket, or raise socket.error.

    :Parameters:
      - `sock`: a raw socket instance
      - `dbname`: name of the database on which to run the command
      - `spec`: a command document as an ordered dict type, eg SON.
      - `slave_ok`: whether to set the SlaveOkay wire protocol bit
      - `is_mongos`: are we connected to a mongos?
      - `read_preference`: a read preference
      - `codec_options`: a CodecOptions instance
      - `session`: optional ClientSession instance.
      - `client`: optional MongoClient instance for updating $clusterTime.
      - `check`: raise OperationFailure if there are errors
      - `allowable_errors`: errors to ignore if `check` is True
      - `address`: the (host, port) of `sock`
      - `check_keys`: if True, check `spec` for invalid keys
      - `listeners`: An instance of :class:`~pymongo.monitoring.EventListeners`
      - `max_bson_size`: The maximum encoded bson size for this server
      - `read_concern`: The read concern for this command.
      - `parse_write_concern_error`: Whether to parse the ``writeConcernError``
        field in the command response.
      - `collation`: The collation for this command.
      - `compression_ctx`: optional compression Context.
      - `use_op_msg`: True if we should use OP_MSG.
      - `unacknowledged`: True if this is an unacknowledged command.
      - `user_fields` (optional): Response fields that should be decoded
        using the TypeDecoders from codec_options, passed to
        bson._decode_all_selective.
      - `exhaust_allowed`: True if we should enable OP_MSG exhaustAllowed.
    z.$cmd   r   readConcernNafterClusterTime	collationF)ctxok   )codec_optionsuser_fields)parse_write_concern_error)/nextiterr   _maybe_add_read_preferencein_transactionleveldocumentoptionscausal_consistencyoperation_time
setdefaultenabled_for_commandsdatetimenowlowerr	   
_encrypter_bypass_auto_encryptionencryptr   MORE_TO_COMEEXHAUST_ALLOWED_op_msg_raise_document_too_largequery_COMMAND_OVERHEADpublish_command_startsocksendallreceive_messagemore_to_comeunpack_response_process_responser   _check_command_responsemax_wire_version	Exception
isinstancer   r   details_convert_exceptionpublish_command_failurepublish_command_successdecryptraw_command_responser   ))	sock_infodbnamespecslave_ok	is_mongosread_preferencer   sessionclientcheckallowable_errorsaddress
check_keys	listenersmax_bson_sizeread_concernr   r   compression_ctx
use_op_msgunacknowledgedr   exhaust_allowednamensflagsorigpublishstart
request_idmsgsizemax_doc_sizeencoding_durationreplyresponse_docunpacked_docsexcdurationfailure	decrypteds)                                            R/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/pymongo/network.pycommandrn   +   s   T T
D	'	BAqE D11$HW)?)?"."7"7D::**69@9O9O OOr##57%[t#F	(F(FG!!%%'4::<?:6$$!!99''//D*m5 	5t 
'5##1?''A.5oo4(J/0+
C|
 }8},--dD-H '2q"dD-!
C 	!}w'@'@@@))$(A(AA	C $--113e;''fj'J!!%%'s#.E !9L#Iz:E%*%7%7I"!11+ 2 FM )+L((w?// )"<"<>N.GI %%))+e37HH))lD*g	? &##%%--e.H.H.JK	,Y-8::;= )  	 ))--/%7;LLH#0@AB++!44S9--'4W>	s   *BN 	PA4PPz<iiBc                 .   | j                   j                         }|rt               |z   }nd}t        t	        | d|            \  }}}}|||k7  rt        d|d|      |dk  rt        d|d      ||kD  rt        d|d|d      |d	k(  r5t        t	        | d
|            \  }}}	t        t	        | |dz
  |      |	      }
nt	        | |dz
  |      }
	 t        |   } ||
      S # t        $ r$ t        d|dt        j                               w xY w)z1Receive a raw BSON message or raise socket.error.N   zGot response id z but expected zMessage length (z3) not longer than standard message header size (16)z*) is larger than server max message size ()i  	      zGot opcode )r8   
gettimeoutr   _UNPACK_HEADER_receive_data_on_socketr   _UNPACK_COMPRESSION_HEADERr   r   KeyErrorkeys)rH   ra   max_message_sizetimeoutdeadlinelength_response_toop_codecompressor_iddataunpack_replys               rm   r:   r:      s\   nn'')G6G#&4	2x8':#FA{G $(3Z!A B B|:@C D 	D  39;KM N 	N$$>#Iq(;%=!M#Iv{HE 'y&2+xHD$W-   D$+]-?-?-AC D 	DDs   	C' '-Dg      ?c                    | j                   }|r| j                  }	 t        |d      r|j                         dkD  rd}nL|r&t	        t        |t               z
  t              d      }nt        }| j                  j                  |d|      }|j                  rt        d      |ry|r"t               |kD  rt        j                  d      y)	zABlock until at least one byte is read, or a timeout, or a cancel.Tpendingr   gMbP?)readr{   zisMaster cancelledNz	timed out)cancel_contextr8   hasattrr   maxminr   _POLL_TIMEOUTsocket_checkerselect	cancelledr   socketr{   )rH   r|   contextr8   readabler{   s         rm   wait_for_readr      s    &&G~~tY'DLLNQ,> !#h&7"GOG+G$33::tW ; 6  )*>??DFX-nn[11%      c                 x   t        |      }d}|rh	 t        | |       | j                  j                  |      }|dk(  rt        d      ||||t        |      z    |t        |      z  }|t        |      z  }|rht        |      S # t        t
        f$ r'}t        |      t        j                  k(  rY d }~ d }~ww xY w)Nr   r   connection closed)	bytearrayr   r8   recvIOErrorOSErrorr   errnoEINTRr
   lenbytes)rH   r}   r|   bufichunkri   s          rm   rv   rv     s    i2!++F3
 |#$788$)C!c%j.!UOAc%j F  Sz W% (-<s   'B B9B43B44B9c                 >   t        |      }t        |      }d}||k  rF	 t        | |       | j                  j	                  ||d        }|dk(  rt        d      ||z  }||k  rF|S # t
        t        f$ r'}t        |      t        j                  k(  rY d }~} d }~ww xY w)Nr   r   )r   
memoryviewr   r8   	recv_intor   r   r   r   r   r
   )rH   r}   r|   r   mv
bytes_readchunk_lengthri   s           rm   rv   rv     s    _
6!i2(~~77:;H
 q #$788,&J 6! 	 W% (-<s   *A& &B5BBB)TNNFNNNFNNFFNF)(__doc__r+   r   r   structbsonr   bson.py3compatr   pymongor   r   pymongo.commonr   pymongo.compression_supportr   r	   pymongo.errorsr
   r   r   r   r   r   pymongo.messager   r   pymongo.monotonicr   pymongo.socket_checkerr   Structunpackru   rn   rw   r:   r   r   rv    r   rm   <module>r      s    -     '  $ + C1 1 2 " 8 w'.. DH+/<@&+  !KZ +V]]6299 <L #L 2> (r   