
    i)i                    P   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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$dqdZ%drd$Z&dsd'Z'dtd,Z( G d- d.          Z) G d/ d0e)          Z* G d1 d2e)          Z+ G d3 d4e)          Z, G d5 d6          Z- G d7 d8e-          Z. G d9 d:e-          Z/ G d; d<e-          Z0 G d= d>e-          Z1 G d? d@          Z2 G dA dB          Z3 G dC dD          Z4 G dE dFe4          Z5 G dG dHe5          Z6 G dI dJe5          Z7 G dK dLe6          Z8 G dM dNe5          Z9 G dO dPe4          Z: G dQ dRe6          Z; G dS dTe6          Z< G dU dVe5          Z= G dW dX          Z> G dY dZe>          Z? G d[ d\e>          Z@ G d] d^e>          ZA G d_ d`          ZB G da dbeB          ZC G dc ddeB          ZD G de dfeB          ZE G dg dh          ZF G di djeF          ZG G dk dleF          ZH G dm dneF          ZI G do dp          ZJdS )ua  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.

.. seealso:: This module is compatible with both the synchronous and asynchronous PyMongo APIs.


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)_SENSITIVE_COMMANDS_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dS )_EventListenerz,Abstract base class for all event listeners.N)__name__
__module____qualname____doc__     d/Users/user/workspace/sujinbaek/cqa-test-app/venv/lib/python3.11/site-packages/pymongo/monitoring.pyr   r      s        6666r"   r   c                  *    e Zd ZdZddZddZdd
ZdS )CommandListenerzAbstract base class for command listeners.

    Handles `CommandStartedEvent`, `CommandSucceededEvent`,
    and `CommandFailedEvent`.
    eventCommandStartedEventreturnNonec                    t           )zAbstract method to handle a `CommandStartedEvent`.

        :param event: An instance of :class:`CommandStartedEvent`.
        NotImplementedErrorselfr&   s     r#   startedzCommandListener.started   
    
 "!r"   CommandSucceededEventc                    t           )zAbstract method to handle a `CommandSucceededEvent`.

        :param event: An instance of :class:`CommandSucceededEvent`.
        r+   r-   s     r#   	succeededzCommandListener.succeeded   r0   r"   CommandFailedEventc                    t           )z}Abstract method to handle a `CommandFailedEvent`.

        :param event: An instance of :class:`CommandFailedEvent`.
        r+   r-   s     r#   failedzCommandListener.failed   r0   r"   N)r&   r'   r(   r)   )r&   r1   r(   r)   )r&   r4   r(   r)   r   r   r   r    r/   r3   r6   r!   r"   r#   r%   r%      sZ         " " " "" " " "" " " " " "r"   r%   c                  j    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dS )'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
    r&   PoolCreatedEventr(   r)   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"   PoolReadyEventc                    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"   PoolClearedEventc                    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"   PoolClosedEventc                    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"   ConnectionCreatedEventc                    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_created4  r=   r"   ConnectionReadyEventc                    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"   ConnectionClosedEventc                    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_closedG  r=   r"   ConnectionCheckOutStartedEventc                    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_startedP  r=   r"   ConnectionCheckOutFailedEventc                    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_failedY  r=   r"   ConnectionCheckedOutEventc                    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_outb  r=   r"   ConnectionCheckedInEventc                    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_ink  rM   r"   N)r&   r:   r(   r)   )r&   r>   r(   r)   )r&   rA   r(   r)   )r&   rD   r(   r)   )r&   rG   r(   r)   )r&   rJ   r(   r)   )r&   rN   r(   r)   )r&   rQ   r(   r)   )r&   rT   r(   r)   )r&   rW   r(   r)   )r&   rZ   r(   r)   )r   r   r   r    r<   r@   rC   rF   rI   rL   rP   rS   rV   rY   r\   r!   r"   r#   r9   r9      s         " " " "	" 	" 	" 	"" " " "" " " "" " " "" " " "" " " "" " " "" " " "" " " "" " " " " "r"   r9   c                  *    e Zd ZdZddZddZdd
ZdS )ServerHeartbeatListenerzAbstract base class for server heartbeat listeners.

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

    .. versionadded:: 3.3
    r&   ServerHeartbeatStartedEventr(   r)   c                    t           )zAbstract method to handle a `ServerHeartbeatStartedEvent`.

        :param event: An instance of :class:`ServerHeartbeatStartedEvent`.
        r+   r-   s     r#   r/   zServerHeartbeatListener.started  r0   r"   ServerHeartbeatSucceededEventc                    t           )zAbstract method to handle a `ServerHeartbeatSucceededEvent`.

        :param event: An instance of :class:`ServerHeartbeatSucceededEvent`.
        r+   r-   s     r#   r3   z!ServerHeartbeatListener.succeeded  r0   r"   ServerHeartbeatFailedEventc                    t           )zAbstract method to handle a `ServerHeartbeatFailedEvent`.

        :param event: An instance of :class:`ServerHeartbeatFailedEvent`.
        r+   r-   s     r#   r6   zServerHeartbeatListener.failed  r0   r"   N)r&   r_   r(   r)   )r&   ra   r(   r)   )r&   rc   r(   r)   r7   r!   r"   r#   r^   r^   v  sZ         " " " "" " " "" " " " " "r"   r^   c                  *    e Zd ZdZddZddZdd
ZdS )TopologyListenerzAbstract base class for topology monitoring listeners.
    Handles `TopologyOpenedEvent`, `TopologyDescriptionChangedEvent`, and
    `TopologyClosedEvent`.

    .. versionadded:: 3.3
    r&   TopologyOpenedEventr(   r)   c                    t           )zAbstract method to handle a `TopologyOpenedEvent`.

        :param event: An instance of :class:`TopologyOpenedEvent`.
        r+   r-   s     r#   openedzTopologyListener.opened  r0   r"   TopologyDescriptionChangedEventc                    t           )zAbstract method to handle a `TopologyDescriptionChangedEvent`.

        :param event: An instance of :class:`TopologyDescriptionChangedEvent`.
        r+   r-   s     r#   description_changedz$TopologyListener.description_changed  r0   r"   TopologyClosedEventc                    t           )zAbstract method to handle a `TopologyClosedEvent`.

        :param event: An instance of :class:`TopologyClosedEvent`.
        r+   r-   s     r#   closedzTopologyListener.closed  r0   r"   N)r&   rg   r(   r)   )r&   rj   r(   r)   )r&   rm   r(   r)   r   r   r   r    ri   rl   ro   r!   r"   r#   rf   rf     Z         " " " "" " " "" " " " " "r"   rf   c                  *    e Zd ZdZddZddZdd
ZdS )ServerListenerzAbstract base class for server listeners.
    Handles `ServerOpeningEvent`, `ServerDescriptionChangedEvent`, and
    `ServerClosedEvent`.

    .. versionadded:: 3.3
    r&   ServerOpeningEventr(   r)   c                    t           )z}Abstract method to handle a `ServerOpeningEvent`.

        :param event: An instance of :class:`ServerOpeningEvent`.
        r+   r-   s     r#   ri   zServerListener.opened  r0   r"   ServerDescriptionChangedEventc                    t           )zAbstract method to handle a `ServerDescriptionChangedEvent`.

        :param event: An instance of :class:`ServerDescriptionChangedEvent`.
        r+   r-   s     r#   rl   z"ServerListener.description_changed  r0   r"   ServerClosedEventc                    t           )z{Abstract method to handle a `ServerClosedEvent`.

        :param event: An instance of :class:`ServerClosedEvent`.
        r+   r-   s     r#   ro   zServerListener.closed  r0   r"   N)r&   rt   r(   r)   )r&   rv   r(   r)   )r&   rx   r(   r)   rp   r!   r"   r#   rs   rs     rq   r"   rs   durr   r(   intc                J    t          |                                 dz            S )z'Convert duration 'dur' to microseconds.g    .A)r{   total_seconds)rz   s    r#   
_to_microsr~     s!    s  ""T)***r"   optionstr	listenersSequence[_EventListeners]c           	         t          |t          j                  s!t          |  dt	          |                     |D ]9}t          |t
                    s"t          d|  dt	          |                     :|S )zValidate event listenersz must be a list or tuple, not Listeners for | must be either a CommandListener, ServerHeartbeatListener, ServerListener, TopologyListener, or ConnectionPoolListener,not )
isinstancer   r
   	TypeErrortyper   )r   r   listeners      r#   _validate_event_listenersr     s     i.. T6RRiRRSSS  (N33 	( ( ( H~~	( (  	 r"   r   r)   c                   t          | t                    s"t          d|  dt          |                      t          | t                    rt
          j                            |            t          | t                    rt
          j	                            |            t          | t                    rt
          j                            |            t          | t                    rt
          j                            |            t          | t                    r!t
          j                            |            dS dS )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   r%   
_LISTENERSr   appendr^   r   rs   r   rf   r   r9   r   )r   s    r#   registerr     s0    h// 
$X $ $ >>	$ $
 
 	
 (O,, 6$++H555(344 ?-44X>>>(N++ 5#**8444(,-- 7%,,X666(233 3!((222223 3r"   command_namedocMapping[str, Any]boolc                V    |                                  dt          j        fv rd|v rdS dS )NhellospeculativeAuthenticateTF)lowerr   
LEGACY_CMD)r   r   s     r#   _is_speculative_authenticater     s6    +*@ AAA%,,t5r"   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dS )"_CommandEventzBase class for command events.)
__cmd_name	__rqst_id	__conn_id__op_id__service_id__db__server_conn_idN r   r   
request_idr{   connection_idr   operation_idOptional[int]
service_idOptional[ObjectId]database_nameserver_connection_idr(   r)   c                h    || _         || _        || _        || _        || _        || _        || _        d S N)_CommandEvent__cmd_name_CommandEvent__rqst_id_CommandEvent__conn_id_CommandEvent__op_id_CommandEvent__service_id_CommandEvent__db_CommandEvent__server_conn_id)r.   r   r   r   r   r   r   r   s           r#   __init__z_CommandEvent.__init__  s=     '#&#&!	 4r"   c                    | j         S )zThe command name.)r   r.   s    r#   r   z_CommandEvent.command_name-       r"   c                    | j         S )z"The request id for this operation.)r   r   s    r#   r   z_CommandEvent.request_id2       ~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_id7  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_id<  s       r"   c                    | j         S )z(An id for this series of events or None.)r   r   s    r#   r   z_CommandEvent.operation_idD       |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_nameI  s     y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_idQ  s     $$r"   Nr   N)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    	__slots__r   propertyr   r   r   r   r   r   r   r!   r"   r#   r   r     s%       ((I  *..25 5 5 5 5$    X    X    X ! ! ! X!    X    X % % % X% % %r"   r   c                  j     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 )r'   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``.
    )__cmdNcommandr   r   r   r   r{   r   r   r   r   r   r   r   r(   r)   c           	     2   |st          |d          t          t          |                    }t                                          |||||||           |                                }	|	t          v st          |	|          r	i | _        d S || _        d S )Nz is not a valid commandr   r   r   )	
ValueErrornextitersuperr   r   r   r   _CommandStartedEvent__cmd)r.   r   r   r   r   r   r   r   r   cmd_name	__class__s             r#   r   zCommandStartedEvent.__init__h  s      	DBBBCCCDMM**!'!5 	 	
 	
 	
  %%''***.J8U\.].]*')DJJJ DJJJr"   c                    | j         S )zThe command document.)r   r   s    r#   r   zCommandStartedEvent.command  s     zr"   c                *    t                      j        S )z6The name of the database this command was run against.)r   r   r.   r   s    r#   r   z!CommandStartedEvent.database_name  s     ww$$r"   c           	         d                     | 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__  sD    i
&N#O%
 

	
r"   NN)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   __classcell__r   s   @r#   r'   r'   Z  s        	 	 I *..2! ! ! ! ! ! !:    X % % % % % X%
 
 
 
 
 
 
 
r"   r'   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 ) r1   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__replyNr   durationdatetime.timedeltareplyr   r   r   r   r{   r   r   r   r   r   r   r   r   r(   r)   c
           	         t                                          |||||||	           t          |          | _        |                                }
|
t
          v st          |
|          r	i | _        d S || _        d S Nr   )r   r   r~   '_CommandSucceededEvent__duration_microsr   r   r   _CommandSucceededEvent__reply)r.   r   r   r   r   r   r   r   r   r   r   r   s              r#   r   zCommandSucceededEvent.__init__  s     	!'!5 	 	
 	
 	
 ",H!5!5%%''***.J8UZ.[.[*)+DLLL DLLLr"   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	                  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__  sJ    ~
&N# O%	
 	
	
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   s   @r#   r1   r1     s          1I *..2! ! ! ! ! ! !8 & & & X&    X
 
 
 
 
 
 
 
r"   r1   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 ) r4   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   	__failureNr   r   r   failurer   r   r   r   r{   r   r   r   r   r   r   r   r   r(   r)   c
           	         t                                          |||||||	           t          |          | _        || _        d S r   )r   r   r~   $_CommandFailedEvent__duration_micros_CommandFailedEvent__failure)r.   r   r   r   r   r   r   r   r   r   r   s             r#   r   zCommandFailedEvent.__init__  sX     	!'!5 	 	
 	
 	
 ",H!5!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
        	  	        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__  sR    G
&N# LO%

 

	
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   s   @r#   r4   r4     s          3I *..2! ! ! ! ! ! !0 & & & X&    X
 
 
 
 
 
 
 
r"   r4   c                  >    e Zd ZdZdZddZedd            Zdd
ZdS )
_PoolEventzBase class for pool events.	__addressaddressr   r(   r)   c                    || _         d S r   _PoolEvent__addressr.   r   s     r#   r   z_PoolEvent.__init__*       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.address-      
 ~r"   r   c                0    | j         j         d| j        dS N())r   r   r   r   s    r#   r   z_PoolEvent.__repr__4       .)??DN????r"   Nr   r   r(   r)   r   r   	r   r   r   r    r   r   r   r   r   r!   r"   r#   r   r   %  sm        %%I! ! ! !    X@ @ @ @ @ @r"   r   c                  H     e Zd ZdZdZd fd	Zedd
            ZddZ xZ	S )r:   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
    )	__optionsr   r   optionsdict[str, Any]r(   r)   c                X    t                                          |           || _        d S r   )r   r   _PoolCreatedEvent__options)r.   r   r  r   s      r#   r   zPoolCreatedEvent.__init__C  s&    !!! r"   c                    | j         S )zCAny non-default pool options that were set on this Connection Pool.)r  r   s    r#   r  zPoolCreatedEvent.optionsG  r   r"   r   c                @    | j         j         d| j        d| j        dS Nr  z, r  )r   r   r   r  r   s    r#   r   zPoolCreatedEvent.__repr__L  s*    .)QQDLQQdnQQQQr"   r   r   r  r  r(   r)   )r(   r  r   )
r   r   r   r    r   r   r   r  r   r   r   s   @r#   r:   r:   8  s          I! ! ! ! ! !    XR R R R R R R Rr"   r:   c                      e Zd ZdZdZdS )r>   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#   r>   r>   P            IIIr"   r>   c                  f     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 )rA   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_connectionsNFr   r   r   r   interrupt_connectionsr   r(   r)   c                f    t                                          |           || _        || _        d S r   )r   r   _PoolClearedEvent__service_id(_PoolClearedEvent__interrupt_connections)r.   r   r   r  r   s       r#   r   zPoolClearedEvent.__init__i  s3     	!!!&'<$$$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_ids  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"   r   c                P    | j         j         d| j        d| j        d| j        dS r  )r   r   r   r  r  r   s    r#   r   zPoolClearedEvent.__repr__  s7    .)vvDLvvd>OvvUYUqvvvvr"   )NFr   r   r   r   r  r   r(   r)   r   r(   r   r   )r   r   r   r    r   r   r   r   r  r   r   r   s   @r#   rA   rA   \  s          <I
 *.&+	= = = = = = = ! ! ! X! , , , X,w w w w w w w wr"   rA   c                      e Zd ZdZdZdS )rD   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#   rD   rD     r  r"   rD   c                  (    e Zd ZdZdZ	 dZ	 dZ	 dZdS )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EEr"   r$  c                  "    e Zd ZdZdZ	 dZ	 dZdS )ConnectionCheckOutFailedReasonzyAn enum that defines values for `reason` on a
    :class:`ConnectionCheckOutFailedEvent`.

    .. versionadded:: 3.9
    timeoutr(  connectionErrorN)r   r   r   r    TIMEOUTr,  
CONN_ERRORr!   r"   r#   r.  r.    s5          GJKM"J r"   r.  c                  >    e Zd ZdZdZddZedd            Zdd
ZdS )_ConnectionEventz)Private base class for connection events.r   r   r   r(   r)   c                    || _         d S 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.
        r6  r   s    r#   r   z_ConnectionEvent.address  r  r"   r   c                0    | j         j         d| j        dS r  )r   r   r7  r   s    r#   r   z_ConnectionEvent.__repr__  r  r"   Nr  r   r   r	  r!   r"   r#   r4  r4    sm        33I! ! ! !    X@ @ @ @ @ @r"   r4  c                  H     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_idr   r   r   r{   r(   r)   c                X    t                                          |           || _        d S 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.)r>  r   s    r#   r   z _ConnectionIdEvent.connection_id  s     ##r"   r   c                @    | j         j         d| j        d| j        dS r  )r   r   r   r>  r   s    r#   r   z_ConnectionIdEvent.__repr__  s+    .)WWDLWWd>RWWWWr"   r   r   r   r{   r(   r)   r   r   )
r   r   r   r    r   r   r   r   r   r   r   s   @r#   r;  r;    s        >>$I- - - - - - $ $ $ X$X X X X X X X Xr"   r;  c                  H     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.)
__durationr   r   r   r{   r   Optional[float]r(   r)   c                Z    t                                          ||           || _        d S r   )r   r   "_ConnectionDurationEvent__duration)r.   r   r   r   r   s       r#   r   z!_ConnectionDurationEvent.__init__  s(    -000"r"   c                    | j         S )zMThe duration of the connection event.

        .. versionadded:: 4.7
        )rG  r   s    r#   r   z!_ConnectionDurationEvent.duration  s     r"   r   c                P    | j         j         d| j        d| j        d| j        dS r  )r   r   r   r   rG  r   s    r#   r   z!_ConnectionDurationEvent.__repr__  s7    .)jjDLjjd>PjjVZVejjjjr"   )r   r   r   r{   r   rE  r(   r)   )r(   rE  r   )
r   r   r   r    r   r   r   r   r   r   r   s   @r#   rC  rC    s        CCI# # # # # #    Xk k k k k k k kr"   rC  c                      e Zd ZdZdZdS )rG   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#   rG   rG     s        
 
 IIIr"   rG   c                      e Zd ZdZdZdS )rJ   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#   rJ   rJ               IIIr"   rJ   c                  H     e Zd ZdZdZd fd	Zedd            ZddZ xZ	S )rN   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
    __reasonr   r   r   r{   reasonr   c                Z    t                                          ||           || _        d S r   )r   r   _ConnectionClosedEvent__reason)r.   r   r   rP  r   s       r#   r   zConnectionClosedEvent.__init__!  s(    -000r"   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.
        )rR  r   s    r#   rP  zConnectionClosedEvent.reason%       }r"   c                d    d                     | j        j        | j        | j        | j                  S )Nz{}({!r}, {!r}, {!r}))r   r   r   r   r   rR  r   s    r#   r   zConnectionClosedEvent.__repr__.  s2    %,,N#LM	
 
 	
r"   )r   r   r   r{   rP  r   r   
r   r   r   r    r   r   r   rP  r   r   r   s   @r#   rN   rN     s          I         X
 
 
 
 
 
 
 
r"   rN   c                      e Zd ZdZdZdS )rQ   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#   rQ   rQ   7  r  r"   rQ   c                  H     e Zd ZdZdZd fdZedd            ZddZ xZ	S )rT   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
    rN  r   r   rP  r   r   rE  r(   r)   c                ^    t                                          |d|           || _        d S )Nr   )r   r   r   )r   r   &_ConnectionCheckOutFailedEvent__reason)r.   r   rP  r   r   s       r#   r   z&ConnectionCheckOutFailedEvent.__init__O  s-    HMM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.
        )rZ  r   s    r#   rP  z$ConnectionCheckOutFailedEvent.reasonS  rT  r"   c                P    | j         j         d| j        d| j        d| j        dS r  )r   r   r   rZ  r   r   s    r#   r   z&ConnectionCheckOutFailedEvent.__repr__\  s6    .)ccDLccdmccQUQ^ccccr"   )r   r   rP  r   r   rE  r(   r)   r   rV  r   s   @r#   rT   rT   C  s          I         Xd d d d d d d dr"   rT   c                      e Zd ZdZdZdS )rW   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#   rW   rW   `  rL  r"   rW   c                      e Zd ZdZdZdS )rZ   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#   rZ   rZ   m  rL  r"   rZ   c                  V    e Zd ZdZdZdd	Zedd
            Zedd            ZddZ	dS )_ServerEventzBase class for server events.)__server_address__topology_idserver_addressr   topology_idr   r(   r)   c                "    || _         || _        d S r   )_ServerEvent__server_address_ServerEvent__topology_id)r.   rc  rd  s      r#   r   z_ServerEvent.__init__  s     .(r"   c                    | j         S )z+The address (host, port) pair of the server)rf  r   s    r#   rc  z_ServerEvent.server_address  s     $$r"   c                    | j         S z>A unique identifier for the topology this server is a part of.)rg  r   s    r#   rd  z_ServerEvent.topology_id       !!r"   r   c                B    d| j         j         d| j         d| j         dS )N<  topology_id: >)r   r   rc  rd  r   s    r#   r   z_ServerEvent.__repr__  s/    c4>*ccT-@ccPTP`ccccr"   Nrc  r   rd  r   r(   r)   r   r(   r   r   )
