
    +d%                     f    d dl Z d dlZd dlmZ d dlmZ  G d de          Z G d de          ZdS )    N)islice)
DataSourcec                   l     e Zd ZdZdZdZdZ	 	 	 	 	 	 dded	ed
ededede	de	f fdZ
d Zd Zd Z xZS )JSONFileSourcez
    Read JSON files as a single dictionary or list

    The files can be local or remote. Extra parameters for encoding, etc.,
    go into ``storage_options``.
    json0.0.1pythonTutf8Nurlpath	text_modetext_encodingcompressionreadmetadatastorage_optionsc                 b   ddl m} t          |                                          dgz   }	|| _        |pi | _        d| _        d| _        || _        |||	vrt          d| d|	           |rdnd| _
        || _        || _        t          t          |                               |	           dS )
aD  
        Parameters
        ----------
        urlpath : str
            Target file. Can include protocol specified (e.g., "s3://").
        text_mode : bool
            Whether to open the file in text mode, recoding binary
            characters on the fly
        text_encoding : str
            If text_mode is True, apply this encoding. UTF* is by far the most
            common
        compression : str or None
            If given, decompress the file with the given codec on load. Can
            be something like "zip", "gzip", "bz2", or to try to guess from the
            filename, 'infer'
        storage_options: dict
            Options to pass to the file reader backend, including text-specific
            encoding arguments, and parameters specific to the remote
            file-system driver, if using.
        r   compressionsinferNCompression value  must be one of rtrbr   )fsspec.utilsr   listvalues_urlpath_storage_options
_dataframe_filer   
ValueErrormodeencoding_readsuperr   __init__selfr   r   r   r   r   r   r   r   VALID_COMPRESSIONS	__class__s             7lib/python3.11/site-packages/intake/source/jsonfiles.pyr'   zJSONFileSource.__init__   s    < 	.-----!,"5"5"7"788G9D / 52
& 	i"44 i !gk!g!gSe!g!ghhh%/DD4	%
nd##,,h,?????    c                     dd l }|                     | j                  d         } |j        |f| j        | j        | j        d| j        5 }t          j	        |          cd d d            S # 1 swxY w Y   d S )Nr   r#   r$   r   )
fsspec
_get_cacher   openr#   r$   r   r   r   loadr)   r0   r   fs       r,   r   zJSONFileSource.readD   s    //$-003V[
](	
 

 #
 
 	  9Q<<	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   A,,A03A0c                     d S N r)   s    r,   _load_metadatazJSONFileSource._load_metadataQ       r-   c                     d S r7   r8   r9   s    r,   _get_schemazJSONFileSource._get_schemaT   r;   r-   Tr
   NTNN)__name__
__module____qualname____doc__nameversion	containerstrbooldictr'   r   r:   r=   __classcell__r+   s   @r,   r   r      s          DGI
 # $.@ .@.@ .@ 	.@
 .@ .@ .@ .@ .@ .@ .@ .@ .@`             r-   r   c                        e Zd ZdZdZdZdZ	 	 	 	 	 	 dded	ed
ededede	de	f fdZ
ej        d             Zd ZddefdZd Zd Z xZS )JSONLinesFileSourcez
    Read a JSONL (https://jsonlines.org/) file and return a list of objects,
    each being valid json object (e.g. a dictionary or list)
    jsonlr   r	   Tr
   Nr   r   r   r   r   r   r   c                 T   ddl m} t          |                                          dgz   }	|| _        |pi | _        d| _        d| _        || _        |||	vrt          d| d|	           |rdnd| _
        || _        || _        t                                          |	           dS )
aE  
        Parameters
        ----------
        urlpath : str
            Target file. Can include protocol specified (e.g., "s3://").
        text_mode : bool
            Whether to open the file in text mode, recoding binary
            characters on the fly
        text_encoding : str
            If text_mode is True, apply this encoding. UTF* is by far the most
            common
        compression : str or None
            If given, decompress the file with the given codec on load. Can
            be something like "zip", "gzip", "bz2", or to try to guess from the
            filename, 'infer'.
        storage_options: dict
            Options to pass to the file reader backend, including text-specific
            encoding arguments, and parameters specific to the remote
            file-system driver, if using.
        r   r   r   Nr   r   r   r   r   )r   r   r   r   r   r   r    r!   r   r"   r#   r$   r%   r&   r'   r(   s             r,   r'   zJSONLinesFileSource.__init__b   s    < 	.-----!,"5"5"7"788G9D / 52
& 	i"44 i !gk!g!gSe!g!ghhh%/DD4	%
(+++++r-   c              #      K   ddl }|                     | j                  d         } |j        |f| j        | j        | j        d| j        5 }|V  ddd           dS # 1 swxY w Y   dS )z2
        Yields an fsspec.OpenFile object
        r   Nr/   )r0   r1   r   r2   r#   r$   r   r   r4   s       r,   _openzJSONLinesFileSource._open   s      
 	//$-003V[
](	
 

 #
 
 	 GGG	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AA#&A#c                     |                                  5 }t          t          t          j        |                    cd d d            S # 1 swxY w Y   d S r7   )rP   r   mapr   loads)r)   r5   s     r,   r   zJSONLinesFileSource.read   s    ZZ\\ 	,QDJ**++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s   'A		AAd   nrowsc           
          |                                  5 }t          t          t          j        t          ||                              cddd           S # 1 swxY w Y   dS )z>
        return the first `nrows` lines from the file
        N)rP   r   rR   r   rS   r   )r)   rU   r5   s      r,   headzJSONLinesFileSource.head   s     ZZ\\ 	;QDJq%(8(899::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s   5AAAc                     d S r7   r8   r9   s    r,   r:   z"JSONLinesFileSource._load_metadata   r;   r-   c                     d S r7   r8   r9   s    r,   r=   zJSONLinesFileSource._get_schema   r;   r-   r>   )rT   )r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   r'   
contextlibcontextmanagerrP   r   intrW   r:   r=   rI   rJ   s   @r,   rL   rL   X   s"        
 DGI
 # $-, -,-, -, 	-,
 -, -, -, -, -, -, -, -, -,^    , , ,; ;# ; ; ; ;        r-   rL   )rZ   r   	itertoolsr   intake.source.baser   r   rL   r8   r-   r,   <module>r_      s               ) ) ) ) ) )M M M M MZ M M M`Y Y Y Y Y* Y Y Y Y Yr-   