o
    g                  	   @   s  d dl Z d dlZd dlm  mZ dd ZG dd dejZ		d!ddZ	d"d	d
Z		d#ddZ
dd Z				d$ddZ				d$ddZdd Z		d!ddZ		d!ddddddedddZ		d!ddddddedddZ		d!ddddddedddZe jdd  ZdS )%    Nc                   C   s   dd t j D S )Nc                 S      i | ]\}}||qS  r   .0uriprefixr   r   U/var/www/html/api-tag/env/lib/python3.10/site-packages/et_xmlfile/incremental_tree.py
<dictcomp>0       
z(current_global_nsmap.<locals>.<dictcomp>)ET_namespace_mapitemsr   r   r   r   current_global_nsmap/   s   r   c                   @   s*   e Zd Z				ddddddddZdS )IncrementalTreeNTF)short_empty_elementsnsmaproot_ns_onlyminimal_ns_onlyc                C   sr  |sd}n
|dvrt d| |sd}t||\}
}|dkr8|s1|du r8| dkr8| dvr8|
d|f  |d	krDt|
| j nc|dkrKd
}nd}|r\d|v rWt d| }ni }|rd||d< |sh|	r~t| j||}|	s~|rx|| |r~||d< dd tj	
 D }d|v rt di }t|
| j|||d||d W d   dS W d   dS 1 sw   Y  dS )a-
  Write element tree to a file as XML.

        Arguments:
          *file_or_filename* -- file name or a file object opened for writing

          *encoding* -- the output encoding (default: US-ASCII)

          *xml_declaration* -- bool indicating if an XML declaration should be
                               added to the output. If None, an XML declaration
                               is added if encoding IS NOT either of:
                               US-ASCII, UTF-8, or Unicode

          *default_namespace* -- sets the default XML namespace (for "xmlns").
                                 Takes precedence over any default namespace
                                 provided in nsmap or
                                 xml.etree.ElementTree.register_namespace().

          *method* -- either "xml" (default), "html, "text", or "c14n"

          *short_empty_elements* -- controls the formatting of elements
                                    that contain no content. If True (default)
                                    they are emitted as a single self-closed
                                    tag, otherwise they are emitted as a pair
                                    of start/end tags

          *nsmap* -- a mapping of namespace prefixes to URIs. These take
                     precedence over any mappings registered using
                     xml.etree.ElementTree.register_namespace(). The
                     default_namespace argument, if supplied, takes precedence
                     over any default namespace supplied in nsmap. All supplied
                     namespaces will be declared on the root element, even if
                     unused in the document.

          *root_ns_only* -- bool indicating namespace declrations should only
                            be written on the root element.  This requires two
                            passes of the xml tree adding additional time to
                            the writing process. This is primarily meant to
                            mimic xml.etree.ElementTree's behaviour.

          *minimal_ns_only* -- bool indicating only namespaces that were used
                               to qualify elements or attributes should be
                               declared. All namespace declarations will be
                               written on the root element regardless of the
                               value of the root_ns_only arg. Requires two
                               passes of the xml tree adding additional time to
                               the writing process.

        xml)textr   htmlzunknown method %rus-asciiNunicode)utf-8r   z$<?xml version='1.0' encoding='%s'?>
r   FTzTFound None as default nsmap prefix in nsmap. Use "" as the default namespace prefix. c                 S   r   r   r   r   r   r   r   r	      r
   z)IncrementalTree.write.<locals>.<dictcomp>zxFound None as default nsmap prefix in nsmap registered with register_namespace. Use "" for the default namespace prefix.)is_htmlis_rootr   	new_nsmap)
ValueError_get_writerlowerr   _serialize_text_rootcopy_namespacesupdater   r   _serialize_ns_xml)selffile_or_filenameencodingxml_declarationdefault_namespacemethodr   r   r   r   writedeclared_encodingr   r   global_nsmapnsmap_scoper   r   r   r-   7   sx   =

"zIncrementalTree.write)NNNN)__name__
__module____qualname__r-   r   r   r   r   r   5   s    r   c                 C   sV   t | }|d urd| vr|d7 }	 d| }|| vr&||vr&|r$||vr&|S |d7 }q)Nr      Tns)len)r0   global_prefixeslocal_nsmapr+   ir   r   r   r   _make_new_ns_prefix   s   
r:   Fc           	      C   sh   |  D ]\}}| |kr||vr|} nqt||d}| ||< | ||< |s)||| < |dkr2|| |S )aA  Find a prefix that doesn't conflict with the ns scope or create a new prefix

    This function mutates nsmap_scope, global_nsmap, new_namespace_prefixes and
    uri_to_prefix. It is intended to keep state in _serialize_ns_xml consistent
    while deduplicating the house keeping code or updating these dictionaries.
    )r7   r   )r   r:   add)	r   r0   r/   new_namespace_prefixesuri_to_prefix!for_default_namespace_attr_prefixglobal_prefix
