a
    YÒOcÒJ  ã                   @   s†  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m	Z	m
Z
mZ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 d dlmZmZ d dlmZ d	d
lmZ z,d dlZe  ¡ Z!ee"e"f edœdd„Z#W n4 e$�y   d dl%Z%ee"e"f edœdd„Z#Y n0 G dd„ deƒZ&edd„ ƒZ'G dd„ deƒZ(ee"e(f Z)ee"e*df Z+G dd„ deƒZ,G dd„ deƒZ-G dd„ deƒZ.e.ƒ Z/dS )é    N)Úcontextmanager)ÚNullTranslations)ÚAnyÚCallableÚDictÚIterableÚListÚ
NamedTupleÚOptionalÚSetÚTupleÚUnionÚcastÚoverload)Ú	force_str)Ú_transÚoverride)ÚLiteralÚ	TypedDict)Úsettingsé   )ÚCountriesBase)ÚitemÚreturnc                 C   s   t  | d ¡S ©Nr   )ÚcollatorÚsort_key©r   © r   úP/var/www/html/gsl.tools/lib/python3.9/site-packages/django_countries/__init__.pyr   #   s    r   c                 C   s   t  d| d ¡ dd¡ d¡S )NZNFKDr   ÚasciiÚignore)ÚunicodedataÚ	normalizeÚencodeÚdecoder   r   r   r   r   +   s    ÿþÿc                   @   s   e Zd Zeedœdd„ZdS )ÚEmptyFallbackTranslator)Úmessager   c                 C   s   dS )NÚ r   )Úselfr'   r   r   r   Úgettext4   s    zEmptyFallbackTranslator.gettextN)Ú__name__Ú
__module__Ú__qualname__Ústrr*   r   r   r   r   r&   3   s   r&   c                  c   sD   t jsd V  d S t ¡ } | j}tƒ | _zd V  W || _n|| _0 d S ©N)r   ZUSE_I18Nr   ÚcatalogÚ	_fallbackr&   )r0   Zoriginal_fallbackr   r   r   Úno_translation_fallback8   s    r2   c                   @   s:   e Zd ZU eed< ee ed< eed< eed< eed< dS )ÚComplexCountryNameÚnameÚnamesÚalpha3ÚnumericÚioc_codeN)r+   r,   r-   r.   Ú__annotations__r   Úintr   r   r   r   r3   F   s
   
r3   c                   @   s"   e Zd ZU eed< ee ed< dS )ÚAltCodesr6   r7   N)r+   r,   r-   r.   r9   r
   r:   r   r   r   r   r;   R   s   
r;   c                   @   s,   e Zd ZU eed< eed< edœdd„ZdS )ÚCountryTupleÚcoder4   ©r   c                 C   s   d| j ›d| j›d�S )z…
        Display the repr as a standard tuple for better backwards
        compatibility with outputting this in a template.
        ú(z, ú)©r=   r4   ©r)   r   r   r   Ú__repr__[   s    zCountryTuple.__repr__N)r+   r,   r-   r.   r9   rC   r   r   r   r   r<   W   s   
r<   c                	   @   s(  e Zd ZU dZeeef ed< eeef ed< edœdd„Z	e
eeef dœdd	„ƒZejd
d	„ ƒZe
eeef dœdd„ƒZe
eeef dœdd„ƒZe
dd„ ƒZd;eee dœdd„Zd<eee dœdd„Zdd„ Zeedœdd„Zeedœdd„Zedd d!d"œeed eeed#œd$d%„ƒZed d!d&œeed! eeee d#œd'd%„ƒZdd d!d"œeeeeeeee f d#œd(d%„Zeedœd)d*„Zed=eeedf ed ee d+œd,d-„ƒZ eeeedf ed! ee d+œd.d-„ƒZ d>eeedf ed/œd0d-„Z eedœd1d2„Z!d3d4„ Z"d5d6„ Z#d7d8„ Z$d9d:„ Z%dS )?Ú	Countriesz½
    An object containing a list of ISO3166-1 countries.

    Iterating this object will return the countries as namedtuples (of
    the country ``code`` and ``name``), sorted by name.
    Ú
_countriesÚ
_alt_codes)Úoptionc                 C   s,   t | |dƒ}|dur|S t td| ¡ › �ƒS )z†
        Get a configuration option, trying the options attribute first and
        falling back to a Django project setting.
        NZ
