
    FCf02                    V   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mZ dd	lmZmZmZ dd
lmZmZmZmZ ddlmZmZ er@ddl m!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, ddl-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddddiZ6 G d d      Z7y)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_handle_reauth)_COMMAND_LOGGER_CommandStatusMessage
_debug_log)_convert_exception_GetMore_OpMsg_Query)PinnedResponseResponse)Queue)ReferenceType)ObjectId)MongoClient_MongoClientErrorHandler)Monitor)_EventListeners)
ConnectionPool)_ServerMode)ServerDescription)_DocumentOutcursor   )
firstBatch	nextBatchc                      e Zd Z	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZdddZddZddZe	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z		 d	 	 	 ddZ
edd	       Zej                  dd
       Zedd       Z	 	 	 	 ddZddZy)ServerNc                    || _         || _        || _        || _        |duxr |j                  | _        || _        d| _        | j
                  r |       | _        yy)zRepresent one MongoDB server.N)_description_pool_monitor_topology_idenabled_for_server_publish	_listener_events)selfserver_descriptionpoolmonitortopology_id	listenerseventss          N/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/pymongo/server.py__init__zServer.__init__.   s\     /
'!-N)2N2N"==!8DL     c                z    | j                   j                  j                  s| j                  j	                          yy)z[Start monitoring, or restart after a fork.

        Multiple calls have no effect.
        N)r-   optsload_balancedr.   openr4   s    r;   rA   zServer.openB   s*    
 zz,,MM  -r=   c                :    | j                   j                  |       y)zClear the connection pool.N)r6   reset)r4   
service_ids     r;   rD   zServer.resetJ   s    		
#r=   c                ^   | j                   rm| j                  J | j                  J | j                  j                  | j                  j                  | j
                  j                  | j                  ff       | j                  j                          | j                  j                          y)zXClear the connection pool and stop the monitor.

        Reconnect with open().
        N)r1   r2   r3   putpublish_server_closedr,   addressr/   r.   closer-   rB   s    r;   rJ   zServer.closeN   s    
 ==>>---<<+++LLNN88&&..0A0AB 	

r=   c                8    | j                   j                          y)zCheck the server's state soon.N)r.   request_checkrB   s    r;   rL   zServer.request_check_   s    ##%r=   c                   d}|J |j                   }t        j                         }	|j                  |      }
|j                  xr |j                  j
                  }|rd}n(|j                  |||
      }| j                  |      \  }}}|j                  |      \  }}t        j                  t        j                        rt        t        |j                  j                  t         j"                  |t%        t'        |            ||||j(                  |j*                  |j,                  d   |j,                  d   |j.                         |rV|j                  |      \  }}d|vr||d<   |J |j1                  ||||j,                  |j*                  |j.                         	 |r|j3                  d      }n#|j5                         |j3                  |      }|
r	t6        }d}nd}d} |||j8                  |j:                  ||	      }|
rA|d   }|j<                  j?                  ||j@                         tC        ||jD                         t        j                         |	z
  }|
r|d   }nY|jV                  dk(  r
|r|d   ni }n@|j8                  |jY                         ddd}|jV                  dk(  r	||d   d<   n||d   d<   t        j                  t        j                        rt        t        |j                  j                  t         jZ                  ||t%        t'        |            ||||j(                  |j*                  |j,                  d   |j,                  d   |j.                         |rE|J |j]                  |||jV                  ||j,                  |j*                  |j.                  |       |j<                  }|rN|j^                  rB|
r@|j^                  ja                  |jc                               }te        ||j:                  |      }|jg                  |j@                        s|jh                  r|jk                          tI        |tl              r|j
                  }n#to        |jh                  xr |j8                        }|j                  r|j                  jq                  |       ts        || jt                  j,                  ||||
||      }|S tw        || jt                  j,                  |||
|      }|S # tF        $ rM}t        j                         |	z
  }tI        |tJ        tL        f      r|jN                  }ntQ        |      }t        j                  t        j                        rt        t        |j                  j                  t         jR                  ||t%        t'        |            ||||j(                  |j*                  |j,                  d   |j,                  d   |j.                  tI        |tL              
       |rE|J |jU                  |||jV                  ||j,                  |j*                  |j.                  |        d}~ww xY w)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.
        Nr   r&   )clientIdmessagecommandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionId
