U
    6#^Y                     @   s  d dl Z d dlZe jeje d dlZd dlZ	d dl
T d dlma d dlmZ d dlmZ e jd  dkrveZeZdZed ZdZe jd	kred
 Zdd Zeee Zdd Zi fddZi fddZi fddZdd Z dd Z!G dd de"Z#G dd de"Z$G dd de	j%j&Z&e'e&dd d! e'e&d"d#d! d$d% Z(G d&d' d'Z)G d(d) d)e)e&Z*G d*d+ d+e*e)Z+G d,d- d-e&Z,dS ).    N)*)nsdict)AttrConverters   ))r   r   )i   i  )i  i  ))      )      )      ))      )      i   ))i i )i i )i i )i i )i i )i i )i i )i i )i	 i	 )i
 i
 )i i )i i )i i )i i )i i )i i c                 C   s&   t jdddd | D t jdS )Nz[{}] c                 s   s2   | ]*\}}d  tt|tt|V  qdS )z{}-{}N)formatreescapeunichr).0lohi r   K/var/www/html/UseVoiceDocker/env/lib/python3.8/site-packages/odf/element.py	<genexpr>P   s   z#_range_seq_to_re.<locals>.<genexpr>)flags)r   compiler   joinUNICODE)Z	range_seqr   r   r   _range_seq_to_reM   s    
r   c                 C   s   t d| S )Nu   �)_xml_filtered_chars_resubdatar   r   r   _handle_unrepresentableX   s    r$   c                 C   sF   |  dd} |  dd} |  dd} | D ]\}}|  ||} q,| S )a	   Escape &, <, and > in a string of data.

        You can escape other strings of data by passing a dictionary as
        the optional entities parameter.  The keys and values must all be
        strings; each key will be replaced with its corresponding value.
    &z&amp;<z&lt;>z&gt;)replaceitems)r#   entitiescharsentityr   r   r   _escape^   s    r-   c                 C   s   t t| |dS )N)r*   )r-   r$   r#   r*   r   r   r   	_sanitizel   s    r/   c                 C   sR   d|d< d|d< t | |} d| krFd| kr<d| dd } qNd	|  } nd|  } | S )
a   Escape and quote an attribute value.

        Escape &, <, and > in a string of data, then quote it for use as
        an attribute value.  The " character will be escaped as well, if
        necessary.

        You can escape other strings of data by passing a dictionary as
        the optional entities parameter.  The keys and values must all be
        strings; each key will be replaced with its corresponding value.
    z&#10;
z&#12;"'z"%s"z&quot;z'%s')r/   r(   r.   r   r   r   
_quoteattro   s    

r4   c                 C   s,   |  dd}t|dkr|S d|d fS dS )z= Split a qualified name into namespace part and local part.  :r      Nr   )splitlen)ZqualifiedNamefieldsr   r   r   _nssplit   s    r:   c                 C   s   t | dttt  S )Nns)r   
setdefaultstrr8   )	namespacer   r   r   	_nsassign   s    r?   c                   @   s   e Zd ZdZdS )IllegalChildzE Complains if you add an element to a parent where it is not allowed N__name__
__module____qualname____doc__r   r   r   r   r@      s   r@   c                   @   s   e Zd ZdZdS )IllegalTextzJ Complains if you add text or cdata to an element where it is not allowed NrA   r   r   r   r   rF      s   rF   c                   @   sd   e Zd ZdZdZdZ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dS )Nodez% super class for more specific nodes Nc                 C   s   | j r
dS dS dS )ze Tells whether this element has any children; text nodes,
            subelements, whatever.
        TFN
childNodesselfr   r   r   hasChildNodes   s    zNode.hasChildNodesc                 C   s   | j S NrH   rJ   r   r   r   _get_childNodes   s    zNode._get_childNodesc                 C   s   | j r| j d S d S )Nr   rH   rJ   r   r   r   _get_firstChild   s    zNode._get_firstChildc                 C   s   | j r| j d S d S )NrH   rJ   r   r   r   _get_lastChild   s    zNode._get_lastChildc                 C   s   |j | jkr td|j| jf |jdk	r6|j| |dkrJ| | nvz| j|}W n t	k
rx   t
j Y nX | j|| ||_||_|r| j|d  }||_||_nd|_| |_|S )z Inserts the node newChild before the existing child node refChild.
            If refChild is null, insert newChild at the end of the list of children.
        z%s cannot be child of %sNr   )nodeType_child_node_typesr@   tagName
