a
    ¡˜ëj   ã                   @  sæ   U 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 d dlmZmZmZ d dlmZ d dlmZmZ d d	lmZ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„ƒƒZG dd„ dƒZdS )é    )ÚannotationsN)ÚIOÚAnyÚIterator)Úadd_chunking_strategy)Ú	HtmlTable)ÚElementÚElementMetadataÚTable)ÚFileType)Úapply_metadataÚget_last_modified_date)Úis_temp_file_pathÚlazypropertyÚcsvÚstrÚDETECTION_ORIGINFT)ÚfileÚencodingÚinclude_headerÚinfer_table_structureú
str | NoneúIO[bytes] | NoneÚboolr   zlist[Element])Úfilenamer   r   r   r   ÚkwargsÚreturnc          
      K  s–   t j| ||||d�}| ¡ �(}tj||j|j|jd�}W d  ƒ n1 sJ0    Y  t 	|j
d|dd�¡}t| |j|r||jndd�}	t|j|	td�gS )	aj  Partitions Microsoft Excel Documents in .csv format into its document elements.

    Parameters
    ----------
    filename
        A string defining the target filename path.
    file
        A file-like object using "rb" mode --> open(filename, "rb").
    encoding
        The encoding method used to decode the text input. If None, utf-8 will be used.
    include_header
        Determines whether or not header info info is included in text and medatada.text_as_html.
    infer_table_structure
        If True, any Table elements that are extracted will also have a metadata field
        named "text_as_html" where the table's text content is rendered into an html string.
        I.e., rows and cells are preserved.
        Whether True or False, the "text" field is always present in any Table element
        and is the text content of the table (no structure).
    ©Ú	file_pathr   r   r   r   )ÚheaderÚsepr   NFÚ )Úindexr   Zna_rep)r   Úlast_modifiedZtext_as_html)ÚtextÚmetadataZdetection_origin)Ú_CsvPartitioningContextÚloadÚopenÚpdZread_csvr   Ú	delimiterr   r   Zfrom_html_textZto_htmlr	   r#   Úhtmlr
   r$   r   )
r   r   r   r   r   r   ÚctxZ	dataframeZ
html_tabler%   © r-   úY/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/csv.pyÚpartition_csv   s$    û
6ÿýr/   c                   @  s®   e Zd ZdZdddddddœd	d
„Zedddddd dœdd„ƒZeddœdd„ƒZeddœdd„ƒZ	eddœdd„ƒZ
eddœdd„ƒZejddœdd„ƒZd dœdd„ZdS )r&   zÚEncapsulates the partitioning-run details.

    Provides access to argument values and especially encapsulates computation of values derived
    from those values so they don't obscure the core partitioning logic.
    NFTr   r   r   r   c                 C  s"   || _ || _|| _|| _|| _d S )N)Ú
_file_pathÚ_fileÚ	_encodingÚ_include_headerZ_infer_table_structure)Úselfr   r   r   r   r   r-   r-   r.   Ú__init__Q   s
    z _CsvPartitioningContext.__init__)r   r   r   r   r   r   c                 C  s   | |||||d�  ¡ S )Nr   )Ú	_validate)Úclsr   r   r   r   r   r-   r-   r.   r'   _   s    	ûz_CsvPartitioningContext.load)r   c                   s€   t  ¡ }d}ˆ  ¡ �.}d ‡ fdd„| |¡D ƒ¡}W d  ƒ n1 sH0    Y  z|j|dd�jW S  t jyz   Y dS 0 dS )z�The CSV delimiter, nominally a comma ",".

        `None` for a single-column CSV file which naturally has no delimiter.
        i   Ú
c                 3  s   | ]}|  ˆ jpd ¡V  qdS )zutf-8N)Údecoder2   )Ú.0Úln©r4   r-   r.   Ú	<genexpr>{   s   z4_CsvPartitioningContext.delimiter.<locals>.<genexpr>Nz,;)Ú
delimiters)r   ÚSnifferr(   ÚjoinÚ	readlinesÚsniffr*   ÚError)r4   ZsnifferÚ	num_bytesr   Údatar-   r<   r.   r*   p   s    
ÿ&z!_CsvPartitioningContext.delimiterz
int | Nonec                 C  s   | j r
dS dS )z5Identifies the header row, if any, to Pandas, by idx.r   N)r3   r<   r-   r-   r.   r   …   s    z_CsvPartitioningContext.headerc                 C  s   | j S )z)The encoding to use for reading the file.)r2   r<   r-   r-   r.   r   Š   s    z _CsvPartitioningContext.encodingc                 C  s   | j rt| j ƒrdS t| j ƒS )zHThe best last-modified date available, None if no sources are available.N)r0   r   r   r<   r-   r-   r.   r#   �   s    ÿÿýz%_CsvPartitioningContext.last_modifiedzIterator[IO[bytes]]c                 c  sj   | j r:t| j dƒ�}|V  W d  ƒ qf1 s.0    Y  n,| j}|dusLJ ‚| d¡ |V  | d¡ dS )au  Encapsulates complexity of dealing with file-path or file-like-object.

        Provides an `IO[bytes]` object as the "common-denominator" document source.

        Must be used as a context manager using a `with` statement:

            with self._file as file:
                do things with file

        File is guaranteed to be at read position 0 when called.
        ÚrbNr   )r0   r(   r1   Úseek)r4   Úfr   r-   r-   r.   r(   ˜   s    &
z_CsvPartitioningContext.openc                 C  s    | j du r| jdu rtdƒ‚| S )z!Raise on invalid argument values.Nz5either file-path or file-like object must be provided)r0   r1   Ú
ValueErrorr<   r-   r-   r.   r6   °   s    z!_CsvPartitioningContext._validate)NNNFT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r5   Úclassmethodr'   r   r*   r   r   r#   Ú
contextlibÚcontextmanagerr(   r6   r-   r-   r-   r.   r&   J   s(        úr&   )N)Ú
__future__r   rO   r   Útypingr   r   r   Zpandasr)   Zunstructured.chunkingr   Zunstructured.common.html_tabler   Zunstructured.documents.elementsr   r	   r
   Zunstructured.file_utils.modelr   Z&unstructured.partition.common.metadatar   r   Zunstructured.utilsr   r   r   Ú__annotations__ZCSVr/   r&   r-   r-   r-   r.   Ú<module>   s*    ÿú"5