o
    MfMS                     @   s  d Z ddlZddlZddl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 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZmZ i ae	 Zda dZ!dZ"edej#Z$edej%Z&edZ'eedd Z(G dd dZ)G dd dej*Z+dd Z,dd Z-dd Z.dd  Z/d!d" Z0d#d$ Z1d%d& Z2d'd( Zd)d* Z3d+d, Z4d-d. Z5d/d0 Z6d1d2 Z7d3d4 Z8ej9d5d6d7d8 Z:e9 d9d: Z;ej9d5d6dFd<d=Z<dFd>d?Z=dFd@dAZ>ej9d5d6dBdC Z?dDdE Z@dS )GzTranslation helper functions.    N)Local)apps)settings)	LANG_INFO)AppRegistryNotReady)setting_changed)receiver)_lazy_re_compile)SafeData	mark_safe   )to_language	to_localei  a  
        ([A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*|\*)      # "en", "en-au", "x-y-z", "es-419", "*"
        (?:\s*;\s*q=(0(?:\.\d{,3})?|1(?:\.0{,3})?))?  # Optional "q=1.00", "q=0.8"
        (?:\s*,\s*|$)                                 # Multiple accepts per header.
        z3^[a-z]{1,8}(?:-[a-z0-9]{1,8})*(?:@[a-z0-9]{1,20})?$z^/(\w+([@-]\w+)?)(/|$)c                  K   s,   | d dv rt   t  t  dS dS )zy
    Reset global state when LANGUAGES setting has been changed, as some
    languages should no longer be accepted.
    setting)	LANGUAGESLANGUAGE_CODEN)check_for_languagecache_clearget_languagesget_supported_language_variant)kwargs r   e/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/django/utils/translation/trans_real.pyreset_cache6   s
   r   c                   @   s\   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dddZdd ZdS )TranslationCatalogz
    Simulate a dict for DjangoTranslation._catalog so as multiple catalogs
    with different plural equations are kept separate.
    Nc                 C   s:   |r|j  gni g| _|r|jg| _d S dd g| _d S )Nc                 S      t | dkS Nr   intnr   r   r   <lambda>I       z-TranslationCatalog.__init__.<locals>.<lambda>)_catalogcopy	_catalogsplural_plurals)selftransr   r   r   __init__G   s   "zTranslationCatalog.__init__c              	   C   s4   | j D ]}z|| W   S  ty   Y qw t|N)r&   KeyError)r)   keycatr   r   r   __getitem__K   s   
