B
    èfcu  ã               @   sZ   d Z ddlmZmZ dddhddhdd	d
dhgZdd„ Zdd„ Zdd„ Zdd„ Zeƒ Z	dS )zö
    babel.messages.checkers
    ~~~~~~~~~~~~~~~~~~~~~~~

    Various routines that help with validation of translations.

    :since: version 0.9

    :copyright: (c) 2013-2022 by the Babel Team.
    :license: BSD, see LICENSE for more details.
é    )ÚTranslationErrorÚPYTHON_FORMATÚiÚdÚuÚxÚXÚfÚFÚgÚGc             C   sd   |j st|jtƒstdƒ‚dS | dkr*dS |j}t|ttfƒsD|f}t|ƒ| jkr`td| j ƒ‚dS )z0Verify the number of plurals in the translation.z/Found plural forms for non-pluralizable messageNz*Wrong number of plural forms (expected %d))	ZpluralizableÚ
isinstanceÚstringÚstrr   ÚlistÚtupleÚlenÚnum_plurals)ÚcatalogÚmessageÚmsgstrs© r   ú6lib/python3.7/site-packages/babel/messages/checkers.pyr      s    r   c             C   sl   d|j krdS |j}t|ttfƒs(|f}|j}t|ttfƒsB|f}x$t||ƒD ]\}}|rNt||ƒ qNW dS )z9Verify the format string placeholders in the translation.zpython-formatN)ÚflagsÚidr   r   r   r   ÚzipÚ_validate_format)r   r   Zmsgidsr   ZmsgidZmsgstrr   r   r   Úpython_format,   s    
r   c             C   s   dd„ }dd„ }dd„ }t || |fƒ\}}t |||fƒ\}}|rR|sR|sRtdƒ‚n||krbtdƒ‚|rÈt|ƒt|ƒkr~td	ƒ‚xœtt||ƒƒD ]4\}	\\}
}\}
}|||ƒsŽtd
|	d ||f ƒ‚qŽW nTt|ƒ}xJ|D ]B\}}||krôtd| ƒ‚qÖ|||| ƒsÖtd|||| f ƒ‚qÖW dS )a‹  Test format string `alternative` against `format`.  `format` can be the
    msgid of a message and `alternative` one of the `msgstr`\s.  The two
    arguments are not interchangeable as `alternative` may contain less
    placeholders if `format` uses named placeholders.

    The behavior of this function is undefined if the string does not use
    string formattings.

    If the string formatting of `alternative` is compatible to `format` the
    function returns `None`, otherwise a `TranslationError` is raised.

    Examples for compatible format strings:

    >>> _validate_format('Hello %s!', 'Hallo %s!')
    >>> _validate_format('Hello %i!', 'Hallo %d!')

    Example for an incompatible format strings:

    >>> _validate_format('Hello %(name)s!', 'Hallo %s!')
    Traceback (most recent call last):
      ...
    TranslationError: the format strings are of different kinds

    This function is used by the `python_format` checker.

    :param format: The original format string
    :param alternative: The alternative format string that should be checked
                        against format
    :raises TranslationError: on formatting errors
    c             S   sN   g }xDt  | ¡D ]6}| ¡ \}}}|dkr4|d kr4q| |t|ƒf¡ qW |S )Nú%)r   ÚfinditerÚgroupsÚappendr   )r   ÚresultÚmatchÚnameÚformatÚtypecharr   r   r   Ú_parse\   s    z _validate_format.<locals>._parsec             S   s2   | |krdS x t D ]}| |kr||krdS qW dS )NTF)Ú_string_format_compatibilities)ÚaÚbÚsetr   r   r   Ú_compatiblee   s    
z%_validate_format.<locals>._compatiblec             S   sD   d }x6| D ].\}}|d kr$|d k}q
|d k|kr
t dƒ‚q
W t|ƒS )Nz5format string mixes positional and named placeholders)r   Úbool)ÚresultsZ
positionalr$   Úcharr   r   r   Ú_check_positionalm   s    
z+_validate_format.<locals>._check_positionalzplaceholders are incompatiblez)the format strings are of different kindsz-positional format placeholders are unbalancedzDincompatible format for placeholder %d: %r and %r are not compatibleé   zunknown named placeholder %rzDincompatible format for placeholder %r: %r and %r are not compatibleN)Úmapr   r   Ú	enumerater   Údict)r%   Zalternativer'   r,   r0   r)   r*   Za_positionalZb_positionalÚidxÚ_ÚfirstÚsecondZtype_mapr$   r&   r   r   r   r   <   s.     	
$
r   c              C   sd   g } yddl m} W n tk
r(   Y n$X x | d¡D ]}|  | ¡ ¡ q6W t| ƒdkr`ttgS | S )Nr   )Úworking_setzbabel.checkers)	Zpkg_resourcesr9   ÚImportErrorZiter_entry_pointsr!   Úloadr   r   r   )Úcheckersr9   Zentry_pointr   r   r   Ú_find_checkers›   s    r=   N)
Ú__doc__Zbabel.messages.catalogr   r   r(   r   r   r   r=   r<   r   r   r   r   Ú<module>   s   _