
    FCf>                       d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	m
Z
mZmZmZmZmZmZmZ ddlmZ ddlmZmZ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" dd
l#m$Z$m%Z%m&Z& ddl'm(Z(m)Z)m*Z* ddl+m,Z, ddl-m.Z. e
rFddlm/Z/ ddl0m1Z1 ddlm2Z2m3Z3m4Z4 ddl5m6Z6 ddl+m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z?m@Z@mAZAmBZB ddlCmDZD  ej                  d      j                  ZG	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d dZH ej                  d      j                  ZIef	 	 	 	 	 	 	 d!dZJdZKd"dZLeMgej                  ZNd#dZOy)$z&Internal network layer helper methods.    )annotationsN)TYPE_CHECKINGAnyMappingMutableMappingOptionalSequenceUnioncast)_decode_all_selective)_csothelpersmessagessl_support)MAX_MESSAGE_SIZE)_NO_COMPRESSION
decompress)NotPrimaryErrorOperationFailureProtocolError_OperationCancelled)_COMMAND_LOGGER_CommandStatusMessage
_debug_log)_UNPACK_REPLY_OpMsg_OpReply)_is_speculative_authenticate)_errno_from_exception)CodecOptions)ClientSession)SnappyContextZlibContextZstdContext)MongoClient)_EventListeners)
Connection)ReadConcern)_ServerMode)_Address_CollationIn_DocumentOut_DocumentType)WriteConcernz<iiiic                   t        t        |            }|dz   }d}|}|r|s|J t        j                  ||      }|r=|r|j                  s/|j
                  r|j                  |d<   |r|j                  ||        |||d<   |duxr |j                  }t        j                  j                         }|rt        ||      }|r|j                         t        v rd}|rA|j                  r5|j                  j                  s|j                  j!                  |||      x}}|r| j#                  ||       t%        j&                  ||       |rl|rt(        j*                  nd}||rt(        j,                  ndz  }t        j.                  ||||||      \  }}}} |r?|=| |kD  r8t        j0                  |||       n t        j2                  d|dd|d||      \  }}}|>||t        j4                  z   kD  r(t        j0                  |||t        j4                  z          |t7        j8                  t:        j<                        rt?        t6        |j@                  jB                  tD        jF                  |t        t        |            |||| jH                  | jJ                  | jL                  d   | jL                  d	   | jN                  
       |r3|J |
J |jQ                  ||||
| jJ                  | jN                         	 | jR                  jU                  |       |r	|rd}!dd	i}"nntW        | |      }!|!jX                  | _,        |!j[                  ||      }#|#d   }"|r|j]                  |"|       |r#t_        j`                  |"| jb                  |	|       t        j                  j                         |z
  }%|t7        j8                  t:        j<                        rt?        t6        |j@                  jB                  tD        jt                  |%|"t        t        |            |||| jH                  | jJ                  | jL                  d   | jL                  d	   | jN                  d|v        |r6|J |
J |jw                  |%|"|||
| jJ                  | jN                  ||	       |rQ|j                  rE|!rC|j                  jy                  |!j{                               }'t}        dt        |'||      d         }"|"S # td        $ rS}$t        j                  j                         |z
  }%tg        |$th        tj        f      r|$jl                  }&nt        jn                  |$      }&|t7        j8                  t:        j<                        rt?        t6        |j@                  jB                  tD        jp                  |%|&t        t        |            |||| jH                  | jJ                  | jL                  d   | jL                  d	   | jN                  tg        |$tj                     |r5|J |
