ó
µ”ù\c           @   s   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 „  Z d „  Z	 d „  Z
 d „  Z e ƒ  Z d S(   sö   
    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.
iÿÿÿÿ(   t   TranslationErrort   PYTHON_FORMAT(   t   string_typest   izipt   it   dt   ut   xt   Xt   ft   Ft   gt   Gc         C   s—   | j  s. t | j t ƒ s* t d ƒ ‚ n  d S|  d k r> d S| j } t | t t f ƒ sh | f } n  t | ƒ |  j	 k r“ t d |  j	 ƒ ‚ n  d S(   s0   Verify the number of plurals in the translation.s/   Found plural forms for non-pluralizable messageNs*   Wrong number of plural forms (expected %d)(
   t   pluralizablet
   isinstancet   stringR   R    t   Nonet   listt   tuplet   lent   num_plurals(   t   catalogt   messaget   msgstrs(    (    s6   lib/python2.7/site-packages/babel/messages/checkers.pyR      s    		c         C   s¡   d | j  k r d S| j } t | t t f ƒ s= | f } n  | j } t | t t f ƒ sg | f } n  x3 t | | ƒ D]" \ } } | rw t | | ƒ qw qw Wd S(   s9   Verify the format string placeholders in the translation.s   python-formatN(   t   flagst   idR   R   R   R   R   t   _validate_format(   R   R   t   msgidsR   t   msgidt   msgstr(    (    s6   lib/python2.7/site-packages/babel/messages/checkers.pyt   python_format.   s    		c         C   sŸ  d „  } d „  } d „  } t  | |  | f ƒ \ } } t  | | | f ƒ \ } } | rt | rt | rt t d ƒ ‚ n | | k r t d ƒ ‚ n  | r&t | ƒ t | ƒ k r¼ t d ƒ ‚ n  xÜ t t | | ƒ ƒ D]M \ }	 \ \ }
 } \ }
 } | | | ƒ sÒ t d |	 d | | f ƒ ‚ qÒ qÒ Wnu t | ƒ } xf | D]^ \ } } | | k rdt d	 | ƒ ‚ q9| | | | ƒ s9t d
 | | | | f ƒ ‚ q9q9Wd S(   s‹  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   sp   g  } xc t  j |  ƒ D]R } | j ƒ  \ } } } | d k rO | d  k rO q n  | j | t | ƒ f ƒ q W| S(   Nt   %(   R   t   finditert   groupsR   t   appendt   str(   R   t   resultt   matcht   namet   formatt   typechar(    (    s6   lib/python2.7/site-packages/babel/messages/checkers.pyt   _parse^   s    c         S   sA   |  | k r t  Sx* t D]" } |  | k r | | k r t  Sq Wt S(   N(   t   Truet   _string_format_compatibilitiest   False(   t   at   bt   set(    (    s6   lib/python2.7/site-packages/babel/messages/checkers.pyt   _compatibleg   s    c         S   sc   d  } xP |  D]H \ } } | d  k r4 | d  k } q | d  k | k r t d ƒ ‚ q q Wt | ƒ S(   Ns5   format string mixes positional and named placeholders(   R   R    t   bool(   t   resultst
   positionalR&   t   char(    (    s6   lib/python2.7/site-packages/babel/messages/checkers.pyt   _check_positionalo   s    s   placeholders are incompatibles)   the format strings are of different kindss-   positional format placeholders are unbalancedsD   incompatible format for placeholder %d: %r and %r are not compatiblei   s   unknown named placeholder %rsD   incompatible format for placeholder %r: %r and %r are not compatibleN(   t   mapR    R   t	   enumerateR   t   dict(   R'   t   alternativeR)   R0   R5   R-   R.   t   a_positionalt   b_positionalt   idxt   _t   firstt   secondt   type_mapR&   R(   (    (    s6   lib/python2.7/site-packages/babel/messages/checkers.pyR   >   s.     				4!c          C   s{   g  }  y d d l  m } Wn t k
 r- n. Xx* | j d ƒ D] } |  j | j ƒ  ƒ q> Wt |  ƒ d k rw t t g S|  S(   Niÿÿÿÿ(   t   working_sets   babel.checkersi    (	   t   pkg_resourcesRA   t   ImportErrort   iter_entry_pointsR"   t   loadR   R   R   (   t   checkersRA   t   entry_point(    (    s6   lib/python2.7/site-packages/babel/messages/checkers.pyt   _find_checkers   s    
N(   t   __doc__t   babel.messages.catalogR    R   t   babel._compatR   R   R+   R   R   R   RH   RF   (    (    (    s6   lib/python2.7/site-packages/babel/messages/checkers.pyt   <module>   s   				_	