3

X'ícuã@sZdZddlmZmZdddhddhdd	d
dhgZdd
„Zdd„Zdd„Zdd„ZeƒZ	dS)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ÚGcCsd|jst|jtƒstdƒ‚dS|dkr*dS|j}t|ttfƒsD|f}t|ƒ|jkr`td|jƒ‚dS)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ú8/tmp/pip-build-gk9425m9/babel/babel/messages/checkers.pyrsrcCsld|jkrdS|j}t|ttfƒs(|f}|j}t|ttfƒsB|f}x$t||ƒD]\}}|rNt||ƒqNWdS)z9Verify the format string placeholders in the translation.z
python-formatN)ÚflagsÚidr
rrrÚzipÚ_validate_format)rrZmsgidsrÚmsgidZmsgstrrrrÚ
python_format,s
rcCs$dd„}dd„}dd„}t|||fƒ\}}t|||fƒ\}}|rV|rV|rVtdƒ‚n||krftdƒ‚|rÌt|ƒt|ƒkr‚td	ƒ‚xœtt||ƒƒD]4\}	\\}
}\}
}|||ƒs’td
|	d||fƒ‚q’WnTt|ƒ}
xJ|D]B\}}||
krøtd|ƒ‚qÚ|||
|ƒsÚtd
|||
|fƒ‚qÚWdS)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
    cSsNg}xDtj|ƒD]6}|jƒ\}}}|dkr4|dkr4q|j|t|ƒfƒqW|S)Nú%)rÚfinditerÚgroupsÚappendr)rÚresultÚmatchÚnameÚformatÚtypecharrrrÚ_parse\sz _validate_format.<locals>._parsecSs2||krdSx tD]}||kr||krdSqWdS)NTF)Ú_string_format_compatibilities)ÚaÚbÚsetrrrÚ_compatiblees
z%_validate_format.<locals>._compatiblecSsDd}x6|D].\}}|dkr$|dk}q
|dk|kr
tdƒ‚q
Wt|ƒS)Nz5format string mixes positional and named placeholders)rÚbool)ÚresultsÚ
positionalr%ÚcharrrrÚ_check_positionalms
z+_validate_format.<locals>._check_positionalzplaceholders are incompatiblez)the format strings are of different kindsz-positional format placeholders are unbalancedzDincompatible format for placeholder %d: %r and %r are not compatibleézunknown named placeholder %rzDincompatible format for placeholder %r: %r and %r are not compatibleN)ÚmaprrÚ	enumeraterÚdict)r&Úalternativer(r-r2r*r+Za_positionalZb_positionalÚidxÚ_ÚfirstÚsecondZtype_mapr%r'rrrr<s. 	
$
rcCsdg}yddlm}Wntk
r(Yn$Xx |jdƒD]}|j|jƒƒq6Wt|ƒdkr`ttgS|S)Nr)Úworking_setzbabel.checkers)	Ú
pkg_resourcesr<ÚImportErrorÚiter_entry_pointsr"Úloadrrr)Úcheckersr<Zentry_pointrrrÚ_find_checkers›srBN)
Ú__doc__Zbabel.messages.catalogrrr)rrrrBrArrrrÚ<module>s_