<s> """<STR_LIT>""" <EOL> import yaml <EOL> from yaml import representer <EOL> from google . appengine . api import validation <EOL> from google . appengine . api import yaml_builder <EOL> from google . appengine . api import yaml_listener <EOL> from google . appengine . api import yaml_object <EOL> NAME_REGEX = r'<STR_LIT>' <EOL> FILE_REGEX = r'<STR_LIT>' <EOL> CLASS_REGEX = r'<STR_LIT>' <EOL> OPTIONS_REGEX = r'<STR_LIT>' <EOL> STATE_REGEX = r'<STR_LIT>' <EOL> BACKENDS = '<STR_LIT>' <EOL> NAME = '<STR_LIT:name>' <EOL> CLASS = '<STR_LIT:class>' <EOL> INSTANCES = '<STR_LIT>' <EOL> OPTIONS = '<STR_LIT>' <EOL> PUBLIC = '<STR_LIT>' <EOL> DYNAMIC = '<STR_LIT>' <EOL> FAILFAST = '<STR_LIT>' <EOL> MAX_CONCURRENT_REQUESTS = '<STR_LIT>' <EOL> START = '<STR_LIT:start>' <EOL> VALID_OPTIONS = frozenset ( [ PUBLIC , DYNAMIC , FAILFAST ] ) <EOL> STATE = '<STR_LIT:state>' <EOL> class BadConfig ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class ListWithoutSort ( list ) : <EOL> def sort ( self ) : <EOL> pass <EOL> class SortedDict ( dict ) : <EOL> def __init__ ( self , keys , data ) : <EOL> super ( SortedDict , self ) . __init__ ( ) <EOL> self . keys = keys <EOL> self . update ( data ) <EOL> def items ( self ) : <EOL> result = ListWithoutSort ( ) <EOL> for key in self . keys : <EOL> if type ( self . get ( key ) ) != type ( None ) : <EOL> result . append ( ( key , self . get ( key ) ) ) <EOL> return result <EOL> representer . SafeRepresenter . add_representer ( <EOL> SortedDict , representer . SafeRepresenter . represent_dict ) <EOL> class BackendEntry ( validation . Validated ) : <EOL> """<STR_LIT>""" <EOL> ATTRIBUTES = { <EOL> NAME : NAME_REGEX , <EOL> CLASS : validation . Optional ( CLASS_REGEX ) , <EOL> INSTANCES : validation . Optional ( validation . TYPE_INT ) , <EOL> MAX_CONCURRENT_REQUESTS : validation . Optional ( validation . TYPE_INT ) , <EOL> OPTIONS : validation . Optional ( OPTIONS_REGEX ) , <EOL> PUBLIC : validation . Optional ( validation . TYPE_BOOL ) , <EOL> DYNAMIC : validation . Optional ( validation . TYPE_BOOL ) , <EOL> FAILFAST : validation . Optional ( validation . TYPE_BOOL ) , <EOL> START : validation . Optional ( FILE_REGEX ) , <EOL> STATE : validation . Optional ( STATE_REGEX ) , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( BackendEntry , self ) . __init__ ( * args , ** kwargs ) <EOL> self . Init ( ) <EOL> def Init ( self ) : <EOL> if self . public : <EOL> raise BadConfig ( "<STR_LIT>" ) <EOL> if self . dynamic : <EOL> raise BadConfig ( "<STR_LIT>" ) <EOL> if self . failfast : <EOL> raise BadConfig ( "<STR_LIT>" ) <EOL> self . ParseOptions ( ) <EOL> return self <EOL> def set_class ( self , Class ) : <EOL> """<STR_LIT>""" <EOL> self . Set ( CLASS , Class ) <EOL> def get_class ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . Get ( CLASS ) <EOL> def ToDict ( self ) : <EOL> """<STR_LIT>""" <EOL> self . ParseOptions ( ) . WriteOptions ( ) <EOL> result = super ( BackendEntry , self ) . ToDict ( ) <EOL> return SortedDict ( [ NAME , <EOL> CLASS , <EOL> INSTANCES , <EOL> START , <EOL> OPTIONS , <EOL> MAX_CONCURRENT_REQUESTS , <EOL> STATE ] , <EOL> result ) <EOL> def ParseOptions ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . options : <EOL> options = [ option . strip ( ) for option in self . options . split ( '<STR_LIT:U+002C>' ) ] <EOL> else : <EOL> options = [ ] <EOL> for option in options : <EOL> if option not in VALID_OPTIONS : <EOL> raise BadConfig ( '<STR_LIT>' , option ) <EOL> self . public = PUBLIC in options <EOL> self . dynamic = DYNAMIC in options <EOL> self . failfast = FAILFAST in options <EOL> return self <EOL> def WriteOptions ( self ) : <EOL> """<STR_LIT>""" <EOL> options = [ ] <EOL> if self . public : <EOL> options . append ( '<STR_LIT>' ) <EOL> if self . dynamic : <EOL> options . append ( '<STR_LIT>' ) <EOL> if self . failfast : <EOL> options . append ( '<STR_LIT>' ) <EOL> if options : <EOL> self . options = '<STR_LIT:U+002CU+0020>' . join ( options ) <EOL> else : <EOL> self . options = None <EOL> return self <EOL> def LoadBackendEntry ( backend_entry ) : <EOL> """<STR_LIT>""" <EOL> builder = yaml_object . ObjectBuilder ( BackendEntry ) <EOL> handler = yaml_builder . BuilderHandler ( builder ) <EOL> listener = yaml_listener . EventListener ( handler ) <EOL> listener . Parse ( backend_entry ) <EOL> entries = handler . GetResults ( ) <EOL> if len ( entries ) < <NUM_LIT:1> : <EOL> raise BadConfig ( '<STR_LIT>' ) <EOL> if len ( entries ) > <NUM_LIT:1> : <EOL> raise BadConfig ( '<STR_LIT>' ) <EOL> return entries [ <NUM_LIT:0> ] . Init ( ) <EOL> class BackendInfoExternal ( validation . Validated ) : <EOL> """<STR_LIT>""" <EOL> ATTRIBUTES = { <EOL> BACKENDS : validation . Optional ( validation . Repeated ( BackendEntry ) ) , <EOL> } <EOL> def LoadBackendInfo ( backend_info ) : <EOL> """<STR_LIT>""" <EOL> builder = yaml_object . ObjectBuilder ( BackendInfoExternal ) <EOL> handler = yaml_builder . BuilderHandler ( builder ) <EOL> listener = yaml_listener . EventListener ( handler ) <EOL> listener . Parse ( backend_info ) <EOL> backend_info = handler . GetResults ( ) <EOL> if len ( backend_info ) < <NUM_LIT:1> : <EOL> return BackendInfoExternal ( backends = [ ] ) <EOL> if len ( backend_info ) > <NUM_LIT:1> : <EOL> raise BadConfig ( "<STR_LIT>" ) <EOL> info = backend_info [ <NUM_LIT:0> ] <EOL> for backend in info . backends : <EOL> backend . Init ( ) <EOL> return info </s>
