U
    ]gO                  	   @  s@  U d Z ddlm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mZ dddd	d
ZG dd deZeeddeddeddeddeddedddZi Zded< G dd deZeG dd de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,eZd-S ).     )annotations)total_ordering)Dict
NamedTupleOptionalTupleTypeUnioncast)ClassVarListzUnion[bytes, str, int]str)valuereturnc                 C  s   t | tr|  S t| S )z-
    converts a byte/str or int to a str
    )
isinstancebytesdecoder   )r    r   1/tmp/pip-unpacked-wheel-kizsipjx/limits/limits.pysafe_string   s    
r   c                   @  s   e Zd ZU ded< ded< dS )Granularityintsecondsr   nameN)__name__
__module____qualname____annotations__r   r   r   r   r      s   
r   iQ dayi ' monthi yeari  hour<   minute   second)r   r    r!   r"   r$   r&   zDict[str, Type[RateLimitItem]]GRANULARITIESc                      s(   e Zd Zdddd d fddZ  ZS )RateLimitItemMetar   zTuple[type, ...]z(Dict[str, Union[Granularity, List[str]]])r   parentsdctr   c                   sH   d|krg |d< t  | |||}d|krDttt |t|d d < |S )N	__slots__GRANULARITYr%   )super__new__r   r	   RateLimitItemr'   )clsr   r)   r*   Zgranularity	__class__r   r   r.   (   s     zRateLimitItemMeta.__new__)r   r   r   r.   __classcell__r   r   r1   r   r(   '   s   r(   c                   @  s   e Zd ZU dZdddgZded< d$d	d
ddddZedddddZd	dddZ	dddddZ
dddddZddddZd dddd Zd	dd!d"Zd#S )%r/   a]  
    defines a Rate limited resource which contains the characteristic
    namespace, amount and granularity multiples of the rate limiting window.

    :param amount: the rate limit amount
    :param multiples: multiple of the 'per' :attr:`GRANULARITY`
     (e.g. 'n' per 'm' seconds)
    :param namespace: category for the specific rate limit
    	namespaceamount	multipleszClassVar[Granularity]r,   r%   LIMITERr   zOptional[int]r   )r5   r6   r4   c                 C  s"   || _ t|| _t|pd| _d S )Nr%   )r4   r   r5   r6   )selfr5   r6   r4   r   r   r   __init__O   s    
zRateLimitItem.__init__bool)granularity_stringr   c                 C  s   |  | jjkS )z
        Checks if this instance matches a *granularity_string*
        of type ``n per hour``, ``n per minute`` etc,
        by comparing with :attr:`GRANULARITY`

        )lowerr,   r   )r0   r;   r   r   r   check_granularity_stringV   s    	z&RateLimitItem.check_granularity_string)r   c                 C  s   | j j| j S )zM
        :return: the duration the limit is enforced for in seconds.
        )r,   r   r6   r8   r   r   r   
get_expirya   s    zRateLimitItem.get_expiry)identifiersr   c                 G  s>   d dd |D t| jt| j| jjg }| j d| S )a"  
        Constructs a key for the current limit and any additional
        identifiers provided.

        :param identifiers: a list of strings to append to the key
        :return: a string key identifying this resource with
         each identifier appended with a '/' delimiter.
        /c                 S  s   g | ]}t |qS r   )r   ).0kr   r   r   
<listcomp>r   s     z)RateLimitItem.key_for.<locals>.<listcomp>)joinr   r5   r6   r,   r   r4   )r8   r@   	remainderr   r   r   key_forh   s    		zRateLimitItem.key_forobject)otherr   c                 C  s2   t |tr.| j|jko,| j|jko,| j|jkS dS )NF)r   r/   r5   r,   r6   r8   rI   r   r   r   __eq__|   s    


zRateLimitItem.__eq__c                 C  s   | j  d| j d| jj S )Nz per r   )r5   r6   r,   r   r>   r   r   r   __repr__   s    zRateLimitItem.__repr__c                 C  s   | j j|j jk S N)r,   r   rJ   r   r   r   __lt__   s    zRateLimitItem.__lt__c                 C  s   t | j| j| j| jfS rM   )hashr4   r5   r6   r,   r>   r   r   r   __hash__   s    zRateLimitItem.__hash__N)r%   r7   )r   r   r   __doc__r+   r   r9   classmethodr=   r?   rG   rK   rL   rN   rP   r   r   r   r   r/   ;   s   


   
	r/   )	metaclassc                   @  s   e Zd ZdZed ZdS )RateLimitItemPerYearz)
    per year rate limited resource.
    r!   Nr   r   r   rQ   
TIME_TYPESr,   r   r   r   r   rT      s   rT   c                   @  s   e Zd ZdZed ZdS )RateLimitItemPerMonthz*
    per month rate limited resource.
    r    NrU   r   r   r   r   rW      s   rW   c                   @  s   e Zd ZdZed ZdS )RateLimitItemPerDayz(
    per day rate limited resource.
    r   NrU   r   r   r   r   rX      s   rX   c                   @  s   e Zd ZdZed ZdS )RateLimitItemPerHourz)
    per hour rate limited resource.
    r"   NrU   r   r   r   r   rY      s   rY   c                   @  s   e Zd ZdZed ZdS )RateLimitItemPerMinutez+
    per minute rate limited resource.
    r$   NrU   r   r   r   r   rZ      s   rZ   c                   @  s   e Zd ZdZed ZdS )RateLimitItemPerSecondz+
    per second rate limited resource.
    r&   NrU   r   r   r   r   r[      s   r[   N)rQ   
__future__r   	functoolsr   typingr   r   r   r   r	   r
   r   Zlimits.typingr   r   r   r   dictrV   r'   r   typer(   r/   rT   rW   rX   rY   rZ   r[   r   r   r   r   <module>   s0   $	S					