o
    1iiS                     @   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mZmZ d dlmZ d dlmZ d dlmZmZmZ d dlmZ d	d
lm Z  ersd dl!m"Z" G dd deZ#ee"e#f Z$ee%e&df Z'zd dl(Z(e() Z*dee%e%f defddZ+W n e,y   d dl-Z-dee%e%f defddZ+Y nw e Z.G dd deZ/edd Z0G dd deZ1G dd deZ2G dd de Z3e3 Z4dS )    N)contextmanager)NullTranslations)TYPE_CHECKINGAnyDictIterableListLiteral
NamedTupleOptionalSetTuple	TypedDictUnioncastoverload)Local)	force_str)get_languageoverride
trans_real)settings   )CountriesBase)
StrPromisec                   @   s6   e Zd ZU ded< ded< eed< eed< eed< dS )	ComplexCountryNamer   namezList[StrPromise]namesalpha3numericioc_codeN)__name__
__module____qualname____annotations__strint r'   r'   Z/var/www/html/humari/django-venv/lib/python3.10/site-packages/django_countries/__init__.pyr   "   s   
 r   itemreturnc                 C   s   t | d S )Nr   )collatorsort_keyr)   r'   r'   r(   r,   6   s   r,   c                 C   s   t d| d dddS )NNFKDr   asciiignore)unicodedata	normalizeencodedecoder-   r'   r'   r(   r,   >   s   c                       s&   e Zd Zdedef fddZ  ZS )EmptyFallbackTranslatormessager*   c                    s   t tddsdS t |S )NfallbackT )getattr_translation_statesupergettext)selfr6   	__class__r'   r(   r<   J   s   zEmptyFallbackTranslator.gettext)r!   r"   r#   r%   r<   __classcell__r'   r'   r>   r(   r5   I   s    r5   c                  c   s^    t js	d V  d S t } | j}t|tst }||_|| _dt_z	d V  W dt_d S dt_w )NFT)	r   USE_I18Nr   catalog	_fallback
isinstancer5   r:   r7   )rB   original_fallbackempty_fallback_translatorr'   r'   r(   no_translation_fallbackQ   s   
rG   c                   @   s"   e Zd ZU eed< ee ed< dS )AltCodesr   r   N)r!   r"   r#   r%   r$   r   r&   r'   r'   r'   r(   rH   e   s   
 rH   c                   @   s,   e Zd ZU eed< eed< defddZdS )CountryTuplecoder   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, ))rJ   r   r=   r'   r'   r(   __repr__n   s   zCountryTuple.__repr__N)r!   r"   r#   r%   r$   rN   r'   r'   r'   r(   rI   j   s   
 rI   c                   @   sR  e Zd ZU dZeedf ed< eeef ed< defddZe	deedf fd	d
Z
e
jdd
 Z
e	deeef fddZe	deeef fddZe	dd ZdAdedeee  fddZdAdeded fddZdd Zdd Zdddefdd Zdddefd!d"Zed#d$d%d&d'ed(ed# d)ed*edef
d+d,Zed$d%d-d'ed(ed% d)ed*edee f
d.d,Zd#d$d%d&d'ed(ed)ed*edeeee f f
d/d,Zdddefd0d1Zedeeedf dee fd2d3ZedBdeeedf dee fd4d3ZdCdeeedf d5efd6d3Zdddefd7d8Z d9d: Z!d;d< Z"d=d> Z#d?d@ Z$dS )D	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.
    CountryName
_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.
        N
COUNTRIES_)r9   r   upper)r=   rS   valuer'   r'   r(   
get_option   s   zCountries.get_optionr*   c                 C   s  t | ds| d}d}|r!t|ts!|D ]}t|tr d} nqi | _|r.|r.t|| _nddlm} t|}|rYi | _|D ]}t|trN|| | j|< q?|\}}|| j|< q?n|	 | _| drw| j
 D ]\}}	|| jv rv|	| j|< qh| d}
|
rtd	| j	 }||
 d
