
    *g>                    4   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	Z	ddl
mZ ddl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mZ erddlmZmZmZmZmZ ddl m!Z!  eed   ed   dz         G d d             Z" G d de"      Z# G d de"      Z$y)zF
:mod:`pandas.io.formats.xml` is a module for formatting data in XML.
    )annotationsN)TYPE_CHECKINGAnyfinalAbstractMethodError)cache_readonlydoc)is_list_like)isna)_shared_docs)
get_handle)get_data_from_filepathpreprocess_data)CompressionOptionsFilePath
ReadBufferStorageOptionsWriteBuffer)	DataFramestorage_optionscompression_optionspath_or_buffer)r   r   c                  <   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dd
       Zedd       Zedd       Zed        Zedd       Zedd       Zy)_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_options}

        .. versionchanged:: 1.4.0 Zstandard support.

    {storage_options}

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

    Nc                   || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        || _
        || _        || _        || _        || _        || _        | j                   j                   j#                         | _        | j'                         | _        | j+                          | j-                          | j/                         | _        | j3                          y N)framer   index	root_namerow_namena_rep	attr_cols	elem_cols
namespacesprefixencodingxml_declarationpretty_print
stylesheetcompressionr   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*   r+   r   s                    _/var/www/html/articles-backend/trend/venv/lib/python3.12/site-packages/pandas/io/formats/xml.py__init__z_BaseXMLFormatter.__init__l   s    & 
,
" ""$ .($/:.++224224 !..0    c                    t        |       )
        Build tree from  data.

        This method initializes the root and builds attributes and elements
        with optional namespaces.
        r   r6   s    r7   _build_treez_BaseXMLFormatter._build_tree   s     "$''r9   c                6   | j                   r@t        | j                         s+t        t        | j                         j                   d      | j
                  rAt        | j
                        s+t        t        | j
                        j                   d      yy)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$   r<   s    r7   r1   z#_BaseXMLFormatter._validate_columns   s     >>,t~~">'0011ST  >>,t~~">'0011ST  #?>r9   c                B    t        j                  | j                         y)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'   r<   s    r7   r2   z$_BaseXMLFormatter._validate_encoding   s     	dmm$r9   c                N   | j                   }| j                  r|j                         }| j                  Tt	        j
                         5  t	        j                  ddt               |j                  | j                        }ddd       |j                  d      S # 1 sw Y   xY w)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.
        NignorezDowncasting object dtype arrays)categoryr   )orient)
r   r   reset_indexr"   warningscatch_warningsfilterwarningsFutureWarningfillnato_dict)r6   dfs     r7   r/   z$_BaseXMLFormatter._process_dataframe   s     ZZ::!B;;"((* ,''5*
 YYt{{+, zzz)), ,s   	8BB$c                X   | j                   syt        t        | j                              }| j                  |   j	                         D cg c]  }|| j
                  vs| }}| j                  r|| j                  z   | _        | j                  r|| j                  z   | _        yyc c}w )zd
        Handle indexes.

        This method will add indexes into attr_cols or elem_cols.
        N)r   nextiterr0   keysr.   r#   r$   )r6   	first_keyxindexess       r7   r5   z!_BaseXMLFormatter._handle_indexes   s     zzd../0	''	2779
Qdnn=TA
 
 >>$t~~5DN>>$t~~5DN 
s   B'B'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   r<   s    r7   r3   z!_BaseXMLFormatter._get_prefix_uri   s     "$''r9   c                    i }| j                   rL| j                   j                         D ci c]'  \  }}|| j                  dd k7  rd|dk(  r|nd|  |) }}}|S c c}}w )z
        Define other namespaces.

        This method will build dictionary of namespaces attributes
        for root element, conditionally with optional namespaces and
        prefix.
           xmlns :)r%   itemsr4   )r6   	nmsp_dictpns       r7   _other_namespacesz#_BaseXMLFormatter._other_namespaces  s{     %'	?? !OO113Aq"-- QU!A30114I  s   ,Ac                    | j                   s|S | j                   D ]=  }| j                  |      }	 t        ||         st        ||         |j                  |<   ? |S # t
        $ r t        d|       w xY w)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, )r#   _get_flat_col_namer   strattribKeyError)r6   delem_rowcol	attr_names        r7   _build_attribsz _BaseXMLFormatter._build_attribs  s     ~~O>> 	:C//4I:AcF|14QsVHOOI.		:   :!23%899:s   )AA5c                :   |}t        |t              rpd|v r6dj                  |D cg c]  }t        |       c}      j	                         n5dj                  |D cg c]  }t        |       c}      j	                         }| j
                   | S c c}w c c}w )Nr]   _)
