a
    ¡˜ëj  ã                   @  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 eƒ eejƒe	ddddddddœdd„ƒƒƒZdS )aÛ  Provides `partition_json()`.

Note this does not partition arbitrary JSON. 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_json_processable)Ú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  sL  |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ƒ‚z:t |¡}	t|	ƒ}
t|	ƒdk�rt|
ƒdk�rt
dƒ‚W n tj�y,   t
d	ƒ‚Y n0 |
D ]}|�p>||j_�q2|
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_textzJJSON cannot be partitioned. Schema does not match the Unstructured schema.zNot a valid json)Ústripr   r   ÚopenÚreadÚ
isinstanceÚstrÚdecodeÚseekr   Ú
ValueErrorÚjsonÚloadsr   ÚlenÚJSONDecodeErrorÚmetadataÚlast_modified)r   r   r   r   r   r&   r   ÚfZfile_contentZelement_dictsÚelementsÚelement© r*   úZ/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/json.pyÚpartition_json   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.partition.common.commonr   Z&unstructured.partition.common.metadatar   Zunstructured.staging.baser   ZJSONr,   r*   r*   r*   r+   Ú<module>   s"   	    ü