B
    `DF                 @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ dZe ddei Z	ed	Z
G d
d deZdd Zdd Zdd Zdd ZdddZdd Zdd ZdddZdS )    N)NullTranslations)	text_type)string_types)PY3z[a-zA-Z][-a-zA-Z0-9_]*z (?<!\$)(\$(?:(%(n)s)|{(%(n)s)}))nz%s%sc               @   s@   e Zd ZdZdZdddZdd Zddd	Zd
d Zdd Z	dS )TranslationStringa	  
    The constructor for a :term:`translation string`.  A translation
    string is a Unicode-like object that has some extra metadata.

    This constructor accepts one required argument named ``msgid``.
    ``msgid`` must be the :term:`message identifier` for the
    translation string.  It must be a ``unicode`` object or a ``str``
    object encoded in the default system encoding.

    Optional keyword arguments to this object's constructor include
    ``domain``, ``default``, and ``mapping``.

    ``domain`` represents the :term:`translation domain`.  By default,
    the translation domain is ``None``, indicating that this
    translation string is associated with the default translation
    domain (usually ``messages``).

    ``default`` represents an explicit *default text* for this
    translation string.  Default text appears when the translation
    string cannot be translated.  Usually, the ``msgid`` of a
    translation string serves double duty as its default text.
    However, using this option you can provide a different default
    text for this translation string.  This feature is useful when the
    default of a translation string is too complicated or too long to
    be used as a message identifier. If ``default`` is provided, it
    must be a ``unicode`` object or a ``str`` object encoded in the
    default system encoding (usually means ASCII).  If ``default`` is
    ``None`` (its default value), the ``msgid`` value used by this
    translation string will be assumed to be the value of ``default``.

    ``mapping``, if supplied, must be a dictionary-like object which
    represents the replacement values for any :term:`translation
    string` *replacement marker* instances found within the ``msgid``
    (or ``default``) value of this translation string.

    ``context`` represents the :term:`translation context`.  By default,
    the translation context is ``None``.

    After a translation string is constructed, it behaves like most
    other ``unicode`` objects; its ``msgid`` value will be displayed
    when it is treated like a ``unicode`` object.  Only when its
    ``ugettext`` method is called will it be translated.

    Its default value is available as the ``default`` attribute of the
    object, its :term:`translation domain` is available as the
    ``domain`` attribute, and the ``mapping`` is available as the
    ``mapping`` attribute.  The object otherwise behaves much like a
    Unicode string.
    )domaincontextdefaultmappingNc             C   s   t | |} t|| jr|p.|jo.|jd d  }|pF|joF|jd d  }|p^|jo^|jd d  }|jr|rx.|j D ]\}}|	|| qvW n
|j
 }t |}|| _|| _|d krt |}|| _|| _| S )N)r   __new__
isinstance	__class__r   r	   r
   r   items
setdefaultcopy)selfmsgidr   r
   r   r	   kv r   b/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/translationstring/__init__.pyr   B   s$    
zTranslationString.__new__c             C   sB   t |tstd| jr.| j }|| n| }t| |dS )zCreate a new TranslationString instance with an updated mapping.
        This makes it possible to use the standard python %-style string
        formatting with translatable strings. Only dictionary
        arguments are supported.
        z9Can only interpolate translationstring with dictionaries.)r   )r   dict
ValueErrorr   r   updater   )r   optionsr   r   r   r   __mod__\   s    

zTranslationString.__mod__c                s4   |dkr j } jr0|r0 fdd}t||}|S )a   Interpolate the value ``translated`` which is assumed to
        be a Unicode object containing zero or more *replacement
        markers* (``$foo`` or ``${bar}``) using the ``mapping``
        dictionary attached to this instance.  If the ``mapping``
        dictionary is empty or ``None``, no interpolation is
        performed.

        If ``translated`` is ``None``, interpolation will be performed
        against the ``default`` value.
        Nc                s$   |   \}}}t j|p||S )N)groupsr   r   get)matchZwholeZparam1Zparam2)r   r   r   replace   s    z.TranslationString.interpolate.<locals>.replace)r
   r   _interp_regexsub)r   
translatedr    r   )r   r   interpolatem   s    
zTranslationString.interpolatec             C   s   | j |  fS )N)r   __getstate__)r   r   r   r   
__reduce__   s    zTranslationString.__reduce__c             C   s   t | | j| j| j| jfS )N)r   r   r
   r   r	   )r   r   r   r   r%      s    zTranslationString.__getstate__)NNNN)N)
__name__
__module____qualname____doc__	__slots__r   r   r$   r&   r%   r   r   r   r   r      s   1

r   c                s   d fdd	}|S )aa   Create a factory which will generate translation strings
    without requiring that each call to the factory be passed a
    ``domain`` value.  A single argument is passed to this class'
    constructor: ``domain``.  This value will be used as the
    ``domain`` values of :class:`translationstring.TranslationString`
    objects generated by the ``__call__`` of this class.  The
    ``msgid``, ``mapping``, and ``default`` values provided to the
    ``__call__`` method of an instance of this class have the meaning
    as described by the constructor of the
    :class:`translationstring.TranslationString`Nc                s,   t | tr| jp }n }t| ||||dS )z Provided a msgid (Unicode object or :term:`translation
        string`) and optionally a mapping object, and a *default
        value*, return a :term:`translation string` object.)r   r
   r   r	   )r   r   r   )r   r   r
   r	   r   )factory_domainr   r   create   s
    
z(TranslationStringFactory.<locals>.create)NNNr   )r,   r-   r   )r,   r   TranslationStringFactory   s    r.   c                s   d fdd	}|S )a  
    When necessary, use the result of calling this function as a
    Chameleon template 'translate' function (e.g. the ``translate``
    argument to the ``chameleon.zpt.template.PageTemplate``
    constructor) to allow our translation machinery to drive template
    translation.  A single required argument ``translator`` is
    passsed.  The ``translator`` provided should be a callable which
    accepts a single argument ``translation_string`` ( a
    :class:`translationstring.TranslationString` instance) which
    returns a ``unicode`` object as a translation.  ``translator`` may
    also optionally be ``None``, in which case no translation is
    performed (the ``msgid`` or ``default`` value is returned
    untranslated).
    Nc                sZ   t | ts| d k	rt| } | S | }t|ds<t| ||||} d krN| }n |}|S )Nr$   )r   r   r   hasattrr   r$   )r   r   r   r	   Ztarget_languager
   tstringresult)
translatorr   r   	translate   s     


z%ChameleonTranslate.<locals>.translate)NNNNNr   )r2   r3   r   )r2   r   ChameleonTranslate   s     /r4   c             C   s@   t r| j}n| j}|r$t||f }n|}||}||kr<|S |S )zm A translator policy function which unconditionally uses the
    ``ugettext`` API on the translations object.)r   gettextugettextCONTEXT_MASK)translationsr0   r   r	   _gettextr   r#   r   r   r   ugettext_policy   s    r:   c             C   s   |dkr(t | ddpd}t |ddp&|}|p6t |dd}|rJt||f }n|}t | dddk	rl| ||}ntrx| j}n| j}||}||kr|S |S )z A translator policy function which assumes the use of a
    :class:`babel.support.Translations` translations object, which
    supports the dugettext API; fall back to ugettext.Nr   messagesr	   	dugettext)getattrr7   r<   r   r5   r6   )r8   r0   r   r	   default_domainr   r#   r9   r   r   r   dugettext_policy   s    r?   c                s     dkrt  d fdd	}|S )aJ  
    Return a translator object based on the ``translations`` and
    ``policy`` provided.  ``translations`` should be an object
    supporting *at least* the Python :class:`gettext.NullTranslations`
    API but ideally the :class:`babel.support.Translations` API, which
    has support for domain lookups like dugettext.

    ``policy`` should be a callable which accepts three arguments:
    ``translations``, ``tstring`` and ``domain``.  It must perform the
    actual translation lookup.  If ``policy`` is ``None``, the
    :func:`translationstring.dugettext_policy` policy will be used.

    The callable returned accepts three arguments: ``tstring``
    (required), ``domain`` (optional) and ``mapping`` (optional).
    When called, it will translate the ``tstring`` translation string
    to a ``unicode`` object using the ``translations`` provided.  If
    ``translations`` is ``None``, the result of interpolation of the
    default value is returned.  The optional ``domain`` argument can
    be used to specify or override the domain of the ``tstring``
    (useful when ``tstring`` is a normal string rather than a
    translation string).  The optional ``mapping`` argument can
    specify or override the ``tstring`` interpolation mapping, useful
    when the ``tstring`` argument is a simple string instead of a
    translation string.
    Nc                s   t | dst| |||d} n4|rP| jr<| j }|| n|}t| |||d} | }|p\| j}|pf| j}d k	r~ | ||}|| kr| j}|rd|kr| jr| |}|S )Nr$   )r   r   r	   $)	r/   r   r   r   r   r   r	   r
   r$   )r0   r   r   r	   Znew_mappingr#   )policyr8   r   r   r2   2  s$    




zTranslator.<locals>.translator)NNN)r?   )r8   rA   r2   r   )rA   r8   r   
Translator  s    rB   c       	      C   sD   t r| j}n| j}|r$t||f }n|}||||}||kr@|S |S )zn A pluralizer policy function which unconditionally uses the
    ``ungettext`` API on the translations object.)r   ngettext	ungettextr7   )	r8   singularpluralr   r   r	   r9   r   r#   r   r   r   ungettext_policyH  s    rG   c       
      C   s~   t | ddpd}|p|}|r*t||f }n|}t | dddk	rP| ||||}ntr\| j}	n| j}	|	|||}||krz|S |S )z A pluralizer policy function which assumes the use of the
    :class:`babel.support.Translations` class, which supports the
    dungettext API; falls back to ungettext.r   Nr;   
dungettext)r=   r7   rH   r   rC   rD   )
r8   rE   rF   r   r   r	   r>   r   r#   r9   r   r   r   dungettext_policyZ  s    rI   c                s.    dkrt  dkrt d fdd	}|S )aL  
    Return a pluralizer object based on the ``translations`` and
    ``policy`` provided.  ``translations`` should be an object
    supporting *at least* the Python :class:`gettext.NullTranslations`
    API but ideally the :class:`babel.support.Translations` API, which
    has support for domain lookups like dugettext.

    ``policy`` should be a callable which accepts five arguments:
    ``translations``, ``singular`` and ``plural``, ``n`` and
    ``domain``.  It must perform the actual pluralization lookup.  If
    ``policy`` is ``None``, the
    :func:`translationstring.dungettext_policy` policy will be used.

    The object returned will be a callable which has the following
    signature::

        def pluralizer(singular, plural, n, domain=None, mapping=None):
            ...

    The ``singular`` and ``plural`` objects passed may be translation
    strings or unicode strings.  ``n`` represents the number of
    elements.  ``domain`` is the translation domain to use to do the
    pluralization, and ``mapping`` is the interpolation mapping that
    should be used on the result.  Note that if the objects passed are
    translation strings, their domains and mappings are ignored.  The
    domain and mapping arguments must be used instead.  If the ``domain`` is
    not supplied, a default domain is used (usually ``messages``).
    Nc                s:   t  | ||||}|r6d|kr6|r6t||d S |S )z Pluralize this object r@   )r   )r   r   r$   )rE   rF   r   r   r   r	   r#   )rA   r8   r   r   
pluralizer  s
    zPluralizer.<locals>.pluralizer)NNN)rI   r   )r8   rA   rJ   r   )rA   r8   r   
Pluralizerq  s    rK   )NN)NN)rer5   r   Ztranslationstring.compatr   r   r   ZNAME_REcompiler!   r7   r   r.   r4   r:   r?   rB   rG   rI   rK   r   r   r   r   <module>   s$    A
2