o
    C©Ìh:W  ã                   @  s–  d Z ddlmZ ddlZddl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 ddlmZmZ erBdd	lmZ dd
lmZ dZdZdZdZdZdZe	eef Zee	eef  Zd;dd„Zd<dd„Z d=dd„Z!d>dd „Z"G d!d"„ d"ƒZ#G d#d$„ d$e#ƒZ$G d%d&„ d&e#ƒZ%G d'd(„ d(e#ƒZ&G d)d*„ d*e#ƒZ'G d+d,„ d,e#ƒZ(G d-d.„ d.ƒZ)e$e%e&e'e(fZ*	/d?d@d1d2„Z+d3Z,G d4d5„ d5ƒZ-dAd7d8„Z.G d9d:„ d:ƒZ/dS )Bz¤Utilities for choosing which member of a replica set to read from.

.. seealso:: This module is compatible with both the synchronous and asynchronous PyMongo APIs.
é    )ÚannotationsN)Úabc)ÚTYPE_CHECKINGÚAnyÚMappingÚOptionalÚSequence)Úmax_staleness_selectors)ÚConfigurationError)Ú member_with_tags_server_selectorÚ#secondary_with_tags_server_selector)Ú	Selection)ÚTopologyDescriptioné   é   é   é   )ÚprimaryÚprimaryPreferredÚ	secondaryÚsecondaryPreferredÚnearestÚtag_setsúOptional[_TagSets]Úreturnc                 C  st   | du r| S t | ttfƒstd| ›d�ƒ‚t| ƒdkr#td| ›d�ƒ‚| D ]}t |tjƒs5td|›d�ƒ‚q%t| ƒS )z$Validate tag sets for a MongoClient.Nz	Tag sets z invalid, must be a sequencer   z: invalid, must be None or contain at least one set of tagszTag set zg invalid, must be an instance of dict, bson.son.SON or other type that inherits from collection.Mapping)Ú
isinstanceÚlistÚtupleÚ	TypeErrorÚlenÚ
ValueErrorr   r   )r   Útags© r"   úg/var/www/html/Moveonwheels/move_on_wheels/venv/lib/python3.10/site-packages/pymongo/read_preferences.pyÚ_validate_tag_sets9   s   
ÿ
ÿÿr$   Úmax_stalenessr   Ústrc                 C  s   d|  S )Nz6maxStalenessSeconds must be a positive integer, not %sr"   ©r%   r"   r"   r#   Ú_invalid_max_staleness_msgP   s   r(   Úintc                 C  s:   | dkrdS t | tƒstt| ƒƒ‚| dkrtt| ƒƒ‚| S )zValidate max_staleness.éÿÿÿÿr   )r   r)   r   r(   r    r'   r"   r"   r#   Ú_validate_max_stalenessU   s   
r+   ÚhedgeúOptional[_Hedge]c                 C  s8   | du rdS t | tƒstd| ›�ƒ‚tjdtdd� | S )zValidate hedge.Nz hedge must be a dictionary, not ú²The read preference 'hedge' option is deprecated in PyMongo 4.12+ because hedged reads are deprecated in MongoDB version 8.0+. Support for 'hedge' will be removed in PyMongo 5.0.r   ©Ú
stacklevel)r   Údictr   ÚwarningsÚwarnÚDeprecationWarning)r,   r"   r"   r#   Ú_validate_hedgec   s   
ýr5   c                   @  sÒ   e Zd ZdZdZ			d6d7dd„Zed8dd„ƒZed8dd„ƒZed9dd„ƒZ	ed:dd„ƒZ
ed;dd„ƒZed:dd„ƒZed<dd „ƒZed:d!d"„ƒZd8d#d$„Zd=d(d)„Zd=d*d+„Zd9d,d-„Zd>d0d1„Zd?d4d5„ZdS )@Ú_ServerModez$Base class for all read preferences.)Ú__mongos_modeÚ__modeÚ
__tag_setsÚ__max_stalenessÚ__hedgeNr*   Úmoder)   r   r   r%   r,   r-   r   ÚNonec                 C  s2   t | | _|| _t|ƒ| _t|ƒ| _t|ƒ| _d S ©N)	Ú_MONGOS_MODESÚ_ServerMode__mongos_modeÚ_ServerMode__moder$   Ú_ServerMode__tag_setsr+   Ú_ServerMode__max_stalenessr5   Ú_ServerMode__hedge)Úselfr<   r   r%   r,   r"   r"   r#   Ú__init__x   s
   