global_urir   r   r   r   _get_or_create_prefix   s   
rA   c                 C   s~   |  D ]\}}|| kr|dkr|  S q|  D ]\}}|| kr)|dkr)|  S qtj| }|r8||vr8|S t||||S )Nr   )r   r   r   getr:   )r+   r   r8   r7   provided_default_namespacer   r   r   r   r   #_find_default_namespace_attr_prefix   s"   	rD   c              
   C   s  g }|   D ]\}}	t|tjr|j}z}|d d dkr|dd  dd}
z||
d  }W n tyM   |sA| }| }d}t|
d ||||}Y nw |s|rU|}n1|  D ]\}}||
d krk|dkrk|} nqY|sx| }| }d}t|
d ||||dd}|}| d|
d  }W n t	y   t
| Y nw t|	tjr|	jd d dkr|	j}	n<|	jdd  dd}
z||
d  }W n ty   |s| }| }d}t|
d ||||}Y nw | d|
d  }	|||	f q|||fS )	Nr4   {}r   Tr   )r>   :)r   
isinstancer   QNamer   rsplitKeyErrorr#   rA   	TypeError_raise_serialization_errorappend)elemis_nsmap_scope_changeddefault_ns_attr_prefixr0   r/   r<   r=   
item_partskvuri_and_namer   	known_urir   r   r   process_attribs  s   	
rW   c
              	   K   sh  |j }|j}|tju r| d|  d}d}n|tju r(| d|  d}d}n|	rEd}| }||	 t|	 }|	d d}d}nd}t }|du rid|v rVt
ddd	 | D }d
|v rid
||d
 < |du rz|}|rx| t| n3d}t|tjr|j}zU|dd dkr|dd dd}z||d  }W n ty   |s| }| }d}t|d ||||}Y nw |r| d|d  }n|d }nd
|v rt
dW n ty   t| Y nw | d|  |jrt|||||||\}}}ng }|rAg }t|D ]}|| }|rd| }nd}|||f q|r5| d
dd |D  n| d
dd |D  |r`|rT| d
dd |D  n| d
dd |D  |r| d | }|r|dksx|dkr}| | n| t| |tjv rd}n |st|s|s| d |r| t| nd}| d |||||fS )az  Write the opening tag (including self closing) and element text.

    Refer to _serialize_ns_xml for description of arguments.

    nsmap_scope should be an empty dictionary on first call. All nsmap prefixes
    must be strings with the default namespace prefix represented by "".

    eg.
    - <foo attr1="one">      (returns tag = 'foo')
    - <foo attr1="one">text  (returns tag = 'foo')
    - <foo attr1="one" />    (returns tag = None)

    Returns:
        tag:
            The tag name to be closed or None if no closing required.
        nsmap_scope:
            The current nsmap after any prefix to uri additions from this
            element. This is the input dict if unmodified or an updated copy.
        default_ns_attr_prefix:
            The prefix for the default namespace to use with attrs.
        uri_to_prefix:
            The current uri to prefix map after any uri to prefix additions
            from this element. This is the input dict if unmodified or an
            updated copy.
        next_remains_root:
            A bool indicating if the child element(s) should be treated as
            their own roots.
    z	<!--%s-->NFz<?%s?>Tr   zIFound None as a namespace prefix. Use "" as the default namespace prefix.c                 S   r   r   r   r   r   r   r   r   r   r	         z$write_elem_start.<locals>.<dictcomp>r   r4   rE   rF   r   rG   <cannot use non-qualified names with default_namespace option<zxmlns:xmlnsc                 S   (   g | ]\}}d | dt | dqS  z=""r   _escape_attrib_htmlr   rS   rT   r   r   r   
<listcomp>     ( z$write_elem_start.<locals>.<listcomp>c                 S   r]   r^   r   _escape_attribrc   r   r   r   rd     re   c                 S   r]   r^   ra   rc   r   r   r   rd     re   c                 S   r]   r^   rf   rc   r   r   r   rd     re   >scriptstylez />)tagr   r   CommentProcessingInstructionr#   r%   setkeysdiscardr   r   _escape_cdatarH   rI   rJ   rK   rA   rL   rM   attribrW   sortedrN   joinr    
HTML_EMPTYr6   )r-   rO   r0   r/   r   r   r   r=   rQ   r   kwargsrk   r   next_remains_rootrP   r<   rU   r   rR   ns_attrsrS   rT   ltagr   r   r   write_elem_startt  s   )







rz   c
                 K   s~   t | |||||||||	d
\}}}}}|D ]}t| ||||||||dd
 q|r0| d| d |jr=| t|j dS dS )a  Serialize an element or tree using 'write' for output.

    Args:
        write:
            A function to write the xml to its destination.
        elem:
            The element to serialize.
        nsmap_scope:
            The current prefix to uri mapping for this element. This should be
            an empty dictionary for the root element. Additional namespaces are
            progressively added using the new_nsmap arg.
        global_nsmap:
            A dict copy of the globally registered _namespace_map in uri to
            prefix form
        short_empty_elements:
          Controls the formatting of elements that contain no content. If True
          (default) they are emitted as a single self-closed tag, otherwise
          they are emitted as a pair of start/end tags.
        is_html:
            Set to True to serialize as HTML otherwise XML.
        is_root:
            Boolean indicating if this is a root element.
        uri_to_prefix:
            Current state of the mapping of uri to prefix.
        default_ns_attr_prefix:
        new_nsmap:
            New prefix -> uri mapping to be applied to this element.
    )r   Nz</rh   )rz   r&   tailr   rq   )r-   rO   r0   r/   r   r   r   r=   rQ   r   rv   rk   rw   er   r   r   r&   6  sH   /r&   c                 c   sF   t  }t  }|  D ]}|j}t|tr$||vr#|| |dfV  n+t|tjr<|j}||vr;|| |dfV  n|durO|tj	urO|tj
urOt| | D ]2\}}t|tjr`|j}||vrn|| |dfV  t|tjr|j|vr||j |jdfV  qS|j}t|tjr|j|vr||j |jdfV  qdS )z/Iterate through all the qualified names in elemTNF)rn   iterrk   rH   strr;   r   rI   r   rm   rl   rM   r   )rO   seen_el_qnamesseen_other_qnames	this_elemrk   keyvaluer   r   r   r   _qnames_iter  sL   