r   r   r   r    r   r   r   rc  rd  r   r!   r"   r#   r`  r`  z  s        ''5I) ) ) ) % % % X% " " " X"d d d d d dr"   r`  c                  `     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 )rv   zJPublished when server description changes.

    .. versionadded:: 3.3
    __previous_description__new_descriptionprevious_descriptionr   new_descriptionargsr   r(   r)   c                P     t                      j        |  || _        || _        d S r   )r   r   4_ServerDescriptionChangedEvent__previous_description/_ServerDescriptionChangedEvent__new_descriptionr.   rw  rx  ry  r   s       r#   r   z&ServerDescriptionChangedEvent.__init__  /     	$&:#!0r"   c                    | j         S )zUThe previous
        :class:`~pymongo.server_description.ServerDescription`.
        )r{  r   s    r#   rw  z2ServerDescriptionChangedEvent.previous_description      
 **r"   c                    | j         S )zPThe new
        :class:`~pymongo.server_description.ServerDescription`.
        )r|  r   s    r#   rx  z-ServerDescriptionChangedEvent.new_description      
 %%r"   r   c                d    d                     | j        j        | j        | j        | j                  S )Nz <{} {} changed from: {}, to: {}>)r   r   r   rc  rw  rx  r   s    r#   r   z&ServerDescriptionChangedEvent.__repr__  s4    188N#% 	
 
 	
