<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import re <EOL> import time <EOL> try : <EOL> from google . appengine . api import memcache <EOL> from google . appengine . ext import db <EOL> from google . appengine . api import validation <EOL> from google . appengine . api import yaml_object <EOL> except : <EOL> from google . appengine . api import memcache <EOL> from google . appengine . ext import db <EOL> from google . appengine . ext import validation <EOL> from google . appengine . ext import yaml_object <EOL> DATASTORE_DEADLINE = <NUM_LIT> <EOL> RESERVED_MARKER = '<STR_LIT>' <EOL> NAMESPACE = '<STR_LIT:_>' + RESERVED_MARKER <EOL> CONFIG_KIND = '<STR_LIT>' <EOL> ACTIVE_KEY_NAME = '<STR_LIT>' <EOL> FILENAMES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> PARAMETERS = '<STR_LIT>' <EOL> PARAMETER_NAME_REGEX = '<STR_LIT>' <EOL> _cached_config = None <EOL> class Config ( db . Expando ) : <EOL> """<STR_LIT>""" <EOL> ah__conf__version = db . IntegerProperty ( default = <NUM_LIT:0> , required = True ) <EOL> @ classmethod <EOL> def kind ( cls ) : <EOL> """<STR_LIT>""" <EOL> return CONFIG_KIND <EOL> def ah__conf__load_from_yaml ( self , parsed_config ) : <EOL> """<STR_LIT>""" <EOL> for key , value in parsed_config . parameters . iteritems ( ) : <EOL> setattr ( self , key , value ) <EOL> class _ValidParameterName ( validation . Validator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . regex = validation . Regex ( PARAMETER_NAME_REGEX ) <EOL> def Validate ( self , value , key ) : <EOL> """<STR_LIT>""" <EOL> value = self . regex . Validate ( value , key ) <EOL> try : <EOL> db . check_reserved_word ( value ) <EOL> except db . ReservedWordError : <EOL> raise validation . ValidationError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % value ) <EOL> if value . startswith ( RESERVED_MARKER ) : <EOL> raise validation . ValidationError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> value , RESERVED_MARKER ) ) <EOL> return value <EOL> class _Scalar ( validation . Validator ) : <EOL> """<STR_LIT>""" <EOL> ALLOWED_PARAMETER_VALUE_TYPES = frozenset ( <EOL> [ bool , int , long , float , str , unicode ] ) <EOL> def Validate ( self , value , key ) : <EOL> """<STR_LIT>""" <EOL> if type ( value ) not in self . ALLOWED_PARAMETER_VALUE_TYPES : <EOL> raise validation . ValidationError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( key , value , type ( value ) . __name__ ) ) <EOL> return value <EOL> class _ParameterDict ( validation . ValidatedDict ) : <EOL> """<STR_LIT>""" <EOL> KEY_VALIDATOR = _ValidParameterName ( ) <EOL> VALUE_VALIDATOR = _Scalar ( ) <EOL> class YAMLConfiguration ( validation . Validated ) : <EOL> """<STR_LIT>""" <EOL> ATTRIBUTES = { PARAMETERS : _ParameterDict } <EOL> def LoadSingleConf ( stream ) : <EOL> """<STR_LIT>""" <EOL> return yaml_object . BuildSingleObject ( YAMLConfiguration , stream ) <EOL> def _find_yaml_path ( ) : <EOL> """<STR_LIT>""" <EOL> current , last = os . getcwd ( ) , None <EOL> while current != last : <EOL> for yaml_name in FILENAMES : <EOL> yaml_path = os . path . join ( current , yaml_name ) <EOL> if os . path . exists ( yaml_path ) : <EOL> return yaml_path <EOL> last = current <EOL> current , last = os . path . dirname ( current ) , current <EOL> return None <EOL> def _fetch_from_local_file ( pathfinder = _find_yaml_path , fileopener = open ) : <EOL> """<STR_LIT>""" <EOL> yaml_path = pathfinder ( ) <EOL> if yaml_path : <EOL> config = Config ( ) <EOL> config . ah__conf__load_from_yaml ( LoadSingleConf ( fileopener ( yaml_path ) ) ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return config <EOL> return None <EOL> def _get_active_config_key ( app_version ) : <EOL> """<STR_LIT>""" <EOL> return db . Key . from_path ( <EOL> CONFIG_KIND , <EOL> '<STR_LIT>' % ( app_version , ACTIVE_KEY_NAME ) , <EOL> namespace = NAMESPACE ) <EOL> def _fetch_latest_from_datastore ( app_version ) : <EOL> """<STR_LIT>""" <EOL> rpc = db . create_rpc ( deadline = DATASTORE_DEADLINE , <EOL> read_policy = db . EVENTUAL_CONSISTENCY ) <EOL> key = _get_active_config_key ( app_version ) <EOL> config = None <EOL> try : <EOL> config = Config . get ( key , rpc = rpc ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> except : <EOL> logging . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if config : <EOL> memcache . set ( app_version , db . model_to_protobuf ( config ) . Encode ( ) , <EOL> namespace = NAMESPACE ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return config <EOL> def _fetch_latest_from_memcache ( app_version ) : <EOL> """<STR_LIT>""" <EOL> proto_string = memcache . get ( app_version , namespace = NAMESPACE ) <EOL> if proto_string : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return db . model_from_protobuf ( proto_string ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return None <EOL> def _inspect_environment ( ) : <EOL> """<STR_LIT>""" <EOL> app_version = os . environ [ '<STR_LIT>' ] . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> conf_version = int ( os . environ . get ( '<STR_LIT>' , '<STR_LIT:0>' ) ) <EOL> development = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . startswith ( '<STR_LIT>' ) <EOL> return ( app_version , conf_version , development ) <EOL> def refresh ( ) : <EOL> """<STR_LIT>""" <EOL> app_version , _ , _ = _inspect_environment ( ) <EOL> global _cached_config <EOL> new_config = _fetch_latest_from_memcache ( app_version ) <EOL> if not new_config : <EOL> new_config = _fetch_latest_from_datastore ( app_version ) <EOL> if new_config : <EOL> _cached_config = new_config <EOL> def _new_request ( ) : <EOL> """<STR_LIT>""" <EOL> if RESERVED_MARKER in os . environ : <EOL> return False <EOL> os . environ [ RESERVED_MARKER ] = RESERVED_MARKER <EOL> return True <EOL> def _get_config ( ) : <EOL> """<STR_LIT>""" <EOL> app_version , current_config_version , development = _inspect_environment ( ) <EOL> global _cached_config <EOL> if ( development and _new_request ( ) ) or not _cached_config : <EOL> _cached_config = _fetch_from_local_file ( ) or Config ( ) <EOL> if _cached_config . ah__conf__version < current_config_version : <EOL> newconfig = _fetch_latest_from_memcache ( app_version ) <EOL> if not newconfig or newconfig . ah__conf__version < current_config_version : <EOL> newconfig = _fetch_latest_from_datastore ( app_version ) <EOL> _cached_config = newconfig or _cached_config <EOL> return _cached_config <EOL> def get ( name , default = None ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( _get_config ( ) , name , default ) <EOL> def get_all ( ) : <EOL> """<STR_LIT>""" <EOL> return _get_config ( ) </s>
