<s> from babel import localedata <EOL> from grow . pods import errors <EOL> from grow . pods import messages <EOL> import pickle <EOL> import os <EOL> import babel <EOL> import re <EOL> class Locales ( object ) : <EOL> def __init__ ( self , pod ) : <EOL> self . pod = pod <EOL> def list_groups ( self ) : <EOL> if '<STR_LIT>' not in self . pod . yaml : <EOL> return [ ] <EOL> return self . pod . yaml [ '<STR_LIT>' ] . keys ( ) <EOL> def get_regions ( self , group_name = '<STR_LIT:default>' ) : <EOL> if '<STR_LIT>' not in self . pod . yaml : <EOL> return [ ] <EOL> try : <EOL> return self . pod . yaml [ '<STR_LIT>' ] [ group_name ] . get ( '<STR_LIT>' , [ ] ) <EOL> except errors . PodConfigurationError : <EOL> return [ ] <EOL> def get_languages ( self , group_name = '<STR_LIT:default>' ) : <EOL> if '<STR_LIT>' not in self . pod . yaml : <EOL> return [ ] <EOL> try : <EOL> return self . pod . yaml [ '<STR_LIT>' ] [ group_name ] . get ( '<STR_LIT>' , [ ] ) <EOL> except errors . PodConfigurationError : <EOL> return [ ] <EOL> def to_message ( self ) : <EOL> message = messages . LocalesMessage ( ) <EOL> message . groups = [ ] <EOL> for group_name in self . list_groups ( ) : <EOL> group_message = messages . LocaleGroupMessage ( ) <EOL> group_message . group_name = group_name <EOL> group_message . regions = self . get_regions ( group_name ) <EOL> group_message . languages = self . get_languages ( group_name ) <EOL> message . groups . append ( group_message ) <EOL> return message <EOL> class Locale ( babel . Locale ) : <EOL> RTL_REGEX = re . compile ( '<STR_LIT>' ) <EOL> _alias = None <EOL> def __init__ ( self , language , * args , ** kwargs ) : <EOL> parts = language . rsplit ( '<STR_LIT:_>' , <NUM_LIT:1> ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> language = '<STR_LIT>' . format ( parts [ <NUM_LIT:0> ] , parts [ <NUM_LIT:1> ] . upper ( ) ) <EOL> super ( Locale , self ) . __init__ ( language , * args , ** kwargs ) <EOL> @ classmethod <EOL> def parse ( cls , * args , ** kwargs ) : <EOL> locale = super ( Locale , cls ) . parse ( * args , ** kwargs ) <EOL> if locale and locale . get_display_name ( ) is None : <EOL> raise ValueError ( '<STR_LIT>' . format ( args [ <NUM_LIT:0> ] ) ) <EOL> return locale <EOL> def __hash__ ( self ) : <EOL> return hash ( str ( self ) ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , basestring ) : <EOL> return str ( self ) . lower ( ) == other . lower ( ) <EOL> return super ( Locale , self ) . __eq__ ( other ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> @ classmethod <EOL> def parse_codes ( cls , codes ) : <EOL> return [ cls . parse ( code ) for code in codes ] <EOL> @ property <EOL> def is_rtl ( self ) : <EOL> return Locale . RTL_REGEX . match ( self . language ) <EOL> @ property <EOL> def direction ( self ) : <EOL> return '<STR_LIT>' if self . is_rtl else '<STR_LIT>' <EOL> @ classmethod <EOL> def from_alias ( cls , pod , alias ) : <EOL> podspec = pod . get_podspec ( ) <EOL> config = podspec . get_config ( ) <EOL> if '<STR_LIT>' in config and '<STR_LIT>' in config [ '<STR_LIT>' ] : <EOL> aliases = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> for custom_locale , babel_locale in aliases . iteritems ( ) : <EOL> if custom_locale == alias : <EOL> return cls . parse ( babel_locale ) <EOL> return cls . parse ( alias ) <EOL> def set_alias ( self , pod ) : <EOL> normalized_locale = str ( self ) . lower ( ) <EOL> podspec = pod . get_podspec ( ) <EOL> config = podspec . get_config ( ) <EOL> if '<STR_LIT>' in config and '<STR_LIT>' in config [ '<STR_LIT>' ] : <EOL> aliases = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> for custom_locale , babel_locale in aliases . iteritems ( ) : <EOL> normalized_babel_locale = babel_locale . lower ( ) <EOL> if normalized_locale == normalized_babel_locale : <EOL> normalized_locale = normalized_locale . replace ( <EOL> normalized_babel_locale , custom_locale ) <EOL> self . _alias = normalized_locale <EOL> @ property <EOL> def alias ( self ) : <EOL> return self . _alias <EOL> @ alias . setter <EOL> def alias ( self , alias ) : <EOL> self . _alias = alias <EOL> def fuzzy_load ( name , merge_inherited = True ) : <EOL> localedata . _cache_lock . acquire ( ) <EOL> try : <EOL> data = localedata . _cache . get ( name ) <EOL> if not data : <EOL> if name == '<STR_LIT:root>' or not merge_inherited : <EOL> data = { } <EOL> else : <EOL> parts = name . split ( '<STR_LIT:_>' ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> parent = '<STR_LIT:root>' <EOL> else : <EOL> parent = '<STR_LIT:_>' . join ( parts [ : - <NUM_LIT:1> ] ) <EOL> data = fuzzy_load ( parent ) . copy ( ) <EOL> filename = os . path . join ( localedata . _dirname , '<STR_LIT>' % name ) <EOL> try : <EOL> fileobj = open ( filename , '<STR_LIT:rb>' ) <EOL> try : <EOL> if name != '<STR_LIT:root>' and merge_inherited : <EOL> localedata . merge ( data , pickle . load ( fileobj ) ) <EOL> else : <EOL> data = pickle . load ( fileobj ) <EOL> localedata . _cache [ name ] = data <EOL> finally : <EOL> fileobj . close ( ) <EOL> except IOError : <EOL> pass <EOL> return data <EOL> finally : <EOL> localedata . _cache_lock . release ( ) <EOL> localedata . exists = lambda name : True <EOL> localedata . load = fuzzy_load </s>
