
    PiC                        d Z ddlmZ ddlZddlZddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ erddlmZmZmZmZmZ ddlmZ  G d d          Z G d de          Z G d de          ZdS )zF
:mod:`pandas.io.formats.xml` is a module for formatting data in XML.
    )annotationsN)TYPE_CHECKINGAnyfinalAbstractMethodError)cache_readonly)is_list_like)isna)
get_handle)get_data_from_filepath)CompressionOptionsFilePath
ReadBufferStorageOptionsWriteBuffer)	DataFramec                  8   e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d;d<d%Zd=d'Zed>d(            Zed>d)            Zed?d+            Z	ed>d,            Z
d@d-ZedAd/            ZedBd4            ZedCd7            Zed8             ZedDd9            ZedEd:            ZdS )F_BaseXMLFormattera  
    Subclass for formatting data in XML.

    Parameters
    ----------
    path_or_buffer : str or file-like
        This can be either a string of raw XML, a valid URL,
        file or file-like object.

    index : bool
        Whether to include index in xml document.

    row_name : str
        Name for root of xml document. Default is 'data'.

    root_name : str
        Name for row elements of xml document. Default is 'row'.

    na_rep : str
        Missing data representation.

    attrs_cols : list
        List of columns to write as attributes in row element.

    elem_cols : list
        List of columns to write as children in row element.

    namespaces : dict
        The namespaces to define in XML document as dicts with key
        being namespace and value the URI.

    prefix : str
        The prefix for each element in XML document including root.

    encoding : str
        Encoding of xml object or document.

    xml_declaration : bool
        Whether to include xml declaration at top line item in xml.

    pretty_print : bool
        Whether to write xml document with line breaks and indentation.

    stylesheet : str or file-like
        A URL, file, file-like object, or a raw string containing XSLT.

    compression : str or dict, default 'infer'
        For on-the-fly compression of the output data. If 'infer' and 'path_or_buffer'
        is path-like, then detect compression from the following extensions: '.gz',
        '.bz2', '.zip', '.xz', '.zst', '.tar', '.tar.gz', '.tar.xz' or '.tar.bz2'
        (otherwise no compression).
        Set to ``None`` for no compression.
        Can also be a dict with key ``'method'`` set
        to one of {``'zip'``, ``'gzip'``, ``'bz2'``, ``'zstd'``, ``'xz'``, ``'tar'``}
        and other key-value pairs are forwarded to
        ``zipfile.ZipFile``, ``gzip.GzipFile``,
        ``bz2.BZ2File``, ``zstandard.ZstdCompressor``, ``lzma.LZMAFile`` or
        ``tarfile.TarFile``, respectively.
        As an example, the following could be passed for faster compression and to
        create a reproducible gzip archive:
        ``compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}``.

    storage_options : dict, optional
        Extra options that make sense for a particular storage connection, e.g.
        host, port, username, password, etc. For HTTP(S) URLs the key-value pairs
        are forwarded to ``urllib.request.Request`` as header options. For other
        URLs (e.g. starting with "s3://", and "gcs://") the key-value pairs are
        forwarded to ``fsspec.open``. Please see ``fsspec`` and ``urllib`` for more
        details, and for more examples on storage options refer `here
        <https://pandas.pydata.org/docs/user_guide/io.html?
        highlight=storage_options#reading-writing-remote-files>`_.

    See also
    --------
    pandas.io.formats.xml.EtreeXMLFormatter
    pandas.io.formats.xml.LxmlXMLFormatter

    NTdatarowutf-8inferframer   path_or_buffer7FilePath | WriteBuffer[bytes] | WriteBuffer[str] | Noneindexbool	root_name
str | Nonerow_namena_rep	attr_colslist[str] | None	elem_cols
namespacesdict[str | None, str] | Noneprefixencodingstrxml_declarationbool | Nonepretty_print
stylesheet5FilePath | ReadBuffer[str] | ReadBuffer[bytes] | Nonecompressionr   storage_optionsStorageOptions | NonereturnNonec                   || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        || _
        || _        || _        || _        || _        || _        | j         j                                        | _        |                                 | _        |                                  |                                  |                                 | _        |                                  d S N)r   r   r   r   r!   r"   r#   r%   r&   r(   r)   r+   r-   r.   r0   r1   columnstolist	orig_cols_process_dataframeframe_dicts_validate_columns_validate_encoding_get_prefix_uri
