
    FCfG                    n   U d Z ddlmZ ddlZddlmZmZ ddl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 erdd
lmZ ddlmZ ddlmZ  edd      Z eg g g g g       Z G d d      Z G d de      Z G d de      Z  G d de      Z! G d de      Z" G d de      Z#dfdZ$	 	 	 	 	 	 dgdZ%dhdZ&h dZ'de(d <   did!Z) G d" d#      Z* G d$ d%e*      Z+ G d& d'e*      Z, G d( d)e*      Z- G d* d+      Z. G d, d-e.      Z/ G d. d/e.      Z0 G d0 d1e.      Z1 G d2 d3e.      Z2 G d4 d5      Z3 G d6 d7      Z4 G d8 d9      Z5 G d: d;e5      Z6 G d< d=e6      Z7 G d> d?e6      Z8 G d@ dAe7      Z9 G dB dCe6      Z: G dD dEe5      Z; G dF dGe7      Z< G dH dIe7      Z= G dJ dKe6      Z> G dL dM      Z? G dN dOe?      Z@ G dP dQe?      ZA G dR dSe?      ZB G dT dU      ZC G dV dWeC      ZD G dX dYeC      ZE G dZ d[eC      ZF G d\ d]      ZG G d^ d_eG      ZH G d` daeG      ZI G db dceG      ZJ G dd de      ZKy)ja  Tools to monitor driver events.

.. versionadded:: 3.1

.. attention:: Starting in PyMongo 3.11, the monitoring classes outlined below
    are included in the PyMongo distribution under the
    :mod:`~pymongo.event_loggers` submodule.

Use :func:`register` to register global listeners for specific events.
Listeners must inherit from one of the abstract classes below and implement
the correct functions for that class.

For example, a simple command logger might be implemented like this::

    import logging

    from pymongo import monitoring

    class CommandLogger(monitoring.CommandListener):

        def started(self, event):
            logging.info("Command {0.command_name} with request id "
                         "{0.request_id} started on server "
                         "{0.connection_id}".format(event))

        def succeeded(self, event):
            logging.info("Command {0.command_name} with request id "
                         "{0.request_id} on server {0.connection_id} "
                         "succeeded in {0.duration_micros} "
                         "microseconds".format(event))

        def failed(self, event):
            logging.info("Command {0.command_name} with request id "
                         "{0.request_id} on server {0.connection_id} "
                         "failed in {0.duration_micros} "
                         "microseconds".format(event))

    monitoring.register(CommandLogger())

Server discovery and monitoring events are also available. For example::

    class ServerLogger(monitoring.ServerListener):

        def opened(self, event):
            logging.info("Server {0.server_address} added to topology "
                         "{0.topology_id}".format(event))

        def description_changed(self, event):
            previous_server_type = event.previous_description.server_type
            new_server_type = event.new_description.server_type
            if new_server_type != previous_server_type:
                # server_type_name was added in PyMongo 3.4
                logging.info(
                    "Server {0.server_address} changed type from "
                    "{0.previous_description.server_type_name} to "
                    "{0.new_description.server_type_name}".format(event))

        def closed(self, event):
            logging.warning("Server {0.server_address} removed from topology "
                            "{0.topology_id}".format(event))


    class HeartbeatLogger(monitoring.ServerHeartbeatListener):

        def started(self, event):
            logging.info("Heartbeat sent to server "
                         "{0.connection_id}".format(event))

        def succeeded(self, event):
            # The reply.document attribute was added in PyMongo 3.4.
            logging.info("Heartbeat to server {0.connection_id} "
                         "succeeded with reply "
                         "{0.reply.document}".format(event))

        def failed(self, event):
            logging.warning("Heartbeat to server {0.connection_id} "
                            "failed with error {0.reply}".format(event))

    class TopologyLogger(monitoring.TopologyListener):

        def opened(self, event):
            logging.info("Topology with id {0.topology_id} "
                         "opened".format(event))

        def description_changed(self, event):
            logging.info("Topology description updated for "
                         "topology id {0.topology_id}".format(event))
            previous_topology_type = event.previous_description.topology_type
            new_topology_type = event.new_description.topology_type
            if new_topology_type != previous_topology_type:
                # topology_type_name was added in PyMongo 3.4
                logging.info(
                    "Topology {0.topology_id} changed type from "
                    "{0.previous_description.topology_type_name} to "
                    "{0.new_description.topology_type_name}".format(event))
            # The has_writable_server and has_readable_server methods
            # were added in PyMongo 3.4.
            if not event.new_description.has_writable_server():
                logging.warning("No writable servers available.")
            if not event.new_description.has_readable_server():
                logging.warning("No readable servers available.")

        def closed(self, event):
            logging.info("Topology with id {0.topology_id} "
                         "closed".format(event))

Connection monitoring and pooling events are also available. For example::

    class ConnectionPoolLogger(ConnectionPoolListener):

        def pool_created(self, event):
            logging.info("[pool {0.address}] pool created".format(event))

        def pool_ready(self, event):
            logging.info("[pool {0.address}] pool is ready".format(event))

        def pool_cleared(self, event):
            logging.info("[pool {0.address}] pool cleared".format(event))

        def pool_closed(self, event):
            logging.info("[pool {0.address}] pool closed".format(event))

        def connection_created(self, event):
            logging.info("[pool {0.address}][connection #{0.connection_id}] "
                         "connection created".format(event))

        def connection_ready(self, event):
            logging.info("[pool {0.address}][connection #{0.connection_id}] "
                         "connection setup succeeded".format(event))

        def connection_closed(self, event):
            logging.info("[pool {0.address}][connection #{0.connection_id}] "
                         "connection closed, reason: "
                         "{0.reason}".format(event))

        def connection_check_out_started(self, event):
            logging.info("[pool {0.address}] connection check out "
                         "started".format(event))

        def connection_check_out_failed(self, event):
            logging.info("[pool {0.address}] connection check out "
                         "failed, reason: {0.reason}".format(event))

        def connection_checked_out(self, event):
            logging.info("[pool {0.address}][connection #{0.connection_id}] "
                         "connection checked out of pool".format(event))

        def connection_checked_in(self, event):
            logging.info("[pool {0.address}][connection #{0.connection_id}] "
                         "connection checked into pool".format(event))


Event listeners can also be registered per instance of
:class:`~pymongo.mongo_client.MongoClient`::

    client = MongoClient(event_listeners=[CommandLogger()])

Note that previously registered global listeners are automatically included
when configuring per client event listeners. Registering a new global listener
will not add that listener to existing client instances.

.. note:: Events are delivered **synchronously**. Application threads block
  waiting for event handlers (e.g. :meth:`~CommandListener.started`) to
  return. Care must be taken to ensure that your event handlers are efficient
  enough to not adversely affect overall application performance.

.. warning:: The command documents published through this API are *not* copies.
  If you intend to modify them in any way you must copy them in your event
  handler first.
    )annotationsN)abc