z_ServerMode.__init__r&   c                 C  s   | j jS )z!The name of this read preference.)Ú	__class__Ú__name__©rE   r"   r"   r#   Úname…   s   z_ServerMode.namec                 C  ó   | j S )z(The mongos mode of this read preference.)r@   rI   r"   r"   r#   Úmongos_modeŠ   ó   z_ServerMode.mongos_modeúdict[str, Any]c                 C  sT   d| j i}| jdi gfvr| j|d< | jdkr| j|d< | jdi fvr(| j|d< |S )zRead preference as a document.r<   Nr!   r*   ÚmaxStalenessSecondsr,   )r@   rB   rC   rD   )rE   Údocr"   r"   r#   Údocument�   s   




z_ServerMode.documentc                 C  rK   )z*The mode of this read preference instance.)rA   rI   r"   r"   r#   r<   ›   rM   z_ServerMode.modeÚ_TagSetsc                 C  s   | j rt| j ƒS i gS )aW  Set ``tag_sets`` to a list of dictionaries like [{'dc': 'ny'}] to
        read only from members whose ``dc`` tag has the value ``"ny"``.
        To specify a priority-order for tag sets, provide a list of
        tag sets: ``[{'dc': 'ny'}, {'dc': 'la'}, {}]``. A final, empty tag
        set, ``{}``, means "read from any member that matches the mode,
        ignoring tags." MongoClient tries each set of tags in turn
        until it finds a set of tags with at least one matching member.
        For example, to only send a query to an analytic node::

           Nearest(tag_sets=[{"node":"analytics"}])

        Or using :class:`SecondaryPreferred`::

           SecondaryPreferred(tag_sets=[{"node":"analytics"}])

           .. seealso:: `Data-Center Awareness
               <https://www.mongodb.com/docs/manual/data-center-awareness/>`_
        )rB   r   rI   r"   r"   r#   r       s   z_ServerMode.tag_setsc                 C  rK   )zØThe maximum estimated length of time (in seconds) a replica set
        secondary can fall behind the primary in replication before it will
        no longer be selected for operations, or -1 for no maximum.
        ©rC   rI   r"   r"   r#   r%   ¶   s   z_ServerMode.max_stalenessc                 C  s    | j durtjdtdd� | j S )am  **DEPRECATED** - The read preference 'hedge' option is deprecated in PyMongo 4.12+ because hedged reads are deprecated in MongoDB version 8.0+. Support for 'hedge' will be removed in PyMongo 5.0.

        The read preference ``hedge`` parameter.

        A dictionary that configures how the server will perform hedged reads.
        It consists of the following keys:

        - ``enabled``: Enables or disables hedged reads in sharded clusters.

        Hedged reads are automatically enabled in MongoDB 4.4+ when using a
        ``nearest`` read preference. To explicitly enable hedged reads, set
        the ``enabled`` key  to ``true``::

            >>> Nearest(hedge={'enabled': True})

        To explicitly disable hedged reads, set the ``enabled`` key  to
        ``False``::

            >>> Nearest(hedge={'enabled': False})

        .. versionadded:: 3.11
        Nr.   r   r/   )rD   r2   r3   r4   rI   r"   r"   r#   r,   ¾   s   