r   c              	   C   s  |du ri }i }i }dd |  D }|durd||< n
d|v r&d||d < ttj }d}d}t| D ]\}	}
zp|	dd dkr|	dd dd}||d	 }|du r||d	 }|du sf||v r}tj|d	 }|du sv||v r}t||||}|s|
r|d	 ||< |||d	 < |
s|s|st	|d	 ||||}|d	 ||< n|
rd
}W q5 t
y   t|	 Y q5w d|v r|rtd|dd |S )zGFind all namespaces used in the document and return a prefix to uri mapNc                 S   r   r   r   rX   r   r   r   r	     rY   z_namespaces.<locals>.<dictcomp>r   Fr4   rE   rF   r   TrZ   r   )r   rn   r   r   valuesr   rJ   rB   r:   rD   rL   rM   r   pop)rO   r+   r   	out_nsmapseen_uri_to_prefixlocal_prefix_mapr7   has_unqual_eldefault_namespace_attr_prefixqnameis_elrU   r   r   r   r   r$     sj   
r$   T)r*   r+   r   r   r   r   tree_clsc                C   s@   |dkrt  nt  }
|	| j|
||||||||d	 |
 S )a
  Generate string representation of XML element.

    All subelements are included.  If encoding is "unicode", a string
    is returned. Otherwise a bytestring is returned.

    *element* is an Element instance, *encoding* is an optional output
    encoding defaulting to US-ASCII, *method* is an optional output which can
    be one of "xml" (default), "html", "text" or "c14n", *default_namespace*
    sets the default XML namespace (for "xmlns").

    Returns an (optionally) encoded string containing the XML data.

    r   r*   r+   r,   r   r   r   r   )ioStringIOBytesIOr-   getvalue)elementr)   r,   r*   r+   r   r   r   r   r   streamr   r   r   tostring  s   r   c                C   s2   g }
t |
}|	| j|||||||||d	 |
S )Nr   )r   _ListDataStreamr-   )r   r)   r,   r*   r+   r   r   r   r   r   lstr   r   r   r   tostringlist$  s   
r   c          
      C   s   t | |||||||||	d
S )ztostring with options that produce the same results as xml.etree.ElementTree.tostring

    root_ns_only=True is a bit slower than False as it needs to traverse the
    tree one more time to collect all the namespaces.
    )	r)   r,   r*   r+   r   r   r   r   r   )r   )
r   r)   r,   r*   r+   r   r   r   r   r   r   r   r   compat_tostringA  s   r   c                 c   sf   z| j }W n2 ty8   | dkrd}t| d|dd}|j |fV  W d    Y d S 1 s0w   Y  Y d S w | dkrL|t| dd pGdfV  d S t Y}t| tj	rZ| }n2t| tj
rlt| }||j n t	 }dd |_||_ z
| j|_| j|_W n	 ty   Y nw tj||dd	d
}||j |j |fV  W d    d S 1 sw   Y  d S )Nr   r   wxmlcharrefreplace)r)   errorsr)   c                   S   s   dS )NTr   r   r   r   r   <lambda>  s    z_get_writer.<locals>.<lambda>
)r)   r   newline)r-   AttributeErrorr    opengetattr
contextlib	ExitStackrH   r   BufferedIOBase	RawIOBaseBufferedWritercallbackdetachwritableseekabletellTextIOWrapper)r(   r)   r-   filestackr   r   r   r   d  sL   
&



"r   )NN)F)N)FNNN)r   r   xml.etree.ElementTreeetreeElementTreer   r   r   r:   rA   rD   rW   rz   r&   r   r$   r   r   r   contextmanagerr   r   r   r   r   <module>   s   ( 

)
d
 J
N+
P+#