o
    h]                     @  s  d dl mZ 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	 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 d dlmZmZ d d	lmZmZmZmZ ed
ZG dd dZG dd de Z!										d5ddZ"dd Z#dd Z$e%ddZ&							d6ddZ'	d7d8d%d&Z(d'd( Z)d)d* Z*d+d, Z+d-d. Z,						d9d/d0Z-d1d2 Z.G d3d4 d4ej/Z0dS ):    )annotationsN)	has_magic)Path)	BaseCache
BlockCache
BytesCache	MMapCacheReadAheadCachecaches)compr)conf)
filesystemget_filesystem_class)_unstrip_protocolbuild_name_functioninfer_compressionstringify_pathfsspecc                   @  s`   e Zd ZdZ					dddZdd Zdd	 Zd
d Zdd Ze	dd Z
dd Zd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                 C  s:   || _ || _|| _t||| _|| _|| _|| _g | _d S N)	fspathmodeget_compressioncompressionencodingerrorsnewlinefobjects)selfr   r   r   r   r   r   r    r!   _/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/fsspec/core.py__init__A   s   

zOpenFile.__init__c                 C  s$   t | j| j| j| j| j| j| jffS r   )r   r   r   r   r   r   r   r   r    r!   r!   r"   
__reduce__T   s   zOpenFile.__reduce__c                 C  s   d| j  dS )Nz<OpenFile 'z'>)r   r$   r!   r!   r"   __repr__b   s   zOpenFile.__repr__c              
   C  s   | j ddddd }z| jj| j|d}W n ty1 } zt| jr,td| j| d }~ww |g| _| jd urNt	| j }|||d d}| j
| d| j vrdt|| j| j| jd}| j
| | jd S )	Nt b)r   a   %s not found. The URL contains glob characters: you maybe needed
to pass expand=True in fsspec.open() or the storage_options of 
your library. You can also set the config value 'open_expand'
before import, or fsspec.core.DEFAULT_EXPAND at runtime, to True.r   )r   r   r   )r   replacer   openr   FileNotFoundErrorr   r   r   r   appendPickleableTextIOWrapperr   r   r   )r    r   fecompressr!   r!   r"   	__enter__e   s2   




zOpenFile.__enter__c                 G  s   |    d S r   )close)r    argsr!   r!   r"   __exit__   s   zOpenFile.__exit__c                 C  s   t | j| jS r   )r   r   r   r$   r!   r!   r"   	full_name   s   zOpenFile.full_namec                 C  s   |   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.
        r3   r$   r!   r!   r"   r,      s   zOpenFile.openc                 C  s>   t | jD ]}d| jvr|js|  |  q| j  dS )z#Close all encapsulated file objectsrN)reversedr   r   closedflushr4   clear)r    r0   r!   r!   r"   r4      s
   
zOpenFile.close)r   NNNN)__name__
__module____qualname____doc__r#   r%   r&   r3   r6   propertyr7   r,   r4   r!   r!   r!   r"   r       s     $
 
	r   c                      sL   e Zd ZdZddd fdd
Zdd Zd	d
 Z fddZdd Z  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                  s"   || _ || _g | _t j|  d S r   )r   r   filessuperr#   )r    r   r   r5   	__class__r!   r"   r#      s   zOpenFiles.__init__c                 C  sd   | j d u r	td| j }	 t|dr|| | _| jS t|dr)|j d ur)|j }nnqdd | D S )NzContext has already been usedT	open_manyr   c                 S  s   g | ]}|  qS r!   r8   .0sr!   r!   r"   
<listcomp>       z'OpenFiles.__enter__.<locals>.<listcomp>)r   
ValueErrorhasattrrI   rE   )r    r   r!   r!   r"   r3      s   

	zOpenFiles.__enter__c                   sd   | j } fdd| D  d| jvr0	 t|dr|| j d S t|dr-|j d ur-|j }nd S qd S )Nc                   s   g | ]}|j   qS r!   )r6   rJ   r5   r!   r"   rM          z&OpenFiles.__exit__.<locals>.<listcomp>r9   TrI   r   )r   r   rP   commit_manyrE   )r    r5   r   r!   rQ   r"   r6      s   

zOpenFiles.__exit__c                   s,   t  |}t|trt|| j| jdS |S )NrD   )rF   __getitem__
isinstanceslicerC   r   r   )r    itemoutrG   r!   r"   rT      s   
zOpenFiles.__getitem__c                 C  s   dt |  dS )Nz	<List of z OpenFile instances>)lenr$   r!   r!   r"   r&      s   zOpenFiles.__repr__)
r>   r?   r@   rA   r#   r3   r6   rT   r&   __classcell__r!   r!   rG   r"   rC      s    rC   r   utf8   Tc              	     s   t | |||||
d\}}jdkr|	_n&dvr=|	r=fdd|D }|D ]}z	j|dd W q( ty<   Y q(w t fdd	|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filer9   c                   s   h | ]}  |qS r!   )_parentrK   r   r   r!   r"   	<setcomp>3  rR   zopen_files.<locals>.<setcomp>T)exist_okc                   s"   g | ]}t | d qS ))r   r   r   r   r   )r   rd   r   r   r   r   r   r   r!   r"   rM   :  s    
zopen_files.<locals>.<listcomp>rD   )get_fs_token_pathsr`   
auto_mkdirmakedirsPermissionErrorrC   )urlpathr   r   r   r   r^   r]   r`   r   rj   ra   kwargsfs_tokenpathsparentsparentr!   rh   r"   
open_files   s4   O
	
rs   c                   s8  ddl m} d| v r0td}g }| dD ]}d|v s!||r'|| q||d  qn| g}g }d }| }t|D ]V}|	dd pNt
|d pNd}	t|	}
|
|}|	|	i  ||d u ri | tdi  fdd	| D  }|
|}d
|vrt|
|r|}|||	|f |}q?|  |S )Nr   )CachingFileSystemz::z.*[^a-z]+.*://r`   rb   c                   s*   i | ]\}}| vs| | kr||qS r!   r!   rK   kvkwsr!   r"   
<dictcomp>e  s   * z_un_chain.<locals>.<dictcomp>target_protocolr!   )fsspec.implementations.cachedrt   recompilesplitmatchr.   copyr:   popsplit_protocolr   _get_kwargs_from_urlsupdatedictitems_strip_protocol
issubclassreverse)r   rn   rt   xbitsprX   previous_bitbitr`   clsextra_kwargskwr!   ry   r"   	_un_chainK  s@   



r   c                   s   t | } h d  fdd| D }t| |}i }tt|D ].\}}|\}}}|t|d kr;td
i ||}q td
i |||d< ||d< ||d< q |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``.
    >   r]   r   r   ra   r   r   r   r^   c                   s   i | ]\}}| vr||qS r!   r!   rv   known_kwargsr!   r"   r{     s    zurl_to_fs.<locals>.<dictcomp>r\   target_optionsr|   for   Nr!   )r   r   r   	enumerater:   rY   r   r   )urlrn   chaininkwargsichurlsr`   r   rm   _r   r!   r   r"   	url_to_fsq  s    