d | D | _| dr| j
D ]}|| jv r||
vr|| g| j|< q| j D ]\}}|| jv r||
vr| j|g }|| qg | _| dpg }|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.
        rQ   onlyTFr   )	COUNTRIEScommon_namesr   z#Dict[str, Union[CountryName, None]]c                 S   s   i | ]\}}|d ur||qS Nr'   ).0rJ   r   r'   r'   r(   
<dictcomp>   s
    z'Countries.countries.<locals>.<dictcomp>first)hasattrrW   rD   dictr%   _shadowed_namesrQ   django_countries.datarY   copyCOMMON_NAMESitemsr   update	OLD_NAMES
setdefaultextendcountries_firstalpha2append)r=   rX   only_choicesr)   rY   countries_dictkeyrV   rJ   r   r   rQ   r   country_shadowedr^   r'   r'   r(   	countries   sr   














zCountries.countriesc                 C   sN   t | dr| `t | dr| `t | dr| `t | dr| `t | dr%| `dS 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?
        rQ   rR   
_ioc_codesra   _iter_cacheN)r_   rQ   rR   rr   ra   rs   rM   r'   r'   r(   rq      s   




c                 C   s   t | dsRddlm} || _d}| j D ]<\}}t|trQd|v s&d|v rQ|s0| j | _d}| j	|d\}}d|v rA|d }d|v rI|d }t
||| j|< q| jS )	NrR   r   )	ALT_CODESFr   r   T)r8   N)r_   rb   rt   rR   rq   re   rD   r`   rc   getrH   )r=   rt   alteredrJ   countryr   r   r'   r'   r(   	alt_codes   s$   

zCountries.alt_codesc                 C   sn   t | ds4ddlm} || _d}| j D ]\}}t|tr3d|v r3|s,| j | _d}|d | j|< q| jS )Nrr   r   )
ISO_TO_IOCFr    T)	r_   django_countries.ioc_datary   rr   rq   re   rD   r`   rc   )r=   ry   rv   rJ   rw   r'   r'   r(   	ioc_codes   s   
zCountries.ioc_codesc                 C   s   t | dds	| j | jS )Nra   F)r9   rq   ra   rM   r'   r'   r(   shadowed_names
  s   zCountries.shadowed_namesNrJ   ignore_firstc                 c   sl    | j | }t|trd|v r|d n|d g}n|g}|r(||v r(|dd }|D ]	}| ||V  q*dS )zA
        Return translated countries for a country code.
        r   r   r   N)rq   rD   r`   translate_pair)r=   rJ   r}   rw   r   r   r'   r'   r(   translate_code  s   

zCountries.translate_coder   c                 C   s   |du r	| j | }t|tr(d|v r!|d dd }|d d }ng }|d }n| j|g }|r`t  t|}|sJ|D ]}t|}|rI|} nq=W d   n1 sTw   Y  |s_t|}nt|}t||S )z
        Force a country to the current activated translation.

        :returns: ``CountryTuple(code, translated_country_name)`` namedtuple
        Nr   r   r   r   )rq   rD   r`   r|   ru   rG   r   rI   )r=   rJ   r   fallback_namescountry_namefallback_namefallback_name_strr'   r'   r(   r~     s4   




zCountries.translate_pairc                 C   s   | S )z
        Make Countries callable to support Django 5.0+ lazy callable choices.
        Returns self to enable deferred evaluation while maintaining caching.
        r'   rM   r'   r'   r(   __call__B  s   zCountries.__call__c                 #   s    t  }tdsi _|jv rj| E dH  dS g }j}fddjD }dr5t|td}|| jrNd}|rN|	t
dt| d	rUdnj ttj fd
d|D }|t|td |j|< |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``.

        Results are cached per language to avoid expensive re-translation
        and re-sorting on every iteration (issue #454).
        rs   Nc                 3   s    | ]}  |V  qd S r[   )r~   r\   rJ   rM   r'   r(   	<genexpr>p  s    z%Countries.__iter__.<locals>.<genexpr>