parentNoderemoveChildappendChildrI   index
ValueErrorxmldomNotFoundErrinsertnextSiblingpreviousSibling)rK   newChildrefChildrX   noder   r   r   insertBefore   s(    
zNode.insertBeforec                 C   st   |j | jkr*t|jD ]}| | q|S |j | jkrJtd|j| jf |jdk	r`|j	| t
| | d|_|S )z Adds the node newChild to the end of the list of children of this node.
            If the newChild is already in the tree, it is first removed.
        z<%s> is not allowed in %sN)rR   ZDOCUMENT_FRAGMENT_NODEtuplerI   rW   rS   r@   rT   rU   rV   _append_childr^   )rK   r`   cr   r   r   rW      s    

zNode.appendChildc                 C   s   z| j | W n tk
r.   tj Y nX |jdk	rD|j|j_|jdk	rX|j|j_d |_|_| jrv| j	| d|_
|S )za Removes the child node indicated by oldChild from the list of children, and returns it.
        N)rI   removerY   rZ   r[   r\   r^   r_   ownerDocumentZremove_from_cachesrU   rK   oldChildr   r   r   rV      s    



zNode.removeChildc                 C   s(   g }| j D ]}|t| q
d|S Nr   )rI   appendr=   r   rK   valrf   r   r   r   __str__   s    
zNode.__str__c                 C   s(   g }| j D ]}|t| q
d|S rk   )rI   rl   unicoder   rm   r   r   r   __unicode__   s    
zNode.__unicode__)rB   rC   rD   rE   rU   r^   r_   rL   rN   rO   rQ   rc   rW   rV   ro   rq   r   r   r   r   rG      s   	rG   
firstChildzFirst child node, or None.)doc	lastChildzLast child node, or None.c                 C   s>   | j }|r&|d }||jd< ||jd< || | |jd< d S )NrP   r_   r^   rU   )rI   __dict__rl   )rK   rb   rI   lastr   r   r   re      s    


re   c                   @   sZ   e Zd ZdZdZe ZdZdZdd Z	dd Z
dd Zd	d
 Zdd Zdd Zdd ZdS )	Childlessz Mixin that makes childless-ness easy to implement and avoids
        the complexity of the Node methods that deal with children.
    Nc                 C   s   d S rM   r   rJ   r   r   r   rO     s    zChildless._get_firstChildc                 C   s   d S rM   r   rJ   r   r   r   rQ     s    zChildless._get_lastChildc                 C   s   t j| jd dS ) Raises an error z nodes cannot have childrenNrZ   r[   ZHierarchyRequestErrrT   )rK   rb   r   r   r   rW     s    zChildless.appendChildc                 C   s   dS )NFr   rJ   r   r   r   rL     s    zChildless.hasChildNodesc                 C   s   t j| jd dS rx   z nodes do not have childrenNry   )rK   r`   ra   r   r   r   rc   !  s    zChildless.insertBeforec                 C   s   t j| jd dS rz   )rZ   r[   r\   rT   ri   r   r   r   rV   &  s    zChildless.removeChildc                 C   s   t j| jd dS rz   ry   )rK   r`   rj   r   r   r   replaceChild+  s    zChildless.replaceChild)rB   rC   rD   rE   
attributesZEmptyNodeListrI   rr   rt   rO   rQ   rW   rL   rc   rV   r{   r   r   r   r   rw   	  s   rw   c                   @   s6   e Zd ZejZd Zdd Zdd Zdd Z	dd Z
d	S )
Textc                 C   s
   || _ d S rM   r"   )rK   r#   r   r   r   __init__4  s    zText.__init__c                 C   s   | j S rM   r"   rJ   r   r   r   ro   7  s    zText.__str__c                 C   s   | j S rM   r"   rJ   r   r   r   rq   :  s    zText.__unicode__c                 C   s   | j r|tt| j  dS )z Write XML in UTF-8 N)r#   writer/   rp   rK   levelfr   r   r   toXml=  s    z
Text.toXmlN)rB   rC   rD   rG   	TEXT_NODErR   rT   r~   ro   rq   r   r   r   r   r   r}   0  s   r}   c                   @   s   e Zd ZejZdd ZdS )CDATASectionc                 C   s"   | j r|d| j dd  dS )z Generate XML output of the node. If the text contains "]]>", then
            escape it by going out of CDATA mode (]]>), then write the string
            and then go into CDATA mode again. (<![CDATA[)
        z<![CDATA[%s]]>z]]>z]]>]]><![CDATA[N)r#   r   r(   r   r   r   r   r   E  s    zCDATASection.toXmlN)rB   rC   rD   rG   CDATA_SECTION_NODErR   r   r   r   r   r   r   B  s   r   c                   @   s   e Zd ZdZejZi Zejejej	ej
ejejfZd,ddZdd Zdd	 Zd
d Zdd Zd-ddZd.ddZd/ddZd0ddZd1d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 d*d+ Z!dS )2Elementa   Creates a arbitrary element and is intended to be subclassed not used on its own.
        This element is the base of every element it defines a class which resembles
        a xml-element. The main advantage of this kind of implementation is that you don't
        have to create a toXML method for every different object. Every element
        consists of an attribute, optional subelements, optional text and optional cdata.
    NTc                 K   s  |d k	r|| _ t| dstd | _g | _tj| j | _| | j d }|d | j d  | _	|d k	rn| 
