
    ce?                         d Z ddlZddlZddlmZ ddlmZ  G d dej                        Z G d dej                        Z G d	 d
ej                        Z
d Z G d dej                        Zy)z+Test fixtures for working with oslo_i18n.

    N)_lazy)_messagec                   $    e Zd ZdZddZd Zd Zy)Translationa  Fixture for managing translatable strings.

    This class provides methods for creating translatable strings
    using both lazy translation and immediate translation. It can be
    used to generate the different types of messages returned from
    oslo_i18n to test code that may need to know about the type to
    handle them differently (for example, error handling in WSGI apps,
    or logging).

    Use this class to generate messages instead of toggling the global
    lazy flag and using the regular translation factory.

    c                     || _         y)zInitialize the fixture.

        :param domain: The translation domain. This is not expected to
                       coincide with an actual set of message
                       catalogs, but it can.
        :type domain: str
        Ndomain)selfr	   s     3/usr/lib/python3/dist-packages/oslo_i18n/fixture.py__init__zTranslation.__init__'   s         c                 D    t        j                  || j                        S )zReturn a lazily translated message.

        :param msg: Input message string. May optionally include
                    positional or named string interpolation markers.
        :type msg: str or unicode

        r   )r   Messager	   r
   msgs     r   lazyzTranslation.lazy1   s     DKK88r   c                     t        |      S )zReturn a string as though it had been translated immediately.

        :param msg: Input message string. May optionally include
                    positional or named string interpolation markers.
        :type msg: str or unicode

        )strr   s     r   	immediatezTranslation.immediate;   s     3xr   N)ztest-domain)__name__
__module____qualname____doc__r   r   r    r   r   r   r      s    9r   r   c                   2     e Zd ZdZ fdZ fdZd Z xZS )
ToggleLazyz8Fixture to toggle lazy translation on or off for a test.c                 b    t         t        |           || _        t        j
                  | _        y)zForce lazy translation on or off.

        :param enabled: Flag controlling whether to enable or disable
            lazy translation, passed to :func:`~oslo_i18n.enable_lazy`.
        :type enabled: bool
        N)superr   r   _enabledr   USE_LAZY_original_value)r
   enabled	__class__s     r   r   zToggleLazy.__init__I   s%     	j$(*$~~r   c                     t         t        |           | j                  | j                         t        j                  | j                         y N)r   r   setUp
addCleanup_restore_originalr   enable_lazyr   r
   r#   s    r   r&   zToggleLazy.setUpT   s4    j$%'../$--(r   c                 B    t        j                  | j                         y r%   )r   r)   r!   )r
   s    r   r(   zToggleLazy._restore_originalY   s    $../r   )r   r   r   r   r   r&   r(   __classcell__r#   s   @r   r   r   F   s    B	.)
0r   r   c                   $    e Zd ZdZddZd Zd Zy)_PrefixTranslatorzTranslator that adds prefix to message ids

    NOTE: gettext.NullTranslations is an old style class

    :parm prefix: prefix to add to message id.  If not specified (None)
                  then 'noprefix' is used.
    :type prefix: string

    Nc                 R    t         j                  j                  | |       || _        y r%   )gettextNullTranslationsr   prefix)r
   fpr3   s      r   r   z_PrefixTranslator.__init__h   s      ))$3r   c                 `    t         j                  j                  | |      }| j                  |z   S r%   )r1   r2   r3   r
   messager   s      r   r1   z_PrefixTranslator.gettextl   s)    &&..tW={{S  r   c                 `    t         j                  j                  | |      }| j                  |z   S r%   )r1   r2   ugettextr3   r6   s      r   r9   z_PrefixTranslator.ugettextp   s)    &&//g>{{S  r   )Nnoprefix)r   r   r   r   r   r1   r9   r   r   r   r/   r/   ]   s    !!r   r/   c                  >    t        | d   dz   |d   d   z   dz         S )zEUse message id prefixed with domain and language as translation

    r   /	languagesz: )r3   )r/   )xys     r   _prefix_translationsr@   u   s*     AaD3J;1B$BT$IJJr   c                   2     e Zd ZdZdZd fd	Z fdZ xZS )PrefixLazyTranslationaH  Fixture to prefix lazy translation enabled messages

    Use of this fixture will cause messages supporting lazy translation to
    be replaced with the message id prefixed with 'domain/language:'.
    For example, 'oslo/en_US: message about something'.  It will also
    override the available languages returned from
    oslo_18n.get_available_languages to the specified languages.

    This will enable tests to ensure that messages were translated lazily
    with the specified language and not immediately with the default language.

    NOTE that this does not work unless lazy translation is enabled, so it
    uses the ToggleLazy fixture to enable lazy translation.

    :param languages: list of languages to support.  If not specified (None)
                      then ['en_US'] is used.
    :type languages: list of strings

    en_USc                 l    t         t        |           |xs t        j                  g| _        || _        y r%   )r   rB   r   _DEFAULT_LANGr=   locale)r
   r=   rF   r#   s      r   r   zPrefixLazyTranslation.__init__   s.    #T35"K'<'J'J&Kr   c                     t         t                    j                  t	        d              j                  t        j                  d fd              j                  t        j                  d fd              j                  t        j                  dt                      j                  t        j                  d fd             y )	NTz/oslo_i18n._gettextutils.get_available_languagesc                      j                   S r%   r=   r>   r?   r
   s     r   <lambda>z-PrefixLazyTranslation.setUp.<locals>.<lambda>       DNN r   z!oslo_i18n.get_available_languagesc                      j                   S r%   rI   rJ   s     r   rK   z-PrefixLazyTranslation.setUp.<locals>.<lambda>   rL   r   zgettext.translationzlocale.getlocalec                      j                   S r%   )rF   rJ   s     r   rK   z-PrefixLazyTranslation.setUp.<locals>.<lambda>   s    T[[ r   )r   rB   r&   
useFixturer   fixturesMonkeyPatchr@   r*   s   `r   r&   zPrefixLazyTranslation.setUp   s    #T02
4(),,=*, 	- 	,,/*, 	- 	,,-B-AC 	D,,-?-HJ 	Kr   )NN)r   r   r   r   rE   r   r&   r,   r-   s   @r   rB   rB   |   s     ( M
K Kr   rB   )r   r1   rP   	oslo_i18nr   r   Fixturer   r   r2   r/   r@   rB   r   r   r   <module>rT      si       +("" +\0!! 0.!00 !0K(KH,, (Kr   