
    wgr                        d dl 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Zd dl	Z	d dl
mZmZmZmZ d dlmZ d dlmZ d dlmZ d dlmZmZmZmZmZ d	d
lmZ  ej:                  d      Z ej:                  d      Z ej@                  d      Z!d Z" G d de      Z# G d de      Z$ ej:                  d      Z%d Z& G d de      Z' G d de      Z(ddZ)ddZ*ddZ+ ee+      Z,y)    N)copy)urlparse)AbstractAsyncStreamedFileAsyncFileSystemsyncsync_wrapper)DEFAULT_CALLBACK)FSTimeoutError)AbstractBufferedFile)DEFAULT_BLOCK_SIZEglob_translate
isfilelikenullcontexttokenize   )AllBytesz6<(a|A)\s+(?:[^>]*?\s+)?(href|HREF)=["'](?P<url>[^"']+)z-(?P<url>http[s]?://[-a-zA-Z0-9@:%_+.~#?&/=]+)zfsspec.httpc                  4   K   t        j                  di | S w)N )aiohttpClientSession)kwargss    `/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/fsspec/implementations/http.py
get_clientr       s       *6**s   c                       e Zd ZdZdZdddddddddedf fd	Zed        Zd	 Z	e
d
        Zd Zed        Ze fd       ZddZddZ ee      Zd Zd dZdefdZdeddfdZd Zd Z	 	 	 	 	 	 d!dZd"dZd Zd Zd#dZd Z  xZ!S )$HTTPFileSystema2  
    Simple File-System for fetching data via HTTP(S)

    ``ls()`` is implemented by loading the parent page and doing a regex
    match on the result. If simple_link=True, anything of the form
    "http(s)://server.com/stuff?thing=other"; otherwise only links within
    HTML href tags will be used.
    /TNbytesFc                    t        |   | f||d| ||nt        | _        || _        || _        || _        || _        |	xs i | _        |
| _	        || _
        || _        d| _        t        |      }|j                  dd      | _        |j                  dd       |j                  dd       |j                  dd       || _        y)a  
        NB: if this is called async, you must await set_client

        Parameters
        ----------
        block_size: int
            Blocks to read bytes; if 0, will default to raw requests file-like
            objects instead of HTTPFile instances
        simple_links: bool
            If True, will consider both HTML <a> tags and anything that looks
            like a URL; if False, will consider only the former.
        same_scheme: True
            When doing ls/glob, if this is True, only consider paths that have
            http/https matching the input URLs.
        size_policy: this argument is deprecated
        client_kwargs: dict
            Passed to aiohttp.ClientSession, see
            https://docs.aiohttp.org/en/stable/client_reference.html
            For example, ``{'auth': aiohttp.BasicAuth('user', 'pass')}``
        get_client: Callable[..., aiohttp.ClientSession]
            A callable which takes keyword arguments and constructs
            an aiohttp.ClientSession. It's state will be managed by
            the HTTPFileSystem class.
        storage_options: key-value
            Any other parameters passed on to requests
        cache_type, cache_options: defaults used in open
        )asynchronousloopNuse_listings_cacheFlistings_expiry_time	max_pathsskip_instance_cache)super__init__r   
block_sizesimple_linkssame_schema
cache_typecache_optionsclient_kwargsr   encodedr   _sessionr   popr!   )selfr(   r'   same_schemesize_policyr*   r+   r   r    r,   r   r-   storage_optionsrequest_options	__class__s                 r   r&   zHTTPFileSystem.__init__0   s    T 	WLtWW(2(>*DV(&$**0b$% /"1"5"56JE"R2D9K.148%    c                      y)Nhttpr   r0   s    r   fsidzHTTPFileSystem.fsidq   s    r6   c                 D    t        j                  || j                        S )N)r-   )yarlURLr-   r0   urls     r   
encode_urlzHTTPFileSystem.encode_urlu   s    xxT\\22r6   c                     | *| j                         r	 t        | |j                  d       y t        |dd       }||j                          y y # t        t        t
        f$ r Y 7w xY w)Ng?)timeout
