a
    /$ZW                     @   s   d Z ddlmZ ddlZejdkr*eZeZddlZddl	Z	ddl
Z
ddlZddlmZmZmZmZ ddlmZmZmZmZmZmZmZ eeZG dd	 d	eZdS )
zSimple XML manipulation    )unicode_literalsN3   )
__author____copyright____license____version__)TYPE_MAPTYPE_MARSHAL_FNTYPE_UNMARSHAL_FNREVERSE_TYPE_MAPStructDateDecimalc                   @   s^  e Zd ZdZdddddi dfddZdIddZd	d
 Zdd Zdd ZdJddZ	e
jdkrddd Zndd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& ZdKd'd(Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Ze
jdkreZnd7d8 Zd9d: Zd;d< Z e!d=d> Z"dLd?d@Z#dAdB Z$dMdCdDZ%dEdF Z&dNdGdHZ'dS )OSimpleXMLElementz#Simple XML manipulation (simil PHP)NFc           	      C   s   || _ d| }td| | _|| _|| _|| _|durzzt	j
j|| _W n   t|  Y n0 | jjg| _n|| _|| _dS )z
        :param namespaces_map: How to map our namespace prefix to that given by the client;
          {prefix: received_prefix}
        |z	^(%s):.*$N)!_SimpleXMLElement__namespaces_mapjoinkeysrecompile_SimpleXMLElement__ns_rx_SimpleXMLElement__ns_SimpleXMLElement__prefix_SimpleXMLElement__jettyxmldomminidomZparseString_SimpleXMLElement__documentlogerrordocumentElement_SimpleXMLElement__elements)	selftextelementsdocument	namespaceprefixnamespaces_mapjettyZ_rx r+   8/usr/lib/python3/dist-packages/pysimplesoap/simplexml.py__init__'   s    
zSimpleXMLElement.__init__Tc                 C   s   |r| j du r| j|}nZt|trD| j|}|rv|d| n2| jrf| j| j d| j|f }n| j| j |}|durt|tj	j
jr|| j|j n|| j| | j| t|g| j| j | j| j| jdS )zAdding a child tag to a nodeFxmlnsz%s:%sNr%   r&   r'   r(   r*   r)   )r   r   ZcreateElement
isinstance
basestringsetAttributer   ZcreateElementNSr   r   r   CDATASectionappendChildZcreateCDATASectiondataZcreateTextNode_elementr   r   r   )r#   namer$   nselementr+   r+   r,   	add_childA   s,    
zSimpleXMLElement.add_childc                 C   s*   | drt| || n| || dS )z$Add text child tag node (short form)_N)
startswithobject__setattr__r:   )r#   tagr$   r+   r+   r,   r>   `   s    
zSimpleXMLElement.__setattr__c                 C   s,   dd | j jD }|D ]}| j | qdS )z#Remove a child tag (non recursive!)c                 S   s   g | ]}|j |jkr|qS r+   nodeTypeZELEMENT_NODE.0_SimpleXMLElement__elementr+   r+   r,   
<listcomp>j   s   z0SimpleXMLElement.__delattr__.<locals>.<listcomp>N)r6   
childNodesZremoveChild)r#   r?   r%   r9   r+   r+   r,   __delattr__h   s    zSimpleXMLElement.__delattr__c                 C   s   | j |}| j| dS )z Add an xml comment to this childN)r   ZcreateCommentr6   r4   )r#   r5   Zcommentr+   r+   r,   add_commento   s    zSimpleXMLElement.add_commentc                 C   s"   |s| j dS | j jddS dS )z-Return the XML representation of the documentUTF-8)encodingN)r   toxmlZtoprettyxml)r#   filenameZprettyr+   r+   r,   as_xmlt   s    zSimpleXMLElement.as_xmlr   c                 C   s
   | j  S ))Return the XML representation of this tagr6   rK   r#   r+   r+   r,   __repr__|   s    zSimpleXMLElement.__repr__c                 C   s   | j dS )rN   rI   rO   rP   r+   r+   r,   rQ      s    c                 C   s   | j jS )z Return the tag name of this node)r6   tagNamerP   r+   r+   r,   get_name   s    zSimpleXMLElement.get_namec                 C   s   | j jS )z4Return the tag local name (prefix:name) of this node)r6   Z	localNamerP   r+   r+   r,   get_local_name   s    zSimpleXMLElement.get_local_namec                 C   s   | j jS )z(Return the namespace prefix of this node)r6   r(   rP   r+   r+   r,   