| |d k	r| | |  }	|	d k	rdd |	D }
i | _|r| D ]\}}| || q|r| D ]\}}| |d |d | q|	d k	r| D ]}| |||  qn| D ]}|| | j|< q&|sFd S tj| j }|r|D ]B}| |d |d d kr^td|d  dd	| j	f q^d S )
Nqnamer   r5   r   c                 S   s    g | ]}|d    ddqS r   -r   lowerr(   r   ar   r   r   
<listcomp>o  s     z$Element.__init__.<locals>.<listcomp>z&Required attribute missing: %s in <%s>r   r   )r   hasattrAssertionErrorrh   rI   grammarallowed_childrengetget_nsprefixrT   addTextaddCDATAallowed_attributesr|   r)   setAttribute	setAttrNSkeysZrequired_attributes	getAttrNSAttributeErrorr   r(   )rK   r|   textcdatar   Zqattributescheck_grammarargsprefixallowed_attrsallowed_argsattrvalueargrequiredrr   r   r   r~   _  sD    


zElement.__init__c                 C   s&   t  D ]\}}||kr|  S qdS )z Odfpy maintains a list of known namespaces. In some cases a prefix is used, and
            we need to know which namespace it resolves to.
        N)r   r)   )rK   r   r;   pr   r   r   get_knownns  s     
zElement.get_knownnsc                 C   s,   |dkrd}t |}|| jkr(|| j|< |S )z Odfpy maintains a list of known namespaces. In some cases we have a namespace URL,
            and needs to look up or assign the prefix for it.
        Nr   )r?   
namespaces)rK   r>   r   r   r   r   r     s     

zElement.get_nsprefixc                 C   s   t j| jS rM   )r   r   r   r   rJ   r   r   r   r     s    zElement.allowed_attributesc                 C   s"   | j |_ |jD ]}| | qd S rM   )rh   rI   _setOwnerDoc)rK   elementchildr   r   r   r     s    
zElement._setOwnerDocc                 C   sX   |r.| j dk	r.|j| j kr.td|j| jf | | | | | jrT| j| dS )zP adds an element to an Element

            Element.addElement(Element)
        Nz<%s> is not allowed in <%s>)r   r   r@   rT   rW   r   rh   Zrebuild_caches)rK   r   r   r   r   r   
addElement  s    

zElement.addElementc                 C   s:   |r | j tjkr td| j n|dkr6| t| dS )zd Adds text to an element
            Setting check_grammar=False turns off grammar checking
        $The <%s> element does not allow textr   N)r   r   allows_textrF   rT   rW   r}   )rK   r   r   r   r   r   r     s    zElement.addTextc                 C   s2   |r | j tjkr td| j n| t| dS )ze Adds CDATA to an element
            Setting check_grammar=False turns off grammar checking
        r   N)r   r   r   rF   rT   rW   r   )rK   r   r   r   r   r   r     s    zElement.addCDATAc                 C   s   |   }|dkr@t|tdkr6|\}}| || qtdnRdd |D }|rl||krltd|| jf ||}| || d || d  dS )	z Removes an attribute by name. Nr   ;Unable to add simple attribute - use (namespace, localpart)c                 S   s    g | ]}|d    ddqS r   r   r   r   r   r   r     s     z+Element.removeAttribute.<locals>.<listcomp>#Attribute %s is not allowed in <%s>r   r   )r   typeremoveAttrNSr   rT   rX   )rK   r   r   r   r   	localnamer   ir   r   r   removeAttribute  s    

zElement.removeAttributec           	      C   s   |dkr|dk	r| |  n|  }|dkr^t|tdkrT|\}}| ||| qtdnTdd |D }|r||krtd|| jf ||}| || d || d	 | dS )
a   Add an attribute to the element
            This is sort of a convenience method. All attributes in ODF have
            namespaces. The library knows what attributes are legal and then allows
            the user to provide the attribute as a keyword argument and the
            library will add the correct namespace.
            Must overwrite, If attribute already exists.
        parentNr   r   c                 S   s    g | ]}|d    ddqS r   r   r   r   r   r   r     s     z(Element.setAttribute.<locals>.<listcomp>r   r   r   )r   r   r   r   r   rT   rX   )	rK   r   r   r   r   r   r   r   r   r   r   r   r     s    