prefix_uri_handle_indexes)selfr   r   r   r   r!   r"   r#   r%   r&   r(   r)   r+   r-   r.   r0   r1   s                    i/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/pandas/io/formats/xml.py__init__z_BaseXMLFormatter.__init__t   s    & 
,
" ""$ .($/:.+22442244   !!!..00    bytesc                     t          |           )
        Build tree from  data.

        This method initializes the root and builds attributes and elements
        with optional namespaces.
        r   rA   s    rB   _build_treez_BaseXMLFormatter._build_tree   s     "$'''rD   c                   | j         r=t          | j                   s)t          t          | j                   j         d          | j        r=t          | j                  s+t          t          | j                  j         d          dS dS )z
        Validate elems_cols and attrs_cols.

        This method will check if columns is list-like.

        Raises
        ------
        ValueError
            * If value is not a list and less then length of nodes.
        z" is not a valid type for attr_colsz" is not a valid type for elem_colsN)r#   r
   	TypeErrortype__name__r%   rH   s    rB   r<   z#_BaseXMLFormatter._validate_columns   s     > 	,t~">"> 	''0TTT   > 	,t~">"> 	''0TTT  	 	 	 	rD   c                8    t          j        | j                   dS )z
        Validate encoding.

        This method will check if encoding is among listed under codecs.

        Raises
        ------
        LookupError
            * If encoding is not available in codecs.
        N)codecslookupr)   rH   s    rB   r=   z$_BaseXMLFormatter._validate_encoding   s     	dm$$$$$rD   dict[int | str, dict[str, Any]]c                    | j         }| j        r|                                }| j        |                    | j                  }|                    d          S )z
        Adjust Data Frame to fit xml output.

        This method will adjust underlying data frame for xml output,
        including optionally replacing missing values and including indexes.
        Nr   )orient)r   r   reset_indexr"   fillnato_dict)rA   dfs     rB   r:   z$_BaseXMLFormatter._process_dataframe   sS     Z: 	"!!B;"4;''Bzzz)))rD   c                     j         sdS t          t           j                            } fd j        |                                         D             } j        r| j        z    _         j        r| j        z    _        dS dS )zd
        Handle indexes.

        This method will add indexes into attr_cols or elem_cols.
        Nc                &    g | ]}|j         v|S  )r9   ).0xrA   s     rB   
<listcomp>z5_BaseXMLFormatter._handle_indexes.<locals>.<listcomp>   s,     
 
 
Qdn=T=TA=T=T=TrD   )r   nextiterr;   keysr#   r%   )rA   	first_keyindexess   `  rB   r@   z!_BaseXMLFormatter._handle_indexes   s     z 	Fd.//00	
 
 
 
'	27799
 
 
 > 	6$t~5DN> 	6$t~5DNNN	6 	6rD   c                     t          |           )z
        Get uri of namespace prefix.

        This method retrieves corresponding URI to prefix in namespaces.

        Raises
        ------
        KeyError
            *If prefix is not included in namespace dict.
        r   rH   s    rB   r>   z!_BaseXMLFormatter._get_prefix_uri   s     "$'''rD   dictc                d     i } j         r% fd j                                         D             }|S )z
        Define other namespaces.

        This method will build dictionary of namespaces attributes
        for root element, conditionally with optional namespaces and
        prefix.
        c                ^    i | ])\  }}|j         d d         k    d|dk    r|nd|  |*S )   xmlns :)r?   )r[   pnrA   s      rB   
<dictcomp>z7_BaseXMLFormatter._other_namespaces.<locals>.<dictcomp>  sY       Aq"--- 4Q"WW'a''33Q---rD   )r&   items)rA   	nmsp_dicts   ` rB   _other_namespacesz#_BaseXMLFormatter._other_namespaces  sR     %'	? 	    O1133  I rD   ddict[str, Any]elem_rowr   c                   | j         s|S | j         D ]o}|                     |          }	 t          ||                   st          ||                   |j        |<   K# t
          $ r}t          d|           |d}~ww xY w|S )z
        Create attributes of row.

        This method adds attributes using attr_cols to row element and
        works with tuples for multindex or hierarchical columns.
        no valid column, N)r#   _get_flat_col_namer   r*   attribKeyError)rA   rr   rt   col	attr_nameerrs         rB   _build_attribsz _BaseXMLFormatter._build_attribs  s     ~ 	O> 	C 	CC//44ICAcF|| =14QsVHOI. C C C838899sBCs   2A
