<s> """<STR_LIT>""" <EOL> import copy <EOL> import gettext <EOL> import locale <EOL> from logging import handlers <EOL> import os <EOL> from babel import localedata <EOL> import six <EOL> _AVAILABLE_LANGUAGES = { } <EOL> USE_LAZY = False <EOL> class TranslatorFactory ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , domain , localedir = None ) : <EOL> """<STR_LIT>""" <EOL> self . domain = domain <EOL> if localedir is None : <EOL> localedir = os . environ . get ( domain . upper ( ) + '<STR_LIT>' ) <EOL> self . localedir = localedir <EOL> def _make_translation_func ( self , domain = None ) : <EOL> """<STR_LIT>""" <EOL> if domain is None : <EOL> domain = self . domain <EOL> t = gettext . translation ( domain , <EOL> localedir = self . localedir , <EOL> fallback = True ) <EOL> m = t . gettext if six . PY3 else t . ugettext <EOL> def f ( msg ) : <EOL> """<STR_LIT>""" <EOL> if USE_LAZY : <EOL> return Message ( msg , domain = domain ) <EOL> return m ( msg ) <EOL> return f <EOL> @ property <EOL> def primary ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _make_translation_func ( ) <EOL> def _make_log_translation_func ( self , level ) : <EOL> return self . _make_translation_func ( self . domain + '<STR_LIT>' + level ) <EOL> @ property <EOL> def log_info ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _make_log_translation_func ( '<STR_LIT:info>' ) <EOL> @ property <EOL> def log_warning ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _make_log_translation_func ( '<STR_LIT>' ) <EOL> @ property <EOL> def log_error ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _make_log_translation_func ( '<STR_LIT:error>' ) <EOL> @ property <EOL> def log_critical ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _make_log_translation_func ( '<STR_LIT>' ) <EOL> _translators = TranslatorFactory ( '<STR_LIT>' ) <EOL> _ = _translators . primary <EOL> _LI = _translators . log_info <EOL> _LW = _translators . log_warning <EOL> _LE = _translators . log_error <EOL> _LC = _translators . log_critical <EOL> def enable_lazy ( ) : <EOL> """<STR_LIT>""" <EOL> global USE_LAZY <EOL> USE_LAZY = True <EOL> def install ( domain ) : <EOL> """<STR_LIT>""" <EOL> from six import moves <EOL> tf = TranslatorFactory ( domain ) <EOL> moves . builtins . __dict__ [ '<STR_LIT:_>' ] = tf . primary <EOL> class Message ( six . text_type ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , msgid , msgtext = None , params = None , <EOL> domain = '<STR_LIT>' , * args ) : <EOL> """<STR_LIT>""" <EOL> if not msgtext : <EOL> msgtext = Message . _translate_msgid ( msgid , domain ) <EOL> msg = super ( Message , cls ) . __new__ ( cls , msgtext ) <EOL> msg . msgid = msgid <EOL> msg . domain = domain <EOL> msg . params = params <EOL> return msg <EOL> def translate ( self , desired_locale = None ) : <EOL> """<STR_LIT>""" <EOL> translated_message = Message . _translate_msgid ( self . msgid , <EOL> self . domain , <EOL> desired_locale ) <EOL> if self . params is None : <EOL> return translated_message <EOL> translated_params = _translate_args ( self . params , desired_locale ) <EOL> translated_message = translated_message % translated_params <EOL> return translated_message <EOL> @ staticmethod <EOL> def _translate_msgid ( msgid , domain , desired_locale = None ) : <EOL> if not desired_locale : <EOL> system_locale = locale . getdefaultlocale ( ) <EOL> if not system_locale [ <NUM_LIT:0> ] : <EOL> desired_locale = '<STR_LIT>' <EOL> else : <EOL> desired_locale = system_locale [ <NUM_LIT:0> ] <EOL> locale_dir = os . environ . get ( domain . upper ( ) + '<STR_LIT>' ) <EOL> lang = gettext . translation ( domain , <EOL> localedir = locale_dir , <EOL> languages = [ desired_locale ] , <EOL> fallback = True ) <EOL> if six . PY3 : <EOL> translator = lang . gettext <EOL> else : <EOL> translator = lang . ugettext <EOL> translated_message = translator ( msgid ) <EOL> return translated_message <EOL> def __mod__ ( self , other ) : <EOL> params = self . _sanitize_mod_params ( other ) <EOL> unicode_mod = super ( Message , self ) . __mod__ ( params ) <EOL> modded = Message ( self . msgid , <EOL> msgtext = unicode_mod , <EOL> params = params , <EOL> domain = self . domain ) <EOL> return modded <EOL> def _sanitize_mod_params ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if other is None : <EOL> params = ( other , ) <EOL> elif isinstance ( other , dict ) : <EOL> params = { } <EOL> if isinstance ( self . params , dict ) : <EOL> for key , val in self . params . items ( ) : <EOL> params [ key ] = self . _copy_param ( val ) <EOL> for key , val in other . items ( ) : <EOL> params [ key ] = self . _copy_param ( val ) <EOL> else : <EOL> params = self . _copy_param ( other ) <EOL> return params <EOL> def _copy_param ( self , param ) : <EOL> try : <EOL> return copy . deepcopy ( param ) <EOL> except Exception : <EOL> return six . text_type ( param ) <EOL> def __add__ ( self , other ) : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise TypeError ( msg ) <EOL> def __radd__ ( self , other ) : <EOL> return self . __add__ ( other ) <EOL> if six . PY2 : <EOL> def __str__ ( self ) : <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise UnicodeError ( msg ) <EOL> def get_available_languages ( domain ) : <EOL> """<STR_LIT>""" <EOL> if domain in _AVAILABLE_LANGUAGES : <EOL> return copy . copy ( _AVAILABLE_LANGUAGES [ domain ] ) <EOL> localedir = '<STR_LIT>' % domain . upper ( ) <EOL> find = lambda x : gettext . find ( domain , <EOL> localedir = os . environ . get ( localedir ) , <EOL> languages = [ x ] ) <EOL> language_list = [ '<STR_LIT>' ] <EOL> list_identifiers = ( getattr ( localedata , '<STR_LIT:list>' , None ) or <EOL> getattr ( localedata , '<STR_LIT>' ) ) <EOL> locale_identifiers = list_identifiers ( ) <EOL> for i in locale_identifiers : <EOL> if find ( i ) is not None : <EOL> language_list . append ( i ) <EOL> aliases = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> for ( locale_ , alias ) in six . iteritems ( aliases ) : <EOL> if locale_ in language_list and alias not in language_list : <EOL> language_list . append ( alias ) <EOL> _AVAILABLE_LANGUAGES [ domain ] = language_list <EOL> return copy . copy ( language_list ) <EOL> def translate ( obj , desired_locale = None ) : <EOL> """<STR_LIT>""" <EOL> message = obj <EOL> if not isinstance ( message , Message ) : <EOL> message = six . text_type ( obj ) <EOL> if isinstance ( message , Message ) : <EOL> return message . translate ( desired_locale ) <EOL> return obj <EOL> def _translate_args ( args , desired_locale = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( args , tuple ) : <EOL> return tuple ( translate ( v , desired_locale ) for v in args ) <EOL> if isinstance ( args , dict ) : <EOL> translated_dict = { } <EOL> for ( k , v ) in six . iteritems ( args ) : <EOL> translated_v = translate ( v , desired_locale ) <EOL> translated_dict [ k ] = translated_v <EOL> return translated_dict <EOL> return translate ( args , desired_locale ) <EOL> class TranslationHandler ( handlers . MemoryHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , locale = None , target = None ) : <EOL> """<STR_LIT>""" <EOL> handlers . MemoryHandler . __init__ ( self , capacity = <NUM_LIT:0> , target = target ) <EOL> self . locale = locale <EOL> def setFormatter ( self , fmt ) : <EOL> self . target . setFormatter ( fmt ) <EOL> def emit ( self , record ) : <EOL> original_msg = record . msg <EOL> original_args = record . args <EOL> try : <EOL> self . _translate_and_log_record ( record ) <EOL> finally : <EOL> record . msg = original_msg <EOL> record . args = original_args <EOL> def _translate_and_log_record ( self , record ) : <EOL> record . msg = translate ( record . msg , self . locale ) <EOL> record . args = _translate_args ( record . args , self . locale ) <EOL> self . target . emit ( record ) </s>