_connector)
is_runningr   closeTimeoutErrorr
   NotImplementedErrorgetattr_close)r    session	connectors      r   close_sessionzHTTPFileSystem.close_sessionx   sl     1T7==#6 G\48	  ! !.2EF s   A A%$A%c                 ,  K   | j                   w | j                  dd| j                  i| j                   d {   | _         | j                  s6t        j                  | | j                  | j                  | j                          | j                   S 7 Ww)Nr    r   )r.   r   r    r,   r   weakreffinalizerL   r9   s    r   set_sessionzHTTPFileSystem.set_session   sr     == "1$//"Wtyy"WDDVDV"WWDM$$  t'9'9499dmmT}} Xs   8BBABc                     |S )z-For HTTP, we always want to keep the full URLr   )clspaths     r   _strip_protocolzHTTPFileSystem._strip_protocol   s	     r6   c                 D    t         |   |      }t        |      dkD  r|S y)N    )r%   _parentlen)rR   rS   parr5   s      r   rX   zHTTPFileSystem._parent   s&     god#s8a<Jr6   c           	        K   | j                   j                         }|j                  |       t        j	                  |       | j                          d {   } |j                  | j                  |      fi | j                   4 d {   }| j                  ||       	 |j                          d {   }| j                  r=t        j                  |      t        j                  |      D cg c]  }|d   	 c}z   }	n%t        j                  |      D cg c]  }|d   	 }	}d d d       d {    t               }
t!        |      }	D ]B  }t#        |t$              r|d   }|j'                  d      r+t)        |      dkD  r|j*                   d|j,                   | }|j'                  d      r| j.                  r5|j'                  |j1                  d      dz         r|
j3                  |       |j5                  dd      j'                  |j5                  dd      j1                  d      dz         s|
j3                  |       |dvs|
j3                  dj7                  |j1                  d      |j9                  d      g             E |
s;|j;                  d      r*| j=                  |j1                  d      d	       d {   }
|r'|
D cg c]  }|d |j;                  d      rd
ndd c}S t?        |
      S 7 7 7 Wc c}w c c}w # t        $ r g }	Y w xY w7 # 1 d {  7  sw Y   	xY w7 vc c}w w)Nr      r   z://r8   https)z..z../F)detail	directoryfilenamesizetype) r   r   updateloggerdebugrP   getr@   _raise_not_found_for_statustextr(   ex2findallexUnicodeDecodeErrorsetr   
isinstancetuple
startswithrY   schemenetlocr)   rstripaddreplacejoinlstripendswith_ls_realsorted)r0   r?   r^   r   kwrJ   rrj   ulinksoutpartsls                r   r{   zHTTPFileSystem._ls_real   s    [[
		&S((**7;;ts3Ct{{C 		 		q,,Q4VVX~$$KK-rzz$?O0P!10PPE+-::d+;<aQqT<E<		 		 e 	HA!U#aD||C SVaZ||nC~aS9||F###SZZ_s5J(KGGAJYYw/::KK077<sB GGAJM)GGCHHcjjoqxx}%EFG#	H$ s||C(cjjoeDDC  	  +,::c?K  #;W +		 &0P<% 		 		 		 		> Es   AML4ML
ML/#L6L7;L2L>LL'L)M4L,5DM;MA;MM
M M9M
ML
LL)%L/(L))L/,M/M5L86M=	MMc                    K   | j                   r|| j                  v r| j                  |   }|S  | j                  |fd|i| d {   }|| j                  |<   |S 7 w)Nr^   )r!   dircacher{   )r0   r?   r^   r   r   s        r   _lszHTTPFileSystem._ls   sg     ""sdmm';--$C 
 &cC&CFCCC!$DMM#
 Ds   AAAAc                 X    |j                   dk(  rt        |      |j                          y)zU
        Raises FileNotFoundError for 404s, otherwise uses raise_for_status.
        i  N)statusFileNotFoundErrorraise_for_status)r0   responser?   s      r   ri   z*HTTPFileSystem._raise_not_found_for_status   s'     ??c!#C((!!#r6   c                 b  K   | j                   j                         }|j                  |       t        j	                  |       ||I||k(  ry|j                  di       j                         }| j                  |||       d {   |d<   ||d<   | j                          d {   } |j                  | j                  |      fi |4 d {   }|j                          d {   }	| j                  ||       d d d       d {    |	S 7 7 m7 D7 .7 # 1 d {  7  sw Y   	S xY ww)Nr6   headersRange)r   r   re   rf   rg   r/   _process_limitsrP   rh   r@   readri   )