zTranslationCatalog.__getitem__c                 C   s   || j d |< d S Nr   )r&   )r)   r.   valuer   r   r   __setitem__S   s   zTranslationCatalog.__setitem__c                    s   t  fdd| jD S )Nc                 3   s    | ]} |v V  qd S r,   r   ).0r/   r.   r   r   	<genexpr>W   s    z2TranslationCatalog.__contains__.<locals>.<genexpr>)anyr&   )r)   r.   r   r5   r   __contains__V   s   zTranslationCatalog.__contains__c                 c        | j D ]	}| E d H  qd S r,   )r&   itemsr)   r/   r   r   r   r:   Y      
zTranslationCatalog.itemsc                 c   r9   r,   )r&   keysr;   r   r   r   r=   ]   r<   zTranslationCatalog.keysc                 C   s`   t | j| jD ]\}}|jj|jkr||j  d S q| jd|j  | jd|j d S r1   )	zipr&   r(   r'   __code__updater$   insertr%   )r)   r*   r/   r'   r   r   r   r@   a   s   zTranslationCatalog.updatec                 C   s2   t  }| jD ]}|||}||ur|  S q|S r,   )objectr&   get)r)   r.   defaultmissingr/   resultr   r   r   rC   k   s   
zTranslationCatalog.getc                 C   s>   t | j| jD ]\}}||||f}|d ur|  S qtr,   )r>   r&   r(   rC   r-   )r)   msgidnumr/   r'   tmsgr   r   r   r'   s   s   zTranslationCatalog.pluralr,   )__name__
__module____qualname____doc__r+   r0   r3   r8   r:   r=   r@   rC   r'   r   r   r   r   r   B   s    


r   c                   @   sr   e Zd ZdZdZdddZdd Zdd	d
Zdd Zdd Z	dd Z
dddZdd Zdd Zdd Zdd ZdS )DjangoTranslationa]  
    Set up the GNUTranslations context with regard to output charset.

    This translation object will be constructed out of multiple GNUTranslations
    objects by merging their catalogs. It will construct an object for the
    requested language and add a fallback to the default language, if it's
    different from the requested language.
    djangoNc                 C   s   t j|  |dur|| _|| _t|| _t|| _d| _	dd | _
| jdkr7|dur3tdt d}|   |rI|D ]}| |}| | q;n|   |   | jtjkrh| jdkrh| j	du rhtdtj | | | j	du rxt | _	dS dS )z8Create a GNUTranslations() using many locale directoriesNc                 S   r   r   r   r    r   r   r   r"      r#   z,DjangoTranslation.__init__.<locals>.<lambda>rO   z.localedirs is ignored when domain is 'django'.z3No translation files found for default language %s.)gettext_moduleGNUTranslationsr+   domain_DjangoTranslation__languager   _DjangoTranslation__to_languager   _DjangoTranslation__localer$   r'   warningswarnRuntimeWarning_init_translation_catalog_new_gnu_transmerge _add_installed_apps_translations_add_local_translationsr   r   OSError_add_fallbackr   )r)   languagerR   
localedirs	localedirtranslationr   r   r   r+      s4   




 

zDjangoTranslation.__init__c                 C   s
   d| j  S )Nz<DjangoTranslation lang:%s>rS   r)   r   r   r   __repr__   s   
zDjangoTranslation.__repr__Tc                 C   s   t j| j|| jg|dS )z
        Return a mergeable gettext.GNUTranslations instance.

        A convenience wrapper. By default gettext uses 'fallback=False'.
        Using param `use_null_fallback` to avoid confusion with any other
        references to 'fallback'.
        )rR   rb   	languagesfallback)rP   rc   rR   rU   )r)   rb   use_null_fallbackr   r   r   rZ      s   z DjangoTranslation._new_gnu_transc                 C   s<   t jtj j}tjtj|d}| 	|}| 
| dS )z7Create a base catalog using global django translations.localeN)sysmodulesr   rK   __file__ospathjoindirnamerZ   r[   )r)   settingsfilerb   rc   r   r   r   rY      s   
z+DjangoTranslation._init_translation_catalogc                 C   sj   z
t tt }W n ty   tdw |D ]}tj|jd}tj|r2| 	|}| 
| qdS )z+Merge translations from each installed app.zThe translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.rj   N)reversedlistr   get_app_configsr   rn   ro   rp   existsrZ   r[   )r)   app_configs
app_configrb   rc   r   r   r   r\      s   

z2DjangoTranslation._add_installed_apps_translationsc                 C   s(   t tjD ]}| |}| | qdS )z+Merge translations defined in LOCALE_PATHS.N)rs   r   LOCALE_PATHSrZ   r[   )r)   rb   rc   r   r   r   r]      s   
z)DjangoTranslation._add_local_translationsc                 C   sR   | j tjks| j drdS | jdkrttj}n	ttj| j|d}| | dS )z=Set the GNUTranslations() fallback with the default language.enNrO   )rR   ra   )rS   r   r   
startswithrR   rc   rN   add_fallback)r)   ra   default_translationr   r   r   r_      s   

zDjangoTranslation._add_fallbackc                 C   s`   t |ddsdS | jdu r|j| _|j | _t|| _n| j| |jr.| |j dS dS )z,Merge another translation into this catalog.r$   N)	getattrr$   r'   _infor%   r   r@   	_fallbackr|   )r)   otherr   r   r   r[      s   
zDjangoTranslation.mergec                 C      | j S )z Return the translation language.rd   re   r   r   r   r`         zDjangoTranslation.languagec                 C   r   )z%Return the translation language name.)rT   re   r   r   r   r      r   zDjangoTranslation.to_languagec                 C   sZ   z
| j ||}W |S  ty,   | jr| j||| Y S |dkr'|}Y |S |}Y |S w r   )r$   r'   r-   r   ngettext)r)   msgid1msgid2r!   rI   r   r   r   r      s   zDjangoTranslation.ngettext)NN)Tr,   )rJ   rK   rL   rM   rR   r+   rf   rZ   rY   r\   r]   r_   r[   r`   r   r   r   r   r   r   rN   {   s    
%

rN   c                 C   s   | t vr
t| t | < t |  S )zE
    Return a translation object in the default 'django' domain.
    )_translationsrN   r`   r   r   r   rc     s   rc   c                 C   s   | sdS t | t_dS )z
    Fetch the translation object for a given language and install it as the
    current translation object for the current thread.
    N)rc   _activer2   r   r   r   r   activate  s   r   c                   C   s   t tdr	t`dS dS )zz
    Uninstall the active translation object so that further _() calls resolve
    to the default translation object.
    r2   N)hasattrr   r2   r   r   r   r   
