
    ̒geV                     x   d dl Z d dlZd dlZd dlZd dlmZ ddlmZmZm	Z	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mZ  ej        d          Z G d	 d
          Z G d de          Z	 	 	 	 	 	 	 	 	 	 ddZd Zd Z	 	 	 	 	 	 ddZddZ d Z!d Z"d Z#d Z$	 	 	 	 	 	 d dZ%d Z& G d de j'                  Z(dS )!    N)	has_magic   )	BaseCache
BlockCache
BytesCache	MMapCacheReadAheadCachecaches)compr)
filesystemget_filesystem_class)_unstrip_protocolbuild_name_functioninfer_compressionstringify_pathfsspecc                   ^    e Zd ZdZ	 	 	 	 	 ddZd Zd Zd Zd Ze	d	             Z
d
 Zd ZdS )OpenFileaF  
    File-like object to be used in a context

    Can layer (buffered) text-mode and compression over any file-system, which
    are typically binary-only.

    These instances are safe to serialize, as the low-level file object
    is not created until invoked using ``with``.

    Parameters
    ----------
    fs: FileSystem
        The file system to use for opening the file. Should be a subclass or duck-type
        with ``fsspec.spec.AbstractFileSystem``
    path: str
        Location to open
    mode: str like 'rb', optional
        Mode of the opened file
    compression: str or None, optional
        Compression to apply
    encoding: str or None, optional
        The encoding to use if opened in text mode.
    errors: str or None, optional
        How to handle encoding errors if opened in text mode.
    newline: None or str
        Passed to TextIOWrapper in text mode, how to handle line endings.
    autoopen: bool
        If True, calls open() immediately. Mostly used by pickle
    pos: int
        If given and autoopen is True, seek to this location immediately
    rbNc                     || _         || _        || _        t          ||          | _        || _        || _        || _        g | _        d S N)	fspathmodeget_compressioncompressionencodingerrorsnewlinefobjects)selfr   r   r   r   r   r   r   s           +lib/python3.11/site-packages/fsspec/core.py__init__zOpenFile.__init__=   sK     		*4==     c                 h    t           | j        | j        | j        | j        | j        | j        | j        ffS r   )r   r   r   r   r   r   r   r   r!   s    r"   
__reduce__zOpenFile.__reduce__P   s8    		 
 	
r$   c                     d| j          dS )Nz<OpenFile 'z'>)r   r&   s    r"   __repr__zOpenFile.__repr__^   s    *TY****r$   c                    | j                             dd                              dd          dz   }| j                            | j        |          }|g| _        | j        ?t          | j                 } |||d                   }| j                            |           d| j         vr<t          || j
        | j        | j                  }| j                            |           | j        d         S )Nt b)r   r   )r   r   r   )r   replacer   openr   r    r   r   appendPickleableTextIOWrapperr   r   r   )r!   r   fcompresss       r"   	__enter__zOpenFile.__enter__a   s    y  b))11#r::S@GLLL..'T-.Ha)))AM  ###di'DM$+t|  A M  ###}R  r$   c                 .    |                                   d S r   )close)r!   argss     r"   __exit__zOpenFile.__exit__v   s    

r$   c                 6    t          | j        | j                  S r   )r   r   r   r&   s    r"   	full_namezOpenFile.full_namey   s     DG444r$   c                 *    |                                  S )a  Materialise this as a real open file without context

        The OpenFile object should be explicitly closed to avoid enclosed file
        instances persisting. You must, therefore, keep a reference to the OpenFile
        during the life of the file-like it generates.
        r5   r&   s    r"   r0   zOpenFile.open}   s     ~~r$   c                     t          | j                  D ]:}d| j        vr|j        s|                                 |                                 ;| j                                         dS )z#Close all encapsulated file objectsrN)reversedr    r   closedflushr7   clear)r!   r3   s     r"   r7   zOpenFile.close   se    $-(( 	 	A$)##AH#			GGIIIIr$   )r   NNNN)__name__
__module____qualname____doc__r#   r'   r)   r5   r9   propertyr;   r0   r7    r$   r"   r   r      s         H    &
 
 
+ + +! ! !*   5 5 X5         r$   r   c                   F     e Zd ZdZddd fd
Zd Zd Z fdZd	 Z xZ	S )
	OpenFilesa  List of OpenFile instances

    Can be used in a single context, which opens and closes all of the
    contained files. Normal list access to get the elements works as
    normal.

    A special case is made for caching filesystems - the files will
    be down/uploaded together at the start or end of the context, and
    this may happen concurrently, if the target filesystem supports it.
    r   Nr   r   c                ^    || _         || _        g | _         t                      j        |  d S r   )r   r   filessuperr#   )r!   r   r   r8   	__class__s       r"   r#   zOpenFiles.__init__   s2    	
