o
    sDi7                     @  sd  d Z ddlmZ ddlZddlmZ ddlmZmZ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 dd	lmZmZmZmZmZ dd
lmZmZmZmZ ddlmZm Z  ddl!m"Z" erddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6 ddl7m8Z8 dZ9ddddiZ:G dd dZ;dS )z2Communicate with one MongoDB server in a topology.    )annotationsN)datetime)TYPE_CHECKINGAnyCallableContextManagerOptionalUnion)_decode_all_selective)NotPrimaryErrorOperationFailure)_check_command_response)_COMMAND_LOGGER_SDAM_LOGGER_CommandStatusMessage
_debug_log_SDAMStatusMessage)_convert_exception_GetMore_OpMsg_Query)PinnedResponseResponse)_handle_reauth)Queue)ReferenceType)ObjectId)_EventListeners)_ServerMode)ServerDescription)MongoClient_MongoClientErrorHandler)Monitor)
ConnectionPool)_DocumentOutTcursor   )
firstBatch	nextBatchc                   @  s   e Zd Z			d@dAddZdBddZdCdDddZdBddZdBddZ	dEdFd#d$Ze	dGd,d-Z
	dCdHd1d2ZedId3d4ZejdJd5d4ZedKd6d7ZdLd;d<ZdMd>d?ZdS )NServerNserver_descriptionr   poolr$   monitorr"   topology_idOptional[ObjectId]	listenersOptional[_EventListeners]events#Optional[ReferenceType[Queue[Any]]]returnNonec                 C  sJ   || _ || _|| _|| _|duo|j| _|| _d| _| jr#| | _dS dS )zRepresent one MongoDB server.N)_description_pool_monitor_topology_idenabled_for_server_publish	_listener_events)selfr+   r,   r-   r.   r0   r2    r?   X/home/alumno/antzosa8961/venv/lib/python3.10/site-packages/pymongo/synchronous/server.py__init__>   s   
zServer.__init__c                 C  s   | j jjs| j  dS dS )z[Start monitoring, or restart after a fork.

        Multiple calls have no effect.
        N)r7   optsload_balancedr8   openr>   r?   r?   r@   rD   R   s   
zServer.open
service_idc                 C  s   | j | dS )zClear the connection pool.N)r,   reset)r>   rF   r?   r?   r@   rG   Z   s   zServer.resetc                 C  s   | j r | jdus
J | jdusJ | j| jj| jj| jff t	t
jr9tttj| j| jjd | jjd d | j  | j  dS )zXClear the connection pool and stop the monitor.

        Reconnect with open().
        Nr   r'   )message
topologyId
serverHost
serverPort)r;   r<   r=   putpublish_server_closedr6   addressr9   r   isEnabledForloggingDEBUGr   r   STOP_SERVERr8   closer7   rE   r?   r?   r@   rS   ^   s$   


zServer.closec                 C  s   | j   dS )zCheck the server's state soon.N)r8   request_checkrE   r?   r?   r@   rT   x   s   zServer.request_checkF	operationUnion[_Query, _GetMore]connr#   apply_timeoutbooltuple[dict[str, Any], str]c                 C  sJ   | ||\}}|jjr|jjjs|jj|j||j}|| ||fS N)
as_commandclient
_encrypter_bypass_auto_encryptionencryptdbcodec_optionsupdate_command)r>   rU   rW   rX   cmdra   r?   r?   r@   operation_to_command|   s   

zServer.operation_to_commandread_preferencer   
unpack_res!Callable[..., list[_DocumentOut]]r]   MongoClient[Any]r   c                 C  s  |dusJ |j }t }||}	|jo|jj}
| |||	\}}|
r'd}n||||	}| |\}}}t	
tjr]tt	tj|jj|tt|||||j|j|jd |jd |jd |r{d|vrg||d< |dusmJ |j||||j|j|jd zB|
r|d}n||| ||}|	rt}d}nd}d}|||j|j||d	}|	r|d }|j||j  t!||j"|j#d
 W nk t$y( } z^t | }t%|t&t'fr|j(}nt)|}t	
tjrtt	tj*|jj||tt|||||j|j|jd |jd |jt%|t'd |r#|dusJ |j+|||j,||j|j|j|d  d}~ww t | }|	r7|d }n.|j,dkrG|rD|d ni }n|j|- ddd}|j,dkr_||d d< n||d d< t	
tjrtt	tj.|jj||tt|||||j|j|jd |jd |jd |r|dusJ |j/|||j,||j|j|j|d |j}|r|j0r|	r|j01|2 }t3||j|}|4|j s|j5r|6  t%|t7r|j}
n	t8|j5o|j}
|jr|j9|
 t:|| j;j||||	||