r0   r?   startendr   r}   r   rJ   r~   r   s
             r   	_cat_filezHTTPFileSystem._cat_file   s    [[
		&S|ffY+002G%)%9%9#uc%JJGG#ByM((**7;;ts3:r: 	5 	5a.C,,Q4	5 	5 
  K*	5 	5 	5 	5 	5 
s~   BD/DD/#D$*D/DD/D&D'D=D/D	D/D/D/DD/D,D" D,'D/i  P c                 n  K   | j                   j                         }|j                  |       t        j	                  |       | j                          d {   } |j                  | j                  |      fi |4 d {   }	 t        |j                  d         }	|j                  |	       | j                  ||       t        |      r|}
nt        |d      }
	 d}|rQ|j                   j#                  |       d {   }|
j%                  |       |j'                  t)        |             |rQt        |      s|
j+                          	 d d d       d {    y 7 	7 # t        t        f$ r d }	Y w xY w7 {# t        |      s|
j+                          w w xY w7 D# 1 d {  7  sw Y   y xY ww)Nzcontent-lengthwbT)r   r   re   rf   rg   rP   rh   r@   intr   
ValueErrorKeyErrorset_sizeri   r   opencontentr   writerelative_updaterY   rE   )r0   rpathlpath
chunk_sizecallbackr   r}   rJ   r~   rc   outfilechunks               r   	_get_filezHTTPFileSystem._get_file   s~     [[
		&U((**7;;tu5<< 	$ 	$199%567 d#,,Q6% ud+$"#)).."<<EMM%(,,SZ8 
 "%(MMO+	$ 	$ 	$ +	$ )  = "%(MMO ))	$ 	$ 	$ 	$s   AF5E*F5E"F5F E$=F "E=?E; 1E=2F F5FF5"F5$E85F 7E88F ;E==FF F5 F2&F)'F2.F5post	overwritec                   K   |dk7  rt        d      fd}| j                  j                         }	|	j                  |       | j	                          d {   }
|j                         }|dvrt        d|      t        |
|      } || j                  |      fd |       i|	4 d {   }| j                  ||       d d d       d {    y 7 }7 )7 	# 1 d {  7  sw Y   y xY ww)Nr   zExclusive writec                   K   t        t        j                        rt              } d}nt	        d      } d}| 5 }|r3j                  |j                  dd             |j                  d       nj                  t        |dd              |j                        }|r3| j                  t        |             |j                        }|r3d d d        y # 1 sw Y   y xY ww)NFrbTr   r   rc   )rp   ioIOBaser   r   r   seekrH   r   r   rY   )contextuse_seekfr   r   r   r   s       r   
gen_chunksz,HTTPFileSystem._put_file.<locals>.gen_chunks  s      %+%e, ud+ /A%%affQl3FF1I%%ga&>?z*K,,SZ8FF:.E / / /s   9C)BC	C)C&"C))r   putz.method has to be either 'post' or 'put', not: data)
rG   r   r   re   rP   lowerr   rH   r@   ri   )r0   r   r   r   r   methodmoder   r   r}   rJ   methresps    ` ``        r   	_put_filezHTTPFileSystem._put_file  s      ;%&788	/. [[
		&((**(@
K  w'.HZ\HRH 	: 	:D,,T59	: 	: 	: +	: 	: 	: 	: 	:s[   AC4CAC41C2C45CC4CC4C4C4C1%C(&C1-C4c                   K   | j                   j                         }|j                  |       	 t        j	                  |       | j                          d {   } |j                  | j                  |      fi | d {   }|4 d {    |j                  dk  cd d d       d {    S 7 Y7 17 (7 # 1 d {  7  sw Y   y xY w# t        j                  $ r Y yw xY ww)Ni  F)r   r   re   rf   rg   rP   rh   r@   r   r   ClientError)r0   rS   r   r}   rJ   r~   s         r   _existszHTTPFileSystem._exists?  s     [[
		&	LL ,,..G!gkk$//$"7>2>>A & &xx#~& & & />& & & & &"" 		s   ,C'(C B1)C B3
C B5C B9C +B7,C 0C'1C 3C 5C 7C 9C?C CC 
C'C C$!C'#C$$C'c                 D   K    | j                   |fi | d {   S 7 wN)r   )r0   rS   r   s      r   _isfilezHTTPFileSystem._isfileK  s"     !T\\$1&1111s     c                 "   |dk7  rt         ||n| j                  }| j                  j                         }	| j                  |	d<   |	j                  |       i }
|xs) |
j                   | j                  |fi |      xs |
d   }t        | j                  | j                        }|rO|rM|
j                  dd      r;t        | |f|||||xs | j                  |xs | j                  | j                  d|	S t        | |f|| j                  |d|	S )a  Make a file-like object

        Parameters
        ----------
        path: str
            Full URL with protocol
        mode: string
            must be "rb"
        block_size: int or None
            Bytes to download in one request; use instance value if None. If
            zero, will return a streaming Requests file-like instance.
        kwargs: key-value
            Any other parameters, passed to requests calls
        r   r   rc   partialT)rJ   r'   r   rc   r*   r+   r    )r   r    rJ   )rG   r'   r   r   r   re   infor   r    rP   rh   HTTPFiler*   r+   HTTPStreamFile)r0   rS   r   r'   
autocommitr*   r+   rc   r   r}   r   rJ   s               r   _openzHTTPFileSystem._openN  s,   2 4<%%#-#9Zt
[[!..>
		&Mt{{9499T#<V#<=Mftyy$"2"23$488It#<  %%8+At/A/AYY   " YY  r6   c                    K   | j                          d {   }|	  | j                  |fi | d {   d   }t        | |f| j                  ||d|S 7 @7 $# t        $ r Y *w xY ww)Nrc   )r    rJ   rc   )rP   _infor   AsyncStreamFiler    )r0   rS   r   rc   r   rJ   s         r   
open_asynczHTTPFileSystem.open_async  s     ((**<(djj888&A 
 
 
 	
 + 9$ sA   A*AA*A AA A*A 	A'$A*&A''A*c                 D    t        || j                  | j                        S )z;Unique identifier; assume HTTP files are static, unchanging)r   r   protocolr>   s     r   ukeyzHTTPFileSystem.ukey  s    T[[$--88r6   c           	        K   i }| j                          d{   }dD ]W  }	 |j                  t        | j                  |      f||d| j                  | d{          |j                  d       nY |dd|d	d
iS 7 k7 *# t        $ r2}|dk(  rt        |      |t        j                  d|       Y d}~d}~ww xY ww)aH  Get info of URL

        Tries to access location via HEAD, and then GET methods, but does
        not fetch the data.

        It is possible that the server does not supply any size information, in
        which case size will be given as None (and certain operations on the
        corresponding file will not work).
        N)headrh   )r2   rJ   rc   rh   rW   )exc_inforb   rc   rd   r`   )