isinstancetuplejoinrg   stripr4   )r6   rl   flat_colcs       r7   rf   z$_BaseXMLFormatter._get_flat_col_name,  s    c5! 9 -AQ-.446XXs3!s1v34::< 
 //"8*--	 .3s   BBc                    t        |       r   r   r<   s    r7   _sub_element_clsz"_BaseXMLFormatter._sub_element_cls7  s    !$''r9   c                   | j                   }| j                  sy| j                  D ]H  }| j                  |      }	 t        ||         s||   dk(  rdnt	        ||         }| |||      _        J y# t        $ r t        d|       w xY w)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.
        Nr]   re   )rx   r$   rf   r   rg   textri   )r6   rj   rk   sub_element_clsrl   	elem_namevals          r7   _build_elemsz_BaseXMLFormatter._build_elems;  s     //~~>> 	:C//4I:"1S6lafldAcF<?)49		:
  :!23%899:s   4A22B
c                J   | j                         }| j                  St        | j                  d| j                  | j                  d      5 }|j
                  j                  |       d d d        y |j                  | j                        j                         S # 1 sw Y   y xY w)NwbF)r+   r   is_text)
r=   r   r   r+   r   handlewritedecoder'   rstrip)r6   xml_dochandless      r7   write_outputz_BaseXMLFormatter.write_outputP  s    ""$*## ,, $ 4 4 . $$W-.  >>$--07799. s   BB")NTdatarowNNNNNzutf-8TTNinferN)"r   r   r   z7FilePath | WriteBuffer[bytes] | WriteBuffer[str] | Noner   boolr    
str | Noner!   r   r"   r   r#   list[str] | Noner$   r   r%   zdict[str | None, str] | Noner&   r   r'   rg   r(   bool | Noner)   r   r*   z5FilePath | ReadBuffer[str] | ReadBuffer[bytes] | Noner+   r   r   zStorageOptions | NonereturnNoner   bytesr   r   )r   zdict[int | str, dict[str, Any]]r   rg   )r   dict)rj   dict[str, Any]rk   r   r   r   )rl   zstr | tupler   rg   )rj   r   rk   r   r   r   )r   r   )rA   
__module____qualname____doc__r8   r=   r   r1   r2   r/   r5   r3   rc   rn   rf   r	   rx   r~   r    r9   r7   r   r   ,   s   
9| SW &$!&*&*37!'+$(LP*115#** P* 	*
 * * * $* $* 1* * * %* "* J*  (!*" /#*$ 
%*X(  * % % * *0 6 6*(  &  ( . . ( ( : :( : :r9   r   c                  8    e Zd ZdZddZddZed        ZddZy)	EtreeXMLFormatter
    Class for formatting data in xml using Python standard library
    modules: `xml.etree.ElementTree` and `xml.dom.minidom`.
    c                   ddl m}m}m}  || j                   | j
                   | j                               | _        | j                  j                         D ]  } || j                  | j                   | j                         }| j                  s=| j                  s1t        |j                               | _        | j                  ||       w| j!                  ||      }| j                  ||         || j                  d| j"                  | j$                        | _        | j(                  r| j+                         | _        | j,                  t/        d      | j&                  S )Nr   Element
SubElementtostring)rh   xml)methodr'   r(   zBTo use stylesheet, you need lxml installed and selected as parser.)xml.etree.ElementTreer   r   r   r4   r    rc   rootr0   valuesr!   r#   r$   listrT   r~   rn   r'   r(   out_xmlr)   _prettify_treer*   
ValueErrorr6   r   r   r   rj   rk   s         r7   r=   zEtreeXMLFormatter._build_treei  s8   	
 	
 /09O9O9Q
	 !!((* 		/A!$))/@-PQH>>$..!%affh!!!X.  ..q(;!!!X.		/  II]] 00	
 ..0DL??&T  ||r9   c                   ddl m} d}| j                  r| j                  j                         D ]0  \  }}t	        |t
              st	        |t
              s( |||       2 | j                  r 	 d| j                  | j                      d}|S d| j                  v rd| j                  d    d}|S d}|S # t        $ r t        | j                   d      w xY w)Nr   )register_namespacer]   {} is not included in namespaces)r   r   r%   r_   rq   rg   r&   ri   )r6   r   urira   rb   s        r7   r3   z!EtreeXMLFormatter._get_prefix_uri  s    <??--/ -1a%*Q*<&q!,- {{Stt{{;<B?C 
 t&4??2./r2 
 
   S"dkk]2P#QRRSs   /B5 5"Cc                    ddl m} |S Nr   )r   )r   r   r6   r   s     r7   rx   z"EtreeXMLFormatter._sub_element_cls  s    4r9   c                l    ddl m}  || j                        }|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'   )r6   r   doms      r7   r   z EtreeXMLFormatter._prettify_tree  s,     	0$,,'dT]]CCr9   Nr   r   )	rA   r   r   r   r=   r3   r	   rx   r   r   r9   r7   r   r   c  s,    