get_prefix   s    zSimpleXMLElement.get_prefixc                 C   sJ   | j }|durF|jdurFz|jd|  jW S  tyB   |j}Y q0 qdS )z%Return the namespace uri for a prefixNzxmlns:%s)r6   
attributesvalueKeyErrorZ
parentNode)r#   r8   r9   r+   r+   r,   get_namespace_uri   s    z"SimpleXMLElement.get_namespace_uric                 C   s   | j jS )z(Return a dict of attributes for this tag)r6   rV   rP   r+   r+   r,   rV      s    zSimpleXMLElement.attributesc                 C   sr   t |tr&| j|rn| jj| jS nHt |trDt| jj | S | j	| }t
|g| j| j| j| j| jdS dS )z>Return xml tag attribute value or a slice of attributes (iter)r/   N)r0   r1   r6   ZhasAttributerV   rW   slicelistitemsr"   r   r   r   r   r   r   )r#   itemr9   r+   r+   r,   __getitem__   s    


zSimpleXMLElement.__getitem__c                 C   s   | j || dS )z$Set an attribute value from a stringN)r6   r2   )r#   r7   rW   r+   r+   r,   add_attribute   s    zSimpleXMLElement.add_attributec                 C   sD   t |tr| || n(t |tr@| D ]\}}| || q*dS )zSet an attribute valueN)r0   r1   r_   rZ   r\   )r#   r]   rW   kvr+   r+   r,   __setitem__   s
    

zSimpleXMLElement.__setitem__c                 C   s   | j | dS )zRemove an attributeN)r6   ZremoveAttributer#   r]   r+   r+   r,   __delitem__   s    zSimpleXMLElement.__delitem__c           	   
   C   s@  z|r.t | jjg| j| j| j| j| jdW S |du r@|  W S |rN|  W S d}t	|t
rh| j| g}|r|st	|ttfr|p|fD ]}| j||}|r qq| jr|s| j| j|}|s| j|}|s|rtdnW dS t || j| j| j| j| jdW S  ty: } ztd||f W Y d}~n
d}~0 0 dS )z;Search (even in child nodes) and return a child tag by namer/   NzNo elements foundzTag not found: %s (%s))r   r   r!   r   r   r   r   __iter__childrenr0   intr"   tupler[   r6   ZgetElementsByTagNameNSgetElementsByTagNameAttributeError)	r#   r?   r8   rf   rootr    r%   Zns_urier+   r+   r,   __call__   sP    




zSimpleXMLElement.__call__c                 C   s
   |  |S )zShortcut for __call__)rm   )r#   r?   r+   r+   r,   __getattr__   s    zSimpleXMLElement.__getattr__c              	   c   sF   z2| j D ]&}t|g| j| j| j| j| jdV  qW n    Y n0 dS )z#Iterate over xml tags at this levelr/   N)r"   r   r   r   r   r   r   )r#   rD   r+   r+   r,   re      s    
zSimpleXMLElement.__iter__c                 C   s   dd | j jD S )zList xml children tags namesc                 S   s   g | ]}|j |jkr|jqS r+   )rA   	TEXT_NODErR   )rC   noder+   r+   r,   rE     s   z,SimpleXMLElement.__dir__.<locals>.<listcomp>)r6   rF   rP   r+   r+   r,   __dir__  s    zSimpleXMLElement.__dir__c                 C   s8   dd | j jD }|sdS t|| j| j| j| j| jdS )z Return xml children tags elementc                 S   s   g | ]}|j |jkr|qS r+   r@   rB   r+   r+   r,   rE     s   z-SimpleXMLElement.children.<locals>.<listcomp>Nr/   )r6   rF   r   r   r   r   r   r   )r#   r%   r+   r+   r,   rf     s    zSimpleXMLElement.childrenc                 C   s
   t | jS )zReturn element count)lenr"   rP   r+   r+   r,   __len__!  s    zSimpleXMLElement.__len__c                 C   s   | j |S )z4Search for a tag name in this element or child nodes)r6   ri   rc   r+   r+   r,   __contains__%  s    zSimpleXMLElement.__contains__c                 C   s8   d}| j jD ]&}|j|jks(|j|jkr||j }q|S )z5Returns the unicode text nodes of the current element )r6   rF   rA   ro   ZCDATA_SECTION_NODEr5   )r#   Zrcrp   r+   r+   r,   __unicode__)  s
    zSimpleXMLElement.__unicode__c                 C   s   |   dS )Nzutf-8)rv   encoderP   r+   r+   r,   __str__4  s    zSimpleXMLElement.__str__c                 C   s   t |  S )z0Returns the integer value of the current element)rg   rx   rP   r+   r+   r,   __int__7  s    zSimpleXMLElement.__int__c                 C   s.   zt |  W S    t| j Y n0 dS )z.Returns the float value of the current elementN)floatrx   
IndexErrorr6   rK   rP   r+   r+   r,   	__float__;  s    zSimpleXMLElement.__float__c                 C   s
   | j d S )Nr   )r"   rP   r+   r+   r,   <lambda>B      zSimpleXMLElement.<lambda>c                 C   sv  i }|  D ]d}t | }d}d|  v rz|d dd }| dddD ]*}|d |krN|}|d d	d } qzqNzHt|tr|| }	td