rP   re   
_file_infor@   r   rh   	Exceptionr   rf   rg   )r0   r?   r   r   rJ   policyexcs          r   r   zHTTPFileSystem._info  s      ((**% 	/F/$,$* ' ++	
 !  88F#/ 0	/& TBTB66BB+ +
  /U?+C0c9R#..	/sJ   CB	C8BB
B5CB	C(B>9C>CCc           	        K   ||dk  rt        d      ddl}|j                  d      }| j                  |      }|xs |j                  d      }|j	                  d      dk\  r|j	                  d      n
t        |      }|j	                  d      dk\  r|j	                  d      n
t        |      }t        ||      }	|j                  d	d
      }
t        |      sC | j                  |fi | d{   r"|
s|gS | | j                  |fi | d{   iS |
sg S i S d|d|	 v r7|d|	 j                  d      }	|d|	dz    }||	dz   d j                  d      dz   }nd}||	dz   d j                  d      dz   }d|v r5|1|j	                  d      }||d j                  d      dz   }||z
  |z   }nd} | j                  |f|ddd| d{   }t        ||rdndz         } |j                  |      }t!        |j#                               D ci c]T  \  }}|j%                  |j'                  d            r/|s*|d   dk(  r"|j                  d      r|j'                  d      n||V }}}|
