o
    NiU                     @   s   d dl mZmZ d dlmZ ddlmZmZ ddlm	Z	 ddl
mZmZ er,d dlmZ 								dd
e	dededee	 dedededed defddZde	dededefddZdS )    )TYPE_CHECKINGOptional)urlparse   )cached_pathget_from_cache)	PathOrStr)get_scheme_clientget_supported_schemes)ProgressNFurl_or_filenameindexlength	cache_dirextract_archiveforce_extractquietprogressr   returnc                 C   s0  t | ts	t| } | d}|rP|dkrP| d| }	| |d d }
t|	|d|||d}| s7t|  d||
 }| sJtd|
 d	|	 dt|||S t	| j
t v rt| }zt| |||d|d
\}}t|||W S  tyx   Y nw z|||W S  ty   Y nw t| |||d}t|||S )a  
    Get a range of up to ``length`` bytes starting at ``index``.

    In some cases the entire file may need to be downloaded, such as when the server does not support
    a range download or when you're trying to get a bytes range from a file within an archive.

    .. caution::
        You may get less than ``length`` bytes sometimes, such as when fetching a range from an HTTP
        resource starting at 0 since headers will be omitted in the bytes returned.

    Parameters
    ----------

    url_or_filename :
        A URL or path to parse and possibly download.

    index :
        The index of the byte to start at.

    length :
        The number of bytes to read.

    cache_dir :
        The directory to cache downloads. If not specified, the global default cache directory
        will be used (``~/.cache/cached_path``). This can be set to something else with
        :func:`set_cache_dir()`.

        This is only relevant when the bytes range cannot be obtained directly from the resource.

    extract_archive :
        Set this to ``True`` when you want to get a bytes range from a file within an archive.
        In this case the ``url_or_filename`` must contain an "!" followed by the relative path of the file
        within the archive, e.g. "s3://my-archive.tar.gz!my-file.txt".

        Note that the entire archive has to be downloaded in this case.

    force_extract :
        If ``True`` and the resource is a file within an archive (when the path contains an "!" and
        ``extract_archive=True``), it will be extracted regardless of whether or not the extracted
        directory already exists.

        .. caution::
            Use this flag with caution! This can lead to race conditions if used
            from multiple processes on the same file.

    quiet :
        If ``True``, progress displays won't be printed.

        This is only relevant when the bytes range cannot be obtained directly from the resource.

    progress :
        A custom progress display to use. If not set and ``quiet=False``, a default display
        from :func:`~cached_path.get_download_progress()` will be used.

        This is only relevant when the bytes range cannot be obtained directly from the resource.
    !r   Nr   T)r   r   r   r   r   z9 uses the ! syntax, but does not specify an archive file.'z' not found within ')r   r   r   no_downloads_client)r   r   r   )
isinstancestrfindr   is_dir
ValueErrorexistsFileNotFoundError_bytes_range_from_filer   schemer
   r	   r   get_bytes_rangeNotImplementedError)r   r   r   r   r   r   r   r   exclamation_indexarchive_path	file_namecached_archive_path	file_pathclient
cache_path_ r,   K/home/ubuntu/.local/lib/python3.10/site-packages/cached_path/bytes_range.pyr"      sX   
B

r"   pathc                 C   sB   t | d}|| ||W  d    S 1 sw   Y  d S )Nrb)openseekread)r.   r   r   fr,   r,   r-   r       s   
$r    )NFFFN)typingr   r   urllib.parser   _cached_pathr   r   commonr   schemesr	   r
   rich.progressr   intboolbytesr"   r    r,   r,   r,   r-   <module>   s@    	
}