B
    }<[                 @   sj   d Z ddlmZm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-2018 by the Babel Team.
    :license: BSD, see LICENSE for more details.
    )TranslationErrorPYTHON_FORMAT)string_typesizip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stringr   r   listtuplelennum_plurals)catalogmessagemsgstrs r   Llib/python3.7/site-packages/Babel-2.6.0-py3.7.egg/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   r   _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|rt|t|kr~td	xtt||D ]4\}	\\}
}\}
}|||std
|	d ||f qW nTt|}xJ|D ]B\}}||krtd| q|||| std|||| f qW 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appendstr)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   _compatibleg   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)ZresultsZ
positionalr%   charr   r   r   _check_positionalo   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   Zbabel._compatr   r   r)   r   r   r   r=   r<   r   r   r   r   <module>   s   _