zElement.setAttributec                 C   s8   |   }| |}t }|||f|| | j||f< dS )a?   Add an attribute to the element
            In case you need to add an attribute the library doesn't know about
            then you must provide the full qualified name
            It will not check that the attribute is legal according to the schema.
            Must overwrite, If attribute already exists.
        N)r   r   r   convertr|   )rK   r>   	localpartr   r   r   rf   r   r   r   r     s    
zElement.setAttrNSc                 C   sv   |  |}| j||f}t|t|t|tdktdtdfsrt|t|t|tdktdtdfsrt|S )a  
        gets an attribute, given a namespace and a key
        @param namespace a unicode string or a bytes: the namespace
        @param localpart a unicode string or a bytes:
        the key to get the attribute
        @return an attribute as a unicode string or a bytes: if both paramters
        are byte strings, it will be a bytes; if both attributes are
        unicode strings, it will be a unicode string
            r   )r   r|   r   r   r   )rK   r>   r   r   resultr   r   r   r     s"    

  zElement.getAttrNSc                 C   s   | j ||f= d S rM   )r|   )rK   r>   r   r   r   r   r     s    zElement.removeAttrNSc                 C   sv   |   }|dkr>t|tdkr4|\}}| ||S tdn4dd |D }||}| || d || d S dS )zV Get an attribute value. The method knows which namespace the attribute is in
        Nr   z;Unable to get simple attribute - use (namespace, localpart)c                 S   s    g | ]}|d    ddqS r   r   r   r   r   r   r   "  s     z(Element.getAttribute.<locals>.<listcomp>r   r   )r   r   r   r   rX   )rK   r   r   r   r   r   r   r   r   r   getAttribute  s    

zElement.getAttributec              	   C   s   | d| j  |dkrN| j D ]*\}}| d| d tt| d  q"| j D ]J}| |d }| dtt|d |d   d	 t	t
| j|   qX| d
 d S )Nr&   r    xmlns:="r2    r5   r   =r'   )r   rT   r   r)   r/   r=   r|   r   r   r4   rp   )rK   r   r   r>   r   r   r   r   r   write_open_tag&  s    $:zElement.write_open_tagc                 C   s   | d| j d  d S )N</r'   )r   rT   r   r   r   r   write_close_tag0  s    zElement.write_close_tagc              	   C   s   | d| j  |dkrN| j D ]*\}}| d| d tt| d  q"| j D ]J}| |d }| dtt	|d |d   d	 t
t	| j|   qX| jr| d
 | jD ]}||d | q| d| j d
  n
| d dS )z
        Generate an XML stream out of the tree structure
        @param level integer: level in the XML tree; zero at root of the tree
        @param f an open writable file able to accept unicode strings
        r&   r   r   r   r2   r   r5   r   r   r'   r   z/>N)r   rT   r   r)   r/   r=   r|   r   r   rp   r4   rI   r   )rK   r   r   r>   r   r   r   r   r   r   r   3  s    $:

zElement.toXmlc                 C   s>   | j |j kr||  | jD ]}|jtjkr|||}q|S rM   )r   rl   rI   rR   rG   ELEMENT_NODE_getElementsByObj)rK   objZaccumulatorer   r   r   r   H  s    

zElement._getElementsByObjc                 C   s   |dd}|  |g S )zO Gets elements based on the type, which is function from text.py, draw.py etc. Fr   )r   rK   r   r   r   r   r   getElementsByTypeP  s    
zElement.getElementsByTypec                 C   s   |dd}| j |j kS )z? This is a check to see if the object is an instance of a type Fr   )r   r   r   r   r   isInstanceOfU  s    
zElement.isInstanceOf)NNNNNT)T)T)T)T)T)"rB   rC   rD   rE   rG   r   rR   r   ZPROCESSING_INSTRUCTION_NODEZCOMMENT_NODEr   r   ZENTITY_REFERENCE_NODErS   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   M  s<   
*	




	


r   )-sysos.pathospathrl   dirname__file__r   Zxml.domrZ   Zxml.dom.minicompatZodf.namespacesr   Zodf.grammarr   Zodf.attrconvertersr   version_infor=   rp   chrr   Z_xml11_illegal_rangesZ_xml10_illegal_rangesZ_xml_discouraged_ranges
maxunicoder   r    r$   r-   r/   r4   r:   r?   	Exceptionr@   rF   r[   rG   Zdefpropertyre   rw   r}   r   r   r   r   r   r   <module>   sB   
	d
'