deactivate  s   
r   c                   C   s   t  t_dd tj_dS )z
    Make the active translation object a NullTranslations() instance. This is
    useful when we want delayed translations to appear as the original string
    for some reason.
    c                  W   s   d S r,   r   )argsr   r   r   r"   /  s    z deactivate_all.<locals>.<lambda>N)rP   NullTranslationsr   r2   r   r   r   r   r   deactivate_all(  s   
r   c                  C   s<   t tdd} | durz|  W S  ty   Y tjS w tjS )z'Return the currently selected language.r2   N)r~   r   r   AttributeErrorr   r   tr   r   r   get_language2  s   
r   c                  C   s,   t  } | du r	dS t  dd }|tjv S )zw
    Return selected language's BiDi layout.

    * False = left-to-right layout
    * True = right-to-left layout
    NF-r   )r   splitr   LANGUAGES_BIDI)lang	base_langr   r   r   get_language_bidi>  s
   
r   c                  C   s.   t tdd} | dur| S tdu rttjatS )z
    Return the current active catalog for further processing.
    This can be used if you need to modify the catalog or want to access the
    whole message catalog instead of just translating one string.
    r2   Nr~   r   _defaultrc   r   r   r   r   r   r   catalogM  s   
r   c                 C   s`   |  dd dd}|rtpttjattdt}||}nt| d}t	| t
r.t|S |S )z
    Translate the 'message' string. It uses the current thread to find the
    translation object to use. If no current translation is activated, the
    message will be run through the default translation object.
    z

r2    )replacer   rc   r   r   r~   r   gettexttype
isinstancer
   r   )messageeol_messagetranslation_objectrF   r   r   r   r   ]  s   
r   c                 C   s<   d| t |f }t|}t |v r|}|S t|trt|}|S Nz%s%s%s)CONTEXT_SEPARATORr   r   r
   r   )contextr   msg_with_ctxtrF   r   r   r   pgettextw  s   
r   c                 C   s   | S )z
    Mark strings for translation but don't translate them now. This can be
    used to store strings in global variables that should stay in the base
    language (because they might be used externally) and will be translated
    later.
    r   )r   r   r   r   gettext_noop  s   r   c                 C   sJ   t tdd }|d urt ||| ||S td u rttjat t|| ||S )Nr2   r   )singularr'   numbertranslation_functionr   r   r   r   do_ntranslate  s   
r   c                 C   s   t | ||dS )zg
    Return a string of the translation of either the singular or plural,
    based on the number.
    r   )r   )r   r'   r   r   r   r   r     s   r   c                 C   s>   d| t |f d| t |f |f}t| }t |v rt|||}|S r   )r   r   )r   r   r'   r   msgs_with_ctxtrF   r   r   r   	npgettext  s   r   c                  C   sh   t jt jtjtj jd} g }t	
 D ]}t j|jd}t j|r+|| q| gtj|S )zB
    Return a list of paths to user-provides languages files.
    rj   )rn   ro   rp   rq   rk   rl   r   rK   rm   r   ru   rv   appendry   )
globalpath	app_pathsrx   locale_pathr   r   r   all_locale_paths  s   
r   i  )maxsizec                    s.    du s	t  sdS t fddt D S )a  
    Check whether there is a global language file for the given language
    code. This is used to decide whether a user-provided language is
    available.

    lru_cache should have a maxsize to prevent from memory exhaustion attacks,
    as the provided language codes are taken from the HTTP request. See also
    <https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
    NFc                 3   s(    | ]}t d |t gduV  qdS )rO   N)rP   findr   )r4   ro   	lang_coder   r   r6     s
    
z%check_for_language.<locals>.<genexpr>)language_code_researchr7   r   r   r   r   r   r     s
   r   c                   C   s
   t tjS )zN
    Cache of settings.LANGUAGES in a dictionary for easy lookups by key.
    )dictr   r   r   r   r   r   r     s   
r   Fc                 C   s   | rL| g}z| t|  d  W n	 ty   Y nw | dd }|| t }|D ]}||v r9t|r9|  S q+|sL|D ]}||d rK|  S q>t| )a  
    Return the language code that's listed in supported languages, possibly
    selecting a more generic variant. Raise LookupError if nothing is found.

    If `strict` is False (the default), look for a country-specific variant
    when neither the language code nor its generic variant is found.

    lru_cache should have a maxsize to prevent from memory exhaustion attacks,
    as the provided language codes are taken from the HTTP request. See also
    <https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
    rh   r   r   )	extendr   r-   r   r   r   r   r{   LookupError)r   strictpossible_lang_codesgeneric_lang_codesupported_lang_codescodesupported_coder   r   r   r     s(   
r   c                 C   s>   t | }|s	dS |d }zt||dW S  ty   Y dS w )z
    Return the language code if there's a valid language code found in `path`.

    If `strict` is False (the default), look for a country-specific variant
    when neither the language code nor its generic variant is found.
    Nr   )r   )language_code_prefix_rematchr   r   )ro   r   regex_matchr   r   r   r   get_language_from_path  s   
r   c              	   C   s   |rt | j}|dur|S | jtj}|dur#|t v r#t|r#|S zt|W S  t	y1   Y nw | j
dd}t|D ] \}}|dkrG nt|sMq=zt|W   S  t	y]   Y q=w zttjW S  t	yp   tj Y S w )a  
    Analyze the request to find what language the user wants the system to
    show. Only languages listed in settings.LANGUAGES are taken into account.
    If the user requests a sublanguage where we have a main language, we send
    out the main language.

    If check_path is True, the URL path prefix will be checked for a language
    code, otherwise this is skipped for backwards compatibility.
    NHTTP_ACCEPT_LANGUAGEr   *)r   	path_infoCOOKIESrC   r   LANGUAGE_COOKIE_NAMEr   r   r   r   METAparse_accept_lang_headerr   r   r   )request
check_pathr   acceptaccept_langunusedr   r   r   get_language_from_request  s8   




r   c                 C   s   g }t |  }|d rdS tdt|d dD ]"}|||d  \}}}|r+ dS |r2t|}nd}|||f q|jdd d	d
 t|S )z
    Parse the lang_string, which is the body of an HTTP Accept-Language
    header, and return a tuple of (lang, q-value), ordered by 'q' values.

    Return an empty tuple if there are any format errors in lang_string.
    r   r   r      g      ?c                 S   s   | d S r   r   )kr   r   r   r"   H  s    z+_parse_accept_lang_header.<locals>.<lambda>T)r.   reverse)	accept_language_rer   lowerrangelenfloatr   sorttuple)lang_stringrF   piecesifirstr   priorityr   r   r   _parse_accept_lang_header3  s   
r   c                 C   s>   t | tkr
t| S | ddt}|dkrt| d| S dS )a  
    Parse the value of the Accept-Language header up to a maximum length.

    The value of the header is truncated to a maximum length to avoid potential
    denial of service and memory exhaustion attacks. Excessive memory could be
    used if the raw value is very large as it would be cached due to the use of
    functools.lru_cache() to avoid repetitive parsing of common header values.
    ,r   Nr   )r   !ACCEPT_LANGUAGE_HEADER_MAX_LENGTHr   rfind)r   indexr   r   r   r   L  s   
r   )F)ArM   	functoolsr   rP   rn   rerk   rV   asgiref.localr   django.appsr   django.confr   django.conf.localer   django.core.exceptionsr   django.core.signalsr   django.dispatchr   django.utils.regex_helperr	   django.utils.safestringr
   r   r   r   r   r   r   r   r   r   VERBOSEr   
IGNORECASEr   r   r   r   rQ   rN   rc   r   r   r   r   r   r   r   r   r   r   r   r   	lru_cacher   r   r   r   r   r   r   r   r   r   r   <module>   sx    
9 

	






"

+