r"   )rw  r   rx  r   ry  r   r(   r)   )r(   r   r   r   r   r   r    r   r   r   rw  rx  r   r   r   s   @r#   rv   rv              
 @I1 1 1 1 1 1 + + + X+ & & & X&
 
 
 
 
 
 
 
r"   rv   c                      e Zd ZdZdZdS )rt   zEPublished when server is initialized.

    .. versionadded:: 3.3
    r!   Nr  r!   r"   r#   rt   rt              
 IIIr"   rt   c                      e Zd ZdZdZdS )rx   z@Published when server is closed.

    .. versionadded:: 3.3
    r!   Nr  r!   r"   r#   rx   rx     r  r"   rx   c                  >    e Zd ZdZdZddZedd            Zdd
ZdS )TopologyEventz+Base class for topology description events.)rb  rd  r   r(   r)   c                    || _         d S r   _TopologyEvent__topology_id)r.   rd  s     r#   r   zTopologyEvent.__init__  s    (r"   c                    | j         S rj  r  r   s    r#   rd  zTopologyEvent.topology_id  rk  r"   r   c                2    d| j         j         d| j         dS )Nrm  ro  rp  )r   r   rd  r   s    r#   r   zTopologyEvent.__repr__  s#    M4>*MM$:JMMMMr"   Nrd  r   r(   r)   rr  r   )	r   r   r   r    r   r   r   rd  r   r!   r"   r#   r  r    sm        55"I) ) ) ) " " " X"N N N N N Nr"   r  c                  `     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 )rj   zPPublished when the topology description changes.

    .. versionadded:: 3.3
    rt  rw  r   rx  ry  r   r(   r)   c                P     t                      j        |  || _        || _        d S 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#   rw  z4TopologyDescriptionChangedEvent.previous_description  r  r"   c                    | j         S )zTThe new
        :class:`~pymongo.topology_description.TopologyDescription`.
        )r  r   s    r#   rx  z/TopologyDescriptionChangedEvent.new_description  r  r"   r   c                d    d                     | j        j        | j        | j        | j                  S )Nz-<{} topology_id: {} changed from: {}, to: {}>)r   r   r   rd  rw  rx  r   s    r#   r   z(TopologyDescriptionChangedEvent.__repr__  s4    >EEN#% 	
 
 	
