<s> """<STR_LIT>""" <EOL> import re <EOL> from xml . etree import ElementTree <EOL> import ipaddr <EOL> from google . appengine . tools import xml_parser_utils <EOL> from google . appengine . tools . app_engine_config_exception import AppEngineConfigException <EOL> MISSING_SUBNET = '<STR_LIT>' <EOL> BAD_IPV_SUBNET = '<STR_LIT>' <EOL> BAD_PREFIX_LENGTH = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def GetDosYaml ( unused_application , dos_xml_str ) : <EOL> return _MakeDosListIntoYaml ( DosXmlParser ( ) . ProcessXml ( dos_xml_str ) ) <EOL> def _MakeDosListIntoYaml ( dos_list ) : <EOL> """<STR_LIT>""" <EOL> statements = [ '<STR_LIT>' ] <EOL> for entry in dos_list : <EOL> statements += entry . ToYaml ( ) <EOL> return '<STR_LIT:\n>' . join ( statements ) + '<STR_LIT:\n>' <EOL> class DosXmlParser ( object ) : <EOL> """<STR_LIT>""" <EOL> def ProcessXml ( self , xml_str ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . blacklist_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 . ProcessBlacklistNode ( child ) <EOL> if self . errors : <EOL> raise AppEngineConfigException ( '<STR_LIT:\n>' . join ( self . errors ) ) <EOL> return self . blacklist_entries <EOL> except ElementTree . ParseError : <EOL> raise AppEngineConfigException ( '<STR_LIT>' ) <EOL> def ProcessBlacklistNode ( 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 = BlacklistEntry ( ) <EOL> entry . subnet = xml_parser_utils . GetChildNodeText ( node , '<STR_LIT>' ) <EOL> entry . description = xml_parser_utils . GetChildNodeText ( node , '<STR_LIT:description>' ) <EOL> validation = self . _ValidateEntry ( entry ) <EOL> if validation : <EOL> self . errors . append ( validation ) <EOL> return <EOL> self . blacklist_entries . append ( entry ) <EOL> def _ValidateEntry ( self , entry ) : <EOL> if not entry . subnet : <EOL> return MISSING_SUBNET <EOL> try : <EOL> ipaddr . IPNetwork ( entry . subnet ) <EOL> except ValueError : <EOL> return BAD_IPV_SUBNET % entry . subnet <EOL> parts = entry . subnet . split ( '<STR_LIT:/>' ) <EOL> if len ( parts ) == <NUM_LIT:2> and not re . match ( '<STR_LIT>' , parts [ <NUM_LIT:1> ] ) : <EOL> return BAD_PREFIX_LENGTH % entry . subnet <EOL> class BlacklistEntry ( object ) : <EOL> """<STR_LIT>""" <EOL> def ToYaml ( self ) : <EOL> statements = [ '<STR_LIT>' % self . subnet ] <EOL> if self . description : <EOL> statements . append ( <EOL> '<STR_LIT>' % self . _SanitizeForYaml ( self . description ) ) <EOL> return statements <EOL> def _SanitizeForYaml ( self , dirty_str ) : <EOL> return "<STR_LIT>" % dirty_str . replace ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' ) </s>
