a
    ¡˜ëjp  ã                   @  s¾   d Z ddlmZ ddl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mZ ddlmZ dd	lmZ dd
lmZ ddlmZ eƒ eejƒe	ddddddddœdd„ƒƒƒZdS )aß  Provides `partition_ndjson()`.

Note this does not partition arbitrary NDJSON. Its only use-case is to "rehydrate" unstructured
document elements serialized to JSON, essentially the same function as `elements_from_json()`, but
this allows a document of already-partitioned elements to be combined transparently with other
documents in a partitioning run. It also allows multiple (low-cost) chunking runs to be performed on
a document while only incurring partitioning cost once.
é    )ÚannotationsN)ÚIOÚAnyÚOptional)Úadd_chunking_strategy)ÚElementÚprocess_metadata)ÚFileTypeÚadd_metadata_with_filetypeÚis_ndjson_processable)Úloads)Úexactly_one)Úget_last_modified_date)Úelements_from_dictszOptional[str]zOptional[IO[bytes]]r   zlist[Element])ÚfilenameÚfileÚtextÚmetadata_last_modifiedÚkwargsÚreturnc                 K  sJ  |dur |  ¡ dkr |s | s g S t| ||d� | r:t| ƒnd}d}| dur€t| dd��}| ¡ }W d  ƒ qÂ1 st0    Y  nB|dur²| ¡ }t|tƒrž|n| ¡ }| d¡ n|durÂt|ƒ}t	|d�sÔt
dƒ‚z8t|ƒ}	t|	ƒ}
t|	ƒdk�r
t|
ƒdk�r
t
d	ƒ‚W n tj�y*   t
d
ƒ‚Y n0 |
D ]}|�p<||j_�q0|
S )a‚  Partitions serialized Unstructured output into its constituent elements.

    Parameters
    ----------
    filename
        A string defining the target filename path.
    file
        A file-like object as bytes --> open(filename, "rb").
    text
        The string representation of the .json document.
    metadata_last_modified
        The last modified date for the document.
    NÚ )r   r   r   Úutf8)Úencodingr   )Ú	file_textzLNDJSON cannot be partitioned. Schema does not match the Unstructured schema.zJJSON cannot be partitioned. Schema does not match the Unstructured schema.zNot a valid ndjson)Ústripr   r   ÚopenÚreadÚ
isinstanceÚstrÚdecodeÚseekr   Ú
ValueErrorÚndjson_loadsr   ÚlenÚjsonÚJSONDecodeErrorÚmetadataÚlast_modified)r   r   r   r   r   r'   r   ÚfZfile_contentZelement_dictsÚelementsÚelement© r+   ú\/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/ndjson.pyÚpartition_ndjson   s<    (
ÿÿr-   )NNNN)Ú__doc__Ú
__future__r   r$   Útypingr   r   r   Zunstructured.chunkingr   Zunstructured.documents.elementsr   r   Z unstructured.file_utils.filetyper	   r
   r   Zunstructured.file_utils.ndjsonr   r"   Z$unstructured.partition.common.commonr   Z&unstructured.partition.common.metadatar   Zunstructured.staging.baser   ZNDJSONr-   r+   r+   r+   r,   Ú<module>   s$   	    ü