U
    ]go                     @   s  d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddl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 dd
lmZmZ edZedejejB Zedj ej!ej!dejejB Z"G dd deZ#ej$G dd dZ%e	ree&e%f Z'neZ'G dd de'Z(G dd dZ)e&eee ee f dddZ*e&e+dddZ,e&ee dddZ-e&edd d!Z.e&ee d"d#d$Z/dS )%     N)UserDict)
ModuleType)TYPE_CHECKINGcast)Version)DictList
NamedTupleOptionalTupleTypeUnion   )ConfigurationError)GRANULARITIESRateLimitItemz[,;|]{1}zf
    \s*([0-9]+)
    \s*(/|\s*per\s*)
    \s*([0-9]+)
    *\s*(hour|minute|second|day|month|year)s?\s*z#^{SINGLE}(:?{SEPARATORS}{SINGLE})*$)ZSINGLE
SEPARATORSc                   @   s"   e Zd ZU dZeed< eed< dS )WindowStatsz1
    Tuple to describe a rate limited window
    Z
reset_time	remainingN)__name__
__module____qualname____doc__int__annotations__ r   r   //tmp/pip-unpacked-wheel-kizsipjx/limits/util.pyr   #   s   
r   c                   @   s6   e Zd ZU eed< ee ed< ee ed< eed< dS )
Dependencynameversion_requiredversion_foundmoduleN)r   r   r   strr   r   r   r   r   r   r   r   r   .   s   
r   c                       s6   e Zd ZedddedZeed fddZ  ZS )DependencyDictMissingN)keyreturnc                    s\   t  |}|tjkr&t| dn2|jrX|jr>|j|jk rXtd|j d|j d|S )Nz prerequisite not availablezThe minimum version of z of z could not be found)super__getitem__r$   r%   r   r    r!   r   )selfr&   
dependency	__class__r   r   r)   ?   s    

zDependencyDict.__getitem__)	r   r   r   r   r   r%   r#   r)   __classcell__r   r   r,   r   r$   <   s   r$   c                   @   sV   e Zd ZU dZg Zeeeee	 f e
e f ed< ddddZeedddZdS )	LazyDependencyz
    Simple utility that provides an :attr:`dependency`
    to the child class to fetch any dependencies
    without having to import them explicitly.
    DEPENDENCIESN)r'   c                 C   s   t  | _d S N)r$   _dependencies)r*   r   r   r   __init__]   s    zLazyDependency.__init__c                 C   s   t | dds|t }t| jtr0dd | jD }n| j}| D ]6\}}t|\}}|sbtj||< q>t||||||< q>|| _	| j	S )z
        Cached mapping of the modules this storage depends on.
        This is done so that the module is only imported lazily
        when the storage is instantiated.

        :meta private:
        r2   Nc                 S   s   i | ]
}|d qS r1   r   ).0r+   r   r   r   
<dictcomp>o   s      z/LazyDependency.dependencies.<locals>.<dictcomp>)
getattrr$   
isinstancer0   listitemsget_dependencyr%   r   r2   )r*   dependenciesmappingr   minimum_versionr+   versionr   r   r   r;   `   s"    
   
zLazyDependency.dependencies)r   r   r   r   r0   r   r   r#   r   r   r	   r   r3   propertyr$   r;   r   r   r   r   r/   P   s
   
$r/   )module_pathr'   c                 C   sb   zF| t jkrt|  | dd }tt j| dd}t j|  t|fW S  tk
r\   Y dS X dS )z5
    safe function to import a module at runtime
    .r   __version__z0.0.0)NNN)sysmodules
__import__splitr6   r   ImportError)r@   rootr>   r   r   r   r:      s    
r:   )pathr'   c                 C   s   t ttd|  S )Nlimits)r   bytesimportlib_resourcesfilesjoinpath
read_bytes)rI   r   r   r   get_package_data   s    rP   )limit_stringr'   c           	      C   s~   t | trt| s td|  g }t| D ]J}t|}|r.| \}}}}t	|}|
|t||rpt|prd q.|S )z
    parses rate limits in string notation containing multiple rate limits
    (e.g. ``1/second; 5/minute``)

    :param limit_string: rate limit string using :ref:`ratelimit-string`
    :raise ValueError: if the string notation is invalid.

    z%couldn't parse rate limit string '%s'N)r7   r#   EXPRmatch
ValueErrorr   rF   SINGLE_EXPRgroupsgranularity_from_stringappendr   )	rQ   rJ   limitrS   amount_Z	multiplesgranularity_stringgranularityr   r   r   
parse_many   s    

r^   c                 C   s   t t| d S )z
    parses a single rate limit in string notation
    (e.g. ``1/second`` or ``1 per second``)

    :param limit_string: rate limit string using :ref:`ratelimit-string`
    :raise ValueError: if the string notation is invalid.

    r   )r8   r^   )rQ   r   r   r   parse   s    
r_   )r\   r'   c                 C   s0   t  D ]}|| r|  S qtd|  dS )z<

    :param granularity_string:
    :raise ValueError:
    zno granularity matched for %sN)r   valuesZcheck_granularity_stringrT   )r\   r]   r   r   r   rW      s    

rW   )0r   dataclassesrerC   collectionsr   typesr   typingr   r   rL   Zpackaging.versionr   Zlimits.typingr   r	   r
   r   r   r   r   errorsr   rJ   r   r   compiler   
IGNORECASEVERBOSErU   formatpatternrR   r   	dataclassr   r#   Z	_UserDictr$   r/   r:   rK   rP   r^   r_   rW   r   r   r   r   <module>   sH   $

 
1 