r"   )rw  r   rx  r   ry  r   r(   r)   )r(   r   r   r  r   s   @r#   rj   rj     r  r"   rj   c                      e Zd ZdZdZdS )rg   zKPublished when the topology is initialized.

    .. versionadded:: 3.3
    r!   Nr  r!   r"   r#   rg   rg     r  r"   rg   c                      e Zd ZdZdZdS )rm   zFPublished when the topology is closed.

    .. versionadded:: 3.3
    r!   Nr  r!   r"   r#   rm   rm     r  r"   rm   c                  X    e Zd ZdZdZddd
Zedd            Zedd            ZddZ	dS )_ServerHeartbeatEventz'Base class for server heartbeat events.)r<  	__awaitedFr   r   awaitedr   r(   r)   c                "    || _         || _        d S r   )$_ServerHeartbeatEvent__connection_id_ServerHeartbeatEvent__awaited)r.   r   r  s      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.awaited(  s     ~r"   r   c                B    d| j         j         d| j         d| j         dS )Nrm  rn  z
 awaited: rp  )r   r   r   r  r   s    r#   r   z_ServerHeartbeatEvent.__repr__0  s-    Z4>*ZZT-?ZZ4<ZZZZr"   NFr   r   r  r   r(   r)   r   r!  r   )
