U
    ~fh                     @  s2  d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
 ddlmZ er\ddlmZ ddlmZ e	dZeeef Zee ZG dd	 d	Zddd
ddZd	d	d
ddZd	d	d
ddZd	d	d
ddZd	d	d
ddZd	d	d
ddZdd	d	dddZdd	d	dddZdd	d	ddd Zdd	d	dd!d"Zd#S )$zFCriteria to select some ServerDescriptions from a TopologyDescription.    )annotations)TYPE_CHECKINGAnyMappingOptionalSequenceTypeVarcast)SERVER_TYPE)ServerDescription)TopologyDescriptionTc                   @  s   e Zd ZdZedd dddZddddd	d
dZdd dddZddddZe	d dddZ
e	ddddZe	ddddZddddZdddddZd S )!	Selectionz.Input or output of a server selector function.r   )topology_descriptionreturnc                 C  s:   |j }d }|D ]}|jtjkr|} q(qt||j |j|S N)known_serversserver_typer
   Z	RSPrimaryr   common_wire_version)clsr   r   primarysd r   </tmp/pip-unpacked-wheel-36gvocj8/pymongo/server_selectors.pyfrom_topology_description#   s    z#Selection.from_topology_descriptionzlist[ServerDescription]zOptional[int]zOptional[ServerDescription])r   server_descriptionsr   r   c                 C  s   || _ || _|| _|| _d S r   )r   r   r   r   )selfr   r   r   r   r   r   r   __init__3   s    zSelection.__init__)r   r   c                 C  s   t | j|| j| jS r   )r   r   r   r   )r   r   r   r   r   with_server_descriptions?   s       z"Selection.with_server_descriptions)r   c                 C  s$   t | }|jr t|jdd dS d S )Nc                 S  s   t t| jS r   )r	   floatZlast_write_date)r   r   r   r   <lambda>H       z>Selection.secondary_with_max_last_write_date.<locals>.<lambda>)key)secondary_server_selectorr   max)r   Zsecondariesr   r   r   "secondary_with_max_last_write_dateD   s     z,Selection.secondary_with_max_last_write_datec                 C  s   | j r| j gng }| |S r   )r   r   )r   Z	primariesr   r   r   primary_selectionL   s    zSelection.primary_selectionintc                 C  s   | j jS r   )r   heartbeat_frequencyr   r   r   r   r(   Q   s    zSelection.heartbeat_frequencyc                 C  s   | j jS r   )r   topology_typer)   r   r   r   r*   U   s    zSelection.topology_typeboolc                 C  s
   t | jS r   )r+   r   r)   r   r   r   __bool__Y   s    zSelection.__bool__r   )itemr   c                 C  s
   | j | S r   )r   )r   r-   r   r   r   __getitem__\   s    zSelection.__getitem__N)__name__
__module____qualname____doc__classmethodr   r   r   r%   propertyr&   r(   r*   r,   r.   r   r   r   r   r       s   r   )	selectionr   c                 C  s   | S r   r   r5   r   r   r   any_server_selector`   s    r7   c                 C  s   |  dd | jD S )Nc                 S  s   g | ]}|j r|qS r   )Zis_readable.0sr   r   r   
<listcomp>f   s      z,readable_server_selector.<locals>.<listcomp>r   r   r6   r   r   r   readable_server_selectord   s    r=   c                 C  s   |  dd | jD S )Nc                 S  s   g | ]}|j r|qS r   )is_writabler8   r   r   r   r;   l   s      z,writable_server_selector.<locals>.<listcomp>r<   r6   r   r   r   writable_server_selectorj   s    r?   c                 C  s   |  dd | jD S )Nc                 S  s   g | ]}|j tjkr|qS r   )r   r
   ZRSSecondaryr8   r   r   r   r;   r   s      z-secondary_server_selector.<locals>.<listcomp>r<   r6   r   r   r   r#   p   s    r#   c                 C  s   |  dd | jD S )Nc                 S  s   g | ]}|j tjkr|qS r   )r   r
   Z	RSArbiterr8   r   r   r   r;   x   s      z+arbiter_server_selector.<locals>.<listcomp>r<   r6   r   r   r   arbiter_server_selectorv   s    r@   c                 C  s   t | pt| S )z6Like PrimaryPreferred but doesn't use tags or latency.)r?   r#   r6   r   r   r   "writable_preferred_server_selector|   s    rA   TagSet)tag_setr5   r   c                   s.   ddd fdd| fdd|jD S )zAll servers matching one tag set.

    A tag set is a dict. A server matches if its tags are a superset:
    A server tagged {'a': '1', 'b': '2'} matches the tag set {'a': '1'}.

    The empty tag set {} matches any server.
    zMapping[str, Any]r+   )server_tagsr   c                   s0      D ]"\}}|| ks$| | |kr dS qdS )NFT)items)rD   r"   value)rC   r   r   
tags_match   s    z(apply_single_tag_set.<locals>.tags_matchc                   s   g | ]} |j r|qS r   )tagsr8   )rG   r   r   r;      s     
 z(apply_single_tag_set.<locals>.<listcomp>r<   )rC   r5   r   )rC   rG   r   apply_single_tag_set   s    	rI   TagSets)tag_setsr5   r   c                 C  s*   | D ]}t ||}|r|  S q|g S )aN  All servers match a list of tag sets.

    tag_sets is a list of dicts. The empty tag set {} matches any server,
    and may be provided at the end of the list as a fallback. So
    [{'a': 'value'}, {}] expresses a preference for servers tagged
    {'a': 'value'}, but accepts any server if none matches the first
    preference.
    )rI   r   )rK   r5   rC   Zwith_tag_setr   r   r   apply_tag_sets   s
    	

rL   c                 C  s   t | t|S )z2All near-enough secondaries matching the tag sets.)rL   r#   rK   r5   r   r   r   #secondary_with_tags_server_selector   s    rN   c                 C  s   t | t|S )z.All near-enough members matching the tag sets.)rL   r=   rM   r   r   r    member_with_tags_server_selector   s    rO   N) r2   
__future__r   typingr   r   r   r   r   r   r	   Zpymongo.server_typer
   Zpymongo.server_descriptionr   Zpymongo.topology_descriptionr   r   strrB   rJ   r   r7   r=   r?   r#   r@   rA   rI   rL   rN   rO   r   r   r   r   <module>   s(   $@