r   open_expandFc           
      K  sF   |du rt n|}td| g|||||||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.
    expand: bool or None
        Whether to regard file paths containing special glob characters as needing
        expansion (finding the first match) or absolute. Setting False allows using
        paths which do embed such characters. If None (default), this argument
        takes its value from the DEFAULT_EXPAND module variable, which takes
        its initial value from the "open_expand" config value at startup, which will
        be False if not set.
    **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
    N)rm   r   r   r   r   r`   r   ra   r   r!   )DEFAULT_EXPANDrs   r-   )
rm   r   r   r   r   r`   r   ra   rn   rX   r!   r!   r"   r,     s    D	r,   r   #str | list[str] | Path | list[Path]r   strr_   r   returnstr | list[str]c                 K  s   d|vrt dt| fd|i|}t|d jddst d|}dd	 |D }W d
   n1 s3w   Y  t| trAt| rFt| trJ|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)
    r9   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                 S  s   g | ]}|j qS r!   )namerK   r0   r!   r!   r"   rM     s    zopen_local.<locals>.<listcomp>N)rO   rs   getattrr   rU   r   r   r   )r   r   r_   ofrE   rp   r!   r!   r"   
open_local  s   r   c                 C  s4   |dkrt | }|d ur|tvrtd| d|S )NinferzCompression type z not supported)r   r   rO   )rm   r   r!   r!   r"   r     s
   r   c                 C  sR   t | } d| v r| dd\}}t|dkr||fS | dr%| ddS d| fS )zReturn protocol, path pairru   r\   zdata::N)r   r   rY   
