
    nh6                    j   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r@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-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      Z;y)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                     e Zd Z	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZdddZddZddZ	 d	 	 	 	 	 	 	 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          /var/www/html/SchoolMeal/SchoolMeal/pds_admin_SchoolMeal/Backend/venv/lib/python3.12/site-packages/pymongo/synchronous/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_balancedr0   openr6   s    r=   rC   zServer.openR   s*    
 zz,,MM  -r?   c                :    | j                   j                  |       y)zClear the connection pool.N)r8   reset)r6   
service_ids     r=   rF   zServer.resetZ   s    		
#r?   c                X   | j                   rm| j                  J | j                  J | j                  j                  | j                  j                  | j
                  j                  | j                  ff       t        j                  t        j                        rZt        t        t        j                  | j                  | j
                  j                  d   | j
                  j                  d          | j                  j!                          | j"                  j!                          y)zXClear the connection pool and stop the monitor.

        Reconnect with open().
        Nr   r(   )message
topologyId
serverHost
serverPort)r3   r4   r5   putpublish_server_closedr.   addressr1   r   isEnabledForloggingDEBUGr   r   STOP_SERVERr0   closer/   rD   s    r=   rT   zServer.close^   s    
 ==>>---<<+++LLNN88&&..0A0AB $$W]]3*66,,,,44Q7,,44Q7 	

r?   c                8    | j                   j                          y)zCheck the server's state soon.N)r0   request_checkrD   s    r=   rV   zServer.request_checkx   s    ##%r?   c                8   |j                  ||      \  }}|j                  j                  r[|j                  j                  j                  s;|j                  j                  j	                  |j
                  ||j                        }|j                  |       ||fS N)
as_commandclient
_encrypter_bypass_auto_encryptionencryptdbcodec_optionsupdate_command)r6   	operationconnapply_timeoutcmdr^   s         r=   operation_to_commandzServer.operation_to_command|   s     &&t];R&&y/?/?/J/J/b/b""--55c9#:#:C 	  %Bwr?   c                   |J |j                   }t        j                         }|j                  |      }	|j                  xr |j                  j
                  }
| j                  |||	      \  }}|
rd}n(|j                  |||	      }| j                  |      \  }}}t        j                  t        j                        rt        t        t        j                  |j                   j"                  |t%        t'        |            ||||j(                  |j*                  |j,                  d   |j,                  d   |j.                         |rBd|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        t        jZ                  |j                   j"                  ||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        t        jR                  |j                   j"                  ||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.
        :param client: A MongoClient instance.
        Nr   r(   )rI   clientIdcommandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionIdrK   rL   	serviceIdz$db)rG   FT)legacy_responseuser_fields)rI   rg   
durationMSfailureri   rj   rk   rl   rm   rn   rK   rL   ro   isServerSideError)rG   database_nameexplain)idns)r'   okfindr'   r)   r*   )rI   rg   rr   replyri   rj   rk   rl   rm   rn   rK   rL   ro   )datarO   rb   duration
request_idfrom_commanddocsmore_to_come)r|   rO   r}   r~   r   r   )<enabled_for_commandsr   nowuse_commandconn_mgrr   re   get_message_split_messager   rP   rQ   rR   r   r   STARTED_topology_settingsr1   nextiterrw   server_connection_idrO   rG   publish_command_startreceive_messagesend_message_CURSOR_DOC_FIELDS	cursor_idr_   rZ   _process_responsesessionr   max_wire_version	Exception
isinstancer   r   detailsr   FAILEDpublish_command_failurename	namespace	SUCCEEDEDpublish_command_successr[   decryptraw_command_responser   _should_pin_cursorexhaust
pin_cursorr   boolupdate_exhaustr   r.   r   )r6   rb   ra   read_preferencer;   
unpack_resrZ   publishstartuse_cmdr   rd   dbnr~   rI   r|   max_doc_sizer{   rq   rp   r   firstexcr}   rs   res	decryptedresponses                               r=   run_operationzServer.run_operation   s   . $$$00''- ))Mi.@.@.M.M,,YgFSJ++OT7KG-1-@-@-I*Jl''6-5522?? cO $&#'77#'#<#<<<?<<?//  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-7722??# 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:#188#66CC'# $T#Y!$( *'+ww'+'@'@#||A#||A"oo&06F&G"  ,,,11NNLL--#"% 2 	 I$	s   B*S, ,
Y6EX>>Yc                8    | j                   j                  |      S rX   )r8   checkout)r6   handlers     r=   r   zServer.checkout^  s     yy!!'**r?   c                    | j                   S rX   )r.   rD   s    r=   descriptionzServer.descriptionc  s       r?   c                \    |j                   | j                  j                   k(  sJ || _        y rX   )rO   r.   )r6   r7   s     r=   r   zServer.descriptiong  s*    !))T->->-F-FFFF.r?   c                    | j                   S rX   )r/   rD   s    r=   r8   zServer.pooll  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)r6   rI   r~   r|   s       r=   r   zServer._split_messagep  s.     w<1N  'JtQ&&r?   c                P    d| j                   j                   d| j                  dS )N< >)	__class____name__r.   rD   s    r=   __repr__zServer.__repr__~  s)    4>>**+1T->->,ACCr?   )NNN)r7   r    r8   r%   r9   r#   r:   Optional[ObjectId]r;   Optional[_EventListeners]r<   zOptional[ReferenceType[Queue]]returnNone)r   r   rX   )rG   r   r   r   )F)ra   Union[_Query, _GetMore]rb   r$   rc   r   r   ztuple[dict[str, Any], str])rb   r$   ra   r   r   r   r;   r   r   z!Callable[..., list[_DocumentOut]]rZ   r!   r   r   )r   z"Optional[_MongoClientErrorHandler]r   zContextManager[Connection])r   r    )r7   r    r   r   )r   r%   )rI   z,Union[tuple[int, Any], tuple[int, Any, int]]r   ztuple[int, Any, int])r   str)r   
__module____qualname__r>   rC   rF   rT   rV   re   r   r   r   propertyr   setterr8   r   r    r?   r=   r,   r,   =   sp    +//315$-$ $ 	$
 ($ -$ /$ 
$(!$4&
 [`08BSW	# RR +R %	R
 -R 6R R 
R Rj =A+9+	#+
 ! ! / /  'C'	'Dr?   r,   )<__doc__
__future__r   rQ   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=   <module>r      s    9 "    ' < :  I H 5 6%&24<V39,qq AB BD BDr?   