ýz_ServerMode.hedgec                 C  s   | j dkrdS dS )a‚  The wire protocol version the server must support.

        Some read preferences impose version requirements on all servers (e.g.
        maxStalenessSeconds requires MongoDB 3.4 / maxWireVersion 5).

        All servers' maxWireVersion must be at least this read preference's
        `min_wire_version`, or the driver raises
        :exc:`~pymongo.errors.ConfigurationError`.
        r*   r   é   rS   rI   r"   r"   r#   Úmin_wire_versionÞ   s   z_ServerMode.min_wire_versionc                 C  s   d  | j| j| j| j¡S )Nz1{}(tag_sets={!r}, max_staleness={!r}, hedge={!r}))ÚformatrJ   rB   rC   rD   rI   r"   r"   r#   Ú__repr__ë   s   üz_ServerMode.__repr__Úotherr   Úboolc                 C  s>   t |tƒr| j|jko| j|jko| j|jko| j|jkS tS r>   )r   r6   r<   r   r%   r,   ÚNotImplemented©rE   rX   r"   r"   r#   Ú__eq__ó   s   

ÿ
þ
üz_ServerMode.__eq__c                 C  s
   | |k S r>   r"   r[   r"   r"   r#   Ú__ne__ý   ó   
z_ServerMode.__ne__c                 C  s   | j | j| j| jdœS )zeReturn value of object for pickling.

        Needed explicitly because __slots__() defined.
        )r<   r   r%   r,   )rA   rB   rC   rD   rI   r"   r"   r#   Ú__getstate__   s
   üz_ServerMode.__getstate__ÚvalueúMapping[str, Any]c                 C  sD   |d | _ t| j  | _t|d ƒ| _t|d ƒ| _t|d ƒ| _dS )zRestore from pickling.r<   r   r%   r,   N)	rA   r?   r@   r$   rB   r+   rC   r5   rD   )rE   r`   r"   r"   r#   Ú__setstate__  s
   
z_ServerMode.__setstate__Ú	selectionr   c                 C  s   |S r>   r"   ©rE   rc   r"   r"   r#   Ú__call__  ó   z_ServerMode.__call__©Nr*   N)
r<   r)   r   r   r%   r)   r,   r-   r   r=   ©r   r&   )r   rN   )r   r)   )r   rR   )r   r-   ©rX   r   r   rY   )r`   ra   r   r=   ©rc   r   r   r   )rH   Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__rF   ÚpropertyrJ   rL   rQ   r<   r   r%   r,   rU   rW   r\   r]   r_   rb   re   r"   r"   r"   r#   r6   s   s:    û





r6   c                      sD   e Zd ZdZdZd‡ fdd„Zdd	d
„Zddd„Zddd„Z‡  Z	S )ÚPrimaryaG  Primary read preference.

    * When directly connected to one mongod queries are allowed if the server
      is standalone or a replica set primary.
    * When connected to a mongos queries are sent to the primary of a shard.
    * When connected to a replica set queries are sent to the primary of
      the replica set.
    r"   r   r=   c                   s   t ƒ  t¡ d S r>   )ÚsuperrF   Ú_PRIMARYrI   ©rG   r"   r#   rF   $  s   zPrimary.__init__rc   r   c                 C  s   |j S ©z*Apply this read preference to a Selection.)Úprimary_selectionrd   r"   r"   r#   re   '  ó   zPrimary.__call__r&   c                 C  s   dS )Nz	Primary()r"   rI   r"   r"   r#   rW   +  rf   zPrimary.__repr__rX   r   rY   c                 C  s   t |tƒr
|jtkS tS r>   )r   r6   r<   rr   rZ   r[   r"   r"   r#   r\   .  s   

zPrimary.__eq__©r   r=   rj   rh   ri   )
rH   rk   rl   rm   rn   rF   re   rW   r\   Ú__classcell__r"   r"   rs   r#   rp     s    	

