o
    p.a>                  	   @   s   d dl Z d dlZd dlZd dlZze W n ey#   d dlmZ Y nw dddZdddZ		dd	d
Z	ddddddddZ
e ddee je
  ZdddZdS )    N)reduceutf-8Fc                 C   s.   d| g}|r| d t|| | d|S )ay   Writes the XML content to disk, touching the file only if it has changed.

  Visual Studio files have a lot of pre-defined structures.  This function makes
  it easy to represent these structures as Python data structures, instead of
  having to create a lot of function calls.

  Each XML element of the content is represented as a list composed of:
  1. The name of the element, a string,
  2. The attributes of the element, a dictionary (optional), and
  3+. The content of the element, if any.  Strings are simple text nodes and
      lists are child elements.

  Example 1:
      <test/>
  becomes
      ['test']

  Example 2:
      <myelement a='value1' b='value2'>
         <childtype>This is</childtype>
         <childtype>it!</childtype>
      </myelement>

  becomes
      ['myelement', {'a':'value1', 'b':'value2'},
         ['childtype', 'This is'],
         ['childtype', 'it!'],
      ]

  Args:
    content:  The structured content to be converted.
    encoding: The encoding to report on the first XML line.
    pretty: True if we want pretty printing with indents and new lines.

  Returns:
    The XML content as a string.
  z#<?xml version="1.0" encoding="%s"?>
 )append_ConstructContentListjoin)contentencodingpretty	xml_parts r   ./usr/lib/python3/dist-packages/gyp/easy_xml.pyXmlToString   s
   
'

r   c              	   C   sR  |r	d| }d}nd}d}|d }t |tstdt| | |d |  |dd }|rVt |d trVt|d  D ]\}}	| d	|t|	d
df  q>|dd }|r| d tdd |d
}
|
 }|rq|rq| | |D ]}t |tr| t| qst	| |||d  qs|r|r| | | d||f  dS | d|  dS )a+   Appends the XML parts corresponding to the specification.

  Args:
    xml_parts: A list of XML parts to be appended to.
    specification:  The specification of the element.  See EasyXml docs.
    pretty: True if we want pretty printing with indents and new lines.
    level: Indentation level.
  z  r   r   r   zRThe first item of an EasyXml specification should be a string.  Specification was <   Nz %s="%s"Tattr>c                 S   s   | ot |tS )N)
isinstancestr)xyr   r   r   <lambda>^   s    z'_ConstructContentList.<locals>.<lambda>z</%s>%sz/>%s)
r   r   	Exceptionr   dictsorteditems
_XmlEscaper   r   )r   specificationr   levelindentationnew_linenamerestatvalall_strings
multi_line
child_specr   r   r   r   @   s<   





r   c           	      C   s   t | ||}|r7tjdkr7|dd}t d }|r7| | kr7z
|||}W n	 t	y6   Y nw zt
|d}| }|  W n   d}Y ||krat
|d}|| |  dS dS )a:   Writes the XML content to disk, touching the file only if it has changed.

  Args:
    content:  The structured content to be written.
    path: Location of the file.
    encoding: The encoding to report on the first line of the XML file.
    pretty: True if we want pretty printing with indents and new lines.
  z
r   r   rNw)r   oslinesepreplacelocalegetdefaultlocaleupperdecodeencodeAttributeErroropenreadclosewrite)	r	   pathr
   r   win32
xml_stringdefault_encodingfexistingr   r   r   WriteXmlIfChangedp   s*   



r?   z&quot;z&apos;z&lt;z&gt;z&amp;z&#xA;z&#xD;)"'r   r   &r   z(%s)|c                    s    fdd}t || S )z& Escape a string for inclusion in XML.c                    s.   | j |  |   } r|dkr|S t| S )NrA   )stringstartend_xml_escape_map)matchmr   r   r   r.      s   z_XmlEscape.<locals>.replace)_xml_escape_resub)valuer   r.   r   r   r   r      s   r   )r   F)r   )r   FF)F)rer,   r/   sysr   	NameError	functoolsr   r   r?   rH   compiler   mapescapekeysrK   r   r   r   r   r   <module>   s4   

00
$