A>&A99A>rz   str | tuplec                   |}t          |t                    rfd|v r1d                    d |D                                                       n0d                    d |D                                                       }| j         | S )Nrj   c                ,    g | ]}t          |          S rZ   r*   r[   cs     rB   r]   z8_BaseXMLFormatter._get_flat_col_name.<locals>.<listcomp>3  s    ---AQ---rD   _c                ,    g | ]}t          |          S rZ   r   r   s     rB   r]   z8_BaseXMLFormatter._get_flat_col_name.<locals>.<listcomp>5  s    333!s1vv333rD   )
isinstancetuplejoinstripr?   )rA   rz   flat_cols      rB   rw   z$_BaseXMLFormatter._get_flat_col_name.  s    c5!! 	 99 -----..44666XX33s33344::<< 
 /-8---rD   c                     t          |           r6   r   rH   s    rB   _sub_element_clsz"_BaseXMLFormatter._sub_element_cls9  s    !$'''rD   c                B   | j         }| j        sdS | j        D ]}|                     |          }	 t          ||                   s||         dk    rdnt	          ||                   }| |||          _        b# t          $ r}t          d|           |d}~ww xY wdS )z
        Create child elements of row.

        This method adds child elements using elem_cols to row element and
        works with tuples for multindex or hierarchical columns.
        Nrj   rv   )r   r%   rw   r   r*   textry   )rA   rr   rt   sub_element_clsrz   	elem_namevalr|   s           rB   _build_elemsz_BaseXMLFormatter._build_elems=  s     /~ 	F> 	C 	CC//44IC"1S6llKafllddAcF<?)4499 C C C838899sBC	C 	Cs   A	A::
BBBc                >   |                                  }| j        Wt          | j        d| j        | j        d          5 }|j                            |           d d d            n# 1 swxY w Y   d S |                    | j                  	                                S )NwbF)r0   r1   is_text)
rI   r   r   r0   r1   handlewritedecoder)   rstrip)rA   xml_dochandless      rB   write_outputz_BaseXMLFormatter.write_outputR  s    ""$$*# , $ 4   . $$W---. . . . . . . . . . . . . . . 4 >>$-0077999s   A&&A*-A*)NTr   r   NNNNNr   TTNr   N)"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-   r,   r.   r/   r0   r   r1   r2   r3   r4   r3   rE   r3   r4   )r3   rQ   r3   r*   )r3   rd   )rr   rs   rt   r   r3   r   )rz   r~   r3   r*   )rr   rs   rt   r   r3   r4   )r3   r    )rM   
__module____qualname____doc__rC   rI   r   r<   r=   r:   r@   r>   rq   r}   rw   r	   r   r   r   rZ   rD   rB   r   r   $   s       M Md SW &$!&*&*37!'+$(LP*115#* * * * *X( ( ( (    U* % % % U% * * * U*$ 6 6 6 U6*( ( ( (    U&    U( . . . U. ( ( ^( C C C UC( : : : U: : :rD   r   c                  @    e Zd ZdZd
dZddZed             Zd
dZd	S )EtreeXMLFormatter
    Class for formatting data in xml using Python standard library
    modules: `xml.etree.ElementTree` and `xml.dom.minidom`.
    r3   rE   c                   ddl m}m}m}  || j         | j         |                                           | _        | j        	                                D ]} || j        | j         | j
                   }| j        sD| j        s=t          |                                          | _        |                     ||           l|                     ||          }|                     ||            || j        d| j        | j                  | _        | j        r|                                 | _        | j        t/          d          | j        S )Nr   Element
SubElementtostring)rx   xml)methodr)   r+   zBTo use stylesheet, you need lxml installed and selected as parser.)xml.etree.ElementTreer   r   r   r?   r   rq   rootr;   valuesr!   r#   r%   listr`   r   r}   r)   r+   out_xmlr-   _prettify_treer.   
ValueErrorrA   r   r   r   rr   rt   s         rB   rI   zEtreeXMLFormatter._build_treek  s   	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 G0009O9O9Q9Q
 
 
	 !((** 		/ 		/A!z$)-P-P-PQQH> /$. /!%affhh!!!X....  ..q(;;!!!X....xI] 0	
 
 
  	1..00DL?&T   |rD   r*   c                   ddl m} d}| j        r| j                                        D ];\  }}t	          |t
                    r!t	          |t
                    r |||           <| j        rB	 d| j        | j                  d}nG# t          $ r}t          | j         d          |d }~ww xY wd| j        v rd| j        d          d}nd}|S )Nr   )register_namespacerj   {} is not included in namespaces)r   r   r&   ro   r   r*   r(   ry   )rA   r   urirl   rm   r|   s         rB   r>   z!EtreeXMLFormatter._get_prefix_uri  s   <<<<<<? 	--// - -1a%% -*Q*<*< -&&q!,,,{ 
?tt{;???CC   ";FFF  t&&24?2.222
s   -B 
B+B&&B+c                    ddl m} |S Nr   )r   )r   r   rA   r   s     rB   r   z"EtreeXMLFormatter._sub_element_cls  s    444444rD   c                f    ddl m}  || j                  }|                    d| j                  S )z
        Output tree for pretty print format.

        This method will pretty print xml with line breaks and indentation.
        r   )parseStringz  )indentr)   )xml.dom.minidomr   r   toprettyxmlr)   )rA   r   doms      rB   r   z EtreeXMLFormatter._prettify_tree  s?     	0/////k$,''dT]CCCrD   Nr   r   )	rM   r   r   r   rI   r>   r	   r   r   rZ   rD   rB   r   r   e  s|         