COUNTRIES_)Úgetattrr   Úupper)r)   rG   Úvaluer   r   r   Ú
get_optionn   s    zCountries.get_optionr>   c                 C   s  t | dƒ�s|  d¡}d}|rDt|tƒsD|D ]}t|tƒr,d} qDq,i | _|r`|r`t|ƒ| _�ndddlm} t|ƒ}|r¶i | _|D ]0}t|tƒr || | j|< q‚|\}}|| j|< q‚n
| 	¡ | _|  d¡rò| j
 ¡ D ]\}}	|| jv rÔ|	| j|< qÔ|  d¡}
|
�r@tttttd	f f | j 	¡ ƒ}| |
¡ d
d„ | ¡ D ƒ| _|  d¡�r€| j
D ],}|| jv �rR||
v�rR|| g| j|< �qR| j ¡ D ]8\}}|| jv �rŠ||
v�rŠ| j |g ¡}| |¡ �qŠg | _|  d¡�pØg }|D ](}|  |¡}|| jv �rÞ| j |¡ �qÞ| jS )z¬
        Return the a dictionary of countries, modified by any overriding
        options.

        The result is cached so future lookups are less work intensive.
        rE   ÚonlyTFr   )Ú	COUNTRIESZcommon_namesr   Nc                 S   s   i | ]\}}|d ur||“qS r/   r   )Ú.0r=   r4   r   r   r   Ú
<dictcomp>©   s   þz'Countries.countries.<locals>.<dictcomp>Úfirst)ÚhasattrrK   Ú
isinstanceÚdictr.   Ú_shadowed_namesrE   Údjango_countries.datarM   ÚcopyZCOMMON_NAMESÚitemsr   r   r   ÚCountryNameÚupdateZ	OLD_NAMESÚ
setdefaultÚextendÚcountries_firstÚalpha2Úappend)r)   rL   Zonly_choicesr   rM   Zcountries_dictÚkeyrJ   r=   r4   r   rE   r5   Zcountry_shadowedrP   r   r   r   Ú	countriesx   sh    






ÿÿ
þ

zCountries.countriesc                 C   s<   t | dƒr| `t | dƒr| `t | dƒr*| `t | dƒr8| `dS )z·
        Reset the countries cache in case for some crazy reason the settings or
        internal options change. But surely no one is crazy enough to do that,
        right?
        rE   rF   Ú
_ioc_codesrT   N)rQ   rE   rF   ra   rT   rB   r   r   r   r`   À   s    



c                 C   sª   t | dƒs¤ddlm} || _d}| j ¡ D ]x\}}t|tƒr*d|v sLd|v r*|s`| j ¡ | _d}| j 	|d¡\}}d|v r‚|d }d|v r’|d }t
||ƒ| j|< q*| jS )	NrF   r   )Ú	ALT_CODESFr6   r7   T)r(   N)rQ   rU   rb   rF   r`   rW   rR   rS   rV   Úgetr;   )r)   rb   Úalteredr=   Úcountryr6   r7   r   r   r   Ú	alt_codesÐ   s(    

ÿÿzCountries.alt_codesc                 C   sn   t | dƒshddlm} || _d}| j ¡ D ]<\}}t|tƒr*d|v r*|sX| j ¡ | _d}|d | j|< q*| jS )Nra   r   )Ú
ISO_TO_IOCFr8   T)	rQ   Zdjango_countries.ioc_datarg   ra   r`   rW   rR   rS   rV   )r)   rg   rd   r=   re   r   r   r   Ú	ioc_codesç   s    
zCountries.ioc_codesc                 C   s   t | ddƒs| j | jS )NrT   F)rH   r`   rT   rB   r   r   r   Úshadowed_namesö   s    zCountries.shadowed_namesN)r=   Úignore_firstc                 c   sl   | j | }t|tƒr2d|v r&|d }q8|d g}n|g}|rP||v rP|dd… }|D ]}|  ||¡V  qTdS )zA
        Return translated countries for a country code.
        r5   r4   r   N)r`   rR   rS   Útranslate_pair)r)   r=   rj   re   r5   r4   r   r   r   Útranslate_codeý   s    


