
                             S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SrS	rS
 rS r " S S\5      r " S S\5      rg)zDirectly processes text of dispatch.xml.

DispatchXmlParser is called with an XML string to produce a list of
DispatchEntry objects containing the data from the XML.
    )absolute_import)division)unicode_literals)ElementTree)xml_parser_utils)AppEngineConfigExceptionz$<dispatch> node must contain a <url>z'<dispatch> node must contain a <module>c                 H    [        U [        5       R                  U5      5      $ N)_MakeDispatchListIntoYamlDispatchXmlParser
ProcessXml)applicationdispatch_xml_strs     9lib/googlecloudsdk/appengine/tools/dispatch_xml_parser.pyGetDispatchYamlr   "   s&    	"$&112BC
E E    c                     / nU (       a  UR                  SU -  5        UR                  S5        U H  nX#R                  5       -  nM     SR                  U5      S-   $ )z:Converts list of DispatchEntry objects into a YAML string.zapplication: %sz	dispatch:
)appendToYamljoin)r   dispatch_list
statementsentrys       r   r   r   '   sX    *'+56K e,,. J 	:		%%r   c                   *    \ rS rSrSrS rS rS rSrg)r   2   z:Provides logic for walking down XML tree and pulling data.c                     / U l         / U l        [        R                  " U5      nUR                  S:w  a  [        S5      e[        U5       H  nU R                  U5        M     U R                  (       a$  [        SR                  U R                  5      5      eU R                   $ ! [        R                   a    [        S5      ef = f)a  Parses XML string and returns object representation of relevant info.

Args:
  xml_str: The XML string.
Returns:
  A list of DispatchEntry objects defining how URLs are dispatched to
  modules.
Raises:
  AppEngineConfigException: In case of malformed XML or illegal inputs.
zdispatch-entriesz#Root tag must be <dispatch-entries>r   zBad input -- not valid XML)
dispatch_entrieserrorsr   
fromstringtagr   listProcessDispatchNoder   
ParseError)selfxml_strxml_rootchilds       r   r   DispatchXmlParser.ProcessXml5   s    C ddk''0h	+	+&'LMM>%  ' " 
&tyy'=>>"""!! C$%ABBCs   B"B% % Cc                    [         R                  " U5      nUS:w  a  U R                  R                  SU-  5        g[	        5       n[         R
                  " US5      Ul        [         R
                  " US5      Ul        U R                  U5      nU(       a  U R                  R                  U5        gU R                  R                  U5        g)a  Processes XML <dispatch> nodes into DispatchEntry objects.

The following information is parsed out:
  url: The URL or URL pattern to route.
  module: The module to route it to.
If there are no errors, the data is loaded into a DispatchEntry object
and added to a list. Upon error, a description of the error is added to
a list and the method terminates.

Args:
  node: <dispatch> XML node in dos.xml.
dispatchzUnrecognized node: <%s>Nurlmodule)
r   GetTagr   r   DispatchEntryGetChildNodeTextr,   r-   _ValidateEntryr   )r%   noder!   r   
validations        r   r#   %DispatchXmlParser.ProcessDispatchNodeR   s     
!
!$
'C
j
kk2S89OE 11$>EI#44T8DEL$$U+J
kk$  'r   c                 `    UR                   (       d  [        $ UR                  (       d  [        $ g r
   )r,   MISSING_URLr-   MISSING_MODULE)r%   r   s     r   r1    DispatchXmlParser._ValidateEntryn   s!    99<< r   )r   r   N)	__name__
__module____qualname____firstlineno____doc__r   r#   r1   __static_attributes__ r   r   r   r   2   s    BC:(8r   r   c                   $    \ rS rSrSrS rS rSrg)r/   u   z@Instances contain information about individual dispatch entries.c                 \    SU R                  U R                  5      -  SU R                  -  /$ )Nz- url: '%s'z  module: %s)_SanitizeForYamlr,   r-   )r%   s    r   r   DispatchEntry.ToYamlx   s/    --dhh77$ r   c                 &    UR                  SS5      $ )N'z\')replace)r%   	dirty_strs     r   rC   DispatchEntry._SanitizeForYaml~   s    S%((r   r?   N)r9   r:   r;   r<   r=   r   rC   r>   r?   r   r   r/   r/   u   s    H)r   r/   N)r=   
__future__r   r   r   	xml.etreer   googlecloudsdk.appengine.toolsr   :googlecloudsdk.appengine.tools.app_engine_config_exceptionr   r6   r7   r   r   objectr   r/   r?   r   r   <module>rO      sN    '  ' ! ; _4:E
&@ @F
)F 
)r   