r|S t)        |      S 7 7 l7 c c}}w w)a  
        Find files by glob-matching.

        This implementation is idntical to the one in AbstractFileSystem,
        but "?" is not considered as a character for globbing, because it is
        so common in URLs, often identifying the "query" part.
        Nr\   zmaxdepth must be at least 1r   r   )z/**z/**[r^   FrW   z**T)maxdepthwithdirsr^   rd   r_   )r   rerz   rT   findrY   minr/   	has_magicr   r   rindexcount_findr   compiler|   itemsmatchru   list)r0   rS   r   r   r   ends_with_slashappend_slash_to_dirnameidx_star	idx_bracemin_idxr^   rootdepthidx_double_starsdepth_double_starsallpathspatternpr   r   s                       r   _globzHTTPFileSystem._glob  s     HqL:;;--,##D)"1"QT]]=5Q%)YYs^q%8499S>c$i&*iin&9DIIcNs4y	h	*He,!T\\$1&111 6M 


4(B6(B"BCCIID'N"8Gn++C0G'A+&D1'--c2Q6ED1'--c2Q6E4<##'99T? %)*:*;%<%B%B3%G!%K" 22X=#
 4
@F
 
 !R!HI"**W% "(.."23

 4}}QXXc]+ /LK/JJsO  

 

 J9a 2 #C,


sK   C-J/I90 JI<B8J	I?
AJAJ'J<J?JJc                 ~   K   	 t        | j                  |       d {         S 7 # t        t        f$ r Y yw xY ww)NF)boolr   r   r   )r0   rS   s     r   _isdirzHTTPFileSystem._isdir  s:     	dhhtn,--,!:. 		s(   =( &( =( :=:=)T)NN)r   NNNNN)r   Nr   )"__name__
__module____qualname____doc__sepr   r&   propertyr:   r@   staticmethodrL   rP   classmethodrT   rX   r{   r   r   lsri   r   r	   r   r   r   r   r   r   r   r   r   r   __classcell__r5   s   @r   r   r   $   s    C ?&B  3 
 
    0d 
c	B$& (1;K$D !0:d
2 7r
 9 CDGRr6   r   c                   r     e Zd ZdZ	 	 	 	 	 	 	 	 d fd	Zd fd	Zd Z ee      Zd Z	d Z
 ee
      Z xZS )	r   a  
    A file-like object pointing to a remote HTTP(S) resource

    Supports only reading, with read-ahead of a predetermined block-size.

    In the case that the server does not supply the filesize, only reading of
    the complete file in one go is supported.

    Parameters
    ----------
    url: str
        Full URL of the remote resource, including the protocol
    session: aiohttp.ClientSession or None
        All calls will be made within this session, to avoid restarting
        connections where the server allows this
    block_size: int or None
        The amount of read-ahead to do, in bytes. Default is 5MB, or the value
        configured for the FileSystem creating this file
    size: None or int
        If given, this is the size of the file in bytes, and we don't attempt
        to call the server to find the value.
    kwargs: all other key-values are passed to requests calls.
    c           
          |dk7  rt        d      |
| _        |	| _        || _        || _        ||dd| _        t        |   d||||||d| y )Nr   zFile mode not supportedr`   ra   )fsrS   r   r'   r*   r+   r   )rG   r   r    r?   rJ   detailsr%   r&   )r0   r  r?   rJ   r'   r   r*   r+   rc   r    r   r   r5   s               r   r&   zHTTPFile.__init__'  sq     4<%&?@@(	 #T6B 	
!!'	
 	
r6   c                 >   |dk  rD| j                   dk(  r5| j                  | j                  | j                  k  s| j                          | j                  |dk  r4| j                          n#t	        | j                  | j                   z
  |      }t
        |   |      S )a5  Read bytes from file

        Parameters
        ----------
        length: int
            Read up to this many bytes. If negative, read all content to end of
            file. If the server has not supplied the filesize, attempting to
            read only part of the data will raise a ValueError.
        r   )locrc   	blocksize
_fetch_allr   r%   r   )r0   lengthr5   s     r   r   zHTTPFile.readF  s|     aZDHHMYY*tyyDNN/JOO99z!TXX-v6Fw|F##r6   c                 0  K   t         j                  d|         t        | j                  t              s | j
                  j                  | j                  j                  | j                        fi | j                   d{   }|4 d{    |j                          |j                          d{   }t	        t        |      dd|      | _        t        |      | _        ddd      d{    yy7 v7 m7 G7 # 1 d{  7  sw Y   yxY ww)zRead whole file in one shot, without caching

        This is only called when position is still at zero,
        and read() is called without a byte-count.
        zFetch all for N)rc   fetcherr  r   )rf   rg   rp   cacher   rJ   rh   r  r@   r?   r   r   r   rY   rc   )r0   r~   r   s      r   async_fetch_allzHTTPFile.async_fetch_all]  s      	~dV,-$**h/&dll&&tww'9'9$(('CSt{{SSA % %""$FFHn%S44c
  H	% % % 0S%$% % % %sl   B DC9
DC;D$D5C=61D'D2C?3D;D=D?DDD
DDc                    |j                  dd      }t        j                  d|      }|sy|d   dk(  rdx}}n.|d   j                  d      D cg c]  }t	        |       c}\  }}|d	   dk(  rdnt	        |d	         }|||fS c c}w )
zParse the Content-Range headerContent-RangerW   zbytes (\d+-\d+|\*)/(\d+|\*))NNNr\   r   N-r   )rh   r   r   splitr   )r0   r   smr   r   xtotals           r   _parse_content_rangezHTTPFile._parse_content_rangep  s    KK,HH3Q7#Q43;EC*+A$**S/:Q#a&:JE3!QqTc5   ;s   Bc                   K   t         j                  d|  d| d|        | j                  j                         }|j	                  di       j                         }d| d|dz
   |d<   t         j                  | j
                   d|d            | j                  j                  | j                  j                  | j
                        fd|i| d	{   }|4 d	{    |j                  d
k(  r	 d	d	d	      d	{    y|j                          |j                  dk(  xsQ | j                  |j                        d   |k(  xs. t        |j                  j                  d|dz               ||z
  k  }|r|j                          d	{   }n{|dkD  rt!        d      d}g }	 |j"                  j                  d       d	{   }	|	r(|j%                  |	       |t'        |	      z  }|||z
  kD  rnnOdj)                  |      d	||z
   }|cd	d	d	      d	{    S 7 L7 D7 '7 7 e7 # 1 d	{  7  sw Y   y	xY ww)a3  Download a block of data

        The expectation is that the server returns only the requested bytes,
        with HTTP code 206. If this is not the case, we first check the headers,
        and then stream the output - if the data size is bigger than we
        requested, an exception is raised.
        zFetch range for z: r  r   bytes=r\   r   z : Ni  r6      r   Content-LengthzThe HTTP server doesn't appear to support range requests. Only reading this file from the beginning is supported. Open with block_size=0 for a streaming file interface.i   )rf   rg   r   r   r/   r?   rJ   rh   r  r@   r   r   r  r   r   r   r   r   appendrY   rx   )
r0   r   r   r   r   r~   response_is_ranger   clr   s
             r   async_fetch_rangezHTTPFile.async_fetch_range~  sM     	'vRwau=>!!#**Y+002#E7!C!G95zWW%5$678"$,,""GGtxx(
29
=C
 
  )	 )	xx3)	 )	 )	   C P,,QYY7:eCPqyy}}%5sQw?@C%KO  !FFHn M  "#)).."77E

5)c%j(e+!  hhsmMcEk2S)	 )	 )	
)	 )	$ % 8?)	 )	 )	 )	s   CIH!
I H$!I$H05I H'IBH0H*8H0H,AH0IH.I$I'I*H0,H0.I0I6H97I>I)NNr   r   NNNF)r   r   r   r   r&   r   r  r   r  r  r  _fetch_ranger   r   s   @r   r   r     sU    8 
>$.%" o.J!9v   12Lr6   r   z([*[])c                 4    t         j                  |       }|d uS r   )magic_checksearch)r  r   s     r   r   r     s    q!Er6   c                   P     e Zd Zd fd	ZddZddZ ee      Zd Z fdZ	 xZ
S )	r   c                     j                  dd       _         _        | _        | _        |dk7  rt
        d d _        t            d||dd  fd}t         j                  |       _
        |j                   _        y )	Nr   Fr   r   noner  rS   r   r*   c                     K    j                   j                  j                  j                        fi j	                          d {   } j                  j                  |        | S 7 "wr   )rJ   rh   r  r@   
__aenter__ri   )r~   r   r0   r?   s    r   corz$HTTPStreamFile.__init__.<locals>.cor  s\     &dll&&tww'9'9#'>I&ITTVVAGG//37H Ws   AA0A.#A0r   )r/   r   r?   r    rJ   r   r  r%   r&   r   r~   )	r0   r  r?   r   r    rJ   r   r*  r5   s	   ` `   ` r   r&   zHTTPStreamFile.__init__  s    "JJ~u=	4< #T2QBStQ&Q	
 dii%GG	r6   c                 X    |dk(  r|dk(  ry || j                   k(  r|dk(  ry t        d      )Nr   r\   zCannot seek streaming HTTP file)r  r   )r0   r  whences      r   r   zHTTPStreamFile.seek  s2    !8!$((?v{:;;r6   c                    K   | j                   j                  j                  |       d {   }| xj                  t	        |      z  c_        |S 7 $wr   )r~   r   r   r  rY   )r0   numr   s      r   _readzHTTPStreamFile._read  s=     FFNN'',,CH
 -s   )AA%Ac                 @   K   | j                   j                          y wr   )r~   rE   r9   s    r   rI   zHTTPStreamFile._close  s     s   c                 ~    t        j                  | j                         | j                         t        |           y r   )asynciorun_coroutine_threadsaferI   r    r%   rE   r0   r5   s    r   rE   zHTTPStreamFile.close  s&    ((		Br6   )r   NN)r   r  )r   r   r   r&   r   r/  r   r   rI   rE   r   r   s   @r   r   r     s-    $<
 D r6   r   c                   4     e Zd Z	 d fd	ZddZ fdZ xZS )r   c                     || _         || _        d | _        |dk7  rt        |d d| _        || _        t        |   |||d       || _        y )Nr   r   r&  r'  )	r?   rJ   r~   r   r  r   r%   r&   rc   )	r0   r  r?   r   r    rJ   rc   r   r5   s	           r   r&   zAsyncStreamFile.__init__  sW     4< #T2BStG	r6   c                   K   | j                    | j                  j                  | j                  j	                  | j
                        fi | j                  j                          d {   }| j                  j                  || j
                         || _         | j                   j                  j                  |       d {   }| xj                  t        |      z  c_        |S 7 ~7 &wr   )r~   rJ   rh   r  r@   r?   r   r)  ri   r   r   r  rY   )r0   r.  r~   r   s       r   r   zAsyncStreamFile.read  s     66>&dll&&""488,04jlA GG//488<DFFFNN'',,CH

 -s%   A(C-*C)+AC-C+%C-+C-c                    K   | j                   !| j                   j                          d | _         t        |           d {    y 7 wr   )r~   rE   r%   r4  s    r   rE   zAsyncStreamFile.close  s4     66FFLLNDFgmos   ?A
AA
)r   NNNr  )r   r   r   r&   r   rE   r   r   s   @r   r   r     s    @D	 r6   r   c                   K   |j                         }|j                  di       j                         }d| d|dz
   |d<    | j                  |fd|i| d {   }|j                          |4 d {    |j	                          d {   }d d d       d {    |r8t        |d      5 }	|	j                  |       |	j                         d d d        y S 7 7 j7 T7 F# 1 d {  7  sw Y   VxY w# 1 sw Y   y xY ww)Nr   r  r  r\   r   zr+b)r   r/   rh   r   r   r   r   r   )
rJ   r?   r   r   r`   r   r   r~   r   r   s
             r   	get_ranger:    s     [[]FjjB',,.Gwaay1GGgkk#9w9&99A  FFHn $ 	!FF5MGGCL	 	 
 	:   	 	s   ADCD4C5D8C%C!C%DC#D/#C:DD!C%#D%C7+C.,C73D:D?Dc                 b  K   t         j                  d|        |j                         }|j                  dd      }|j	                  di       j                         }d|d<   ||d<   i }|dk(  r |j
                  | fd|i| d{   }n1|d	k(  r |j                  | fd|i| d{   }nt        d
|       |4 d{    |j                          d|j                  v r;d|j                  vs|j                  d   dv rWt        |j                  d         |d<   n;d|j                  v r-t        |j                  d   j                  d      d         |d<   d|j                  v r$|j                  d   j                  d      d   |d<   |j                  j	                  d      dk(  rd|d<   t        |j                        |d<   dD ]0  }|j                  j	                  |      s|j                  |   ||<   2 ddd      d{    |S 7 7 j7 S7 # 1 d{  7  sw Y   |S xY ww)zCall HEAD on the server to get details about the file (size/checksum etc.)

    Default operation is to explicitly allow redirects and use encoding
    'identity' (no compression) to get the true size of the target.
    zRetrieve file size for %sallow_redirectsTr   identityzAccept-Encodingr   Nrh   z)size_policy must be "head" or "get", got r  zContent-Encoding)r=  rW   rc   r  r   r\   zContent-Type;r   mimetypezAccept-Rangesr&  Fr   r?   )ETagzContent-MD5Digest)rf   rg   r   r/   rh   r   	TypeErrorr   r   r   r  	partitionstrr?   )	r?   rJ   r2   r   arr   r   r~   checksum_fields	            r   r   r     s6     LL,c2[[]F	%t	,B::i$))+D(D	F9Df',,sABA&AA		'++c@2@@@CK=QRR A A	qyy( "2aii@R6S X 7  #199-=#>?V		)qyy9??DQGHDLQYY& yy8BB3GJD99==)V3 $DO!%%jU? 	ANyy}}^,'(yy'@^$	A1A A8 KC B@A A A A A8 Kss   BH/H"H/'H(H/HH/D"H(H<H/HH/H/H/H/H,H" H,'H/c                    K   |t                d {   }t        | g|d|i| d {   }|j                  d      S 7 .7 w)NrJ   rc   )r   r   rh   )r?   rJ   argsr   r   s        r   
_file_sizerI  Q  sI     "$CB4BB6BBD88F %Bs   AAAAAAr   )r   )-r2  r   loggingr   rN   r   urllib.parser   r   r<   fsspec.asynr   r   r   r   fsspec.callbacksr	   fsspec.exceptionsr
   fsspec.specr   fsspec.utilsr   r   r   r   r   cachingr   r   rm   rk   	getLoggerrf   r   r   r   r"  r   r   r   r:  r   rI  	file_sizer   r6   r   <module>rT     s     	  	   !   V V - , ,   RZZMNbjjEF			=	)+g_ gTk3# k3\ bjj"
&) &R/ @"0f $	r6   