$r$   c                     | j         t          d          | j         }	 t          |d          r!|                    |           | _        | j        S t          |d          r|j         |j         }nnRd | D             S )NzContext has already been usedT	open_manyr   c                 6    g | ]}|                                 S rI   r=   ).0ss     r"   
<listcomp>z'OpenFiles.__enter__.<locals>.<listcomp>   s     ,,,!,,,r$   )r   
ValueErrorhasattrrR   rN   )r!   r   s     r"   r5   zOpenFiles.__enter__   s    7?<===W	r;'' "\\$//
z!r4   RU%6U	 -,t,,,,r$   c                     | j         }fd| D              d| j        vrO	 t          |d          r|                    | j                   d S t          |d          r|j         |j         }nd S Nd S )Nc                 $    g | ]} |j          S rI   )r9   )rT   rU   r8   s     r"   rV   z&OpenFiles.__exit__.<locals>.<listcomp>   s"    )))qT	)))r$   r?   TrR   r   )r   r   rX   commit_manyrN   )r!   r8   r   s    ` r"   r9   zOpenFiles.__exit__   s    W))))D))))di2{++ NN4:...F2t$$ ):BBE  r$   c                     t                                          |          }t          |t                    rt	          || j        | j                  S |S )NrL   )rO   __getitem__
isinstanceslicerK   r   r   )r!   itemoutrP   s      r"   r]   zOpenFiles.__getitem__   sJ    gg!!$''dE"" 	>StyTW====
r$   c                 (    dt          |            dS )Nz	<List of z OpenFile instances>)lenr&   s    r"   r)   zOpenFiles.__repr__   s    :3t99::::r$   )
rD   rE   rF   rG   r#   r5   r9   r]   r)   __classcell__rP   s   @r"   rK   rK      s        	 	 $(D              - - -       ; ; ; ; ; ; ;r$   rK   r   utf8Tc           	          t          | |||||
          \  }}j        dk    r|	_        n"dvr|	rfd|D             }fd|D              t          fd|D                       S )av
  Given a path or paths, return a list of ``OpenFile`` objects.

    For writing, a str path must contain the "*" character, which will be filled
    in by increasing numbers, e.g., "part*" ->  "part1", "part2" if num=2.

    For either reading or writing, can instead provide explicit list of paths.

    Parameters
    ----------
    urlpath: string or list
        Absolute or relative filepath(s). Prefix with a protocol like ``s3://``
        to read from alternative filesystems. To read from multiple files you
        can pass a globstring or a list of paths, with the caveat that they
        must all have the same protocol.
    mode: 'rb', 'wt', etc.
    compression: string or None
        If given, open file using compression codec. Can either be a compression
        name (a key in ``fsspec.compression.compr``) or "infer" to guess the
        compression from the filename suffix.
    encoding: str
        For text mode only
    errors: None or str
        Passed to TextIOWrapper in text mode
    name_function: function or None
        if opening a set of files for writing, those files do not yet exist,
        so we need to generate their names by formatting the urlpath for
        each sequence number
    num: int [1]
        if writing mode, number of files we expect to create (passed to
        name+function)
    protocol: str or None
        If given, overrides the protocol found in the URL.
    newline: bytes or None
        Used for line terminator in text mode. If None, uses system default;
        if blank, uses no translation.
    auto_mkdir: bool (True)
        If in write mode, this will ensure the target directory exists before
        writing, by calling ``fs.mkdirs(exist_ok=True)``.
    expand: bool
    **kwargs: dict
        Extra options that make sense to a particular storage connection, e.g.
        host, port, username, password, etc.

    Examples
    --------
    >>> files = open_files('2015-*-*.csv')  # doctest: +SKIP
    >>> files = open_files(
    ...     's3://bucket/2015-*-*.csv.gz', compression='gzip'
    ... )  # doctest: +SKIP

    Returns
    -------
    An ``OpenFiles`` instance, which is a list of ``OpenFile`` objects that can
    be used as a single context

    Notes
    -----
    For a full list of the available protocols and the implementations that
    they map across to see the latest online documentation:

    - For implementations built into ``fsspec`` see
      https://filesystem-spec.readthedocs.io/en/latest/api.html#built-in-implementations
    - For implementations in separate packages see
      https://filesystem-spec.readthedocs.io/en/latest/api.html#other-known-implementations
    )numname_functionstorage_optionsprotocolexpandfiler?   c                 :    h | ]}                     |          S rI   )_parent)rT   r   r   s     r"   	<setcomp>zopen_files.<locals>.<setcomp>$  s%    6662::d##666r$   c                 >    g | ]}                     |d           S )T)exist_ok)makedirs)rT   parentr   s     r"   rV   zopen_files.<locals>.<listcomp>%  s)    BBBVd	+	+BBBr$   c                 <    g | ]}t          |           S ))r   r   r   r   r   )r   )rT   r   r   r   r   r   r   r   s     r"   rV   zopen_files.<locals>.<listcomp>'  sO     	
 	
 	
  '!  	
 	
 	
r$   rL   )get_fs_token_pathsrk   
auto_mkdirrK   )urlpathr   r   r   r   ri   rh   rk   r   rw   rl   kwargsfs_tokenpathsparentsr   s    ````   `      @r"   
open_filesr}      s    ^ -#  B% 
{f"	DZ6666666BBBB'BBBB	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
    r$   c                    t          j        d          d| v r!fd|                     d          D             n| g}g }d }|                                }t	          |          D ]}|                    dd           pt          |          d         pd}t          |          }|                    |          }|                    |i           }	||d         u r|		                    |           t          d	i ||	}