%N(  
Dr9   r   c                  T     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   c                D    t        |   |i | | j                          y r   )superr8   _convert_empty_str_key)r6   argskwargs	__class__s      r7   r8   zLxmlXMLFormatter.__init__  s!    $)&)##%r9   c                   ddl m}m}m}  || j                   | j
                   | j                        | _        | j                  j                         D ]  } || j                  | j                   | j                         }| j                  s=| j                  s1t        |j                               | _        | j                  ||       w| j!                  ||      }| j                  ||         || j                  | j"                  d| j$                  | j&                        | _        | j*                  | j-                         | _        | j(                  S )r;   r   r   )nsmapr   )r)   r   r'   r(   )
lxml.etreer   r   r   r4   r    r%   r   r0   r   r!   r#   r$   r   rT   r~   rn   r)   r'   r(   r   r*   _transform_docr   s         r7   r=   zLxmlXMLFormatter._build_tree  s   	
 	
 t//?@X	!!((* 		/A!$))/@-PQH>>$..!%affh!!!X.  ..q(;!!!X.		/  II**]] 00
 ??&..0DL||r9   c                    | j                   rGd| j                   j                         v r*| j                   j                  dd      | j                   d<   yyy)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.
        r]   defaultN)r%   rT   popr<   s    r7   r   z'LxmlXMLFormatter._convert_empty_str_key  sE     ??rT__%9%9%;;$(OO$7$7I$FDOOD!  <?r9   c                   d}| j                   rQ| j                  r 	 d| j                   | j                      d}|S d| j                   v rd| j                   d    d}|S d}|S # t        $ r t        | j                   d      w xY w)Nr]   r   r   r   )r%   r&   ri   )r6   r   s     r7   r3   z LxmlXMLFormatter._get_prefix_uri  s    ??{{Stt{{;<B?C 
 t&4??2./r2 
 
   S"dkk]2P#QRRSs   A" ""Bc                    ddl m} |S r   )r   r   r   s     r7   rx   z!LxmlXMLFormatter._sub_element_cls  s    )r9   c                   ddl m}m}m}m} | j
                  }|J t        || j                  | j                  | j                        }t        |      5 } || j                        }t        |t        j                        r2 ||j                         j                  | j                        |      }	n
 |||      }	ddd        |	      }
 |
| j                         }t#        |      S # 1 sw Y   .xY w)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'   r+   r   )r'   )parser)r   r   r   r   r   r*   r   r'   r+   r   r   rq   ioStringIOgetvalueencoder   r   )r6   r   r   r   r   	style_dochandle_dataxml_datacurr_parserxsl_doctransformernew_docs               r7   r   zLxmlXMLFormatter._transform_doc
  s    	
 	
 OO	$$$,(]](( 00	
 [) 	>X#T]];K(BKK0$%%'..t}}=k  =	> 7mdii(W~	> 	>s   A*C,,C5r   r   r   )rA   r   r   r   r8   r=   r   r3   r	   rx   r   __classcell__)r   s   @r7   r   r     s7    
&
%N	G  
&r9   r   )%r   
__future__r   rC   r   typingr   r   r   rJ   pandas.errorsr   pandas.util._decoratorsr	   r
   pandas.core.dtypes.commonr   pandas.core.dtypes.missingr   pandas.core.shared_docsr   pandas.io.commonr   pandas.io.xmlr   r   pandas._typingr   r   r   r   r   pandasr   r   r   r   r   r9   r7   <module>r      s    #  	 
  -
 3 + 0 '
   !  !23$%:;>NNp: p:	p:f	RD) RDjx( xr9   