rp   c                      ó8   e Zd ZdZdZ			dd‡ fdd„Zddd„Z‡  ZS )ÚPrimaryPreferreda“  PrimaryPreferred read preference.

    * When directly connected to one mongod queries are allowed to standalone
      servers, to a replica set primary, or to replica set secondaries.
    * When connected to a mongos queries are sent to the primary of a shard if
      available, otherwise a shard secondary.
    * When connected to a replica set queries are sent to the primary if
      available, otherwise a secondary.

    .. note:: When a :class:`~pymongo.mongo_client.MongoClient` is first
      created reads will be routed to an available secondary until the
      primary of the replica set is discovered.

    :param tag_sets: The :attr:`~tag_sets` to use if the primary is not
        available.
    :param max_staleness: (integer, in seconds) The maximum estimated
        length of time a replica set secondary can fall behind the primary in
        replication before it will no longer be selected for operations.
        Default -1, meaning no maximum. If it is set, it must be at least
        90 seconds.
    :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference.

    .. versionchanged:: 3.11
       Added ``hedge`` parameter.
    r"   Nr*   r   r   r%   r)   r,   r-   r   r=   c                   ó   t ƒ  t|||¡ d S r>   )rq   rF   Ú_PRIMARY_PREFERRED©rE   r   r%   r,   rs   r"   r#   rF   Q  ó   zPrimaryPreferred.__init__rc   r   c                 C  s"   |j r|jS t| jt | j|¡ƒS ©z(Apply this read preference to Selection.)r   ru   r   r   r	   Úselectr%   rd   r"   r"   r#   re   Y  s
   ÿzPrimaryPreferred.__call__rg   ©r   r   r%   r)   r,   r-   r   r=   rj   ©rH   rk   rl   rm   rn   rF   re   rx   r"   r"   rs   r#   rz   4  s    ürz   c                      ry   )Ú	SecondaryaÞ  Secondary read preference.

    * When directly connected to one mongod queries are allowed to standalone
      servers, to a replica set primary, or to replica set secondaries.
    * When connected to a mongos queries are distributed among shard
      secondaries. An error is raised if no secondaries are available.
    * When connected to a replica set queries are distributed among
      secondaries. An error is raised if no secondaries are available.

    :param tag_sets: The :attr:`~tag_sets` for this read preference.
    :param max_staleness: (integer, in seconds) The maximum estimated
        length of time a replica set secondary can fall behind the primary in
        replication before it will no longer be selected for operations.
        Default -1, meaning no maximum. If it is set, it must be at least
        90 seconds.
    :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference.

    .. versionchanged:: 3.11
       Added ``hedge`` parameter.
    r"   Nr*   r   r   r%   r)   r,   r-   r   r=   c                   r{   r>   )rq   rF   Ú
