a
    ¡˜ëj@  ã                   @  s  U d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	 d dl
mZ d dlm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mZ d dlmZmZ d dlmZ dZded< eej ƒed"ddddddœdddddddddœdd„ƒƒZ!ddddddœdd„Z"ddddœd d!„Z#dS )#é    )ÚannotationsN)ÚBytesIO)ÚIOÚAnyÚIteratorÚcast)Úetree)Úadd_chunking_strategy)ÚElementÚElementMetadataÚText)Úread_txt_file)ÚFileType)Úexactly_oneÚspooled_to_bytes_io_if_needed)Úapply_metadataÚget_last_modified_date)Úelement_from_textÚxmlÚstrÚDETECTION_ORIGINF)ÚfileÚtextÚencodingÚxml_keep_tagsÚxml_pathz
str | NonezIO[bytes] | NoneÚboolr   zlist[Element])Úfilenamer   r   r   r   r   ÚkwargsÚreturnc                K  sÄ   t | ||d� g }t| | r"t| ƒndd�}t|_|r„| rJt| |d�d }	n*|rdtt|ƒ|d�d }	n|duspJ ‚|}	t|	|d�g}n<t| |||d�}
|
D ]&}|r˜t	|ƒ}t
 |¡|_| |¡ q˜|S )	ap  Partitions an XML document into its document elements.

    Parameters
    ----------
    filename
        A string defining the target filename path.
    file
        A file-like object using "rb" mode --> open(filename, "rb").
    text
        The text of the XML file.
    encoding
        The encoding method used to decode the text input. If None, utf-8 will be used.
    xml_keep_tags
        If True, will retain the XML tags in the output. Otherwise it will simply extract
        the text from within the tags.
    xml_path
        The xml_path to use for extracting the text. Only used if xml_keep_tags=False.
    ©r   r   r   N)r   Zlast_modified)r   r   é   )r   r   )r   Úmetadata)r   r   r   r   )r   r   r   r   Zdetection_originr   r   r   Úget_leaf_elementsr   ÚcopyÚdeepcopyr"   Úappend)r   r   r   r   r   r   r   Úelementsr"   Zraw_textZleaf_elementsZleaf_elementÚelement© r)   úY/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/xml.pyÚpartition_xml   s4    ÿür+   zIterator[str | None])r   r   r   r   r   c                 C  sX   t | ||d� | rt| |d�S |r2tt|ƒ|d�S tttt|ƒdd�ƒ}t||d�S dS )zGGet leaf elements from the XML tree defined in filename, file, or text.r    )r   )r   r   zutf-8)r   N)r   Ú_get_leaf_elementsr   r   Úbytesr   r   )r   r   r   r   Úbr)   r)   r*   r#   Y   s    r#   zstr | IO[bytes])r   r   r   c                 c  s²   g }t j| ddd�}|durDt|ƒ\}}t  |¡}dd„ ||ƒD ƒ}|D ]d\}}|dkrb| |¡ |dkrŽ|jdur†|j ¡ r†|jV  | ¡  |rH|d	  ¡ du rH| 	¡  qŽqHdS )
z<Parse the XML tree in a memory efficient manner if possible.)ÚstartÚendF)ÚeventsÚresolve_entitiesNc                 s  s   | ]}d |fV  qdS )r0   Nr)   )Ú.0Úelr)   r)   r*   Ú	<genexpr>u   ó    z%_get_leaf_elements.<locals>.<genexpr>r/   r0   éÿÿÿÿ)
r   Ú	iterparseÚnextZXPathr&   r   ÚstripÚclearZ	getparentÚpop)r   r   Zelement_stackZelement_iteratorÚ_r(   Zcompiled_pathÚeventr)   r)   r*   r,   g   s    

r,   )N)$Ú
__future__r   r$   Úior   Útypingr   r   r   r   Úlxmlr   Zunstructured.chunkingr	   Zunstructured.documents.elementsr
   r   r   Z unstructured.file_utils.encodingr   Zunstructured.file_utils.modelr   Z$unstructured.partition.common.commonr   r   Z&unstructured.partition.common.metadatar   r   Zunstructured.partition.textr   r   Ú__annotations__ÚXMLr+   r#   r,   r)   r)   r)   r*   Ú<module>   s0    ÿù$@