r   r   r   r    r   r   r   r   r  r   r!   r"   r#   r  r    s        110I! ! ! ! ! $ $ $ X$    X[ [ [ [ [ [r"   r  c                      e Zd ZdZdZdS )r_   zFPublished when a heartbeat is started.

    .. versionadded:: 3.3
    r!   Nr  r!   r"   r#   r_   r_   4  r  r"   r_   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 )ra   zIFired when the server heartbeat succeeds.

    .. versionadded:: 3.3
    rD  r   Fr   floatr   Hello[dict[str, Any]]r   r   r  r   r(   r)   c                h    t                                          ||           || _        || _        d S r   )r   r   (_ServerHeartbeatSucceededEvent__duration%_ServerHeartbeatSucceededEvent__replyr.   r   r   r   r  r   s        r#   r   z&ServerHeartbeatSucceededEvent.__init__E  s1     	000"r"   c                    | j         S z/The duration of this heartbeat in microseconds.)r  r   s    r#   r   z&ServerHeartbeatSucceededEvent.durationP  r   r"   c                    | j         S )z-An instance of :class:`~pymongo.hello.Hello`.)r  r   s    r#   r   z#ServerHeartbeatSucceededEvent.replyU  r   r"   c                *    t                      j        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.awaitedZ       wwr"   r   c                p    d                     | 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__f  s7    =DDN#MLJ
 
 	
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   s   @r#   ra   ra   =  s         
 *I 	 	 	 	 	 	 	    X    X 	 	 	 	 	 X	
 
 
 
 
 
 
 
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 )rc   zxFired when the server heartbeat fails, either with an "ok: 0"
    or a socket exception.

    .. versionadded:: 3.3
    r  Fr   r  r   	Exceptionr   r   r  r   r(   r)   c                h    t                                          ||           || _        || _        d S r   )r   r   %_ServerHeartbeatFailedEvent__duration"_ServerHeartbeatFailedEvent__replyr  s        r#   r   z#ServerHeartbeatFailedEvent.__init__y  s1     	000"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                      j        S r  r  r   s    r#   r  z"ServerHeartbeatFailedEvent.awaited  r  r"   r   c                p    d                     | j        j        | j        | j        | j        | j                  S )Nz.<{} {} duration: {}, awaited: {}, reply: {!r}>r  r   s    r#   r   z#ServerHeartbeatFailedEvent.__repr__  s7    ?FFN#MLJ
 
 	
