
    ce %                         d Z ddlZddlZddlZddlZddlZddlZddlmZ ddlm	Z	 dZ
 ej                  e      Z G d de      Zy)z4Private Message class for lazy translation support.
    N)_locale)
_translatec                   l     e Zd ZdZ	 	 	 d fd	ZddZe	 	 dd       Zd Zd Z	d Z
d Zd	 Zd
 Z xZS )MessagezA Message object is a unicode object that can be translated.

    Translation of Message is done explicitly using the translate() method.
    For all non-translation intents and purposes, a Message is simply unicode,
    and can be treated as such.
    c                     |st         j                  ||      }t        t         |   | |      }||_        ||_        ||_        ||_        ||_        |S )a&  Create a new Message object.

        In order for translation to work gettext requires a message ID, this
        msgid will be used as the base unicode text. It is also possible
        for the msgid and the base unicode text to be different by passing
        the msgtext parameter.
        )	r   _translate_msgidsuper__new__msgiddomainparamshas_contextual_formhas_plural_form)
clsr   msgtextr   r   r   r   argsmsg	__class__s
            4/usr/lib/python3/dist-packages/oslo_i18n/_message.pyr   zMessage.__new__,   sY     ..uf=G GS)#w7	

"5-
    c                    t         j                  | j                  | j                  || j                  | j
                        }| j                  |S t        j                  | j                  |      }| j                  ||      S )aQ  Translate this message to the desired locale.

        :param desired_locale: The desired locale to translate the message to,
                               if no locale is provided the message will be
                               translated to the system's default locale.

        :returns: the translated message in unicode
        )
r   r	   r   r   r   r   r   r   translate_args_safe_translate)selfdesired_localetranslated_messagetranslated_paramss       r   translationzMessage.translationE   s~     %55djj6:kk6D6:6N6N6:6J6J	L ;;%% '55dkk6DF ##$68IJJr   c                    |s2t        j                  t         j                        }|r|d   sd}n|d   }t        j                  j                  t        j                  |            }t        j                  |||gd      }|s|s|j                  } ||       }	|	S |r|rt        d      |r1| \  }
}|j                  }|
t        |} ||      }	t        |	v r|}	|	S |r| \  }}}|j                  } ||||      }		S )Nr   en_UST)	localedir	languagesfallbackzUnimplemented.)locale	getlocaleLC_CTYPEosenvirongetr   get_locale_dir_variable_namegettextr   
ValueErrorCONTEXT_SEPARATORngettext)r   r   r   r   r   system_locale
locale_dirlang
translatorr   msgctxmsgtxtmsg_with_ctx	msgsingle	msgpluralmsgcounts                   r   r	   zMessage._translate_msgida   s     ",,V__=M a(8!(!.q!1ZZ^^008

 ""6-7.<-=,02
 #?J!+E!2. "!+ !_ -.. $VVJ'-/@&IL!+L!9 $66%+" "! /4,Y	8J!+Iy(!K!!r   c                     	 ||z  }|S # t         t        f$ rc}d}t        j                  ||| j                  |fz         t
        j                  ||| j                  |       | j                  |z  }Y d}~|S d}~ww xY w)a  Trap translation errors and fall back to default translation.

        :param translated_message: the requested translation

        :param translated_params: the params to be inserted

        :return: if parameter insertion is successful then it is the
                 translated_message with the translated_params inserted, if the
                 requested translation fails then it is the default translation
                 with the params
        zQFailed to insert replacement values into translated message %s (Original: %r): %sN)KeyError	TypeErrorwarningswarnr   LOGdebug)r   r   r   errr   s        r   r   zMessage._safe_translate   s    	@!36G!G( "!' )$ 	@3CMM#!3TZZ EEFIIc-tzz3?!%.?!?!!'	@s   	 A;AA66A;c                     | j                  |      }| j                  t        |       |      }t        | j                  ||| j
                        }|S )N)r   r   r   )_sanitize_mod_paramsr   strr   r   r   )r   otherr   unicode_modmoddeds        r   __mod__zMessage.__mod__   sN     **51**3t9f=!, & $- r   c                 H    ||f}|S t        |t              rxi }t         j                  t              r2|j                   fd j                  j	                         D               |j                   fd|j	                         D               |S  j                  |      }|S )a  Sanitize the object being modded with this Message.

        - Add support for modding 'None' so translation supports it
        - Trim the modded object, which can be a large dictionary, to only
        those keys that would actually be used in a translation
        - Snapshot the object being modded, in case the message is
        translated, it will be used as it was when the Message was created
        c              3   J   K   | ]  \  }}|j                  |      f  y wN_copy_param.0keyvalr   s      r   	<genexpr>z/Message._sanitize_mod_params.<locals>.<genexpr>   s/      C"*#s  #D$4$4S$9: C    #c              3   J   K   | ]  \  }}|j                  |      f  y wrK   rL   rN   s      r   rR   z/Message._sanitize_mod_params.<locals>.<genexpr>   s-      9&c3  0 0 56 9rS   )
isinstancedictr   updateitemsrM   )r   rE   r   s   `  r   rC   zMessage._sanitize_mod_params   s     =XF  t$ F$++t, C.2kk.?.?.AC CMM 9*/++-9 9  %%e,Fr   c                 b    	 t        j                  |      S # t        $ r t        |      cY S w xY wrK   )copydeepcopy	ExceptionrD   )r   params     r   rM   zMessage._copy_param   s1    	=='' 	 u:	s    ..c                 4    ddl m}  |d      }t        |      )Nr   )_z(Message objects do not support addition.)oslo_i18n._i18nr_   r<   )r   rE   r_   r   s       r   __add__zMessage.__add__   s    %:;nr   c                 $    | j                  |      S rK   )ra   )r   rE   s     r   __radd__zMessage.__radd__   s    ||E""r   )NNosloFFrK   )NFF)__name__
__module____qualname____doc__r   r   staticmethodr	   r   rH   rC   rM   ra   rc   __classcell__)r   s   @r   r   r   $   sX     2638 %2K8 7;DI," ,"\""H
0
#r   r   )rh   rZ   r,   r%   loggingr(   r=   	oslo_i18nr   r   r.   	getLoggerre   r?   rD   r    r   r   <module>ro      sN         	       g!B#c B#r   