d}|S t<|| j;j|||	|d}|S )a<  Run a _Query or _GetMore operation and return a Response object.

        This method is used only to run _Query/_GetMore operations from
        cursors.
        Can raise ConnectionFailure, OperationFailure, etc.

        :param conn: A Connection instance.
        :param operation: A _Query or _GetMore object.
        :param read_preference: The read preference to use.
        :param listeners: Instance of _EventListeners or None.
        :param unpack_res: A callable that decodes the wire protocol response.
        :param client: A MongoClient instance.
        Nr   r'   )rH   clientIdcommandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionIdrJ   rK   	serviceIdz$db)rF   FT)legacy_responseuser_fields)	pool_opts)rH   rj   
durationMSfailurerl   rm   rn   ro   rp   rq   rJ   rK   rr   isServerSideError)rF   database_nameexplain)idns)r&   okfindr&   r(   r)   )rH   rj   rv   replyrl   rm   rn   ro   rp   rq   rJ   rK   rr   )datarN   rW   duration
request_idfrom_commanddocsmore_to_come)r   rN   r   r   r   r   )=enabled_for_commandsr   nowuse_commandconn_mgrr   re   get_message_split_messager   rO   rP   rQ   r   r   STARTED_topology_settingsr9   nextiterr{   server_connection_idrN   rF   publish_command_startreceive_messagesend_message_CURSOR_DOC_FIELDS	cursor_idrb   r]   _process_responsesessionr   max_wire_versionrB   	Exception
isinstancer   r   detailsr   FAILEDpublish_command_failurename	namespace	SUCCEEDEDpublish_command_successr^   decryptraw_command_responser
   _should_pin_cursorexhaust
pin_cursorr   rY   update_exhaustr   r6   r   )r>   rW   rU   rf   r0   rg   r]   publishstartuse_cmdr   rd   dbnr   rH   r   max_doc_sizer   rt   rs   r   firstexcr   rw   res	decryptedresponser?   r?   r@   run_operation   sP  

	


%

	zServer.run_operationhandler"Optional[_MongoClientErrorHandler]ContextManager[Connection]c                 C  s   | j |S r[   )r,   checkout)r>   r   r?   r?   r@   r   ^  s   zServer.checkoutc                 C     | j S r[   )r6   rE   r?   r?   r@   descriptionc     zServer.descriptionc                 C  s   |j | jj ks	J || _d S r[   )rN   r6   )r>   r+   r?   r?   r@   r   g  s   
c                 C  r   r[   )r7   rE   r?   r?   r@   r,   l  r   zServer.poolrH   ,Union[tuple[int, Any], tuple[int, Any, int]]tuple[int, Any, int]c                 C  s"   t |dkr|S |\}}||dfS )zReturn request_id, data, max_doc_size.

        :param message: (request_id, data, max_doc_size) or (request_id, data)
           r   )len)r>   rH   r   r   r?   r?   r@   r   p  s   
zServer._split_messagestrc                 C  s   d| j j d| jdS )N< >)	__class____name__r6   rE   r?   r?   r@   __repr__~  s   zServer.__repr__)NNN)r+   r   r,   r$   r-   r"   r.   r/   r0   r1   r2   r3   r4   r5   )r4   r5   r[   )rF   r/   r4   r5   )F)rU   rV   rW   r#   rX   rY   r4   rZ   )rW   r#   rU   rV   rf   r   r0   r1   rg   rh   r]   ri   r4   r   )r   r   r4   r   )r4   r   )r+   r   r4   r5   )r4   r$   )rH   r   r4   r   )r4   r   )r   
__module____qualname__rA   rD   rG   rS   rT   re   r   r   r   propertyr   setterr,   r   r   r?   r?   r?   r@   r*   =   s0    


 V
r*   )<__doc__
__future__r   rP   r   typingr   r   r   r   r   r	   bsonr
   pymongo.errorsr   r   pymongo.helpers_sharedr   pymongo.loggerr   r   r   r   r   pymongo.messager   r   r   r   pymongo.responser   r   pymongo.synchronous.helpersr   queuer   weakrefr   bson.objectidr   pymongo.monitoringr   pymongo.read_preferencesr   pymongo.server_descriptionr    pymongo.synchronous.mongo_clientr    r!   pymongo.synchronous.monitorr"   pymongo.synchronous.poolr#   r$   pymongo.typingsr%   _IS_SYNCr   r*   r?   r?   r?   r@   <module>   s4    	