namedtuple)TYPE_CHECKINGAnyMappingOptionalSequence)ObjectId)HelloHelloCompat)_handle_exception)_Address_DocumentOut)	timedelta)ServerDescription)TopologyDescription
_Listeners)command_listenersserver_listenersserver_heartbeat_listenerstopology_listenerscmap_listenersc                      e Zd ZdZy)_EventListenerz,Abstract base class for all event listeners.N)__name__
__module____qualname____doc__     R/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/pymongo/monitoring.pyr   r      s    6r!   r   c                  (    e Zd ZdZddZddZddZy)	CommandListenerzAbstract base class for command listeners.

    Handles `CommandStartedEvent`, `CommandSucceededEvent`,
    and `CommandFailedEvent`.
    c                    t         )zAbstract method to handle a `CommandStartedEvent`.

        :param event: An instance of :class:`CommandStartedEvent`.
        NotImplementedErrorselfevents     r"   startedzCommandListener.started   
    
 "!r!   c                    t         )zAbstract method to handle a `CommandSucceededEvent`.

        :param event: An instance of :class:`CommandSucceededEvent`.
        r&   r(   s     r"   	succeededzCommandListener.succeeded   r,   r!   c                    t         )z}Abstract method to handle a `CommandFailedEvent`.

        :param event: An instance of :class:`CommandFailedEvent`.
        r&   r(   s     r"   failedzCommandListener.failed   r,   r!   N)r*   CommandStartedEventreturnNone)r*   CommandSucceededEventr2   r3   )r*   CommandFailedEventr2   r3   r   r   r   r   r+   r.   r0   r    r!   r"   r$   r$      s    """r!   r$   c                  h    e Zd ZdZddZddZddZddZddZddZ	ddZ
dd	Zdd
ZddZddZy)ConnectionPoolListenera-  Abstract base class for connection pool listeners.

    Handles all of the connection pool events defined in the Connection
    Monitoring and Pooling Specification:
    :class:`PoolCreatedEvent`, :class:`PoolClearedEvent`,
    :class:`PoolClosedEvent`, :class:`ConnectionCreatedEvent`,
    :class:`ConnectionReadyEvent`, :class:`ConnectionClosedEvent`,
    :class:`ConnectionCheckOutStartedEvent`,
    :class:`ConnectionCheckOutFailedEvent`,
    :class:`ConnectionCheckedOutEvent`,
    and :class:`ConnectionCheckedInEvent`.

    .. versionadded:: 3.9
    c                    t         )zAbstract method to handle a :class:`PoolCreatedEvent`.

        Emitted when a connection Pool is created.

        :param event: An instance of :class:`PoolCreatedEvent`.
        r&   r(   s     r"   pool_createdz#ConnectionPoolListener.pool_created  
     "!r!   c                    t         )zAbstract method to handle a :class:`PoolReadyEvent`.

        Emitted when a connection Pool is marked ready.

        :param event: An instance of :class:`PoolReadyEvent`.

        .. versionadded:: 4.0
        r&   r(   s     r"   
pool_readyz!ConnectionPoolListener.pool_ready  s
     "!r!   c                    t         )zAbstract method to handle a `PoolClearedEvent`.

        Emitted when a connection Pool is cleared.

        :param event: An instance of :class:`PoolClearedEvent`.
        r&   r(   s     r"   pool_clearedz#ConnectionPoolListener.pool_cleared  r;   r!   c                    t         )zAbstract method to handle a `PoolClosedEvent`.

        Emitted when a connection Pool is closed.

        :param event: An instance of :class:`PoolClosedEvent`.
        r&   r(   s     r"   pool_closedz"ConnectionPoolListener.pool_closed(  r;   r!   c                    t         )zAbstract method to handle a :class:`ConnectionCreatedEvent`.

        Emitted when a connection Pool creates a Connection object.

        :param event: An instance of :class:`ConnectionCreatedEvent`.
        r&   r(   s     r"   connection_createdz)ConnectionPoolListener.connection_created1  r;   r!   c                    t         )zAbstract method to handle a :class:`ConnectionReadyEvent`.

        Emitted when a connection has finished its setup, and is now ready to
        use.

        :param event: An instance of :class:`ConnectionReadyEvent`.
        r&   r(   s     r"   connection_readyz'ConnectionPoolListener.connection_ready:  
     "!r!   c                    t         )zAbstract method to handle a :class:`ConnectionClosedEvent`.

        Emitted when a connection Pool closes a connection.

        :param event: An instance of :class:`ConnectionClosedEvent`.
        r&   r(   s     r"   connection_closedz(ConnectionPoolListener.connection_closedD  r;   r!   c                    t         )zAbstract method to handle a :class:`ConnectionCheckOutStartedEvent`.

        Emitted when the driver starts attempting to check out a connection.

        :param event: An instance of :class:`ConnectionCheckOutStartedEvent`.
        r&   r(   s     r"   connection_check_out_startedz3ConnectionPoolListener.connection_check_out_startedM  r;   r!   c                    t         )zAbstract method to handle a :class:`ConnectionCheckOutFailedEvent`.

        Emitted when the driver's attempt to check out a connection fails.

        :param event: An instance of :class:`ConnectionCheckOutFailedEvent`.
        r&   r(   s     r"   connection_check_out_failedz2ConnectionPoolListener.connection_check_out_failedV  r;   r!   c                    t         )zAbstract method to handle a :class:`ConnectionCheckedOutEvent`.

        Emitted when the driver successfully checks out a connection.

        :param event: An instance of :class:`ConnectionCheckedOutEvent`.
        r&   r(   s     r"   connection_checked_outz-ConnectionPoolListener.connection_checked_out_  r;   r!   c                    t         )zAbstract method to handle a :class:`ConnectionCheckedInEvent`.

        Emitted when the driver checks in a connection back to the connection
        Pool.

        :param event: An instance of :class:`ConnectionCheckedInEvent`.
        r&   r(   s     r"   connection_checked_inz,ConnectionPoolListener.connection_checked_inh  rF   r!   N)r*   PoolCreatedEventr2   r3   )r*   PoolReadyEventr2   r3   )r*   PoolClearedEventr2   r3   )r*   PoolClosedEventr2   r3   )r*   ConnectionCreatedEventr2   r3   )r*   ConnectionReadyEventr2   r3   )r*   ConnectionClosedEventr2   r3   )r*   ConnectionCheckOutStartedEventr2   r3   )r*   ConnectionCheckOutFailedEventr2   r3   )r*   ConnectionCheckedOutEventr2   r3   )r*   ConnectionCheckedInEventr2   r3   )r   r   r   r   r:   r=   r?   rA   rC   rE   rH   rJ   rL   rN   rP   r    r!   r"   r8   r8      s>    "	""""""""""r!   r8   c                  (    e Zd ZdZddZddZddZy)	ServerHeartbeatListenerzAbstract base class for server heartbeat listeners.

    Handles `ServerHeartbeatStartedEvent`, `ServerHeartbeatSucceededEvent`,
    and `ServerHeartbeatFailedEvent`.

    .. versionadded:: 3.3
    c                    t         )zAbstract method to handle a `ServerHeartbeatStartedEvent`.

        :param event: An instance of :class:`ServerHeartbeatStartedEvent`.
        r&   r(   s     r"   r+   zServerHeartbeatListener.started|  r,   r!   c                    t         )zAbstract method to handle a `ServerHeartbeatSucceededEvent`.

        :param event: An instance of :class:`ServerHeartbeatSucceededEvent`.
        r&   r(   s     r"   r.   z!ServerHeartbeatListener.succeeded  r,   r!   c                    t         )zAbstract method to handle a `ServerHeartbeatFailedEvent`.

        :param event: An instance of :class:`ServerHeartbeatFailedEvent`.
        r&   r(   s     r"   r0   zServerHeartbeatListener.failed  r,   r!   N)r*   ServerHeartbeatStartedEventr2   r3   )r*   ServerHeartbeatSucceededEventr2   r3   )r*   ServerHeartbeatFailedEventr2   r3   r6   r    r!   r"   r]   r]   s  s    """r!   r]   c                  (    e Zd ZdZddZddZddZy)	TopologyListenerzAbstract base class for topology monitoring listeners.
    Handles `TopologyOpenedEvent`, `TopologyDescriptionChangedEvent`, and
    `TopologyClosedEvent`.

    .. versionadded:: 3.3
    c                    t         )zAbstract method to handle a `TopologyOpenedEvent`.

        :param event: An instance of :class:`TopologyOpenedEvent`.
        r&   r(   s     r"   openedzTopologyListener.opened  r,   r!   c                    t         )zAbstract method to handle a `TopologyDescriptionChangedEvent`.

        :param event: An instance of :class:`TopologyDescriptionChangedEvent`.
        r&   r(   s     r"   description_changedz$TopologyListener.description_changed  r,   r!   c                    t         )zAbstract method to handle a `TopologyClosedEvent`.

        :param event: An instance of :class:`TopologyClosedEvent`.
        r&   r(   s     r"   closedzTopologyListener.closed  r,   r!   N)r*   TopologyOpenedEventr2   r3   )r*   TopologyDescriptionChangedEventr2   r3   )r*   TopologyClosedEventr2   r3   r   r   r   r   rg   ri   rk   r    r!   r"   re   re         """r!   re   c                  (    e Zd ZdZddZddZddZy)	ServerListenerzAbstract base class for server listeners.
    Handles `ServerOpeningEvent`, `ServerDescriptionChangedEvent`, and
    `ServerClosedEvent`.

    .. versionadded:: 3.3
    c                    t         )z}Abstract method to handle a `ServerOpeningEvent`.

        :param event: An instance of :class:`ServerOpeningEvent`.
        r&   r(   s     r"   rg   zServerListener.opened  r,   r!   c                    t         )zAbstract method to handle a `ServerDescriptionChangedEvent`.

        :param event: An instance of :class:`ServerDescriptionChangedEvent`.
        r&   r(   s     r"   ri   z"ServerListener.description_changed  r,   r!   c                    t         )z{Abstract method to handle a `ServerClosedEvent`.

        :param event: An instance of :class:`ServerClosedEvent`.
        r&   r(   s     r"   rk   zServerListener.closed  r,   r!   N)r*   ServerOpeningEventr2   r3   )r*   ServerDescriptionChangedEventr2   r3   )r*   ServerClosedEventr2   r3   ro   r    r!   r"   rr   rr     rp   r!   rr   c                :    t        | j                         dz        S )z'Convert duration 'dur' to microseconds.g    .A)inttotal_seconds)durs    r"   
_to_microsr}     s    s  "T)**r!   c                    t        |t        j                        st        |  d      |D ]!  }t        |t              rt        d|  d       |S )zValidate event listenersz must be a list or tupleListeners for x must be either a CommandListener, ServerHeartbeatListener, ServerListener, TopologyListener, or ConnectionPoolListener.)
isinstancer   r
   	TypeErrorr   )option	listenerslisteners      r"   _validate_event_listenersr     sd     i.6(":;<< (N3  )* *  r!   c                   t        | t              st        d|  d      t        | t              rt        j
                  j                  |        t        | t              rt        j                  j                  |        t        | t              rt        j                  j                  |        t        | t              rt        j                  j                  |        t        | t              r t        j                  j                  |        yy)zRegister a global event listener.

    :param listener: A subclasses of :class:`CommandListener`,
        :class:`ServerHeartbeatListener`, :class:`ServerListener`,
        :class:`TopologyListener`, or :class:`ConnectionPoolListener`.
    r   r   N)r   r   r   r$   