_SECONDARYr}   rs   r"   r#   rF   {  r~   zSecondary.__init__rc   r   c                 C  ó   t | jt | j|¡ƒS r   )r   r   r	   r€   r%   rd   r"   r"   r#   re   ƒ  ó   ÿzSecondary.__call__rg   r�   rj   r‚   r"   r"   rs   r#   rƒ   c  ó    ürƒ   c                      ry   )ÚSecondaryPreferreda   SecondaryPreferred read preference.

    * When directly connected to one mongod queries are allowed to standalone
      servers, to a replica set primary, or to replica set secondaries.
    * When connected to a mongos queries are distributed among shard
      secondaries, or the shard primary if no secondary is available.
    * When connected to a replica set queries are distributed among
      secondaries, or the primary if no secondary is available.

    .. note:: When a :class:`~pymongo.mongo_client.MongoClient` is first
      created reads will be routed to the primary of the replica set until
      an available secondary is discovered.

    :param tag_sets: The :attr:`~tag_sets` for this read preference.
    :param max_staleness: (integer, in seconds) The maximum estimated
        length of time a replica set secondary can fall behind the primary in
        replication before it will no longer be selected for operations.
        Default -1, meaning no maximum. If it is set, it must be at least
        90 seconds.
    :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference.

    .. versionchanged:: 3.11
       Added ``hedge`` parameter.
    r"   Nr*   r   r   r%   r)   r,   r-   r   r=   c                   r{   r>   )rq   rF   Ú_SECONDARY_PREFERREDr}   rs   r"   r#   rF   ¦  r~   zSecondaryPreferred.__init__rc   r   c                 C  s$   t | jt | j|¡ƒ}|r|S |jS r   )r   r   r	   r€   r%   ru   )rE   rc   Úsecondariesr"   r"   r#   re   ®  s   ÿzSecondaryPreferred.__call__rg   r�   rj   r‚   r"   r"   rs   r#   rˆ   Š  s    ürˆ   c                      ry   )ÚNearestay  Nearest read preference.

    * When directly connected to one mongod queries are allowed to standalone
      servers, to a replica set primary, or to replica set secondaries.
    * When connected to a mongos queries are distributed among all members of
      a shard.
    * When connected to a replica set queries are distributed among all
      members.

    :param tag_sets: The :attr:`~tag_sets` for this read preference.
    :param max_staleness: (integer, in seconds) The maximum estimated
        length of time a replica set secondary can fall behind the primary in
        replication before it will no longer be selected for operations.
        Default -1, meaning no maximum. If it is set, it must be at least
        90 seconds.
    :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference.

    .. versionchanged:: 3.11
       Added ``hedge`` parameter.
    r"   Nr*   r   r   r%   r)   r,   r-   r   r=   c                   r{   r>   )rq   rF   Ú_NEARESTr}   rs   r"   r#   rF   Ò  r~   zNearest.__init__rc   r   c                 C  r…   r   )r   r   r	   r€   r%   rd   r"   r"   r#   re   Ú  r†   zNearest.__call__rg   r�   rj   r‚   r"   r"   rs   r#   r‹   º  r‡   r‹   c                   @  sF   e Zd ZdZdZddd„Zddd„Zddd„Zddd„Zddd„Z	dS )Ú_AggWritePrefzùAgg $out/$merge write preference.

    * If there are readable servers and there is any pre-5.0 server, use
      primary read preference.
    * Otherwise use `pref` read preference.

    :param pref: The read preference to use on MongoDB 5.0+.
    )ÚprefÚeffective_prefrŽ   r6   c                 C  s   || _ tj| _d S r>   )rŽ   ÚReadPreferenceÚPRIMARYr�   )rE   rŽ   r"   r"   r#   rF   í  s   z_AggWritePref.__init__Útopology_descriptionr   r   r=   c                 C  s6   |j }| tj¡r|r|dk rtj| _d S | j| _d S )Né   )Úcommon_wire_versionÚhas_readable_serverr�   ÚPRIMARY_PREFERREDr‘   r�   rŽ   )rE   r’   Ú	common_wvr"   r"   r#   Úselection_hookñ  s   
ÿþz_AggWritePref.selection_hookrc   r   c                 C  s
   |   |¡S rt   )r�   rd   r"   r"   r#   re   ü  ó   
z_AggWritePref.__call__r&   c                 C  s   d| j ›d�S )Nz_AggWritePref(pref=ú))rŽ   rI   r"   r"   r#   rW      s   z_AggWritePref.__repr__rJ   r   c                 C  s   t | j|ƒS r>   )Úgetattrr�   )rE   rJ   r"   r"   r#   Ú__getattr__  s   z_AggWritePref.__getattr__N)rŽ   r6   )r’   r   r   r=   rj   rh   )rJ   r&   r   r   )
rH   rk   rl   rm   rn   rF   r˜   re   rW   rœ   r"   r"   r"   r#   r�   á  s    	



r�   r*   r<   c                 C  sB   | t kr|d i gfvrtdƒ‚|dkrtdƒ‚tƒ S t|  ||ƒS )Nz4Read preference primary cannot be combined with tagsr*   zCRead preference primary cannot be combined with maxStalenessSeconds)rr   r
   rp   Ú_ALL_READ_PREFERENCES)r<   r   r%   r"   r"   r#   Úmake_read_preference  s   ÿrž   )r‘   r–   Ú	SECONDARYÚSECONDARY_PREFERREDÚNEARESTc                   @  s.   e Zd ZdZeƒ Zeƒ Zeƒ Z	e