r"   r  )
r   r  r   r  r   r   r  r   r(   r)   r  )r(   r  r!  r   r  r   s   @r#   rc   rc   p  s          *I [`          X    X 	 	 	 	 	 X	
 
 
 
 
 
 
 
r"   rc   c                  n   e Zd ZdZdLdZedMd            ZedMd            ZedMd	            ZedMd
            Z	edMd            Z
dNdZ	 	 dOdPdZ	 	 	 	 dQdRd%Z	 	 	 dSdTd'ZdUd)ZdVd,ZdWd.ZdXd2ZdXd3ZdYd7ZdZd8ZdZd9Zd[d;Zd\d?Zd]d@Z	 d^d_dBZd]dCZd`dDZdadEZdbdGZd]dHZdcdIZ dadJZ!d`dKZ"dS )d_EventListenerszConfigure event listeners for a client instance.

    Any event listeners registered globally are included by default.

    :param listeners: A list of event listeners.
    r   "Optional[Sequence[_EventListener]]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                            |           t          |t                    r| j                            |           t          |t                    r| j                            |           t          |t                     r| j                            |           t          |t"                    r| j
                            |           t%          | j                  | _        t%          | j                  | _        t%          | j                  | _        t%          | j                  | _        t%          | j
                  | _        d S r   )r   r   "_EventListeners__command_listenersr   !_EventListeners__server_listenersr   +_EventListeners__server_heartbeat_listenersr   #_EventListeners__topology_listenersr   _EventListeners__cmap_listenersr   r%   r   rs   r^   rf   r9   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__  s   #-#?#B ","=aaa"@3,/F)$.$A!!!$D! * 9!!! <   
6 
6c?33 9,33C888c>22 8+223777c#:;; B5<<SAAAc#344 :-44S999c#9:: 6)00555&*4+C&D&D#$()@$A$A!.243T.U.U+&*4+D&E&E#"&t'<"="=r"   r(   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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"   list[_EventListeners]c                P    | 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  s?     $/0%& '( #	$	
r"   Nr   r   r   r   r   r{   r   r   r   r   op_idr   r   r)   c           	         ||}t          |||||||          }| j        D ]6}		 |	                    |           # t          $ r t	                       Y 3w xY wdS )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   )r'   r  r/   r  r   )
r.   r   r   r   r   r   r  r   r&   
subscribers
             r#   publish_command_startz%_EventListeners.publish_command_start  s    * =E#!!5
 
 
 2 	$ 	$J$""5)))) $ $ $!#####$	$ 	$s   ;AAFr   r   r   r   r   speculative_helloc                    ||}|	ri }t          ||||||||
|	  	        }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )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   )r1   r  r3   r  r   )r.   r   r   r   r   r   r   r  r   r  r   r&   r  s                r#   publish_command_successz'_EventListeners.publish_command_success
  s    4 =E 	 E%'!5

 

 

 2 	$ 	$J$$$U++++ $ $ $!#####$	$ 	$s   AAAr   c
                    ||}t          ||||||||	|	  	        }