serverHost
serverPort	serviceIdz$db)rE   FT)legacy_responseuser_fields)rN   rO   
durationMSfailurerQ   rR   rS   rT   rU   rV   rW   rX   rY   isServerSideError)rE   database_nameexplain)idns)r%   okfindr%   r'   r(   )rN   rO   r\   replyrQ   rR   rS   rT   rU   rV   rW   rX   rY   )datarI   connduration
request_idfrom_commanddocsmore_to_come)rf   rI   rh   ri   rj   rk   )<enabled_for_commandsr   nowuse_commandconn_mgrrl   get_message_split_message
as_commandr   isEnabledForloggingDEBUGr   _topology_settingsr/   r   STARTEDnextiterra   server_connection_idrI   rE   publish_command_startreceive_messagesend_message_CURSOR_DOC_FIELDS	cursor_idcodec_optionsclient_process_responsesessionr   max_wire_version	Exception
isinstancer   r   detailsr   FAILEDpublish_command_failurename	namespace	SUCCEEDEDpublish_command_success
_encrypterdecryptraw_command_responser   _should_pin_cursorexhaust
pin_cursorr   boolupdate_exhaustr   r,   r   )r4   rg   	operationread_preferencer9   
unpack_resr   rh   publishstartuse_cmdrl   ri   rO   rf   max_doc_sizecmddbnre   r[   rZ   rk   firstexcr]   res	decryptedresponses                               r;   run_operationzServer.run_operationc   s   , $$$00''- ))Mi.@.@.M.MJ++OT7KG-1-@-@-I*Jl''-S''622??-55 cO $&#'77#'#<#<<<?<<?//   ++D1HCC E
(((++))?? , =	,,T2!!$5,,Z8 0"'""&##'' /'D Q  225):K:KL't/D/DEL <<>E) q'C^^y(!$q'rC$)OO9;N;N;PQYZ[C~~'.2Hl+-1Hk*''622??-77# cO $&#'77#'#<#<<<?<<?//  (((--))??! . 	 !!f''"--55e6P6P6RS	,Y	8O8OQ\] $$Y%6%679;L;LOO%(  %11  $I$5$5$I%//J!!""11,?%))11!%$)	H(   ))11!%$H q  $	||~-H#1ABC(+,S1++GMM:##66CC188'# $T#Y!$( *'+ww'+'@'@#||A#||A"oo&06F&G"  ,,,11NNLL--#"% 2 	 I$	s   !B*T   
Y
EYYc                8    | j                   j                  |      S N)r6   checkout)r4   handlers     r;   r   zServer.checkout9  s     yy!!'**r=   c                    | j                   S r   )r,   rB   s    r;   descriptionzServer.description>  s       r=   c                \    |j                   | j                  j                   k(  sJ || _        y r   )rI   r,   )r4   r5   s     r;   r   zServer.descriptionB  s*    !))T->->-F-FFFF.r=   c                    | j                   S r   )r-   rB   s    r;   r6   zServer.poolG  s    zzr=   c                6    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)r4   rO   ri   rf   s       r;   rr   zServer._split_messageK  s.     w<1N  'JtQ&&r=   c                P    d| j                   j                   d| j                  dS )N< >)	__class____name__r,   rB   s    r;   __repr__zServer.__repr__Y  s)    4>>**+1T->->,ACCr=   )NNN)r5   r#   r6   r!   r7   r   r8   Optional[ObjectId]r9   Optional[_EventListeners]r:   zOptional[ReferenceType[Queue]]returnNone)r   r   r   )rE   r   r   r   )rg   r    r   zUnion[_Query, _GetMore]r   r"   r9   r   r   z!Callable[..., list[_DocumentOut]]r   r   r   r   )r   z"Optional[_MongoClientErrorHandler]r   zContextManager[Connection])r   r#   )r5   r#   r   r   )r   r!   )rO   z,Union[tuple[int, Any], tuple[int, Any, int]]r   ztuple[int, Any, int])r   str)r   
__module____qualname__r<   rA   rD   rJ   rL   r   r   r   propertyr   setterr6   rr   r    r=   r;   r*   r*   -   sD    +//315$-$ $ 	$
 ($ -$ /$ 
$(!$"& SS +S %	S
 -S 6S S 
S Sl =A+9+	#+
 ! ! / /  'C'	'Dr=   r*   )8__doc__
__future__r   ru   r   typingr   r   r   r   r	   r
   bsonr   pymongo.errorsr   r   pymongo.helpersr   r   pymongo.loggerr   r   r   pymongo.messager   r   r   r   pymongo.responser   r   queuer   weakrefr   bson.objectidr   pymongo.mongo_clientr   r   pymongo.monitorr   pymongo.monitoringr   pymongo.poolr    r!   pymongo.read_preferencesr"   pymongo.server_descriptionr#   pymongo.typingsr$   r   r*   r   r=   r;   <module>r      sp    9 "   P P & < C M M H H 5%&J'2-4<,qq AB mD mDr=   