_LISTENERSr   appendr]   r   rr   r   re   r   r8   r   )r   s    r"   registerr     s     h/XJ '& &
 	
 (O,$$++H5(34--44X>(N+##**84(,-%%,,X6(23!!((2 4r!   >	   copydbgetnonce	saslstart
createuser
updateuserauthenticatesaslcontinuecopydbgetnoncecopydbsaslstartset_SENSITIVE_COMMANDSc                R    | j                         dt        j                  fv rd|v ryy)NhellospeculativeAuthenticateTF)lowerr   
LEGACY_CMD)command_namedocs     r"   _is_speculative_authenticater     s,    +*@*@ AA%,r!   c                      e Zd ZdZdZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZedd       Zedd       Zedd       Z	edd       Z
edd	       Zedd
       Zedd       Zy)_CommandEventzBase class for command events.)
__cmd_name	__rqst_id	__conn_id__op_id__service_id__db__server_conn_idNc                f    || _         || _        || _        || _        || _        || _        || _        y N)_CommandEvent__cmd_name_CommandEvent__rqst_id_CommandEvent__conn_id_CommandEvent__op_id_CommandEvent__service_id_CommandEvent__db_CommandEvent__server_conn_id)r)   r   
request_idconnection_idoperation_id
service_iddatabase_nameserver_connection_ids           r"   __init__z_CommandEvent.__init__&  s7     '#&#&!	 4r!   c                    | j                   S )zThe command name.)r   r)   s    r"   r   z_CommandEvent.command_name8       r!   c                    | j                   S )z"The request id for this operation.)r   r   s    r"   r   z_CommandEvent.request_id=       ~~r!   c                    | j                   S )z@The address (host, port) of the server this command was sent to.)r   r   s    r"   r   z_CommandEvent.connection_idB  r   r!   c                    | j                   S )z^The service_id this command was sent to, or ``None``.

        .. versionadded:: 3.12
        )r   r   s    r"   r   z_CommandEvent.service_idG  s        r!   c                    | j                   S )z(An id for this series of events or None.)r   r   s    r"   r   z_CommandEvent.operation_idO       ||r!   c                    | j                   S )z^The database_name this command was sent to, or ``""``.

        .. versionadded:: 4.6
        )r   r   s    r"   r   z_CommandEvent.database_nameT  s     yyr!   c                    | j                   S )zThe server-side connection id for the connection this command was sent on, or ``None``.

        .. versionadded:: 4.7
        )r   r   s    r"   r   z"_CommandEvent.server_connection_id\  s     $$$r!   N N)r   strr   rz   r   r   r   Optional[int]r   Optional[ObjectId]r   r   r   r   r2   r3   r2   r   r2   rz   r2   r   r2   r   )r2   r   )r   r   r   r   	__slots__r   propertyr   r   r   r   r   r   r   r    r!   r"   r   r     s    (I  *..255 5  	5
 $5 '5 5 ,5 
5$       ! !     % %r!   r   c                  |     e Zd ZdZdZ	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZed	d       Zed
 fd       Zd
dZ	 xZ
S )r1   a  Event published when a command starts.

    :param command: The command document.
    :param database_name: The name of the database this command was run against.
    :param request_id: The request id for this operation.
    :param connection_id: The address (host, port) of the server this command
        was sent to.
    :param operation_id: An optional identifier for a series of related events.
    :param service_id: The service_id this command was sent to, or ``None``.
    )__cmdc           	         |st        |d      t        t        |            }t        
|   |||||||       |j                         }	|	t        v st        |	|      ri | _        y || _        y )Nz is not a valid commandr   r   r   )	
ValueErrornextitersuperr   r   r   r   _CommandStartedEvent__cmd)r)   commandr   r   r   r   r   r   r   cmd_name	__class__s             r"   r   zCommandStartedEvent.__init__s  s     {*ABCCDM*!'!5 	 	
  %%'**.J8U\.]')DJ DJr!   c                    | j                   S )zThe command document.)r   r   s    r"   r   zCommandStartedEvent.command  s     zzr!   c                    t         |   S )z6The name of the database this command was run against.)r   r   r)   r   s    r"   r   z!CommandStartedEvent.database_name  s     w$$r!   c           	         dj                  | j                  j                  | j                  | j                  | j
                  | j                  | j                  | j                        S )Nz[<{} {} db: {!r}, command: {!r}, operation_id: {}, service_id: {}, server_connection_id: {}>)	formatr   r   r   r   r   r   r   r   r   s    r"   __repr__zCommandStartedEvent.__repr__  sU    i
&NN##OO%%

	
r!   NN)r   r   r   r   r   rz   r   r   r   r   r   r   r   r   r2   r3   r2   r   r   )r   r   r   r   r   r   r   r   r   r   __classcell__r   s   @r"   r1   r1   e  s    	 I *..2!! ! 	!
  ! $! '! ,! 
!:   % %
r!   r1   c                       e Zd ZdZdZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZed	d       Zed
d       ZddZ	 xZ
S )r4   aO  Event published when a command succeeds.

    :param duration: The command duration as a datetime.timedelta.
    :param reply: The server reply document.
    :param command_name: The command name.
    :param request_id: The request id for this operation.
    :param connection_id: The address (host, port) of the server this command
        was sent to.
    :param operation_id: An optional identifier for a series of related events.
    :param service_id: The service_id this command was sent to, or ``None``.
    :param database_name: The database this command was sent to, or ``""``.
    )__duration_micros__replyc
           	         t         |   |||||||	       t        |      | _        |j	                         }
|
t
        v st        |
|      ri | _        y || _        y Nr   )r   r   r}   '_CommandSucceededEvent__duration_microsr   r   r   _CommandSucceededEvent__reply)r)   durationreplyr   r   r   r   r   r   r   r   r   s              r"   r   zCommandSucceededEvent.__init__  sk     	!'!5 	 	
 ",H!5%%'**.J8UZ.[)+DL DLr!   c                    | j                   S z/The duration of this operation in microseconds.)r   r   s    r"   duration_microsz%CommandSucceededEvent.duration_micros       %%%r!   c                    | j                   S z/The server failure document for this operation.)r   r   s    r"   r   zCommandSucceededEvent.reply  r   r!   c           
         dj                  | j                  j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                        S )Nzp<{} {} db: {!r}, command: {!r}, operation_id: {}, duration_micros: {}, service_id: {}, server_connection_id: {}>)
