o
    {g>                     @  s  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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 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,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2 e	rddlm3Z3 ddlm4Z4m5Z5m6Z6 ddl)m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZCmDZDmEZEmFZF ddlGmHZH dZI														dUdVdDdEZJefdWdJdKZKdXdOdPZLdYdSdTZMdS )Zz&Internal network layer helper methods.    )annotationsN)TYPE_CHECKINGAnyMappingMutableMappingOptionalSequenceUnioncast)_decode_all_selective)_csothelpers_sharedmessage)MAX_MESSAGE_SIZE)_NO_COMPRESSION
decompress)NotPrimaryErrorOperationFailureProtocolError_OperationCancelled)_COMMAND_LOGGER_CommandStatusMessage
_debug_log)_UNPACK_REPLY_OpMsg_OpReply)_is_speculative_authenticate)_POLL_TIMEOUT_UNPACK_COMPRESSION_HEADER_UNPACK_HEADERBLOCKING_IO_ERRORSsendall)_errno_from_exception)CodecOptions)SnappyContextZlibContextZstdContext)_EventListeners)ReadConcern)_ServerMode)ClientSession)MongoClient)
Connection)_Address_CollationIn_DocumentOut_DocumentType)WriteConcernTFconnr,   dbnamestrspecMutableMapping[str, Any]	is_mongosboolread_preferenceOptional[_ServerMode]codec_optionsCodecOptions[_DocumentType]sessionOptional[ClientSession]clientOptional[MongoClient]checkallowable_errors#Optional[Sequence[Union[str, int]]]addressOptional[_Address]	listenersOptional[_EventListeners]max_bson_sizeOptional[int]read_concernOptional[ReadConcern]parse_write_concern_error	collationOptional[_CollationIn]compression_ctx4Union[SnappyContext, ZlibContext, ZstdContext, None]
use_op_msgunacknowledgeduser_fieldsOptional[Mapping[str, Any]]exhaust_allowedwrite_concernOptional[WriteConcern]returnr0   c           (      C  s  t t|}|d }d}|}|r|s|dusJ t||}|r5|r%|js5|jr-|j|d< |r5|||  |dur=||d< |duoC|j}t	j	
 }|rPt||}|rZ| tv rZd}|rm|jrm|jjsm|j||| }}|ru| || t|| |r|rtjnd}||rtjndO }tj||||||d\}}}} |r|dur| |krt||| ntd|dd|d||\}}}|dur||tj krt|||tj  |durttjrtt|j j!t"j#|t t||||| j$| j%| j&d | j&d	 | j'd
 |r|dusJ |
dusJ |j(||||
| j%| j'd z?t)| j*| |r/|r/d}!dd	i}"n*t+| |}!|!j,| _,|!j-||d}#|#d }"|rL|.|"| |rYt/j0|"| j1|	|d W nx t2y }$ zkt	j	
 | }%t3|$t4t5frv|$j6}&nt7|$}&|durttjrtt|j j!t"j8|%|&t t||||| j$| j%| j&d | j&d	 | j't3|$t5d |r|dusJ |
dusJ |j9|%|&|||
| j%| j'|d  d}$~$ww t	j	
 | }%|durttjrtt|j j!t"j:|%|"t t||||| j$| j%| j&d | j&d	 | j'd|v d |r,|dusJ |
dusJ |j;|%|"|||
| j%| j'||d	 |rI|jrI|!rI|j<|!= }'t>dt?|'||d }"|"S )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readConcernrM   r   )ctx   )clientIdr   commandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionId
serverHost
serverPort	serviceId)
service_idok)r;   rS   )rL   )r]   r   
durationMSfailurer_   r`   ra   rb   rc   rd   re   rf   rg   isServerSideError)rh   database_namespeculativeAuthenticate)r]   r   rj   replyr_   r`   ra   rb   rc   rd   re   rf   rg   speculative_authenticate)rh   speculative_hellorm   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_idrD   rh   publish_command_startr!   r2   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   )(r2   r3   r5   r7   r9   r;   r=   r?   rA   rB   rD   rF   rH   rJ   rL   rM   rO   rQ   rR   rS   rU   rV   namensrq   origpublishstartflags
request_idmsgsizemax_doc_sizero   response_docunpacked_docsexcdurationrk   	decrypted r   x/var/www/bot.gig.net.ua/public_html/telegram/P1/HellBot/venv/lib/python3.10/site-packages/pymongo/synchronous/network.pyr^   G   sP  3




	





'

r^   r   max_message_sizeintUnion[_OpReply, _OpMsg]c              	   C  s&  t  r	t  }n| j }|rt | }nd}tt| d|\}}}}|dur7||kr7t	d|d||dkrCt	d|d||krRt	d|d|d|d	krmt
t| d
|\}}}	tt| |d ||	}
nt| |d |}
z	t| }W ||
S  ty   t	d|dt d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_deadliner2   
gettimeouttime	monotonicr   _receive_data_on_socketr   r   r   r   KeyErrorkeys)r2   r   r   deadlinetimeoutlength_response_toop_codecompressor_iddataunpack_replyr   r   r   r   3  sH   





r   r   Optional[float]Nonec                 C  s   | j }d}| dkrdS 	 t|dr| dkrd}n"|r3|t  }|dkr*d}tt|td}nt}| j	j
|d|d}| jjrFtd|rJdS |rQtd	q)
zABlock until at least one byte is read, or a timeout, or a cancel.Fr[   NTpendingr   )readr   zoperation cancelled	timed out)r2   filenohasattrr   r   r   maxminr   socket_checkerselectcancel_context	cancelledr   socketr   )r2   r   sock	timed_outreadable	remainingr   r   r   r   wait_for_read_  s*   
r   r   
memoryviewc              
   C  s   t |}t|}d}||k rjz%t| | t r(|d ur(| t|t  d | j	
||d  }W n& ty@   tdd  tyY } zt|tjkrTW Y d }~q
 d }~ww |dkrbtd||7 }||k s|S )Nr   r   zconnection closed)	bytearrayr   r   r   r   set_conn_timeoutr   r   r   r2   	recv_intor    r   r   OSErrorr"   errnoEINTR)r2   r   r   bufmv
bytes_readchunk_lengthr   r   r   r   r     s,   
r   )TNNNNNFNNFFNFN).r2   r,   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   r8   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   r8   rM   rN   rO   rP   rQ   r8   rR   r8   rS   rT   rU   r8   rV   rW   rX   r0   )r2   r,   r   rI   r   r   rX   r   )r2   r,   r   r   rX   r   )r2   r,   r   r   r   r   rX   r   )N__doc__
__future__r   rz   r   r   r   r   typingr   r   r   r   r   r   r	   r
   bsonr   pymongor   r   r   pymongo.commonr   pymongo.compression_supportr   r   pymongo.errorsr   r   r   r   pymongo.loggerr   r   r   pymongo.messager   r   r   pymongo.monitoringr   pymongo.network_layerr   r   r   r    r!   pymongo.socket_checkerr"   r#   r$   r%   r&   r'   pymongo.read_concernr(   pymongo.read_preferencesr)   "pymongo.synchronous.client_sessionr*    pymongo.synchronous.mongo_clientr+   pymongo.synchronous.poolr,   pymongo.typingsr-   r.   r/   r0   pymongo.write_concernr1   _IS_SYNCr^   r   r   r   r   r   r   r   <module>   sd   ( n
,!