| j        D ]6}	 |                    |
           # t          $ r t	                       Y 3w xY wdS )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   )r4   r  r6   r  r   )r.   r   r   r   r   r   r   r  r   r   r&   r  s               r#   publish_command_failurez'_EventListeners.publish_command_failure;  s    2 =E"!'!5

 

 

 2 	$ 	$J$!!%(((( $ $ $!#####$	$ 	$s   =AAr  c                    t          ||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )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)r_   r  r/   r  r   )r.   r   r  r&   r  s        r#    publish_server_heartbeat_startedz0_EventListeners.publish_server_heartbeat_startedg  sx     ,M7CC; 	$ 	$J$""5)))) $ $ $!#####$	$ 	$   1AAr  r  c                    t          ||||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )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)ra   r  r3   r  r   r.   r   r   r   r  r&   r  s          r#   "publish_server_heartbeat_succeededz2_EventListeners.publish_server_heartbeat_succeededu  s|     .h}gVV; 	$ 	$J$$$U++++ $ $ $!#####$	$ 	$   3AAr  c                    t          ||||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )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  r6   r  r   r  s          r#   publish_server_heartbeat_failedz/_EventListeners.publish_server_heartbeat_failed  s|     +8UM7SS; 	$ 	$J$!!%(((( $ $ $!#####$	$ 	$r  rc  rd  r   c                    t          ||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )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)rt   r  ri   r  r   r.   rc  rd  r&   r  s        r#   publish_server_openedz%_EventListeners.publish_server_opened  sx     #>;??1 	$ 	$J$!!%(((( $ $ $!#####$	$ 	$r  c                    t          ||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )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  ro   r  r   r  s        r#   publish_server_closedz%_EventListeners.publish_server_closed  sx     ".+>>1 	$ 	$J$!!%(((( $ $ $!#####$	$ 	$r  rw  r   rx  c                    t          ||||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )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)rv   r  rl   r  r   )r.   rw  rx  rc  rd  r&   r  s          r#   "publish_server_description_changedz2_EventListeners.publish_server_description_changed  s     . />;
 
 1 	$ 	$J$..u5555 $ $ $!#####$	$ 	$r  c                    t          |          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )zPublish a TopologyOpenedEvent to all topology listeners.

        :param topology_id: A unique identifier for the topology this server
           is a part of.
        N)rg   r  ri   r  r   r.   rd  r&   r  s       r#   publish_topology_openedz'_EventListeners.publish_topology_opened  v     $K003 	$ 	$J$!!%(((( $ $ $!#####$	$ 	$   0A
Ac                    t          |          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )zPublish a TopologyClosedEvent to all topology listeners.

        :param topology_id: A unique identifier for the topology this server
           is a part of.
        N)rm   r  ro   r  r   r  s       r#   publish_topology_closedz'_EventListeners.publish_topology_closed  r  r  r   c                    t          |||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )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)rj   r  rl   r  r   )r.   rw  rx  rd  r&   r  s         r#   $publish_topology_description_changedz4_EventListeners.publish_topology_description_changed  s|     00DoWbcc3 	$ 	$J$..u5555 $ $ $!#####$	$ 	$   2AAr   r  r  c                    t          ||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )z:Publish a :class:`PoolCreatedEvent` to all pool listeners.N)r:   r  r<   r  r   )r.   r   r  r&   r  s        r#   publish_pool_createdz$_EventListeners.publish_pool_created  sv     '22/ 	$ 	$J$''.... $ $ $!#####$	$ 	$r  c                    t          |          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )z8Publish a :class:`PoolReadyEvent` to all pool listeners.N)r>   r  r@   r  r   r.   r   r&   r  s       r#   publish_pool_readyz"_EventListeners.publish_pool_ready  st    w''/ 	$ 	$J$%%e,,,, $ $ $!#####$	$ 	$r  r  c                    t          |||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )z:Publish a :class:`PoolClearedEvent` to all pool listeners.N)rA   r  rC   r  r   )r.   r   r   r  r&   r  s         r#   publish_pool_clearedz$_EventListeners.publish_pool_cleared  s{     !*6KLL/ 	$ 	$J$''.... $ $ $!#####$	$ 	$r  c                    t          |          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )z9Publish a :class:`PoolClosedEvent` to all pool listeners.N)rD   r  rF   r  r   r  s       r#   publish_pool_closedz#_EventListeners.publish_pool_closed  st    ((/ 	$ 	$J$&&u---- $ $ $!#####$	$ 	$r  c                    t          ||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )zWPublish a :class:`ConnectionCreatedEvent` to all connection
        listeners.
        N)rG   r  rI   r  r   r.   r   r   r&   r  s        r#   publish_connection_createdz*_EventListeners.publish_connection_created&  sx     'w>>/ 	$ 	$J$--e4444 $ $ $!#####$	$ 	$r  c                    t          |||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )zDPublish a :class:`ConnectionReadyEvent` to all connection listeners.N)rJ   r  rL   r  r   r.   r   r   r   r&   r  s         r#   publish_connection_readyz(_EventListeners.publish_connection_ready1  sz     %WmXFF/ 	$ 	$J$++E2222 $ $ $!#####$	$ 	$r  rP  c                    t          |||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )zVPublish a :class:`ConnectionClosedEvent` to all connection
        listeners.
        N)rN   r  rP   r  r   )r.   r   r   rP  r&   r  s         r#   publish_connection_closedz)_EventListeners.publish_connection_closed<  sz     &g}fEE/ 	$ 	$J$,,U3333 $ $ $!#####$	$ 	$r  c                    t          |          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )z_Publish a :class:`ConnectionCheckOutStartedEvent` to all connection
        listeners.
        N)rQ   r  rS   r  r   r  s       r#   $publish_connection_check_out_startedz4_EventListeners.publish_connection_check_out_startedG  sv     /w77/ 	$ 	$J$77>>>> $ $ $!#####$	$ 	$r  c                    t          |||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )z^Publish a :class:`ConnectionCheckOutFailedEvent` to all connection
        listeners.
        N)rT   r  rV   r  r   )r.   r   rP  r   r&   r  s         r#   #publish_connection_check_out_failedz3_EventListeners.publish_connection_check_out_failedR  sz     .gvxHH/ 	$ 	$J$66u==== $ $ $!#####$	$ 	$r  c                    t          |||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )zZPublish a :class:`ConnectionCheckedOutEvent` to all connection
        listeners.
        N)rW   r  rY   r  r   r  s         r#   publish_connection_checked_outz._EventListeners.publish_connection_checked_out_  sz     *'=(KK/ 	$ 	$J$11%8888 $ $ $!#####$	$ 	$r  c                    t          ||          }| j        D ]6}	 |                    |           # t          $ r t	                       Y 3w xY wdS )zYPublish a :class:`ConnectionCheckedInEvent` to all connection
        listeners.
        N)rZ   r  r\   r  r   r  s        r#   publish_connection_checked_inz-_EventListeners.publish_connection_checked_inl  sx     )-@@/ 	$ 	$J$007777 $ $ $!#####$	$ 	$r  )r   r  r!  )r(   r  r   )r   r   r   r   r   r{   r   r   r   r   r  r   r   r   r(   r)   )NNFr   )r   r   r   r   r   r   r   r{   r   r   r   r   r  r   r   r   r  r   r   r   r(   r)   )NNr   )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(   r)   rq  )