r   r   r   r   r   r   r   r   r   r   r   s    r"   r   zCommandSucceededEvent.__repr__  s^    ~
&NN##  OO%%	
	
r!   r   )r   datetime.timedeltar   r   r   r   r   rz   r   r   r   r   r   r   r   r   r   r   r2   r3   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   s   @r"   r4   r4     s     1I *..2!$! ! 	!
 !  ! $! '! ! ,! 
!8 & &  
r!   r4   c                       e Zd ZdZdZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZed	d       Zed
d       ZddZ	 xZ
S )r5   aN  Event published when a command fails.

    :param duration: The command duration as a datetime.timedelta.
    :param failure: The server reply document.
    :param command_name: The command name.
    :param request_id: The request id for this operation.
    :param connection_id: The address (host, port) of the server this command
        was sent to.
    :param operation_id: An optional identifier for a series of related events.
    :param service_id: The service_id this command was sent to, or ``None``.
    :param database_name: The database this command was sent to, or ``""``.
    )r   	__failurec
           	     `    t         
|   |||||||	       t        |      | _        || _        y r   )r   r   r}   $_CommandFailedEvent__duration_micros_CommandFailedEvent__failure)r)   r   failurer   r   r   r   r   r   r   r   s             r"   r   zCommandFailedEvent.__init__  sB     	!'!5 	 	
 ",H!5 r!   c                    | j                   S r   )r   r   s    r"   r   z"CommandFailedEvent.duration_micros  r   r!   c                    | j                   S r   )r   r   s    r"   r   zCommandFailedEvent.failure  r   r!   c                    dj                  | j                  j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  	      S )Nz<{} {} db: {!r}, command: {!r}, operation_id: {}, duration_micros: {}, failure: {!r}, service_id: {}, server_connection_id: {}>)r   r   r   r   r   r   r   r   r   r   r   r   s    r"   r   zCommandFailedEvent.__repr__  sh    G
&NN##  LLOO%%

	
r!   r   )r   r   r   r   r   r   r   rz   r   r   r   r   r   r   r   r   r   r   r2   r3   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   s   @r"   r5   r5     s     3I *..2!$! ! 	!
 !  ! $! '! ! ,! 
!0 & &  
r!   r5   c                  6    e Zd ZdZdZddZedd       Zd	dZy)

_PoolEventzBase class for pool events.	__addressc                    || _         y r   _PoolEvent__addressr)   addresss     r"   r   z_PoolEvent.__init__5  	     r!   c                    | j                   S )zbThe address (host, port) pair of the server the pool is attempting
        to connect to.
        r  r   s    r"   r  z_PoolEvent.address8      
 ~~r!   c                N    | j                   j                   d| j                  dS N())r   r   r  r   s    r"   r   z_PoolEvent.__repr__?  %    ..))*!DNN+=Q??r!   Nr  r   r2   r3   r   r   	r   r   r   r   r   r   r   r  r   r    r!   r"   r   r   0  s)    %I!  @r!   r   c                  B     e Zd ZdZdZd fdZedd       ZddZ xZ	S )	rQ   zPublished when a Connection Pool is created.

    :param address: The address (host, port) pair of the server this Pool is
       attempting to connect to.

    .. versionadded:: 3.9
    )	__optionsc                2    t         |   |       || _        y r   )r   r   _PoolCreatedEvent__options)r)   r  optionsr   s      r"   r   zPoolCreatedEvent.__init__N  s    ! r!   c                    | j                   S )zCAny non-default pool options that were set on this Connection Pool.)r  r   s    r"   r  zPoolCreatedEvent.optionsR  r   r!   c                h    | j                   j                   d| j                  d| j                  dS Nr  z, r  )r   r   r  r  r   s    r"   r   zPoolCreatedEvent.__repr__W  s0    ..))*!DLL+;2dnn=OqQQr!   r  r   r  dict[str, Any]r2   r3   )r2   r  r   )
r   r   r   r   r   r   r   r  r   r   r   s   @r"   rQ   rQ   C  s.     I!  Rr!   rQ   c                      e Zd ZdZdZy)rR   zPublished when a Connection Pool is marked ready.

    :param address: The address (host, port) pair of the server this Pool is
       attempting to connect to.

    .. versionadded:: 4.0
    r    Nr   r   r   r   r   r    r!   r"   rR   rR   [       Ir!   rR   c                  h     e Zd ZdZdZ	 	 d	 	 	 	 	 	 	 d fdZed	d       Zed
d       ZddZ	 xZ
S )rS   aw  Published when a Connection Pool is cleared.

    :param address: The address (host, port) pair of the server this Pool is
       attempting to connect to.
    :param service_id: The service_id this command was sent to, or ``None``.
    :param interrupt_connections: True if all active connections were interrupted by the Pool during clearing.

    .. versionadded:: 3.9
    )r   __interrupt_connectionsc                @    t         |   |       || _        || _        y r   )r   r   _PoolClearedEvent__service_id(_PoolClearedEvent__interrupt_connections)r)   r  r   interrupt_connectionsr   s       r"   r   zPoolClearedEvent.__init__t  s"     	!&'<$r!   c                    | j                   S )zConnections with this service_id are cleared.

        When service_id is ``None``, all connections in the pool are cleared.

        .. versionadded:: 3.12
        )r   r   s    r"   r   zPoolClearedEvent.service_id~  s        r!   c                    | j                   S )zdIf True, active connections are interrupted during clearing.

        .. versionadded:: 4.7
        )r!  r   s    r"   r"  z&PoolClearedEvent.interrupt_connections  s     +++r!   c                    | j                   j                   d| j                  d| j                  d| j                  dS r  )r   r   r  r   r!  r   s    r"   r   zPoolClearedEvent.__repr__  sB    ..))*!DLL+;2d>O>O=RRTUYUqUqTttuvvr!   )NFr  r   r   r   r"  boolr2   r3   r   r2   r'  r   )r   r   r   r   r   r   r   r   r"  r   r   r   s   @r"   rS   rS   g  sp     <I
 *.&+	== '=  $	=
 
= ! ! , ,wr!   rS   c                      e Zd ZdZdZy)rT   zPublished when a Connection Pool is closed.

    :param address: The address (host, port) pair of the server this Pool is
       attempting to connect to.

    .. versionadded:: 3.9
    r    Nr  r    r!   r"   rT   rT     r  r!   rT   c                  &    e Zd ZdZdZ	 dZ	 dZ	 dZy)ConnectionClosedReasonzqAn enum that defines values for `reason` on a
    :class:`ConnectionClosedEvent`.

    .. versionadded:: 3.9
    staleidleerror
poolClosedN)r   r   r   r   STALEIDLEERRORPOOL_CLOSEDr    r!   r"   r+  r+    s-     EFD EIKEr!   r+  c                       e Zd ZdZdZ	 dZ	 dZy)ConnectionCheckOutFailedReasonzyAn enum that defines values for `reason` on a
    :class:`ConnectionCheckOutFailedEvent`.

    .. versionadded:: 3.9
    timeoutr/  connectionErrorN)r   r   r   r   TIMEOUTr3  
CONN_ERRORr    r!   r"   r5  r5    s#     GJKM"Jr!   r5  c                  6    e Zd ZdZdZddZedd       Zd	dZy)
_ConnectionEventz)Private base class for connection events.r   c                    || _         y r   _ConnectionEvent__addressr  s     r"   r   z_ConnectionEvent.__init__  r  r!   c                    | j                   S )ziThe address (host, port) pair of the server this connection is
        attempting to connect to.
        r=  r   s    r"   r  z_ConnectionEvent.address  r  r!   c                N    | j                   j                   d| j                  dS r
  )r   r   r>  r   s    r"   r   z_ConnectionEvent.__repr__  r  r!   Nr  r   r   r  r    r!   r"   r;  r;    s)    3I!  @r!   r;  c                  B     e Zd ZdZdZd fdZedd       ZddZ xZ	S )	_ConnectionIdEventz4Private base class for connection events with an id.)__connection_idc                2    t         |   |       || _        y r   )r   r   !_ConnectionIdEvent__connection_id)r)   r  r   r   s      r"   r   z_ConnectionIdEvent.__init__  s    !,r!   c                    | j                   S )zThe ID of the connection.)rE  r   s    r"   r   z _ConnectionIdEvent.connection_id  s     ###r!   c                h    | j                   j                   d| j                  d| j                  dS r  )r   r   r  rE  r   s    r"   r   z_ConnectionIdEvent.__repr__  s3    ..))*!DLL+;2d>R>R=UUVWWr!   r  r   r   rz   r2   r3   r   r   )
r   r   r   r   r   r   r   r   r   r   r   s   @r"   rB  rB    s*    >$I- $ $Xr!   rB  c                  B     e Zd ZdZdZd fdZedd       ZddZ xZ	S )	_ConnectionDurationEventz9Private base class for connection events with a duration.)
__durationc                4    t         |   ||       || _        y r   )r   r   "_ConnectionDurationEvent__duration)r)   r  r   r   r   s       r"   r   z!_ConnectionDurationEvent.__init__  s    -0"r!   c                    | j                   S )zMThe duration of the connection event.

        .. versionadded:: 4.7
        )rM  r   s    r"   r   z!_ConnectionDurationEvent.duration  s     r!   c                    | j                   j                   d| j                  d| j                  d| j                  dS r  )r   r   r  r   rM  r   s    r"   r   z!_ConnectionDurationEvent.__repr__  sB    ..))*!DLL+;2d>P>P=SSUVZVeVeUhhijjr!   )r  r   r   rz   r   Optional[float]r2   r3   )r2   rP  r   )
r   r   r   r   r   r   r   r   r   r   r   s   @r"   rJ  rJ    s*    CI#  kr!   rJ  c                      e Zd ZdZdZy)rU   a  Published when a Connection Pool creates a Connection object.

    NOTE: This connection is not ready for use until the
    :class:`ConnectionReadyEvent` is published.

    :param address: The address (host, port) pair of the server this
       Connection is attempting to connect to.
    :param connection_id: The integer ID of the Connection in this Pool.

    .. versionadded:: 3.9
    r    Nr  r    r!   r"   rU   rU     s    
 Ir!   rU   c                      e Zd ZdZdZy)rV   a&  Published when a Connection has finished its setup, and is ready to use.

    :param address: The address (host, port) pair of the server this
       Connection is attempting to connect to.
    :param connection_id: The integer ID of the Connection in this Pool.

    .. versionadded:: 3.9
    r    Nr  r    r!   r"   rV   rV          Ir!   rV   c                  B     e Zd ZdZdZd fdZedd       ZddZ xZ	S )rW   aK  Published when a Connection is closed.

    :param address: The address (host, port) pair of the server this
       Connection is attempting to connect to.
    :param connection_id: The integer ID of the Connection in this Pool.
    :param reason: A reason explaining why this connection was closed.

    .. versionadded:: 3.9
    __reasonc                4    t         |   ||       || _        y r   )r   r   _ConnectionClosedEvent__reason)r)   r  r   reasonr   s       r"   r   zConnectionClosedEvent.__init__,  s    -0r!   c                    | j                   S )zA reason explaining why this connection was closed.

        The reason must be one of the strings from the
        :class:`ConnectionClosedReason` enum.
        )rX  r   s    r"   rY  zConnectionClosedEvent.reason0       }}r!   c                    dj                  | j                  j                  | j                  | j                  | j
                        S )Nz{}({!r}, {!r}, {!r}))r   r   r   r  r   rX  r   s    r"   r   zConnectionClosedEvent.__repr__9  s9    %,,NN##LLMM	
 	
r!   )r  r   r   rz   rY  r   r   
r   r   r   r   r   r   r   rY  r   r   r   s   @r"   rW   rW     s-     I  
r!   rW   c                      e Zd ZdZdZy)rX   zPublished when the driver starts attempting to check out a connection.

    :param address: The address (host, port) pair of the server this
       Connection is attempting to connect to.

    .. versionadded:: 3.9
    r    Nr  r    r!   r"   rX   rX   B  r  r!   rX   c                  B     e Zd ZdZdZd fdZedd       ZddZ xZ	S )rY   a!  Published when the driver's attempt to check out a connection fails.

    :param address: The address (host, port) pair of the server this
       Connection is attempting to connect to.
    :param reason: A reason explaining why connection check out failed.

    .. versionadded:: 3.9
    rU  c                8    t         |   |d|       || _        y )Nr   )r  r   r   )r   r   &_ConnectionCheckOutFailedEvent__reason)r)   r  rY  r   r   s       r"   r   z&ConnectionCheckOutFailedEvent.__init__Z  s    HMr!   c                    | j                   S )zA reason explaining why connection check out failed.

        The reason must be one of the strings from the
        :class:`ConnectionCheckOutFailedReason` enum.
        )ra  r   s    r"   rY  z$ConnectionCheckOutFailedEvent.reason^  r[  r!   c                    | j                   j                   d| j                  d| j                  d| j                  dS r  )r   r   r  ra  r   r   s    r"   r   z&ConnectionCheckOutFailedEvent.__repr__g  s?    ..))*!DLL+;2dmm=NbQUQ^Q^Paabccr!   )r  r   rY  r   r   rP  r2   r3   r   r]  r   s   @r"   rY   rY   N  s.     I  dr!   rY   c                      e Zd ZdZdZy)rZ   a  Published when the driver successfully checks out a connection.

    :param address: The address (host, port) pair of the server this
       Connection is attempting to connect to.
    :param connection_id: The integer ID of the Connection in this Pool.

    .. versionadded:: 3.9
    r    Nr  r    r!   r"   rZ   rZ   k  rS  r!   rZ   c                      e Zd ZdZdZy)r[   a  Published when the driver checks in a Connection into the Pool.

    :param address: The address (host, port) pair of the server this
       Connection is attempting to connect to.
    :param connection_id: The integer ID of the Connection in this Pool.

    .. versionadded:: 3.9
    r    Nr  r    r!   r"   r[   r[   x  rS  r!   r[   c                  H    e Zd ZdZdZddZed	d       Zed
d       ZddZ	y)_ServerEventzBase class for server events.)__server_address__topology_idc                     || _         || _        y r   )_ServerEvent__server_address_ServerEvent__topology_id)r)   server_addresstopology_ids      r"   r   z_ServerEvent.__init__  s     .(r!   c                    | j                   S )z+The address (host, port) pair of the server)rk  r   s    r"   rm  z_ServerEvent.server_address  s     $$$r!   c                    | j                   S z>A unique identifier for the topology this server is a part of.)rl  r   s    r"   rn  z_ServerEvent.topology_id       !!!r!   c                j    d| j                   j                   d| j                   d| j                   dS )N<  topology_id: >)r   r   rm  rn  r   s    r"   r   z_ServerEvent.__repr__  s8    4>>**+1T-@-@,APTP`P`Oaabccr!   Nrm  r   rn  r   r2   r3   r   r2   r   r   )
r   r   r   r   r   r   r   rm  rn  r   r    r!   r"   rg  rg    s=    '5I) % % " "dr!   rg  c                  d     e Zd ZdZdZ	 	 	 	 	 	 	 	 d fdZedd       Zedd       Zd	dZ	 xZ
S )
rw   zJPublished when server description changes.

    .. versionadded:: 3.3
    __previous_description__new_descriptionc                :    t        |   |  || _        || _        y r   )r   r   4_ServerDescriptionChangedEvent__previous_description/_ServerDescriptionChangedEvent__new_descriptionr)   previous_descriptionnew_descriptionargsr   s       r"   r   z&ServerDescriptionChangedEvent.__init__  "     	$&:#!0r!   c                    | j                   S )zUThe previous
        :class:`~pymongo.server_description.ServerDescription`.
        )r  r   s    r"   r  z2ServerDescriptionChangedEvent.previous_description      
 ***r!   c                    | j                   S )zPThe new
        :class:`~pymongo.server_description.ServerDescription`.
        )r  r   s    r"   r  z-ServerDescriptionChangedEvent.new_description      
 %%%r!   c                    dj                  | j                  j                  | j                  | j                  | j
                        S )Nz <{} {} changed from: {}, to: {}>)r   r   r   rm  r  r  r   s    r"   r   z&ServerDescriptionChangedEvent.__repr__  s=    188NN##%%  	
 	
r!   )r  r   r  r   r  r   r2   r3   )r2   r   r   r   r   r   r   r   r   r   r  r  r   r   r   s   @r"   rw   rw     sh    
 @I1/1 +1 	1
 
1 + + & &
r!   rw   c                      e Zd ZdZdZy)rv   zEPublished when server is initialized.

    .. versionadded:: 3.3
    r    Nr  r    r!   r"   rv   rv         
 Ir!   rv   c                      e Zd ZdZdZy)rx   z@Published when server is closed.

    .. versionadded:: 3.3
    r    Nr  r    r!   r"   rx   rx     r  r!   rx   c                  6    e Zd ZdZdZddZedd       Zd	dZy)
TopologyEventz+Base class for topology description events.)ri  c                    || _         y r   _TopologyEvent__topology_id)r)   rn  s     r"   r   zTopologyEvent.__init__  s
    (r!   c                    | j                   S rq  r  r   s    r"   rn  zTopologyEvent.topology_id  rr  r!   c                P    d| j                   j                   d| j                   dS )Nrt  rv  rw  )r   r   rn  r   s    r"   r   zTopologyEvent.__repr__  s)    4>>**+>$:J:J9K1MMr!   Nrn  r   r2   r3   ry  r   )	r   r   r   r   r   r   r   rn  r   r    r!   r"   r  r    s)    5"I) " "Nr!   r  c                  d     e Zd ZdZdZ	 	 	 	 	 	 	 	 d fdZedd       Zedd       Zd	dZ	 xZ
S )
rm   zPPublished when the topology description changes.

    .. versionadded:: 3.3
    r{  c                :    t        |   |  || _        || _        y r   )r   r   6_TopologyDescriptionChangedEvent__previous_description1_TopologyDescriptionChangedEvent__new_descriptionr  s       r"   r   z(TopologyDescriptionChangedEvent.__init__  r  r!   c                    | j                   S )zYThe previous
        :class:`~pymongo.topology_description.TopologyDescription`.
        )r  r   s    r"   r  z4TopologyDescriptionChangedEvent.previous_description  r  r!   c                    | j                   S )zTThe new
        :class:`~pymongo.topology_description.TopologyDescription`.
        )r  r   s    r"   r  z/TopologyDescriptionChangedEvent.new_description  r  r!   c                    dj                  | j                  j                  | j                  | j                  | j
                        S )Nz-<{} topology_id: {} changed from: {}, to: {}>)r   r   r   rn  r  r  r   s    r"   r   z(TopologyDescriptionChangedEvent.__repr__  s=    >EENN##%%  	
 	
r!   )r  r   r  r   r  r   r2   r3   )r2   r   r   r  r   s   @r"   rm   rm     sh    
 @I111 -1 	1
 
1 + + & &
r!   rm   c                      e Zd ZdZdZy)rl   zKPublished when the topology is initialized.

    .. versionadded:: 3.3
    r    Nr  r    r!   r"   rl   rl     r  r!   rl   c                      e Zd ZdZdZy)rn   zFPublished when the topology is closed.

    .. versionadded:: 3.3
    r    Nr  r    r!   r"   rn   rn     r  r!   rn   c                  J    e Zd ZdZdZdd	dZed
d       Zedd       ZddZ	y)_ServerHeartbeatEventz'Base class for server heartbeat events.)rC  	__awaitedc                     || _         || _        y r   )$_ServerHeartbeatEvent__connection_id_ServerHeartbeatEvent__awaited)r)   r   awaiteds      r"   r   z_ServerHeartbeatEvent.__init__(  s    , r!   c                    | j                   S )zSThe address (host, port) of the server this heartbeat was sent
        to.
        )r  r   s    r"   r   z#_ServerHeartbeatEvent.connection_id,  s    
 ###r!   c                    | j                   S )zgWhether the heartbeat was issued as an awaitable hello command.

        .. versionadded:: 4.6
        )r  r   s    r"   r  z_ServerHeartbeatEvent.awaited3  s     ~~r!   c                j    d| j                   j                   d| j                   d| j                   dS )Nrt  ru  z
 awaited: rw  )r   r   r   r  r   s    r"   r   z_ServerHeartbeatEvent.__repr__;  s4    4>>**+1T-?-?,@
4<<.XYZZr!   NFr   r   r  r'  r2   r3   r   r(  r   )
r   r   r   r   r   r   r   r   r  r   r    r!   r"   r  r  #  s=    10I! $ $  [r!   r  c                      e Zd ZdZdZy)ra   zFPublished when a heartbeat is started.

    .. versionadded:: 3.3
    r    Nr  r    r!   r"   ra   ra   ?  r  r!   ra   c                       e Zd ZdZdZ	 d	 	 	 	 	 	 	 	 	 d	 fdZed
d       Zedd       Zed fd       Z	ddZ
 xZS )rb   zIFired when the server heartbeat succeeds.

    .. versionadded:: 3.3
    rK  r   c                B    t         |   ||       || _        || _        y r   )r   r   (_ServerHeartbeatSucceededEvent__duration%_ServerHeartbeatSucceededEvent__replyr)   r   r   r   r  r   s        r"   r   z&ServerHeartbeatSucceededEvent.__init__P  "     	0"r!   c                    | j                   S z/The duration of this heartbeat in microseconds.)r  r   s    r"   r   z&ServerHeartbeatSucceededEvent.durationW  r   r!   c                    | j                   S )z-An instance of :class:`~pymongo.hello.Hello`.)r  r   s    r"   r   z#ServerHeartbeatSucceededEvent.reply\  r   r!   c                    t         |   S zWhether the heartbeat was awaited.

        If true, then :meth:`duration` reflects the sum of the round trip time
        to the server and the time that the server waited before sending a
        response.

        .. versionadded:: 3.11
        r   r  r   s    r"   r  z%ServerHeartbeatSucceededEvent.awaiteda       wr!   c                    dj                  | j                  j                  | j                  | j                  | j
                  | j                        S )Nz,<{} {} duration: {}, awaited: {}, reply: {}>r   r   r   r   r   r  r   r   s    r"   r   z&ServerHeartbeatSucceededEvent.__repr__m  s@    =DDNN##MMLLJJ
 	
r!   r  )
r   floatr   r   r   r   r  r'  r2   r3   r2   r  )r2   r   r(  r   r   r   r   r   r   r   r   r   r   r  r   r   r   s   @r"   rb   rb   H  s    
 *I W\&+<DOS	     	 	
r!   rb   c                       e Zd ZdZdZ	 d	 	 	 	 	 	 	 	 	 d	 fdZed
d       Zedd       Zed fd       Z	ddZ
 xZS )rc   zxFired when the server heartbeat fails, either with an "ok: 0"
    or a socket exception.

    .. versionadded:: 3.3
    r  c                B    t         |   ||       || _        || _        y r   )r   r   %_ServerHeartbeatFailedEvent__duration"_ServerHeartbeatFailedEvent__replyr  s        r"   r   z#ServerHeartbeatFailedEvent.__init__  r  r!   c                    | j                   S r  )r  r   s    r"   r   z#ServerHeartbeatFailedEvent.duration  r   r!   c                    | j                   S )zA subclass of :exc:`Exception`.)r  r   s    r"   r   z ServerHeartbeatFailedEvent.reply  r   r!   c                    t         |   S r  r  r   s    r"   r  z"ServerHeartbeatFailedEvent.awaited  r  r!   c                    dj                  | j                  j                  | j                  | j                  | j
                  | j                        S )Nz.<{} {} duration: {}, awaited: {}, reply: {!r}>r  r   s    r"   r   z#ServerHeartbeatFailedEvent.__repr__  s@    ?FFNN##MMLLJJ
 	
r!   r  )
r   r  r   	Exceptionr   r   r  r'  r2   r3   r  )r2   r  r(  r   r  r   s   @r"   rc   rc   w  s     *I [`&/@HSW	     	 	
r!   rc   c                  F   e Zd ZdZd!dZed"d       Zed"d       Zed"d       Zed"d       Z	ed"d       Z
d#dZ	 	 d$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d%d
Z	 	 	 	 d&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d'dZ	 	 	 d(	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d)dZd*dZ	 	 	 	 	 	 	 	 	 	 d+dZ	 	 	 	 	 	 	 	 	 	 d,dZd-dZd-dZ	 	 	 	 	 	 	 	 	 	 d.dZd/dZd/dZ	 	 	 	 	 	 	 	 d0dZd1dZd2dZ	 d3	 	 	 	 	 	 	 d4dZd2dZd5dZ	 	 	 	 	 	 	 	 d6dZd7dZd2dZ	 	 	 	 	 	 	 	 d8dZ 	 	 	 	 	 	 	 	 d6dZ!d5d Z"y	)9_EventListenerszConfigure event listeners for a client instance.

    Any event listeners registered globally are included by default.

    :param listeners: A list of event listeners.
    c                   t         j                  d d  | _        t         j                  d d  | _        t         j
                  }|d d  | _        t         j                  d d  | _        t         j                  d d  | _
        ||D ]  }t        |t              r| j                  j                  |       t        |t              r| j                  j                  |       t        |t              r| j                  j                  |       t        |t               r| j                  j                  |       t        |t"              s| j                  j                  |        t%        | j                        | _        t%        | j                        | _        t%        | j                        | _        t%        | j                        | _        t%        | j                        | _        y r   )r   r   "_EventListeners__command_listenersr   !_EventListeners__server_listenersr   +_EventListeners__server_heartbeat_listenersr   #_EventListeners__topology_listenersr   _EventListeners__cmap_listenersr   r$   r   rr   r]   re   r8   r'  %_EventListeners__enabled_for_commands#_EventListeners__enabled_for_server-_EventListeners__enabled_for_server_heartbeat%_EventListeners__enabled_for_topology!_EventListeners__enabled_for_cmap)r)   r   lsts      r"   r   z_EventListeners.__init__  st   #-#?#?#B ","="=a"@33,/F)$.$A$A!$D! * 9 9! <   