d |dd D rt|	ts|	g}	n|}	W n t	fy }
 z|d p|
| }d|  v r|d d	d }zj|dkrpdd |dd D d }|| d	d }d|v rd|d|d }t| g}	nt| }	W n   d}	Y n0 n:|dkrd}	n*d|v rd}	n|rtd|f nt }	W Y d}
~
n
d}
~
0 0 t|	tr||g }| p|}|	rLt|	d tsL|p"g D ]$}||	d |}||  q$nt|	d dkr|D ]@}i }| pvg D ]}|||	d | qx|| qbn(|pg D ]}|||	d | qnt|	trg }i }| }t|	dkot|	d t}|r| pg D ]:}|r>|||	d | n|||	d | q|rh|| ||v rt|| }|| t|}nt|}nt|	tr| }|o||	|}n|	du r|}nt|s|	t krdt|dkrdz0t|	|	}	|	t krt|}n|	t|}W n: ttfy` }
 ztd||
f W Y d}
~
n
d}
~
0 0 nd}|||< q
|S )z;Convert to python values the current serialized xml elementNhrefr   T)rk   ZmultiRefidzxsi:type:c                 S   s   g | ]\}}d |v r|qS Z	arrayTyper+   rC   r`   ra   r+   r+   r,   rE   _  r~   z/SimpleXMLElement.unmarshall.<locals>.<listcomp>r.   ZArrayc                 S   s   g | ]\}}d |v r|qS r   r+   r   r+   r+   r,   rE   j  r~   r   [z http://www.w3.org/2001/XMLSchemaz Tag: %s invalid (type not found)ru   zTag: %s: %s)strrT   rV   r   splitr0   dictanyr[   rX   rY   rU   indexr   	TypeError
setdefaultrf   
unmarshallextendvaluesrr   updateappendrh   unicoder   get
ValueError)r#   typesstrictdrp   r7   Zref_name_typer   Zref_nodefnrl   r.   Zxsd_typeZ
array_typerW   rf   childZtmp_dictparentZ_dZas_dictZ_tmpr+   r+   r,   r   D  s    	
$










"

(
zSimpleXMLElement.unmarshallc                 C   sX   | j |}|rT| d }z||| j| }W n  tyR   td|  Y n0 |S )zBReplace the defined namespace alias with tohse used by the client.r   zUnknown namespace alias %s)r   searchgroupsreplacer   rX   r   Zwarning)r#   r7   Zprefr+   r+   r,   
_update_ns  s    zSimpleXMLElement._update_nsc                 C   s  |  |}t|trx|r&| j||dp(| }| D ]@\}}	|sDd}nt|dr\|j|}nd}|j||	||d q2nxt|t	r|r| j||dp| }|sd}|D ] \}}	t
| |j||	||d qn&t|tr^| j||d}|sd}|r|d t|D ]X\}
}|j||d||d t|trt|dkr|
t|d k r| j||d}qnt|tjjjtfr| j|||d nj|du r| j||d nP|t v r| j||d}|t|  n$tt|t}| j||||d dS )zFAnalyze python value and add the serialized XML element using tag name)r8   F
namespacesN)add_commentsr8   zRepetitive array of:r   )r   r0   r   r:   r\   hasattrr   r   marshallrh   getattrr[   rH   	enumeraterr   r   r   r   r3   r1   r	   r   r
   typer   )r#   r7   rW   r:   r   r8   Zadd_children_nsr   r`   ra   itr   r+   r+   r,   r     sF    




,
zSimpleXMLElement.marshallc                 C   s    | j |jd}| j| d S )NT)r   Z
importNoder6   r4   )r#   otherxr+   r+   r,   import_node  s    zSimpleXMLElement.import_nodec                 C   s*   ddl m} |j| j||rg ndd}|S )z.Generate the canonical version of the XML noder   )c14nN)ZunsuppressedPrefixes)ru   r   ZCanonicalizer6   )r#   outputZ	exclusiver   r   r+   r+   r,   
write_c14n  s
    

zSimpleXMLElement.write_c14n)NT)NF)NNFFT)T)TFFT)NT)(__name__
__module____qualname____doc__r-   r:   r>   rG   rH   rM   sysversionrQ   rS   rT   rU   rY   rV   r^   r_   rb   rd   rm   rn   re   rq   rf   rs   rt   rv   rx   ry   r|   propertyr6   r   r   r   r   r   r+   r+   r+   r,   r   $   sZ   




		  
4
   
7r   )r   Z
__future__r   r   r   r   r1   r   Zloggingr   timeZxml.dom.minidomr   ru   r   r   r   r   Zhelpersr	   r
   r   r   r   r   r   Z	getLoggerr   r   r=   r   r+   r+   r+   r,   <module>   s   
$
