File: //usr/lib/python3.9/site-packages/babel/messages/__pycache__/checkers.cpython-39.pyc
a
�`� � @ sd d Z ddlmZmZ ddlmZmZ h d�ddhh d�gZdd � Zd
d� Z dd
� Z
dd� Ze� ZdS )z�
babel.messages.checkers
~~~~~~~~~~~~~~~~~~~~~~~
Various routines that help with validation of translations.
:since: version 0.9
:copyright: (c) 2013-2021 by the Babel Team.
:license: BSD, see LICENSE for more details.
� )�TranslationError�
PYTHON_FORMAT)�string_types�izip> �i�d�u�x�X> �g�F�f�Gc C sd |j st|jt�std��dS | du r*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)) Zpluralizable�
isinstance�stringr r �list�tuple�len�num_plurals)�catalog�message�msgstrs� r �;/usr/lib/python3.9/site-packages/babel/messages/checkers.pyr s �r c C sh d|j vrdS |j}t|ttf�s(|f}|j}t|ttf�sB|f}t||�D ]\}}|rLt||� qLdS )z9Verify the format string placeholders in the translation.z
python-formatN)�flags�idr r r r r �_validate_format)r r Zmsgidsr ZmsgidZmsgstrr r r �
python_format. s
r c C s dd� }dd� }dd� }t || |f�\}}t |||f�\}}|rR|sR|sRtd��n||krbtd��|r�t|�t|�kr~td ��tt||��D ]4\} \\}
}\}
}|||�s�td
| d ||f ��q�nPt|�}
|D ]B\}}||
vr�td| ��q�|||
| �s�td
|||
| f ��q�dS )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 ]6}|�� \}}}|dkr2|d u r2q|�|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 | |krdS t D ]}| |v r||v r dS qdS )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 |krt d��qt|�S )Nz5format string mixes positional and named placeholders)r �bool)�resultsZ
positionalr% �charr r r �_check_positionalo s
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)�mapr r � enumerater �dict)r&