<s> """<STR_LIT>""" <EOL> from xml . etree import ElementTree <EOL> from google . appengine . tools import xml_parser_utils <EOL> from google . appengine . tools . app_engine_config_exception import AppEngineConfigException <EOL> MISSING_URL = '<STR_LIT>' <EOL> MISSING_MODULE = '<STR_LIT>' <EOL> def GetDispatchYaml ( application , dispatch_xml_str ) : <EOL> return _MakeDispatchListIntoYaml ( <EOL> application , DispatchXmlParser ( ) . ProcessXml ( dispatch_xml_str ) ) <EOL> def _MakeDispatchListIntoYaml ( application , dispatch_list ) : <EOL> """<STR_LIT>""" <EOL> statements = [ <EOL> '<STR_LIT>' % application , <EOL> '<STR_LIT>' , <EOL> ] <EOL> for entry in dispatch_list : <EOL> statements += entry . ToYaml ( ) <EOL> return '<STR_LIT:\n>' . join ( statements ) + '<STR_LIT:\n>' <EOL> class DispatchXmlParser ( object ) : <EOL> """<STR_LIT>""" <EOL> def ProcessXml ( self , xml_str ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . dispatch_entries = [ ] <EOL> self . errors = [ ] <EOL> xml_root = ElementTree . fromstring ( xml_str ) <EOL> if xml_root . tag != '<STR_LIT>' : <EOL> raise AppEngineConfigException ( '<STR_LIT>' ) <EOL> for child in xml_root . getchildren ( ) : <EOL> self . ProcessDispatchNode ( child ) <EOL> if self . errors : <EOL> raise AppEngineConfigException ( '<STR_LIT:\n>' . join ( self . errors ) ) <EOL> return self . dispatch_entries <EOL> except ElementTree . ParseError : <EOL> raise AppEngineConfigException ( '<STR_LIT>' ) <EOL> def ProcessDispatchNode ( self , node ) : <EOL> """<STR_LIT>""" <EOL> tag = xml_parser_utils . GetTag ( node ) <EOL> if tag != '<STR_LIT>' : <EOL> self . errors . append ( '<STR_LIT>' % tag ) <EOL> return <EOL> entry = DispatchEntry ( ) <EOL> entry . url = xml_parser_utils . GetChildNodeText ( node , '<STR_LIT:url>' ) <EOL> entry . module = xml_parser_utils . GetChildNodeText ( node , '<STR_LIT>' ) <EOL> validation = self . _ValidateEntry ( entry ) <EOL> if validation : <EOL> self . errors . append ( validation ) <EOL> return <EOL> self . dispatch_entries . append ( entry ) <EOL> def _ValidateEntry ( self , entry ) : <EOL> if not entry . url : <EOL> return MISSING_URL <EOL> if not entry . module : <EOL> return MISSING_MODULE <EOL> class DispatchEntry ( object ) : <EOL> """<STR_LIT>""" <EOL> def ToYaml ( self ) : <EOL> return [ <EOL> "<STR_LIT>" % self . _SanitizeForYaml ( self . url ) , <EOL> '<STR_LIT>' % self . module , <EOL> ] <EOL> def _SanitizeForYaml ( self , dirty_str ) : <EOL> return dirty_str . replace ( "<STR_LIT:'>" , r"<STR_LIT>" ) </s>