startswith)rm   r`   r   r!   r!   r"   r   $  s   
r   c                 C  s   t | \}}t|}|| S )zCReturn only path part of full URL, according to appropriate backend)r   r   r   )rm   r`   r   r   r!   r!   r"   strip_protocol1  s   
r   c                 C  s   g }t | } d|v rFtdd | D dkrtdt|t| }| D ]}d|v r2|t||| q"|| q"t||krD|d| }|S | D ]}t|rW||	| qH|| qH|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                 s  s    | ]	}d |v rdV  qdS )*r\   Nr!   )rK   r   r!   r!   r"   	<genexpr>L  s    z)expand_paths_if_needed.<locals>.<genexpr>r\   z;When writing data, only one filename mask can be specified.r   N)
listsumrO   maxrY   extend_expand_pathsr.   r   glob)rp   r   r]   r   r^   expanded_paths	curr_pathr!   r!   r"   expand_paths_if_needed8  s(   
r   c                   s  t | tttfr| stdttt| }nt| }pi |r%|d< t|p*i }i }	t	t
|D ].\}
}|\}}}|
t|d krOtdi ||	}	q4tdi ||	|	d< ||	d< ||	d< q4|d \}}}t|fi |	 t | tttfrfdd	| D }td
d |D dkrtd| dd	 |D }n |}t |tttfr|rt||| |}n1t |tst|}n'd|v sd|v r|rt|||}nd|v r fd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`   r\   r   r|   r   r   c                   s"   g | ]}t t| pi d  qS r   )r   r   )rK   u)r_   r!   r"   rM     s    z&get_fs_token_paths.<locals>.<listcomp>c                 S  s   h | ]}|d  qS )r\   r!   rK   pcr!   r!   r"   rf     rN   z%get_fs_token_paths.<locals>.<setcomp>z$Protocol mismatch getting fs from %sc                 S  s   g | ]}|d  qS r   r!   r   r!   r!   r"   rM     rN   r   r   r   c                   s   g | ]	}  |s|qS r!   )isdirr   re   r!   r"   rM     s    Nr!   )rU   r   tuplesetrO   r   nextiterr   r   r:   rY   r   r   r   r   r   sortedr   	_fs_token)rm   r   r]   r^   r_   r`   ra   urlpath0r   r   r   r   r   nested_protocolr   rp   r   pchainsr!   )r   r_   r"   ri   g  sP   





ri   c                   s   t tr>ddkrtddvrtjd d u r%t|d   fddt|D }|t	|kr<t
d |S t ttfrSt|ksMJ t}|S td)	Nr   r\   z.Output path spec must contain exactly one '*'.z*.partc                   s   g | ]
} d  |qS )r   )r+   )rK   r   r^   r   r!   r"   rM     s    z!_expand_paths.<locals>.<listcomp>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')rU   r   countrO   osr   joinr   ranger   loggerwarningr   r   rY   )r   r^   r]   rp   r!   r   r"   r     s(   
r   c                      s4   e Zd ZdZ					d fdd	Zdd Z  ZS )	r/   zTextIOWrapper cannot be pickled. This solves it.

    Requires that ``buffer`` be pickleable, which all instances of
    AbstractBufferedFile are.
    NFc                   s$   ||||||f| _ t j| j   d S r   )r5   rF   r#   )r    bufferr   r   r   line_bufferingwrite_throughrG   r!   r"   r#     s   	z PickleableTextIOWrapper.__init__c                 C  s
   t | jfS r   )r/   r5   r$   r!   r!   r"   r%     s   
z"PickleableTextIOWrapper.__reduce__)NNNFF)r>   r?   r@   rA   r#   r%   rZ   r!   r!   rG   r"   r/     s    	r/   )
r   Nr[   NNr\   NNTT)r   Nr[   NNNN)r   )r   r   r   r   r_   r   r   r   )r   r\   NNNT)1
__future__r   iologgingr   r~   r   r   pathlibr   fsspec.cachingr   r   r   r   r	   r
   fsspec.compressionr   fsspec.configr   fsspec.registryr   r   fsspec.utilsr   r   r   r   	getLoggerr   r   r   rC   rs   r   r   getr   r,   r   r   r   r   r   ri   r   TextIOWrapperr/   r!   r!   r!   r"   <module>   sh     
~<
s&2
W!1
N