rw  r   rx  r   rc  r   rd  r   r(   r)   r  )rw  r   rx  r   rd  r   r(   r)   r  r  r  r   rA  )r   r   r   r{   r   r  r(   r)   )r   r   r   r{   rP  r   r(   r)   )r   r   rP  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  r!   r"   r#   r  r    s        > > > >2 + + + X+ ) ) ) X) 3 3 3 X3 + + + X+ ' ' ' X'
 
 
 
"  $)-$$ $$ $$ $$ $$\  $)-"'/$ /$ /$ /$ /$r  $)-*$ *$ *$ *$ *$X$ $ $ $$ $ $ $&$ $ $ $&$ $ $ $$ $ $ $$ $ $ $0$ $ $ $$ $ $ $$ $ $ $($ $ $ $$ $ $ $ ',	$ $ $ $ $$ $ $ $	$ 	$ 	$ 	$	$ 	$ 	$ 	$	$ 	$ 	$ 	$	$ 	$ 	$ 	$$ $ $ $$ $ $ $	$ 	$ 	$ 	$ 	$ 	$r"   r  )rz   r   r(   r{   )r   r   r   r   r(   r   )r   r   r(   r)   )r   r   r   r   r(   r   )Kr    
__future__r   datetimecollectionsr   r   typingr   r   r   r	   r
   bson.objectidr   pymongo.hellor   r   pymongo.helpers_sharedr   r   pymongo.typingsr   r   r   pymongo.server_descriptionr   pymongo.topology_descriptionr   r   r   r   r%   r9   r^   rf   rs   r~   r   r   r   r   r'   r1   r4   r   r:   r>   rA   rD   r$  r.  r4  r;  rC  rG   rJ   rN   rQ   rT   rW   rZ   r`  rv   rt   rx   r  rj   rg   rm   r  r_   ra   rc   r  r!   r"   r#   <module>r&     s  l l\ # " " " " "  ' ' ' ' ' ' ' ' B B B B B B B B B B B B B B " " " " " " , , , , , , , , I I I I I I I I 2 2 2 2 2 2 2 2 A""""""<<<<<<@@@@@@ Z	 	
 ZBB++
7 7 7 7 7 7 7 7" " " " "n " " ":u" u" u" u" u"^ u" u" u"p" " " " "n " " ">" " " " "~ " " "<" " " " "^ " " "<+ + + +
   $3 3 3 3:   I% I% I% I% I% I% I% I%X@
 @
 @
 @
 @
- @
 @
 @
FB
 B
 B
 B
 B
M B
 B
 B
J@
 @
 @
 @
 @
 @
 @
 @
F@ @ @ @ @ @ @ @&R R R R Rz R R R0	 	 	 	 	Z 	 	 	*w *w *w *w *wz *w *w *wZ	 	 	 	 	j 	 	 	F F F F F F F F*       &@ @ @ @ @ @ @ @&X X X X X) X X X$k k k k k1 k k k*    /    
 
 
 
 
3 
 
 
 
  
  
  
  
.  
  
  
F	 	 	 	 	%5 	 	 	d d d d d$< d d d:
 
 
 
 
 8 
 
 

 
 
 
 
1 
 
 
d d d d d d d d.&
 &
 &
 &
 &
L &
 &
 &
R              N N N N N N N N"&
 &
 &
 &
 &
m &
 &
 &
R    -       -   [ [ [ [ [ [ [ [8    "7   0
 0
 0
 0
 0
$9 0
 0
 0
f-
 -
 -
 -
 -
!6 -
 -
 -
`U$ U$ U$ U$ U$ U$ U$ U$ U$ U$r"   