zCountries.translate_coderA   c                 C   sÒ   |du r| j | }t|tƒrPd|v rB|d dd… }|d d }q^g }|d }n| j |g ¡}|rÀtƒ �: t|ƒ}|s”|D ]}t|ƒ}|rz|} q”qzW d  ƒ n1 s¨0    Y  |sÈt|ƒ}nt|ƒ}t||ƒS )z•
        Force a country to the current activated translation.

        :returns: ``CountryTuple(code, translated_country_name)`` namedtuple
        Nr5   r   r   r4   )r`   rR   rS   ri   rc   r2   r   r<   )r)   r=   r4   Zfallback_namesZcountry_nameZfallback_namer   r   r   rk     s,    


$
zCountries.translate_pairc                 #   s¨   ˆj }‡fdd„ˆjD ƒ}ˆ d¡r0t|td�}|E dH  ˆjr^ˆ d¡}|r^tdt|ƒƒV  ˆ d¡rldnˆj‰ ttj	 
‡ ‡fd	d„|D ƒ¡ƒ}t|td�E dH  dS )
aƒ  
        Iterate through countries, sorted by name.

        Each country record consists of a namedtuple of the two letter
        ISO3166-1 country ``code`` and short ``name``.

        The sorting happens based on the thread's current translation.

        Countries that are in ``settings.COUNTRIES_FIRST`` will be
        displayed before any sorted countries (in the order provided),
        and are only repeated in the sorted list if
        ``settings.COUNTRIES_FIRST_REPEAT`` is ``True``.

        The first countries can be separated from the sorted list by the
        value provided in ``settings.COUNTRIES_FIRST_BREAK``.
        c                 3   s   | ]}ˆ   |¡V  qd S r/   )rk   ©rN   r=   rB   r   r   Ú	<genexpr>F  ó    z%Countries.__iter__.<locals>.<genexpr>Z
first_sort)r_   NÚfirst_breakr(   Zfirst_repeatc                 3   s   | ]}ˆ  |ˆ ¡V  qd S r/   )rl   rm   ©rj   r)   r   r   rn   U  s   )r`   r\   rK   Úsortedr   r<   r   ÚtupleÚ	itertoolsÚchainÚfrom_iterable)r)   r`   r\   rp   r   rq   r   Ú__iter__1  s     


ÿÿzCountries.__iter__)r=   r   c                    sŠ   t |ƒ ¡ }| ¡ r*t|ƒ‰‡fdd„}n"t|ƒdkrH|‰ ‡ fdd„}nd}|rxd}| j ¡ D ]\}}||ƒr^|} qxq^|| jv r†|S dS )af  
        Return the normalized country code when passed any type of ISO 3166-1
        country code.

        Overridden countries objects may actually have country codes that are
        not two characters (for example, "GB-WLS"), so the returned length of
        the code is not guaranteed.

        If no match is found, returns an empty string.
        c                    s   | d ˆ kS r   r   ©rf   )Úlookup_numericr   r   Úfindm  s    zCountries.alpha2.<locals>.findé   c                    s   | d ˆ kS )Nr   r   rx   )Úlookup_alpha3r   r   rz   s  s    Nr(   )r   rI   Úisdigitr:   Úlenrf   rW   r`   )r)   r=   Zcode_strrz   r]   rf   r   )r|   ry   r   r]   ]  s"    
zCountries.alpha2c                 C   s&   |   |¡}|| jvrdS |  |¡d S )zz
        Return the name of a country, based on the code.

        If no match is found, returns an empty string.
        r(   r   )r]   r`   rk   ©r)   r=   r]   r   r   r   r4   ‚  s    

zCountries.nameFÚenT)ÚregexÚlanguageÚinsensitive)re   r�   r‚   rƒ   r   c                C   s   d S r/   r   ©r)   re   r�   r‚   rƒ   r   r   r   Úby_name�  s    	zCountries.by_name)r‚   rƒ   c                C   s   d S r/   r   r„   r   r   r   r…   ˜  s    	c                C   sh  t ƒ }|rt ||otj¡}n|r*| ¡ }t|ƒ�� | j ¡ D ]ø\}}t|t	ƒrpd|v rd|d }	qv|d g}	n|g}	|	D ]P}
|rœ| 
t|
ƒ¡rÊ| |¡ qz|r¨|
 ¡ }
||
krz|    W  d  ƒ S qz|| jv r@| j| D ]V}|�r| 
t|ƒ¡�r6| |¡ qà|�r| ¡ }||krà|    W  d  ƒ S qàq@W d  ƒ n1 �sP0    Y  |�rd|S dS )a	  
        Fetch a country's ISO3166-1 two letter country code from its name.

        An optional language parameter is also available. Warning: This depends
        on the quality of the available translations.

        If no match is found, returns an empty string.

        If ``regex`` is set to True, then rather than returning a string
        containing the matching country code or an empty stirng, a set of
        matching country codes is returned.

        If ``insensitive`` is set to False (True by default), then the search
        will be case sensitive.

        ..warning:: Be cautious about relying on this returning a country code
            (especially with any hard-coded string) since the ISO names of
            countries may change over time.
        r5   r4   Nr(   )ÚsetÚreÚcompileÚ
IGNORECASEÚlowerr   r`   rW   rR   rS   Úsearchr.   Úaddri   )r)   re   r�   r‚   rƒ   Z	code_listÚre_matchr=   Zcheck_countryZcheck_namesr4   Zshadowed_namer   r   r   r…   £  s@    


