o
    'h                     @  s   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Zd&ddZd&ddZd&ddZd&ddZd&ddZd'ddZd(ddZd(d d!Z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                   @  sz   e Zd ZdZed"ddZd#ddZd$ddZd%ddZe	d&ddZ
e	d'ddZe	d'ddZd(ddZd)dd Zd!S )*	Selectionz.Input or output of a server selector function.topology_descriptionr   returnc                 C  s:   |j }d }|D ]}|jtjkr|} nqt||j |j|S N)known_serversserver_typer
   	RSPrimaryr   common_wire_version)clsr   r   primarysd r   T/var/www/html/olx_land/venv/lib/python3.10/site-packages/pymongo/server_selectors.pyfrom_topology_description#   s   z#Selection.from_topology_descriptionserver_descriptionslist[ServerDescription]r   Optional[int]r   Optional[ServerDescription]c                 C  s   || _ || _|| _|| _d S r   )r   r   r   r   )selfr   r   r   r   r   r   r   __init__3   s   
zSelection.__init__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_descriptionsc                 C  s$   t | }|jrt|jdd dS d S )Nc                 S  s   t t| jS r   )r	   floatlast_write_date)r   r   r   r   <lambda>H   s    z>Selection.secondary_with_max_last_write_date.<locals>.<lambda>)key)secondary_server_selectorr   max)r    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    	primariesr   r   r   primary_selectionL   s   
zSelection.primary_selectionintc                 C     | j jS r   )r   heartbeat_frequencyr    r   r   r   r/   Q      zSelection.heartbeat_frequencyc                 C  r.   r   )r   topology_typer0   r   r   r   r2   U   r1   zSelection.topology_typeboolc                 C  s
   t | jS r   )r3   r   r0   r   r   r   __bool__Y      
zSelection.__bool__itemr   c                 C  s
   | j | S r   )r   )r    r6   r   r   r   __getitem__\   r5   zSelection.__getitem__N)r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   )r   r   )r   r   )r   r-   )r   r3   )r6   r-   r   r   )__name__
__module____qualname____doc__classmethodr   r!   r"   r*   propertyr,   r/   r2   r4   r7   r   r   r   r   r       s    



r   	selectionr   c                 C  s   | S r   r   r>   r   r   r   any_server_selector`   s   r@   c                 C     |  dd | jD S )Nc                 S     g | ]}|j r|qS r   )is_readable.0sr   r   r   
<listcomp>f       z,readable_server_selector.<locals>.<listcomp>r"   r   r?   r   r   r   readable_server_selectord      rJ   c                 C  rA   )Nc                 S  rB   r   )is_writablerD   r   r   r   rG   l   rH   z,writable_server_selector.<locals>.<listcomp>rI   r?   r   r   r   writable_server_selectorj   rK   rM   c                 C  rA   )Nc                 S     g | ]
}|j tjkr|qS r   )r   r
   RSSecondaryrD   r   r   r   rG   r       z-secondary_server_selector.<locals>.<listcomp>rI   r?   r   r   r   r'   p   rK   r'   c                 C  rA   )Nc                 S  rN   r   )r   r
   	RSArbiterrD   r   r   r   rG   x   rP   z+arbiter_server_selector.<locals>.<listcomp>rI   r?   r   r   r   arbiter_server_selectorv   rK   rR   c                 C  s   t | pt| S )z6Like PrimaryPreferred but doesn't use tags or latency.)rM   r'   r?   r   r   r   "writable_preferred_server_selector|   s   rS   tag_setTagSetc                   s(   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.
    server_tagsMapping[str, Any]r   r3   c                   s0      D ]\}}|| vs| | |kr dS qdS )NFT)items)rV   r&   value)rT   r   r   
tags_match   s
   z(apply_single_tag_set.<locals>.tags_matchc                   s   g | ]	} |j r|qS r   )tagsrD   )rZ   r   r   rG      s    z(apply_single_tag_set.<locals>.<listcomp>N)rV   rW   r   r3   rI   )rT   r>   r   )rT   rZ   r   apply_single_tag_set   s   	r\   tag_setsTagSetsc                 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.
    )r\   r"   )r]   r>   rT   with_tag_setr   r   r   apply_tag_sets   s   	

r`   c                 C     t | t|S )z2All near-enough secondaries matching the tag sets.)r`   r'   r]   r>   r   r   r   #secondary_with_tags_server_selector      rc   c                 C  ra   )z.All near-enough members matching the tag sets.)r`   rJ   rb   r   r   r    member_with_tags_server_selector   rd   re   N)r>   r   r   r   )r>   r   r   r   )rT   rU   r>   r   r   r   )r]   r^   r>   r   r   r   ) r;   
__future__r   typingr   r   r   r   r   r   r	   pymongo.server_typer
   pymongo.server_descriptionr   pymongo.topology_descriptionr   r   strrU   r^   r   r@   rJ   rM   r'   rR   rS   r\   r`   rc   re   r   r   r   r   <module>   s*   $
@







