U
    d%                     @   s   d Z ddlZddlZddlmZmZmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ ddlmZmZ G d	d
 d
ZdS )z0Represent one server the driver is connected to.    N)AnyDictMappingOptionalSetTuple)EPOCH_NAIVE)ObjectId)Hello)SERVER_TYPE)ClusterTime_Addressc                   @   s  e Zd ZU dZdZdPeee ee ee	 eddddZ
eedd	d
ZeedddZeedddZeeeeef  dddZeeeef dddZeee dddZeeeeef  dddZeedddZeedddZeedddZeedddZeeddd Zeee dd!d"Zeee dd#d$Z eee! dd%d&Z"eeee ee f dd'd(Z#eeeeef  dd)d*Z$eee dd+d,Z%eee dd-d.Z&eedd/d0Z'eee dd1d2Z(eedd3d4Z)eee	 dd5d6Z*ee+dd7d8Z,ee+dd9d:Z-ee+dd;d<Z.ee+dd=d>Z/ee+dd?d@Z0ee+ddAdBZ1eeeeef  ddCdDZ2dQee	 d dEdFdGZ3ee+dHdIdJZ4ee+dHdKdLZ5eddMdNZ6i Z7e8e9dO< dS )RServerDescriptiona^  Immutable representation of one server.

    :Parameters:
      - `address`: A (host, port) pair
      - `hello`: Optional Hello instance
      - `round_trip_time`: Optional float
      - `error`: Optional, the last error attempting to connect to the server
      - `round_trip_time`: Optional float, the min latency from the most recent samples
    )_address_server_type
_all_hosts_tags_replica_set_name_primary_max_bson_size_max_message_size_max_write_batch_size_min_wire_version_max_wire_version_round_trip_time_min_round_trip_time_me_is_writable_is_readable_ls_timeout_minutes_error_set_version_election_id_cluster_time_last_write_date_last_update_time_topology_versionN        )addresshelloround_trip_timeerrormin_round_trip_timereturnc                 C   s  || _ |sti }|j| _|j| _|j| _|j| _	|j
| _|j| _|j| _|j| _|j| _|j| _|j| _|j| _|j| _|j| _|j| _|j | _!|| _"|| _#|j$| _%t&' | _(|| _)|j*| _+|rt,|dd }t-|t.r|/d| _+|  |j0r|j0t1 }|2 | _3nd | _3d S )NdetailsZtopologyVersion)4r   r
   server_typer   	all_hostsr   tagsr   replica_set_namer   primaryr   max_bson_sizer   max_message_sizer   max_write_batch_sizer   min_wire_versionr   max_wire_versionr   set_versionr!   election_idr"   cluster_timer#   is_writabler   is_readabler   logical_session_timeout_minutesr   r   r   mer   time	monotonicr%   r    topology_versionr&   getattr
isinstancedictgetlast_write_dater   total_secondsr$   )selfr(   r)   r*   r+   r,   r.   delta rK   >/tmp/pip-unpacked-wheel-oblwsawz/pymongo/server_description.py__init__B   sD    


zServerDescription.__init__)r-   c                 C   s   | j S )z(The address (host, port) of this server.)r   rI   rK   rK   rL   r(   q   s    zServerDescription.addressc                 C   s   | j S )zThe type of this server.)r   rN   rK   rK   rL   r/   v   s    zServerDescription.server_typec                 C   s   t j| j S )zSThe server type as a human readable string.

        .. versionadded:: 3.4
        )r   _fieldsr   rN   rK   rK   rL   server_type_name{   s    z"ServerDescription.server_type_namec                 C   s   | j S )z;List of hosts, passives, and arbiters known to this server.)r   rN   rK   rK   rL   r0      s    zServerDescription.all_hostsc                 C   s   | j S N)r   rN   rK   rK   rL   r1      s    zServerDescription.tagsc                 C   s   | j S )zReplica set name or None.)r   rN   rK   rK   rL   r2      s    z"ServerDescription.replica_set_namec                 C   s   | j S )z8This server's opinion about who the primary is, or None.)r   rN   rK   rK   rL   r3      s    zServerDescription.primaryc                 C   s   | j S rQ   )r   rN   rK   rK   rL   r4      s    zServerDescription.max_bson_sizec                 C   s   | j S rQ   )r   rN   rK   rK   rL   r5      s    z"ServerDescription.max_message_sizec                 C   s   | j S rQ   )r   rN   rK   rK   rL   r6      s    z&ServerDescription.max_write_batch_sizec                 C   s   | j S rQ   )r   rN   rK   rK   rL   r7      s    z"ServerDescription.min_wire_versionc                 C   s   | j S rQ   r   rN   rK   rK   rL   r8      s    z"ServerDescription.max_wire_versionc                 C   s   | j S rQ   )r!   rN   rK   rK   rL   r9      s    zServerDescription.set_versionc                 C   s   | j S rQ   )r"   rN   rK   rK   rL   r:      s    zServerDescription.election_idc                 C   s   | j S rQ   )r#   rN   rK   rK   rL   r;      s    zServerDescription.cluster_timec                 C   s   t jdtdd | j| jfS )NzL'election_tuple' is deprecated, use  'set_version' and 'election_id' instead   )
