<s> """<STR_LIT>""" <EOL> import datetime <EOL> import gettext as gettext_stdlib <EOL> import babel <EOL> from babel import dates <EOL> from babel import numbers <EOL> from babel import support <EOL> try : <EOL> import pytz . gae <EOL> except ImportError : <EOL> pass <EOL> import pytz <EOL> import webapp2 <EOL> default_config = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:time>' : '<STR_LIT>' , <EOL> '<STR_LIT:date>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } , <EOL> } <EOL> NullTranslations = gettext_stdlib . NullTranslations <EOL> class I18nStore ( object ) : <EOL> """<STR_LIT>""" <EOL> config_key = __name__ <EOL> translations = None <EOL> translations_path = None <EOL> domains = None <EOL> default_locale = None <EOL> default_timezone = None <EOL> date_formats = None <EOL> locale_selector = None <EOL> timezone_selector = None <EOL> def __init__ ( self , app , config = None ) : <EOL> """<STR_LIT>""" <EOL> config = app . config . load_config ( self . config_key , <EOL> default_values = default_config , user_values = config , <EOL> required_keys = None ) <EOL> self . translations = { } <EOL> self . translations_path = config [ '<STR_LIT>' ] <EOL> self . domains = config [ '<STR_LIT>' ] <EOL> self . default_locale = config [ '<STR_LIT>' ] <EOL> self . default_timezone = config [ '<STR_LIT>' ] <EOL> self . date_formats = config [ '<STR_LIT>' ] <EOL> self . set_locale_selector ( config [ '<STR_LIT>' ] ) <EOL> self . set_timezone_selector ( config [ '<STR_LIT>' ] ) <EOL> def set_locale_selector ( self , func ) : <EOL> """<STR_LIT>""" <EOL> if func is None : <EOL> self . locale_selector = self . default_locale_selector <EOL> else : <EOL> if isinstance ( func , basestring ) : <EOL> func = webapp2 . import_string ( func ) <EOL> self . locale_selector = func . __get__ ( self , self . __class__ ) <EOL> def set_timezone_selector ( self , func ) : <EOL> """<STR_LIT>""" <EOL> if func is None : <EOL> self . timezone_selector = self . default_timezone_selector <EOL> else : <EOL> if isinstance ( func , basestring ) : <EOL> func = webapp2 . import_string ( func ) <EOL> self . timezone_selector = func . __get__ ( self , self . __class__ ) <EOL> def default_locale_selector ( self , request ) : <EOL> return self . default_locale <EOL> def default_timezone_selector ( self , request ) : <EOL> return self . default_timezone <EOL> def get_translations ( self , locale ) : <EOL> """<STR_LIT>""" <EOL> trans = self . translations . get ( locale ) <EOL> if not trans : <EOL> locales = ( locale , self . default_locale ) <EOL> trans = self . load_translations ( self . translations_path , locales , <EOL> self . domains ) <EOL> self . translations [ locale ] = trans <EOL> return trans <EOL> def load_translations ( self , dirname , locales , domains ) : <EOL> """<STR_LIT>""" <EOL> trans = None <EOL> trans_null = None <EOL> for domain in domains : <EOL> _trans = support . Translations . load ( dirname , locales , domain ) <EOL> if isinstance ( _trans , NullTranslations ) : <EOL> trans_null = _trans <EOL> continue <EOL> elif trans is None : <EOL> trans = _trans <EOL> else : <EOL> trans . merge ( _trans ) <EOL> return trans or trans_null or NullTranslations ( ) <EOL> class I18n ( object ) : <EOL> """<STR_LIT>""" <EOL> store = None <EOL> locale = None <EOL> translations = None <EOL> timezone = None <EOL> tzinfo = None <EOL> def __init__ ( self , request ) : <EOL> """<STR_LIT>""" <EOL> self . store = store = get_store ( app = request . app ) <EOL> self . set_locale ( store . locale_selector ( request ) ) <EOL> self . set_timezone ( store . timezone_selector ( request ) ) <EOL> def set_locale ( self , locale ) : <EOL> """<STR_LIT>""" <EOL> self . locale = locale <EOL> self . translations = self . store . get_translations ( locale ) <EOL> def set_timezone ( self , timezone ) : <EOL> """<STR_LIT>""" <EOL> self . timezone = timezone <EOL> self . tzinfo = pytz . timezone ( timezone ) <EOL> def gettext ( self , string , ** variables ) : <EOL> """<STR_LIT>""" <EOL> if variables : <EOL> return self . translations . ugettext ( string ) % variables <EOL> return self . translations . ugettext ( string ) <EOL> def ngettext ( self , singular , plural , n , ** variables ) : <EOL> """<STR_LIT>""" <EOL> if variables : <EOL> return self . translations . ungettext ( singular , plural , n ) % variables <EOL> return self . translations . ungettext ( singular , plural , n ) <EOL> def to_local_timezone ( self , datetime ) : <EOL> """<STR_LIT>""" <EOL> if datetime . tzinfo is None : <EOL> datetime = datetime . replace ( tzinfo = pytz . UTC ) <EOL> return self . tzinfo . normalize ( datetime . astimezone ( self . tzinfo ) ) <EOL> def to_utc ( self , datetime ) : <EOL> """<STR_LIT>""" <EOL> if datetime . tzinfo is None : <EOL> datetime = self . tzinfo . localize ( datetime ) <EOL> return datetime . astimezone ( pytz . UTC ) . replace ( tzinfo = None ) <EOL> def _get_format ( self , key , format ) : <EOL> """<STR_LIT>""" <EOL> if format is None : <EOL> format = self . store . date_formats . get ( key ) <EOL> if format in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> rv = self . store . date_formats . get ( '<STR_LIT>' % ( key , format ) ) <EOL> if rv is not None : <EOL> format = rv <EOL> return format <EOL> def format_date ( self , date = None , format = None , rebase = True ) : <EOL> """<STR_LIT>""" <EOL> format = self . _get_format ( '<STR_LIT:date>' , format ) <EOL> if rebase and isinstance ( date , datetime . datetime ) : <EOL> date = self . to_local_timezone ( date ) <EOL> return dates . format_date ( date , format , locale = self . locale ) <EOL> def format_datetime ( self , datetime = None , format = None , rebase = True ) : <EOL> """<STR_LIT>""" <EOL> format = self . _get_format ( '<STR_LIT>' , format ) <EOL> kwargs = { } <EOL> if rebase : <EOL> kwargs [ '<STR_LIT>' ] = self . tzinfo <EOL> return dates . format_datetime ( datetime , format , locale = self . locale , <EOL> ** kwargs ) <EOL> def format_time ( self , time = None , format = None , rebase = True ) : <EOL> """<STR_LIT>""" <EOL> format = self . _get_format ( '<STR_LIT:time>' , format ) <EOL> kwargs = { } <EOL> if rebase : <EOL> kwargs [ '<STR_LIT>' ] = self . tzinfo <EOL> return dates . format_time ( time , format , locale = self . locale , ** kwargs ) <EOL> def format_timedelta ( self , datetime_or_timedelta , granularity = '<STR_LIT>' , <EOL> threshold = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( datetime_or_timedelta , datetime . datetime ) : <EOL> datetime_or_timedelta = datetime . datetime . utcnow ( ) - datetime_or_timedelta <EOL> return dates . format_timedelta ( datetime_or_timedelta , granularity , <EOL> threshold = threshold , <EOL> locale = self . locale ) <EOL> def format_number ( self , number ) : <EOL> """<STR_LIT>""" <EOL> return numbers . format_number ( number , locale = self . locale ) <EOL> def format_decimal ( self , number , format = None ) : <EOL> """<STR_LIT>""" <EOL> return numbers . format_decimal ( number , format = format , <EOL> locale = self . locale ) <EOL> def format_currency ( self , number , currency , format = None ) : <EOL> """<STR_LIT>""" <EOL> return numbers . format_currency ( number , currency , format = format , <EOL> locale = self . locale ) <EOL> def format_percent ( self , number , format = None ) : <EOL> """<STR_LIT>""" <EOL> return numbers . format_percent ( number , format = format , <EOL> locale = self . locale ) <EOL> def format_scientific ( self , number , format = None ) : <EOL> """<STR_LIT>""" <EOL> return numbers . format_scientific ( number , format = format , <EOL> locale = self . locale ) <EOL> def parse_date ( self , string ) : <EOL> """<STR_LIT>""" <EOL> return dates . parse_date ( string , locale = self . locale ) <EOL> def parse_datetime ( self , string ) : <EOL> """<STR_LIT>""" <EOL> return dates . parse_datetime ( string , locale = self . locale ) <EOL> def parse_time ( self , string ) : <EOL> """<STR_LIT>""" <EOL> return dates . parse_time ( string , locale = self . locale ) <EOL> def parse_number ( self , string ) : <EOL> """<STR_LIT>""" <EOL> return numbers . parse_number ( string , locale = self . locale ) <EOL> def parse_decimal ( self , string ) : <EOL> """<STR_LIT>""" <EOL> return numbers . parse_decimal ( string , locale = self . locale ) <EOL> def get_timezone_location ( self , dt_or_tzinfo ) : <EOL> """<STR_LIT>""" <EOL> return dates . get_timezone_name ( dt_or_tzinfo , locale = self . locale ) <EOL> def gettext ( string , ** variables ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . gettext ( string , ** variables ) <EOL> def ngettext ( singular , plural , n , ** variables ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . ngettext ( singular , plural , n , ** variables ) <EOL> def to_local_timezone ( datetime ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . to_local_timezone ( datetime ) <EOL> def to_utc ( datetime ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . to_utc ( datetime ) <EOL> def format_date ( date = None , format = None , rebase = True ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . format_date ( date , format , rebase ) <EOL> def format_datetime ( datetime = None , format = None , rebase = True ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . format_datetime ( datetime , format , rebase ) <EOL> def format_time ( time = None , format = None , rebase = True ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . format_time ( time , format , rebase ) <EOL> def format_timedelta ( datetime_or_timedelta , granularity = '<STR_LIT>' , <EOL> threshold = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . format_timedelta ( datetime_or_timedelta , <EOL> granularity , threshold ) <EOL> def format_number ( number ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . format_number ( number ) <EOL> def format_decimal ( number , format = None ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . format_decimal ( number , format ) <EOL> def format_currency ( number , currency , format = None ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . format_currency ( number , currency , format ) <EOL> def format_percent ( number , format = None ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . format_percent ( number , format ) <EOL> def format_scientific ( number , format = None ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . format_scientific ( number , format ) <EOL> def parse_date ( string ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . parse_date ( string ) <EOL> def parse_datetime ( string ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . parse_datetime ( string ) <EOL> def parse_time ( string ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . parse_time ( string ) <EOL> def parse_number ( string ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . parse_number ( string ) <EOL> def parse_decimal ( string ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . parse_decimal ( string ) <EOL> def get_timezone_location ( dt_or_tzinfo ) : <EOL> """<STR_LIT>""" <EOL> return get_i18n ( ) . get_timezone_location ( dt_or_tzinfo ) <EOL> def lazy_gettext ( string , ** variables ) : <EOL> """<STR_LIT>""" <EOL> return support . LazyProxy ( gettext , string , ** variables ) <EOL> _ = gettext <EOL> _lazy = lazy_gettext <EOL> _store_registry_key = '<STR_LIT>' <EOL> _i18n_registry_key = '<STR_LIT>' <EOL> def get_store ( factory = I18nStore , key = _store_registry_key , app = None ) : <EOL> """<STR_LIT>""" <EOL> app = app or webapp2 . get_app ( ) <EOL> store = app . registry . get ( key ) <EOL> if not store : <EOL> store = app . registry [ key ] = factory ( app ) <EOL> return store <EOL> def set_store ( store , key = _store_registry_key , app = None ) : <EOL> """<STR_LIT>""" <EOL> app = app or webapp2 . get_app ( ) <EOL> app . registry [ key ] = store <EOL> def get_i18n ( factory = I18n , key = _i18n_registry_key , request = None ) : <EOL> """<STR_LIT>""" <EOL> request = request or webapp2 . get_request ( ) <EOL> i18n = request . registry . get ( key ) <EOL> if not i18n : <EOL> i18n = request . registry [ key ] = factory ( request ) <EOL> return i18n <EOL> def set_i18n ( i18n , key = _i18n_registry_key , request = None ) : <EOL> """<STR_LIT>""" <EOL> request = request or webapp2 . get_request ( ) <EOL> request . registry [ key ] = i18n </s>