J |js                  |%|&|||
| jJ                  | jN                  |        d}$~$ww xY w)a  Execute a command over the socket, or raise socket.error.

    :param conn: a Connection instance
    :param dbname: name of the database on which to run the command
    :param spec: a command document as an ordered dict type, eg SON.
    :param is_mongos: are we connected to a mongos?
    :param read_preference: a read preference
    :param codec_options: a CodecOptions instance
    :param session: optional ClientSession instance.
    :param client: optional MongoClient instance for updating $clusterTime.
    :param check: raise OperationFailure if there are errors
    :param allowable_errors: errors to ignore if `check` is True
    :param address: the (host, port) of `conn`
    :param listeners: An instance of :class:`~pymongo.monitoring.EventListeners`
    :param max_bson_size: The maximum encoded bson size for this server
    :param read_concern: The read concern for this command.
    :param parse_write_concern_error: Whether to parse the ``writeConcernError``
        field in the command response.
    :param collation: The collation for this command.
    :param compression_ctx: optional compression Context.
    :param use_op_msg: True if we should use OP_MSG.
    :param unacknowledged: True if this is an unacknowledged command.
    :param user_fields: Response fields that should be decoded
        using the TypeDecoders from codec_options, passed to
        bson._decode_all_selective.
    :param exhaust_allowed: True if we should enable OP_MSG exhaustAllowed.
    z.$cmdFNreadConcern	collationr   )ctx   )clientIdr   commandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionId
serverHost
serverPort	serviceId)
service_idok)codec_optionsuser_fields)parse_write_concern_error)r5   r   
durationMSfailurer7   r8   r9   r:   r;   r<   r=   r>   r?   isServerSideError)r@   database_namespeculativeAuthenticate)r5   r   rE   replyr7   r8   r9   r:   r;   r<   r=   r>   r?   speculative_authenticate)r@   speculative_hellorH   r,   )@nextiterr   _maybe_add_read_preferencein_transactionleveldocument_update_read_concernenabled_for_commandsdatetimenowr   lowerr   
_encrypter_bypass_auto_encryptionencryptapply_timeoutr   apply_write_concernr   MORE_TO_COMEEXHAUST_ALLOWED_op_msg_raise_document_too_large_query_COMMAND_OVERHEADr   isEnabledForloggingDEBUGr   _topology_settings_topology_idr   STARTEDidserver_connection_idaddressr@   publish_command_startconnsendallreceive_messagemore_to_comeunpack_response_process_responser   _check_command_responsemax_wire_version	Exception
isinstancer   r   details_convert_exceptionFAILEDpublish_command_failure	SUCCEEDEDpublish_command_successdecryptraw_command_responser   r   )(rm   dbnamespec	is_mongosread_preferencerB   sessionclientcheckallowable_errorsrk   	listenersmax_bson_sizeread_concernrD   r1   compression_ctx
use_op_msgunacknowledgedrC   exhaust_allowedwrite_concernnamensrL   origpublishstartflags
request_idmsgsizemax_doc_sizerJ   response_docunpacked_docsexcdurationrF   	decrypteds(                                           O/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/pymongo/network.pyr6   r6   A   s   f T
D	'	B D***11$HW)?)?"."7"7D((t4%[t#F	(F(FG!!#E8tD4::<?:&##F,=,=,U,U''//mLLt 64(	dM2'5##1?''A.5oo4-_/
+
C|
 m7L=<X--dD-H 'r1b$m_!

C  TMG<U<U,U%U))$mgF_F_6_`''622??-55 d,#$&#'77#'#<#<<<?<<?// $$$"""''%% 	( 	
=		#.E*.L#D*5E % 2 2D!11+ 2 M )+L((w?// ))$.G	Z   $$&.H''622??-77#" d,#$&#'77#'#<#<<<?<<?//)Bd)J" $$$"""))%%/  	* 
	
 &##%%--e.H.H.JK	1)]KXYZ[
 a  &$$((*U2cO-=>?$'KKG005G++GMM:##66CC188'# $T$Z 0!'( *'+ww'+'@'@#||A#||A"oo&06F&G" (((&&&--))??$ . 	 	M&s   8BS+ +
Y5EYYz<iiBc                   t        j                         rt        j                         }n6| j                  j	                         }|rt        j                         |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                               d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 )r   get_timeoutget_deadlinerm   
gettimeouttime	monotonic_UNPACK_HEADER_receive_data_on_socketr   _UNPACK_COMPRESSION_HEADERr   r   KeyErrorkeys)rm   r   max_message_sizedeadlinetimeoutlength_response_toop_codecompressor_iddataunpack_replys               r   ro   ro   0  s    %%'))&&(~~''1HH&45LTSUW_5`&a#FA{G$"2;/PZ~ ^__|vj([\
 	
   vj )-03
 	
 $$>#D!X6%