ƒ Zeƒ ZdS )r�   a  An enum that defines some commonly used read preference modes.

    Apps can also create a custom read preference, for example::

       Nearest(tag_sets=[{"node":"analytics"}])

    See `Read and Write Settings <https://www.mongodb.com/docs/languages/python/pymongo-driver/current/crud/configure/#read-and-write-settings>`_ for code examples.

    A read preference is used in three cases:

    :class:`~pymongo.mongo_client.MongoClient` connected to a single mongod:

    - ``PRIMARY``: Queries are allowed if the server is standalone or a replica
      set primary.
    - All other modes allow queries to standalone servers, to a replica set
      primary, or to replica set secondaries.

    :class:`~pymongo.mongo_client.MongoClient` initialized with the
    ``replicaSet`` option:

    - ``PRIMARY``: Read from the primary. This is the default, and provides the
      strongest consistency. If no primary is available, raise
      :class:`~pymongo.errors.AutoReconnect`.

    - ``PRIMARY_PREFERRED``: Read from the primary if available, or if there is
      none, read from a secondary.

    - ``SECONDARY``: Read from a secondary. If no secondary is available,
      raise :class:`~pymongo.errors.AutoReconnect`.

    - ``SECONDARY_PREFERRED``: Read from a secondary if available, otherwise
      from the primary.

    - ``NEAREST``: Read from any member.

    :class:`~pymongo.mongo_client.MongoClient` connected to a mongos, with a
    sharded cluster of replica sets:

    - ``PRIMARY``: Read from the primary of the shard, or raise
      :class:`~pymongo.errors.OperationFailure` if there is none.
      This is the default.

    - ``PRIMARY_PREFERRED``: Read from the primary of the shard, or if there is
      none, read from a secondary of the shard.

    - ``SECONDARY``: Read from a secondary of the shard, or raise
      :class:`~pymongo.errors.OperationFailure` if there is none.

    - ``SECONDARY_PREFERRED``: Read from a secondary of the shard if available,
      otherwise from the shard primary.

    - ``NEAREST``: Read from any shard member.
    N)rH   rk   rl   rm   rp   r‘   rz   r–   rƒ   rŸ   rˆ   r    r‹   r¡   r"   r"   r"   r#   r�   #  s    6
r�   rJ   c                 C  s
   t  | ¡S )z2Get the read preference mode from mongos/uri name.)r?   Úindex)rJ   r"   r"   r#   Úread_pref_mode_from_namea  r™   r£   c                   @  sB   e Zd ZU dZded< ddd„Zdd
d„Zddd„Zddd„ZdS )ÚMovingAveragez0Tracks an exponentially-weighted moving average.úOptional[float]Úaverager   r=   c                 C  ó
   d | _ d S r>   ©r¦   rI   r"   r"   r#   rF   k  r^   zMovingAverage.__init__ÚsampleÚfloatc                 C  sB   |dk rt d|› �ƒ‚| jd u r|| _d S d| j d|  | _d S )Nr   zduration cannot be negative gš™™™™™é?gš™™™™™É?)r    r¦   )rE   r©   r"   r"   r#   Ú
add_samplen  s
   

zMovingAverage.add_samplec                 C  rK   )z6Get the calculated average, or None if no samples yet.r¨   rI   r"   r"   r#   Úgetx  rv   zMovingAverage.getc                 C  r§   r>   r¨   rI   r"   r"   r#   Úreset|  r^   zMovingAverage.resetNrw   )r©   rª   r   r=   )r   r¥   )	rH   rk   rl   rm   Ú__annotations__rF   r«   r¬   r­   r"   r"   r"   r#   r¤   f  s   
 



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   r6   )rJ   r&   r   r)   )0rm   Ú
__future__r   r2   Úcollectionsr   Útypingr   r   r   r   r   Úpymongor	   Úpymongo.errorsr
   Úpymongo.server_selectorsr   r   r   Úpymongo.topology_descriptionr   rr   r|   r„   r‰   rŒ   r?   r&   Ú_HedgerR   r$   r(   r+   r5   r6   rp   rz   rƒ   rˆ   r‹   r�   r�   rž   Ú_MODESr�   r£   r¤   r"   r"   r"   r#   Ú<module>   sL   



 &/'0'(ÿ	
>