stacklevel)warningswarnDeprecationWarningr!   r"   rN   rK   rK   rL   election_tuple   s    z ServerDescription.election_tuplec                 C   s   | j S rQ   )r   rN   rK   rK   rL   r?      s    zServerDescription.mec                 C   s   | j S rQ   )r   rN   rK   rK   rL   r>      s    z1ServerDescription.logical_session_timeout_minutesc                 C   s   | j S rQ   )r$   rN   rK   rK   rL   rG      s    z!ServerDescription.last_write_datec                 C   s   | j S rQ   )r%   rN   rK   rK   rL   last_update_time   s    z"ServerDescription.last_update_timec                 C   s   | j | jkr| j| j  S | jS )z$The current average latency or None.)r   _host_to_round_trip_timer   rN   rK   rK   rL   r*      s    z!ServerDescription.round_trip_timec                 C   s   | j S )z-The min latency from the most recent samples.)r   rN   rK   rK   rL   r,      s    z%ServerDescription.min_round_trip_timec                 C   s   | j S )z<The last error attempting to connect to the server, or None.)r    rN   rK   rK   rL   r+      s    zServerDescription.errorc                 C   s   | j S rQ   )r   rN   rK   rK   rL   r<      s    zServerDescription.is_writablec                 C   s   | j S rQ   )r   rN   rK   rK   rL   r=      s    zServerDescription.is_readablec                 C   s   | j tjkS rQ   )r   r   MongosrN   rK   rK   rL   mongos   s    zServerDescription.mongosc                 C   s   | j tjkS rQ   )r/   r   UnknownrN   rK   rK   rL   is_server_type_known   s    z&ServerDescription.is_server_type_knownc                 C   s(   | j dk	r| jtjtjfkp&| jtjkS )0Checks if this server supports retryable writes.N)r   r   r   r[   Z	RSPrimaryZLoadBalancerrN   rK   rK   rL   retryable_writes_supported   s
    

z,ServerDescription.retryable_writes_supportedc                 C   s
   | j dkS )r_      rR   rN   rK   rK   rL   retryable_reads_supported   s    z+ServerDescription.retryable_reads_supportedc                 C   s   | j S rQ   )r&   rN   rK   rK   rL   rB      s    z"ServerDescription.topology_version)r+   r-   c                 C   s   t | j|d}| j|_|S )N)r+   )r   r(   rB   r&   )rI   r+   unknownrK   rK   rL   
to_unknown  s    zServerDescription.to_unknown)otherr-   c                 C   s   t |tr| j|jko| j|jko| j|jko| j|j	ko| j
|jko| j|jko| j|jko| j|jko| j|jko| j|jko| j|jko| j|jko| j|jkS tS rQ   )rD   r   r   r(   r   r/   r   r7   r   r8   r   r?   r   r0   r   r1   r   r2   r!   r9   r"   r:   r   r3   r   r>   r    r+   NotImplementedrI   re   rK   rK   rL   __eq__  s6    








	




zServerDescription.__eq__c                 C   s
   | |k S rQ   rK   rg   rK   rK   rL   __ne__  s    zServerDescription.__ne__c                 C   s2   d}| j rd| j }d| jj| j| j| j|S )N z, error=z"<{} {} server_type: {}, rtt: {}{}>)r+   format	__class____name__r(   rP   r*   )rI   errmsgrK   rK   rL   __repr__  s    zServerDescription.__repr__rZ   )NNNr'   )N):rm   
__module____qualname____doc__	__slots__r   r   r
   float	ExceptionrM   propertyr(   intr/   strrP   r   r   r0   r   r   r1   r2   r3   r4   r5   r6   r7   r8   r9   r	   r:   r   r;   rX   r?   r>   rG   rY   r*   r,   r+   boolr<   r=   r\   r^   r`   rb   rB   rd   rh   ri   ro   rZ   r   __annotations__rK   rK   rK   rL   r      s   

    / r   )rr   r@   rU   typingr   r   r   r   r   r   Zbsonr   Zbson.objectidr	   Zpymongo.hellor
   Zpymongo.server_typer   Zpymongo.typingsr   r   r   rK   rK   rK   rL   <module>   s    