!M 1$XNP]^&tVb[(C$W-
 	  'N=3E3E3G2JK
	s   		D .Eg      ?c                   | j                   }d}|j                         dk(  ry	 t        |d      r|j                         dkD  rd}n_|r9|t	        j
                         z
  }|dk  rd}t        t        |t              d      }nt        }| j                  j                  |d|      }| j                  j                  rt        d      |ry|rt        j                  d	      )
zABlock until at least one byte is read, or a timeout, or a cancel.Fr3   NTpendingr   )readr   zoperation cancelled	timed out)rm   filenohasattrr   r   r   maxmin_POLL_TIMEOUTsocket_checkerselectcancel_context	cancelledr   socketr   )rm   r   sock	timed_outreadable	remainingr   s          r   wait_for_readr   _  s    99DI{{}
4#(:H $t~~'77	 > $Ic)];Q?'**11$T71SH((%&;<<..--/     c                   t        |      }t        |      }d}||k  r	 t        | |       t        j                         r2|0| j                  t        |t        j                         z
  d             | j                  j                  ||d        }|dk(  rt        d      ||z  }||k  r|S # t        $ r t        j                  d      d t        $ r'}t        |      t         j"                  k(  rY d }~ۂ d }~ww xY w)Nr   r   zconnection closed)	bytearray
memoryviewr   r   r   set_conn_timeoutr   r   r   rm   	recv_intoBLOCKING_IO_ERRORSr   r   OSErrorr   errnoEINTR)rm   r   r   bufmv
bytes_readchunk_lengthr   s           r   r   r     s    
F
C	CBJ
v
	$)   "x';%%c(T^^5E*Eq&IJ99..r*+?L 1-..l"
% v
( I " 	8..-47 	$S)U[[8	s   A0B, ,'C:C54C55C:)TNNNNNFNNFFNFN).rm   r'   r   strr   zMutableMapping[str, Any]r   boolr   zOptional[_ServerMode]rB   zCodecOptions[_DocumentType]r   zOptional[ClientSession]r   zOptional[MongoClient]r   r   r   z#Optional[Sequence[Union[str, int]]]rk   zOptional[_Address]r   zOptional[_EventListeners]r   Optional[int]r   zOptional[ReadConcern]rD   r   r1   zOptional[_CollationIn]r   z4Union[SnappyContext, ZlibContext, ZstdContext, None]r   r   r   r   rC   zOptional[Mapping[str, Any]]r   r   r   zOptional[WriteConcern]returnr-   )rm   r'   r   r   r   intr   zUnion[_OpReply, _OpMsg])rm   r'   r   Optional[float]r   None)rm   r'   r   r   r   r   r   r   )P__doc__
__future__r   rU   r   rd   r   structr   typingr   r   r   r   r   r	   r
   r   bsonr   pymongor   r   r   r   pymongo.commonr   pymongo.compression_supportr   r   pymongo.errorsr   r   r   r   pymongo.loggerr   r   r   pymongo.messager   r   r   pymongo.monitoringr   pymongo.socket_checkerr   r    pymongo.client_sessionr!   r"   r#   r$   pymongo.mongo_clientr%   r&   pymongo.poolr'   pymongo.read_concernr(   pymongo.read_preferencesr)   pymongo.typingsr*   r+   r,   r-   pymongo.write_concernr.   Structunpackr   r6   r   ro   r   r   BlockingIOErrorr   r    r   r   <module>r      s6   - "      	 	 	 ' 8 8 + C  N M ; ; ; 8!4SS02'04SS2w'.. <@"&+/#'*.&+(,LP /3!,0-i
ii #i 	i
 +i /i %i "i i :i  i )i !i (i  $i  &!i" J#i$ %i& 'i( -)i* +i, *-i. /iX +V]]6299  JZ)
)"/)CF))X .D &G(F(FG r   