6c?3,,33C8c>2++2237c#:;55<<SAc#34--44S9c#9:))005
6 '+4+C+C&D#$()@)@$A!.243T3T.U+&*4+D+D&E#"&t'<'<"=r!   c                    | j                   S )z-Are any CommandListener instances registered?)r  r   s    r"   enabled_for_commandsz$_EventListeners.enabled_for_commands       ***r!   c                    | j                   S )z,Are any ServerListener instances registered?)r  r   s    r"   enabled_for_serverz"_EventListeners.enabled_for_server  s     (((r!   c                    | j                   S )z5Are any ServerHeartbeatListener instances registered?)r  r   s    r"   enabled_for_server_heartbeatz,_EventListeners.enabled_for_server_heartbeat  s     222r!   c                    | j                   S )z.Are any TopologyListener instances registered?)r  r   s    r"   enabled_for_topologyz$_EventListeners.enabled_for_topology  r  r!   c                    | j                   S )z4Are any ConnectionPoolListener instances registered?)r  r   s    r"   enabled_for_cmapz _EventListeners.enabled_for_cmap  s     &&&r!   c                    | j                   | j                  z   | j                  z   | j                  z   | j                  z   S )z#List of registered event listeners.)r  r  r  r  r  r   s    r"   event_listenersz_EventListeners.event_listeners  sN     $$//0%%& ''( ##	$	
r!   Nc           	         ||}t        |||||||      }| j                  D ]  }		 |	j                  |        y# t        $ r t	                Y -w xY w)a  Publish a CommandStartedEvent to all command listeners.

        :param command: The command document.
        :param database_name: The name of the database this command was run
            against.
        :param request_id: The request id for this operation.
        :param connection_id: The address (host, port) of the server this
            command was sent to.
        :param op_id: The (optional) operation id for this operation.
        :param service_id: The service_id this command was sent to, or ``None``.
        N)r   r   )r1   r  r+   r  r   )
