o
    VÛë\Å  ã                   @   sd   d Z ddlmZmZ ddlmZmZ h d£ddhh d£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-2019 by the Babel Team.
    :license: BSD, see LICENSE for more details.
é    )ÚTranslationErrorÚPYTHON_FORMAT)Ústring_typesÚizip>   ÚdÚiÚuÚxÚX>   ÚFÚGÚfÚgc                 C   sd   |j st|jtƒstdƒ‚dS | du rdS |j}t|ttfƒs"|f}t|ƒ| jkr0t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))	ÚpluralizableÚ
isinstanceÚstringr   r   ÚlistÚtupleÚlenÚnum_plurals)ÚcatalogÚmessageÚmsgstrs© r   ú9/usr/lib/python3/dist-packages/babel/messages/checkers.pyr      s   ÿÿr   c                 C   sh   d|j vrdS |j}t|ttfƒs|f}|j}t|ttfƒs!|f}t||ƒD ]\}}|r1t||ƒ q&dS )z9Verify the format string placeholders in the translation.zpython-formatN)ÚflagsÚidr   r   r   r   r   Ú_validate_format)r   r   Úmsgidsr   ÚmsgidÚmsgstrr   r   r   Úpython_format.   s   

€þr!   c                 C   s  dd„ }dd„ }dd„ }t || |fƒ\}}t |||fƒ\}}|r(|s(|s(tdƒ‚||kr0tdƒ‚|rbt|ƒt|ƒkr>td	ƒ‚tt||ƒƒD ]\}	\\}
}\}
}|||ƒs_td
|	d ||f ƒ‚qEdS t|ƒ}|D ] \}}||vrvtd| ƒ‚|||| ƒsˆtd|||| f ƒ‚qhd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   sJ   g }t  | ¡D ]}| ¡ \}}}|dkr|d u rq| |t|ƒf¡ q|S )Nú%)r   ÚfinditerÚgroupsÚappendÚstr)r   ÚresultÚmatchÚnameÚformatÚtypecharr   r   r   Ú_parse^   s   z _validate_format.<locals>._parsec                 S   s0   | |krdS t D ]}| |v r||v r dS qdS )NTF)Ú_string_format_compatibilities)ÚaÚbÚsetr   r   r   Ú_compatibleg   s   €z%_validate_format.<locals>._compatiblec                 S   s@   d }| D ]\}}|d u r|d u }q|d u |krt dƒ‚qt|ƒS )Nz5format string mixes positional and named placeholders)r   Úbool)ÚresultsÚ
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*   Úalternativer,   r1   r6   r.   r/   Úa_positionalÚb_positionalÚidxÚ_ÚfirstÚsecondÚtype_mapr)   r+   r   r   r   r   >   s:    	"
þÿÿ	ýÿýr   c                  C   s^   g } zddl m} W n	 ty   Y nw | d¡D ]	}|  | ¡ ¡ qt| ƒdkr-ttgS | S )Nr   )Úworking_setzbabel.checkers)	Úpkg_resourcesrC   ÚImportErrorÚiter_entry_pointsr%   Úloadr   r   r!   )ÚcheckersrC   Úentry_pointr   r   r   Ú_find_checkers�   s   ÿrJ   N)Ú__doc__Úbabel.messages.catalogr   r   Úbabel._compatr   r   r-   r   r!   r   rJ   rH   r   r   r   r   Ú<module>   s   ý_