>c                 C   s<   |   |¡}z| j| d }W n ty2   d}Y n0 |p:dS )z¥
        Return the ISO 3166-1 three letter country code matching the provided
        country code.

        If no match is found, returns an empty string.
        r   r(   ©r]   rf   ÚKeyError)r)   r=   r]   r6   r   r   r   r6   ã  s    

zCountries.alpha3)r=   Úpaddedr   c                 C   s   d S r/   r   ©r)   r=   r�   r   r   r   r7   ñ  s    zCountries.numericc                 C   s   d S r/   r   r‘   r   r   r   r7   ÷  s    )r=   r�   c                 C   sP   |   |¡}z| j| d }W n ty2   d}Y n0 |du r@dS |rLd| S |S )a  
        Return the ISO 3166-1 numeric country code matching the provided
        country code.

        If no match is found, returns ``None``.

        :param padded: Pass ``True`` to return a 0-padded three character
            string, otherwise an integer will be returned.
        r   Nz%03drŽ   )r)   r=   r�   r]   Únumr   r   r   r7   ý  s    


c                 C   s   |   |¡}| j |d¡S )z½
        Return the International Olympic Committee three letter code matching
        the provided ISO 3166-1 country code.

        If no match is found, returns an empty string.
        r(   )r]   rh   rc   r   r   r   r   r8     s    
zCountries.ioc_codec                 C   s4   t | jƒ}|t | jƒ7 }| jr0|  d¡r0|d7 }|S )z¢
        len() used by several third party applications to calculate the length
        of choices. This will solve a bug related to generating fixtures.
        rp   r   )r~   r`   r\   rK   )r)   Úcountr   r   r   Ú__len__  s
    
zCountries.__len__c                 C   s
   t | jƒS r/   )Úboolr`   rB   r   r   r   Ú__bool__(  s    zCountries.__bool__c                 C   s
   || j v S )zH
        Check to see if the countries contains the given code.
        )r`   )r)   r=   r   r   r   Ú__contains__+  s    zCountries.__contains__c              
   C   sR   zt t |  ¡ ||d ¡ƒW S  tyL   tt |  ¡ |j|j|j¡ƒ Y S 0 dS )zn
        Some applications expect to be able to access members of the field
        choices by index.
        r   N)	Únextrt   Úislicerw   Ú	TypeErrorÚlistÚstartÚstopÚstep)r)   Úindexr   r   r   Ú__getitem__1  s    ÿzCountries.__getitem__)N)N)F)F)&r+   r,   r-   Ú__doc__r   r.   rX   r9   r;   rK   Úpropertyr`   Údeleterrf   rh   ri   r   rl   r
   rk   rw   ÚCountryCoder]   r4   r   r   r•   r…   r   r   r6   r:   r7   r8   r”   r–   r—   r    r   r   r   r   rD   c   s†   

G

#,%úù
úùúù@ ÿþþ
rD   )0rt   r‡   Ú
contextlibr   r*   r   Útypingr   r   r   r   r   r	   r
   r   r   r   r   r   Zdjango.utils.encodingr   Zdjango.utils.translationr   r   Ztyping_extensionsr   r   Zdjango_countries.confr   Úbaser   ZpyucaZCollatorr   r.   r   ÚImportErrorr"   r&   r2   r3   rX   r:   r¤   r;   r<   rD   r`   r   r   r   r   Ú<module>   s:   8
   ^