r)   r   r   r   r   r   op_idr   r*   
subscribers
             r"   publish_command_startz%_EventListeners.publish_command_start  sn    * =E#!!5
 22 	$J$""5)	$  $!#$s   ;AAc                    ||}|	ri }t        ||||||||
|	      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)a  Publish a CommandSucceededEvent to all command listeners.

        :param duration: The command duration as a datetime.timedelta.
        :param reply: The server reply document.
        :param command_name: The command name.
        :param request_id: The request id for this operation.
        :param connection_id: The address (host, port) of the server this
            command was sent to.
        :param op_id: The (optional) operation id for this operation.
        :param service_id: The service_id this command was sent to, or ``None``.
        :param speculative_hello: Was the command sent with speculative auth?
        :param database_name: The database this command was sent to, or ``""``.
        N)r   r   )r4   r  r.   r  r   )r)   r   r   r   r   r   r   r  r   speculative_hellor   r*   r  s                r"   publish_command_successz'_EventListeners.publish_command_success  s~    4 =E E%'!5

 22 	$J$$$U+	$  $!#$s   AAAc
                    ||}t        ||||||||	|	      }
| j                  D ]  }	 |j                  |
        y# t        $ r t	                Y -w xY w)a  Publish a CommandFailedEvent to all command listeners.

        :param duration: The command duration as a datetime.timedelta.
        :param failure: The server reply document or failure description
            document.
        :param command_name: The command name.
        :param request_id: The request id for this operation.
        :param connection_id: The address (host, port) of the server this
            command was sent to.
        :param op_id: The (optional) operation id for this operation.
        :param service_id: The service_id this command was sent to, or ``None``.
        :param database_name: The database this command was sent to, or ``""``.
        Nr   )r5   r  r0   r  r   )r)   r   r   r   r   r   r   r  r   r   r*   r  s               r"   publish_command_failurez'_EventListeners.publish_command_failureB  st    2 =E"!'!5

 22 	$J$!!%(	$  $!#$s   =AAc                    t        ||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)zPublish a ServerHeartbeatStartedEvent to all server heartbeat
        listeners.

        :param connection_id: The address (host, port) pair of the connection.
        :param awaited: True if this heartbeat is part of an awaitable hello command.
        N)ra   r  r+   r  r   )r)   r   r  r*   r  s        r"    publish_server_heartbeat_startedz0_EventListeners.publish_server_heartbeat_startedn  sO     ,M7C;; 	$J$""5)	$  $!#$   1AAc                    t        ||||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)a  Publish a ServerHeartbeatSucceededEvent to all server heartbeat
        listeners.

        :param connection_id: The address (host, port) pair of the connection.
        :param duration: The execution time of the event in the highest possible
            resolution for the platform.
        :param reply: The command reply.
        :param awaited: True if the response was awaited.
        N)rb   r  r.   r  r   r)   r   r   r   r  r*   r  s          r"   "publish_server_heartbeat_succeededz2_EventListeners.publish_server_heartbeat_succeeded|  sS     .h}gV;; 	$J$$$U+	$  $!#$   3A	A	c                    t        ||||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)a  Publish a ServerHeartbeatFailedEvent to all server heartbeat
        listeners.

        :param connection_id: The address (host, port) pair of the connection.
        :param duration: The execution time of the event in the highest possible
            resolution for the platform.
        :param reply: The command reply.
        :param awaited: True if the response was awaited.
        N)rc   r  r0   r  r   r  s          r"   publish_server_heartbeat_failedz/_EventListeners.publish_server_heartbeat_failed  sS     +8UM7S;; 	$J$!!%(	$  $!#$r  c                    t        ||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)zPublish a ServerOpeningEvent to all server listeners.

        :param server_address: The address (host, port) pair of the server.
        :param topology_id: A unique identifier for the topology this server
           is a part of.
        N)rv   r  rg   r  r   r)   rm  rn  r*   r  s        r"   publish_server_openedz%_EventListeners.publish_server_opened  sO     #>;?11 	$J$!!%(	$  $!#$r  c                    t        ||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)zPublish a ServerClosedEvent to all server listeners.

        :param server_address: The address (host, port) pair of the server.
        :param topology_id: A unique identifier for the topology this server
           is a part of.
        N)rx   r  rk   r  r   r  s        r"   publish_server_closedz%_EventListeners.publish_server_closed  sO     ".+>11 	$J$!!%(	$  $!#$r  c                    t        ||||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)a~  Publish a ServerDescriptionChangedEvent to all server listeners.

        :param previous_description: The previous server description.
        :param server_address: The address (host, port) pair of the server.
        :param new_description: The new server description.
        :param topology_id: A unique identifier for the topology this server
           is a part of.
        N)rw   r  ri   r  r   )r)   r  r  rm  rn  r*   r  s          r"   "publish_server_description_changedz2_EventListeners.publish_server_description_changed  sX     . />;
 11 	$J$..u5	$  $!#$r  c                    t        |      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)zPublish a TopologyOpenedEvent to all topology listeners.

        :param topology_id: A unique identifier for the topology this server
           is a part of.
        N)rl   r  rg   r  r   r)   rn  r*   r  s       r"   publish_topology_openedz'_EventListeners.publish_topology_opened  M     $K033 	$J$!!%(	$  $!#$   0AAc                    t        |      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)zPublish a TopologyClosedEvent to all topology listeners.

        :param topology_id: A unique identifier for the topology this server
           is a part of.
        N)rn   r  rk   r  r   r  s       r"   publish_topology_closedz'_EventListeners.publish_topology_closed  r   r  c                    t        |||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)a:  Publish a TopologyDescriptionChangedEvent to all topology listeners.

        :param previous_description: The previous topology description.
        :param new_description: The new topology description.
        :param topology_id: A unique identifier for the topology this server
           is a part of.
        N)rm   r  ri   r  r   )r)   r  r  rn  r*   r  s         r"   $publish_topology_description_changedz4_EventListeners.publish_topology_description_changed  sS     00DoWbc33 	$J$..u5	$  $!#$   2AAc                    t        ||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)z:Publish a :class:`PoolCreatedEvent` to all pool listeners.N)rQ   r  r:   r  r   )r)   r  r  r*   r  s        r"   publish_pool_createdz$_EventListeners.publish_pool_created  sM     '2// 	$J$''.	$  $!#$r  c                    t        |      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)z8Publish a :class:`PoolReadyEvent` to all pool listeners.N)rR   r  r=   r  r   r)   r  r*   r  s       r"   publish_pool_readyz"_EventListeners.publish_pool_ready  sK    w'// 	$J$%%e,	$  $!#$r  c                    t        |||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)z:Publish a :class:`PoolClearedEvent` to all pool listeners.N)rS   r  r?   r  r   )r)   r  r   r"  r*   r  s         r"   publish_pool_clearedz$_EventListeners.publish_pool_cleared  sR     !*6KL// 	$J$''.	$  $!#$r  c                    t        |      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)z9Publish a :class:`PoolClosedEvent` to all pool listeners.N)rT   r  rA   r  r   r
  s       r"   publish_pool_closedz#_EventListeners.publish_pool_closed$  sK    (// 	$J$&&u-	$  $!#$r  c                    t        ||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)zWPublish a :class:`ConnectionCreatedEvent` to all connection
        listeners.
        N)rU   r  rC   r  r   r)   r  r   r*   r  s        r"   publish_connection_createdz*_EventListeners.publish_connection_created-  sO     'w>// 	$J$--e4	$  $!#$r  c                    t        |||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)zDPublish a :class:`ConnectionReadyEvent` to all connection listeners.N)rV   r  rE   r  r   r)   r  r   r   r*   r  s         r"   publish_connection_readyz(_EventListeners.publish_connection_ready8  sQ     %WmXF// 	$J$++E2	$  $!#$r  c                    t        |||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)zVPublish a :class:`ConnectionClosedEvent` to all connection
        listeners.
        N)rW   r  rH   r  r   )r)   r  r   rY  r*   r  s         r"   publish_connection_closedz)_EventListeners.publish_connection_closedC  sQ     &g}fE// 	$J$,,U3	$  $!#$r  c                    t        |      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)z_Publish a :class:`ConnectionCheckOutStartedEvent` to all connection
        listeners.
        N)rX   r  rJ   r  r   r
  s       r"   $publish_connection_check_out_startedz4_EventListeners.publish_connection_check_out_startedN  sM     /w7// 	$J$77>	$  $!#$r  c                    t        |||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)z^Publish a :class:`ConnectionCheckOutFailedEvent` to all connection
        listeners.
        N)rY   r  rL   r  r   )r)   r  rY  r   r*   r  s         r"   #publish_connection_check_out_failedz3_EventListeners.publish_connection_check_out_failedY  sQ     .gvxH// 	$J$66u=	$  $!#$r  c                    t        |||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)zZPublish a :class:`ConnectionCheckedOutEvent` to all connection
        listeners.
        N)rZ   r  rN   r  r   r  s         r"   publish_connection_checked_outz._EventListeners.publish_connection_checked_outf  sQ     *'=(K// 	$J$11%8	$  $!#$r  c                    t        ||      }| j                  D ]  }	 |j                  |        y# t        $ r t	                Y -w xY w)zYPublish a :class:`ConnectionCheckedInEvent` to all connection
        listeners.
        N)r[   r  rP   r  r   r  s        r"   publish_connection_checked_inz-_EventListeners.publish_connection_checked_ins  sO     )-@// 	$J$007	$  $!#$r  )r   z"Optional[Sequence[_EventListener]]r(  )r2   zlist[_EventListeners]r   )r   r   r   r   r   rz   r   r   r   r   r  r   r   r   r2   r3   )NNFr   )r   r   r   r   r   r   r   rz   r   r   r   r   r  r   r   r   r  r'  r   r   r2   r3   )NNr   )r   r   r   r   r   r   r   rz   r   r   r   r   r  r   r   r   r   r   r2   r3   r  )