first_sort)ro   first_breakr8   first_repeatc                 3   s    | ]	} | V  qd S r[   )r   r   r}   r=   r'   r(   r     s    
)r   r_   rs   rq   rj   rW   sortedr,   ri   rl   rI   r   tuple	itertoolschainfrom_iterable)r=   	cache_keyresultsrq   rj   r   countries_translatedr'   r   r(   __iter__I  s4   





zCountries.__iter__CountryCodec                 C   s   t | }|| jv r|S | rd}t|}nt|dkr#d}|}nd}d}|durAd}| j D ]\}}|| |kr@|} nq2|| jv rH|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.
        r      r   Nr8   )r   rU   rq   isdigitr&   lenrx   re   )r=   rJ   code_str
find_index
find_valuerk   rx   r'   r'   r(   rk     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.
        r8   r   )rk   rq   r~   r=   rJ   rk   r'   r'   r(   r     s   

zCountries.nameFenT)regexlanguageinsensitiverw   r   r   r   c                C      d S r[   r'   r=   rw   r   r   r   r'   r'   r(   by_name     zCountries.by_name)r   r   c                C   r   r[   r'   r   r'   r'   r(   r     r   c                C   s|  t  }|rt||otj}n|r| }t| | j D ]\}}t|t	r7d|v r1|d }	n	|d g}	n|g}	|	D ]8}
|rM|
t|
rL|| q<|rc||
 krb|    W  d   S q<||
krt|    W  d   S q<|| jv r| j| D ](}|r|
t|r|| q|r| }||kr|    W  d   S qqW d   n1 sw   Y  |r|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 string, 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.
        r   r   Nr8   )setrecompile
IGNORECASElowerr   rq   re   rD   r`   searchr%   addr|   )r=   rw   r   r   r   	code_listre_matchrJ   check_countrycheck_namesr   shadowed_namer'   r'   r(   r     sX   








c                 C   s<   |  |}z	| j| d }W n ty   d}Y nw |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   r8   rk   rx   KeyError)r=   rJ   rk   r   r'   r'   r(   r     s   
zCountries.alpha3c                 C   r   r[   r'   r=   rJ   r'   r'   r(   r   #     zCountries.numericc                 C   r   r[   r'   )r=   rJ   paddedr'   r'   r(   r   &  r   r   c                 C   sP   |  |}z	| j| d }W n ty   d}Y nw |du r dS |r&|d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   N03dr   )r=   rJ   r   rk   numr'   r'   r(   r   )  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.
        r8   )rk   r{   ru   r   r'   r'   r(   r    >  s   
zCountries.ioc_codec                 C   s4   t | j}|t | j7 }| jr| dr|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.
        r   r   )r   rq   rj   rW   )r=   countr'   r'   r(   __len__H  s
   
zCountries.__len__c                 C   s
   t | jS r[   )boolrq   rM   r'   r'   r(   __bool__T  s   
zCountries.__bool__c                 C   s
   || j v S )zH
        Check to see if the countries contains the given code.
        )rq   r   r'   r'   r(   __contains__W  s   
zCountries.__contains__c              
   C   sN   zt t|  ||d W S  ty&   tt|  |j|j|j Y S w )zn
        Some applications expect to be able to access members of the field
        choices by index.
        r   )	nextr   islicer   	TypeErrorliststartstopstep)r=   indexr'   r'   r(   __getitem__]  s   zCountries.__getitem__r[   )T)F)%r!   r"   r#   __doc__r   r%   r$   rH   rW   propertyrq   deleterrx   r{   r|   r   r   r   r~   r   r   rk   r   r   r	   r   r   r   r   r   r&   r   r    r   r   r   r   r'   r'   r'   r(   rO   v   s   
 
F

#B)	
B"$
rO   )5r   r   
contextlibr   r<   r   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   asgiref.localr   django.utils.encodingr   django.utils.translationr   r   r   django_countries.confr   baser   django_stubs_extr   r   rP   r%   r&   r   pyucaCollatorr+   r,   ImportErrorr1   r:   r5   rG   rH   rI   rO   rq   r'   r'   r'   r(   <module>   sL   @
   
w