a
    ¡˜ëj¶  ã                   @   s’   d Z ddlZddlmZmZ eeeef  edœdd„Zeeeef  eddœdd	„Z	eeeeef  d
œdd„Z
eeeeef  dœdd„ZdS )z²
Adds support for working with newline-delimited JSON (ndjson) files. This format is useful for
streaming json content that would otherwise not be possible using raw JSON files.
é    N)ÚIOÚAny)ÚobjÚreturnc                    s   d  ‡ fdd„| D ƒ¡S )a'  
    Converts the list of dictionaries into string representation

    Args:
        obj (list[dict[str, Any]]): List of dictionaries to convert
        **kwargs: Additional keyword arguments to pass to json.dumps

    Returns:
        str: string representation of the list of dictionaries
    Ú
c                 3   s    | ]}t j|fi ˆ ¤ŽV  qd S )N)ÚjsonÚdumps)Ú.0Zeach©Úkwargs© ú]/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/file_utils/ndjson.pyÚ	<genexpr>   ó    zdumps.<locals>.<genexpr>)Újoin)r   r   r   r
   r   r   
   s    r   )r   Úfpr   c                 K   s&   d|d< t | fi |¤Ž}| |¡ dS )a8  
    Writes the list of dictionaries to a newline-delimited file

    Args:
        obj (list[dict[str, Any]]): List of dictionaries to convert
        fp (IO): File pointer to write the string representation to
        **kwargs: Additional keyword arguments to pass to json.dumps

    Returns:
        None
    NÚindent)r   Úwrite)r   r   r   Útextr   r   r   Údump   s    r   )Úsr   c                    s   ‡ fdd„|   ¡ D ƒS )a  
    Converts the raw string into a list of dictionaries

    Args:
        s (str): Raw string to convert
        **kwargs: Additional keyword arguments to pass to json.loads

    Returns:
        list[dict[str, Any]]: List of dictionaries parsed from the input string
    c                    s   g | ]}t j|fi ˆ ¤Ž‘qS r   )r   Úloads)r	   Úliner
   r   r   Ú
<listcomp>5   r   zloads.<locals>.<listcomp>)Ú
splitlines)r   r   r   r
   r   r   *   s    r   )r   r   c                 K   s   t |  ¡ fi |¤ŽS )a2  
    Converts the contents of the file into a list of dictionaries

    Args:
        fp (IO): File pointer to read the string representation from
        **kwargs: Additional keyword arguments to pass to json.loads

    Returns:
        list[dict[str, Any]]: List of dictionaries parsed from the file
    )r   Úread)r   r   r   r   r   Úload8   s    r   )Ú__doc__r   Útypingr   r   ÚlistÚdictÚstrr   r   r   r   r   r   r   r   Ú<module>   s   