r   r   r   r  r   r   r  r'  r2   r3   )
r   r   r   r  r   r  r  r'  r2   r3   rx  )
r  r   r  r   rm  r   rn  r   r2   r3   r  )r  r   r  r   rn  r   r2   r3   r  r  r  r&  rH  )r  r   r   rz   r   r  r2   r3   )r  r   r   rz   rY  r   r2   r3   )r  r   rY  r   r   r  r2   r3   )#r   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r    r!   r"   r  r    sY   >2 + + ) ) 3 3 + + ' '
"  $)-$$$$ $$ 	$$
  $$ ,$$ $$ '$$ 
$$\  $)-"'/$/$ /$ 	/$
 /$  /$ ,/$ /$ '/$  /$ /$ 
/$r  $)-*$*$ *$ 	*$
 *$  *$ ,*$ *$ '*$ *$ 
*$X$$%$16$?D$OS$	$&$%$16$?H$SW$	$&$$$/$ +$ !	$
 $ 
$0$$$1$ -$ 	$
 
$($$ ',	$$ '$  $	$
 
$$	$	$	$03	$?D	$		$	$	$$$),$8=$	$$$03$?D$	$	$r!   r  )r|   r   r2   rz   )r   r   r   Sequence[_EventListeners]r2   r   )r   r   r2   r3   )r   r   r   zMapping[str, Any]r2   r'  )Lr   
__future__r   datetimecollectionsr   r   typingr   r   r   r	   r
   bson.objectidr   pymongo.hellor   r   pymongo.helpersr   pymongo.typingsr   r   r   pymongo.server_descriptionr   pymongo.topology_descriptionr   r   r   r   r$   r8   r]   re   rr   r}   r   r   r   __annotations__r   r   r1   r4   r5   r   rQ   rR   rS   rT   r+  r5  r;  rB  rJ  rU   rV   rW   rX   rY   rZ   r[   rg  rw   rv   rx   r  rm   rl   rn   r  ra   rb   rc   r  r    r!   r"   <module>r,     s  iV #  ' B B " , - 2"<@ 	
 BB+
7 7"n ":u"^ u"p"n ">"~ "<"^ "<+
5"3:
 S 
I% I%X@
- @
FB
M B
J@
 @
F@ @&Rz R0	Z 	*wz *wZ	j 	F F* &@ @&X) X$k1 k*/  
3 
 
.  
F	%5 	d$< d:
 8 

1 
d d.&
L &
R  N N"&
m &
R- - [ [8"7 ,
$9 ,
^-
!6 -
`U$ U$r!   