% % % %N   ,   ^
D D D D D DrD   r   c                  Z     e Zd ZdZd fdZddZddZdd	Zed
             Z	ddZ
 xZS )LxmlXMLFormatterr   r3   r4   c                b     t                      j        |i | |                                  d S r6   )superrC   _convert_empty_str_key)rA   argskwargs	__class__s      rB   rC   zLxmlXMLFormatter.__init__  s7    $)&)))##%%%%%rD   rE   c                d   ddl m}m}m}  || j         | j         | j                  | _        | j        	                                D ]} || j        | j         | j
                   }| j        sD| j        s=t          |                                          | _        |                     ||           l|                     ||          }|                     ||            || j        | j        d| j        | j                  | _        | j        |                                 | _        | j        S )rG   r   r   )nsmapr   )r-   r   r)   r+   )
lxml.etreer   r   r   r?   r   r&   r   r;   r   r!   r#   r%   r   r`   r   r}   r-   r)   r+   r   r.   _transform_docr   s         rB   rI   zLxmlXMLFormatter._build_tree  sd   	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 Gt@@@XXX	!((** 		/ 		/A!z$)-P-P-PQQH> /$. /!%affhh!!!X....  ..q(;;!!!X....xI*] 0
 
 
 ?&..00DL|rD   c                    | j         r@d| j                                         v r'| j                             dd          | j         d<   dS dS dS )z
        Replace zero-length string in `namespaces`.

        This method will replace '' with None to align to `lxml`
        requirement that empty string prefixes are not allowed.
        rj   defaultN)r&   r`   poprH   s    rB   r   z'LxmlXMLFormatter._convert_empty_str_key  s]     ? 	GrT_%9%9%;%;;;$(O$7$7I$F$FDOD!!!	G 	G;;rD   r*   c                    d}| j         rf| j        rB	 d| j         | j                  d}nG# t          $ r}t          | j         d          |d }~ww xY wd| j         v rd| j         d          d}nd}|S )Nrj   r   r   r   )r&   r(   ry   )rA   r   r|   s      rB   r>   z LxmlXMLFormatter._get_prefix_uri  s    ? 	{ 
?tt{;???CC   ";FFF  t&&24?2.222
s   ) 
AAAc                    ddl m} |S r   )r   r   r   s     rB   r   z!LxmlXMLFormatter._sub_element_cls
  s    ))))))rD   c                   ddl m}m}m}m} | j        }|J t          || j        | j        | j	                  }|5 } || j                  }t          |t          j                  r8 ||                                                    | j                  |          }	n |||          }	ddd           n# 1 swxY w Y    ||	          }
 |
| j                  }t!          |          S )z
        Parse stylesheet from file or buffer and run it.

        This method will parse stylesheet object into tree for parsing
        conditionally by its specific object type, then transforms
        original tree with XSLT script.
        r   )XSLT	XMLParser
fromstringparseN)filepath_or_bufferr)   r0   r1   )r)   )parser)r   r   r   r   r   r.   r   r)   r0   r1   r   ioStringIOgetvalueencoder   rE   )rA   r   r   r   r   	style_dochandle_dataxml_datacurr_parserxsl_doctransformernew_docs               rB   r   zLxmlXMLFormatter._transform_doc  sx   	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 O	$$$,(]( 0	
 
 
  	>H#)T];;;K(BK00 >$*%%''..t}==k    %===	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> d7mm+di((W~~s   A1B99B= B=r   r   r   )rM   r   r   r   rC   rI   r   r>   r	   r   r   __classcell__)r   s   @rB   r   r     s         
& & & & & &
% % % %N	G 	G 	G 	G   "   ^
& & & & & & & &rD   r   ) r   
__future__r   rO   r   typingr   r   r   pandas.errorsr   pandas.util._decoratorsr	   pandas.core.dtypes.commonr
   pandas.core.dtypes.missingr   pandas.io.commonr   pandas.io.xmlr   pandas._typingr   r   r   r   r   pandasr   r   r   r   rZ   rD   rB   <module>r      s    # " " " " "  				          . - - - - - 2 2 2 2 2 2 2 2 2 2 2 2 + + + + + + ' ' ' ' ' ' 0 0 0 0 0 0 	!              !     ~: ~: ~: ~: ~: ~: ~: ~:B
TD TD TD TD TD) TD TD TDnz z z z z( z z z z zrD   