|                    |          }|dv rd|
vr|}|                    |||
f           |}t          t	          |                    }|S )
Nz.*[^a-z]+.*z::c                 P    g | ]"}d |v s                     |          r|n|d z   #S )://)match)rT   pxs     r"   rV   z_un_chain.<locals>.<listcomp>;  s7    PPP!eqjjAGGAJJjAIPPPr$   rk   r   rm   >   	filecache
blockcachesimplecachetarget_protocolrI   )recompilesplitcopyr@   popsplit_protocolr   _get_kwargs_from_urlsupdatedict_strip_protocolr1   list)r   ry   bitsra   previous_bitbitrk   clsextra_kwargskwskwr   s              @r"   	_un_chainr   8  sy   

=!!A 4<< 	QPPPtzz$?O?OPPPPV 	 CL[[]]F~~  ::j$//S>#3F3Fq3ISV"8,,0055jj2&&$q'>>JJv((L(C((!!#&&BBB!++C

C2&'''
x}}

CJr$   c                 z   h dfd|                                 D             }t          | |          }i }t          t          |                    D ]K\  }}|\  }}}|t	          |          dz
  k    rt          di ||}0t          di |||d<   ||d<   ||d<   L|d         \  }	}}
t          |fi |}||	fS )	a  
    Turn fully-qualified and potentially chained URL into filesystem instance

    Parameters
    ----------
    url : str
        The fsspec-compatible URL
    **kwargs: dict
        Extra options that make sense to a particular storage connection, e.g.
        host, port, username, password, etc.

    Returns
    -------
    filesystem : FileSystem
        The new filesystem discovered from ``url`` and created with
        ``**kwargs``.
    urlpath : str
        The file-systems-specific URL for ``url``.
    >   rh   r   r   rl   r   r   r   ri   c                 $    i | ]\  }}|v	||S rI   rI   )rT   kvknown_kwargss      r"   
<dictcomp>zurl_to_fs.<locals>.<dictcomp>w  s)    GGGtq!,1F1Fa1F1F1Fr$   r   target_optionsr   for   rI   )itemsr   	enumerater@   rc   r   r   )urlry   chaininkwargsichurlsrk   r   rx   _r   r   s               @r"   	url_to_fsr   W  s   ,	 	 	L HGGGv||~~GGGFc6""EH8E??++  2hE

Q--b-H--H%)%;%;B%;(%;%;!"&."# 8GXq	H	)	)	)	)Bw;r$   c                 `    t          d| g||||||dd|}|st          |           |d         S )a  Given a path or paths, return one ``OpenFile`` object.

    Parameters
    ----------
    urlpath: string or list
        Absolute or relative filepath. Prefix with a protocol like ``s3://``
        to read from alternative filesystems. Should not include glob
        character(s).
    mode: 'rb', 'wt', etc.
    compression: string or None
        If given, open file using compression codec. Can either be a compression
        name (a key in ``fsspec.compression.compr``) or "infer" to guess the
        compression from the filename suffix.
    encoding: str
        For text mode only
    errors: None or str
        Passed to TextIOWrapper in text mode
    protocol: str or None
        If given, overrides the protocol found in the URL.
    newline: bytes or None
        Used for line terminator in text mode. If None, uses system default;
        if blank, uses no translation.
    **kwargs: dict
        Extra options that make sense to a particular storage connection, e.g.
        host, port, username, password, etc.

    Examples
    --------
    >>> openfile = open('2015-01-01.csv')  # doctest: +SKIP
    >>> openfile = open(
    ...     's3://bucket/2015-01-01.csv.gz', compression='gzip'
    ... )  # doctest: +SKIP
    >>> with openfile as f:
    ...     df = pd.read_csv(f)  # doctest: +SKIP
    ...

    Returns
    -------
    ``OpenFile`` object.

    Notes
    -----
    For a full list of the available protocols and the implementations that
    they map across to see the latest online documentation:

    - For implementations built into ``fsspec`` see
      https://filesystem-spec.readthedocs.io/en/latest/api.html#built-in-implementations
    - For implementations in separate packages see
      https://filesystem-spec.readthedocs.io/en/latest/api.html#other-known-implementations
    F)rx   r   r   r   r   rk   r   rl   r   rI   )r}   FileNotFoundError)	rx   r   r   r   r   rk   r   ry   ra   s	            r"   r0   r0     sc    x  
	
 
 
 
C  )(((q6Mr$   c                 D   d|vrt          d          t          | fd|i|}t          |d         j        dd          st          d          |5 }d |D             }d	d	d	           n# 1 swxY w Y   t	          | t
                    rt          |           s|d         S |S )
aK  Open file(s) which can be resolved to local

    For files which either are local, or get downloaded upon open
    (e.g., by file caching)

    Parameters
    ----------
    url: str or list(str)
    mode: str
        Must be read mode
    storage_options:
        passed on to FS for or used by open_files (e.g., compression)
    r?   z(Can only ensure local files when readingr   r   
local_fileFzOopen_local can only be used on a filesystem which has attribute local_file=Truec                     g | ]	}|j         
S rI   )name)rT   r3   s     r"   rV   zopen_local.<locals>.<listcomp>  s    '''A'''r$   N)rW   r}   getattrr   r^   strr   )r   r   rj   ofrN   r{   s         r"   
open_localr     s    $CDDD	C	6	6d	6o	6	6B2a58\511 
-
 
 	
 
 (u'''''( ( ( ( ( ( ( ( ( ( ( ( ( ( (#s IcNN QxLs   A))A-0A-c                 l    |dk    rt          |           }||t          vrt          d| d          |S )NinferzCompression type z not supported)r   r   rW   )rx   r   s     r"   r   r     sJ    g'00;e#;#;H[HHHIIIr$   c                     t          |           } d| v r0|                     dd          \  }}t          |          dk    r||fS d| fS )zReturn protocol, path pairr   r   N)r   r   rc   )rx   rk   r   s      r"   r   r     sV    W%%G ua00$x==1T>!=r$   c                 n    t          |           \  }}t          |          }|                    |           S )zCReturn only path part of full URL, according to appropriate backend)r   r   r   )rx   rk   r   r   s       r"   strip_protocolr     s4     ))KHa
x
(
(Cw'''r$   c                 (   g }t          |           } d|v rt          d | D                       dk    rt          d          t          |t	          |                     }| D ]@}d|v r%|                    t          |||                     +|                    |           At	          |          |k    r
|d|         }nR| D ]O}t          |          r)|                    |	                    |                     :|                    |           P|S )a(  Expand paths if they have a ``*`` in them (write mode) or any of ``*?[]``
    in them (read mode).

    :param paths: list of paths
    mode: str
        Mode in which to open files.
    num: int
        If opening in writing mode, number of files we expect to create.
    fs: filesystem object
    name_function: callable
        If opening in writing mode, this callable is used to generate path
        names. Names are generated for each partition by
        ``urlpath.replace('*', name_function(partition_index))``.
    :return: list of paths
    wc                     g | ]}d |v d	S )*r   rI   )rT   r   s     r"   rV   z*expand_paths_if_needed.<locals>.<listcomp>  s    ---aC1HHHHHr$   r   z;When writing data, only one filename mask can be specified.r   N)
r   sumrW   maxrc   extend_expand_pathsr1   r   glob)r{   r   rh   r   ri   expanded_paths	curr_paths          r"   expand_paths_if_neededr     sB     NKKE
d{{--5---..22M   #s5zz"" 	1 	1Ii%%mI}c&R&RSSSS%%i0000~$$+DSD1N  	1 	1I## 1%%bggi&8&89999%%i0000r$   c                 Z   t          | t          t          t          f          r4| st	          d          t          t          |           d                   }nt          |           }pi |r|d<   t          |pi           }i }	t          t          |                    D ]K\  }
}|\  }}}|
t          |          dz
  k    rt          di ||	}	0t          di ||	|	d<   ||	d<   ||	d<   L|d         \  }}}t          |fi |	t          | t          t          t          f          rHfd| D             }t          d	 |D                       dk    rt	          d
|           d |D             }n                    |          }t          |t          t          t          f          rt          ||||          }nfd|v r|rt          |||          }nNd|v r|rt          |||          }n6d|v r/fdt                              |                    D             }n|g}j        |fS )a  Filesystem, deterministic token, and paths from a urlpath and options.

    Parameters
    ----------
    urlpath: string or iterable
        Absolute or relative filepath, URL (may include protocols like
        ``s3://``), or globstring pointing to data.
    mode: str, optional
        Mode in which to open files.
    num: int, optional
        If opening in writing mode, number of files we expect to create.
    name_function: callable, optional
        If opening in writing mode, this callable is used to generate path
        names. Names are generated for each partition by
        ``urlpath.replace('*', name_function(partition_index))``.
    storage_options: dict, optional
        Additional keywords to pass to the filesystem class.
    protocol: str or None
        To override the protocol specifier in the URL
    expand: bool
        Expand string paths for writing, assuming the path is a directory
    zempty urlpath sequencer   rk   r   r   r   r   c                 Z    g | ]'}t          t          |          pi           d          (S r   )r   r   )rT   urj   s     r"   rV   z&get_fs_token_paths.<locals>.<listcomp>p  sB     
 
 
GHInQ'')>B??B
 
 
r$   c                     h | ]
}|d          S )r   rI   rT   pcs     r"   rp   z%get_fs_token_paths.<locals>.<setcomp>s  s    ((("1(((r$   z$Protocol mismatch getting fs from %sc                     g | ]
}|d          S r   rI   r   s     r"   rV   z&get_fs_token_paths.<locals>.<listcomp>u  s    )))2A)))r$   r   r   r   c                 >    g | ]}                     |          |S rI   )isdir)rT   r3   r   s     r"   rV   z&get_fs_token_paths.<locals>.<listcomp>  s(    JJJ1bhhqkkJQJJJr$   rI   )r^   r   tuplesetrW   r   r   r   r@   rc   r   r   r   r   r   sortedr   	_fs_token)rx   r   rh   ri   rj   rk   rl   urlpath0r   r   r   r   r   nested_protocolr   r{   r   pchainsr   s       `             @r"   rv   rv   :  s   > 'D%-.. + 	75666!$w--"233!'**%+O /&.
#h 5266EH8E??++  2$&!orE

Q--b-H--H%)%;%;B%;(%;%;!"&5"#qE8Q	H	)	)	)	)B'D%-.. *
 
 
 
LS
 
 
 ((((())A--CWMMM)))))""5))%$s+,, 
&udC]KK$;;6;!%<<EED[[V[!%<<EEE\\JJJJrwwu~~ 6 6JJJEEGEr|U""r$   c                 (    t           t                    r                     d          dk    rt          d          d vr t          j                             d           t          |dz
             fdt          |          D             }|t          |          k    rt                              d           nPt           t          t          f          r%t                     |k    sJ t                     }nt          d          |S )Nr   r   z.Output path spec must contain exactly one '*'.z*.partc                 N    g | ]!}                     d  |                    "S )r   )r/   )rT   r   ri   r   s     r"   rV   z!_expand_paths.<locals>.<listcomp>  s1    IIIc==#3#344IIIr$   zqIn order to preserve order between partitions paths created with ``name_function`` should sort to partition orderzPath should be either
1. A list of paths: ['foo.json', 'bar.json', ...]
2. A directory: 'foo/
3. A path with a '*' in it: 'foo.*.json')r^   r   countrW   osr   joinr   ranger   loggerwarningr   r   rc   )r   ri   rh   r{   s   ``  r"   r   r     s   $ 
::c??QMNNN__7<<h//D /a88MIIIIIeCjjIIIF5MM!!NN*  
 
D5$-	(	( 	
4yyCT

7
 
 	
 Lr$   c                   4     e Zd ZdZ	 	 	 	 	 d fd	Zd Z xZS )r2   zTextIOWrapper cannot be pickled. This solves it.

    Requires that ``buffer`` be pickleable, which all instances of
    AbstractBufferedFile are.
    NFc                 X    ||||||f| _          t                      j        | j           d S r   )r8   rO   r#   )r!   bufferr   r   r   line_bufferingwrite_throughrP   s          r"   r#   z PickleableTextIOWrapper.__init__  s5     Hfg~}T	$)$$$$r$   c                     t           | j        fS r   )r2   r8   r&   s    r"   r'   z"PickleableTextIOWrapper.__reduce__  s    &	11r$   )NNNFF)rD   rE   rF   rG   r#   r'   rd   re   s   @r"   r2   r2     sg          
% 
% 
% 
% 
% 
%2 2 2 2 2 2 2r$   r2   )
r   Nrf   NNr   NNTT)r   Nrf   NNN)r   )r   r   NNNT))iologgingr   r   r   r   cachingr   r   r   r   r	   r
   r   r   registryr   r   utilsr   r   r   r   	getLoggerr   r   r   rK   r}   r   r   r0   r   r   r   r   r   rv   r   TextIOWrapperr2   rI   r$   r"   <module>r      s   				  				 				                            6 6 6 6 6 6 6 6            
	8	$	$p p p p p p p pf7; 7; 7; 7; 7; 7; 7; 7;x 
	l l l l^  >. . .f 
I I I IX   :    ( ( (, , ,b 
	J# J# J# J#Z  <2 2 2 2 2b. 2 2 2 2 2r$   