
    wgj                        d dl Z d dlZ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
mZm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 d dlmZ d d	lmZmZmZ  ej>                  d
      Z  G d de!      Z"d Z#d Z$d Z% G d dejL                  jN                        Z( G d dejL                  jR                        Z*d Z+ G d dejL                  jX                        Z- G d de      Z.y# e$ r	 esd dlZY w xY w)    N)	lru_cache)chain)TYPE_CHECKINGLiteral)AsyncFileSystem)DEFAULT_CALLBACK)
filesystemopensplit_protocol)AsyncFileSystemWrapper)
isfilelikemerge_offset_rangesother_pathszfsspec.referencec                   $     e Zd Z fdZd Z xZS )ReferenceNotReachablec                 :    t        |   |  || _        || _        y N)super__init__	referencetarget)selfr   r   args	__class__s       e/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/fsspec/implementations/reference.pyr   zReferenceNotReachable.__init__   s    $"    c                 :    d| j                    d| j                   S )NzReference "z" failed to fetch target )r   r   r   s    r   __str__zReferenceNotReachable.__str__#   s    T^^,,Edkk]SSr   )__name__
__module____qualname__r   r   __classcell__r   s   @r   r   r      s    
Tr   r   c                 F    t        t        | j                                     S r   )nextitervalues)ds    r   _firstr*   '   s    QXXZ !!r   c                     |j                  |       }t        |t        t        f      r/t        |d   t              r|d   rt        |d         d   S |d   S y y Nr   )get
isinstancelisttuplestrr   )path
referencesrefs      r   _prot_in_referencesr5   +   sW    
..
C#e}%*SVS*A,/F~c!f%a(>A> +B%r   c                     t        | t              rt        | |      | giS i }| D ]/  }t        ||      }|j                  |g       j	                  |       1 |S r   )r.   r1   r5   
setdefaultappend)pathsr3   outr2   protocols        r   _protocol_groupsr<   1   s`    %#E:6@@
C 2&tZ8x$++D12 Jr   c                       e Zd Zd Zy)RefsValuesViewc              #     K   | j                   j                  j                         D ]'  }t        j                  |      j                          ) | j                   j                  j                         E d {    | j                   j                         D ]c  }| j                   j                  |      }t        |      dk(  r| j                   |dz       A| j                   j                  |      E d {    e y 7 7 	w)Nr   /0)_mapping	zmetadatar(   jsondumpsencode_itemslistdir_get_chunk_sizeslen_generate_all_records)r   valfieldchunk_sizess       r   __iter__zRefsValuesView.__iter__<   s     ==**113 	+C**S/((**	+==''..000]]**, 	BE--88?K;1$mmEDL11}}::5AAA	B 	1 Bs%   A6D8C>9A=D6D 7D DNr    r!   r"   rN    r   r   r>   r>   ;   s    	Br   r>   c                       e Zd Zd Zy)RefsItemsViewc                 z    t        | j                  j                         | j                  j                               S r   )ziprA   keysr(   r   s    r   rN   zRefsItemsView.__iter__I   s)    4==%%')=)=)?@@r   NrO   rP   r   r   rR   rR   H   s    Ar   rR   c                 h    d}d}t        | d d d   |d d d         D ]  \  }}|||z  z  }||z  } |S )Nr      )rT   )idxsizesrK   multiss         r   ravel_multi_indexr^   M   sS    
CDC"IuTrT{+ 1q4x	 Jr   c                      e Zd ZdZed        Zed        Z	 	 	 	 	 d ded   fdZd Z	d	 Z
ed!d
       Z e       d        Zd"dZd Z ed      d        Zd Zd Zd Zd Zd Zd Zd Zd Zed        Zd Zd#dZd#dZd Zd Zd Z d Z!y)$LazyReferenceMapperao  This interface can be used to read/write references from Parquet stores.
    It is not intended for other types of references.
    It can be used with Kerchunk's MultiZarrToZarr method to combine
    references into a parquet store.
    Examples of this use-case can be found here:
    https://fsspec.github.io/kerchunk/advanced.html?highlight=parquet#parquet-storagec                     dd l }|S r,   )numpy)r   nps     r   rc   zLazyReferenceMapper.np_   s
    	r   c                     dd l }|S r,   )pandas)r   pds     r   rf   zLazyReferenceMapper.pde   s
    	r   Nengine)fastparquetpyarrowc                 *   || _         i | _        |xs | j                   | _        || _        || _        || _        | j                   dz   | _        |t        j                  d      n|| _	        ddl
m} | j                  dk(  r |d      t        d      yy)a  

        This instance will be writable, storing changes in memory until full partitions
        are accumulated or .flush() is called.

        To create an empty lazy store, use .create()

        Parameters
        ----------
        root : str
            Root of parquet store
        fs : fsspec.AbstractFileSystem
            fsspec filesystem object, default is local filesystem.
        cache_size : int, default=128
            Maximum size of LRU cache, where cache_size*record_size denotes
            the total number of references that can be loaded in memory at once.
        categorical_threshold : int
            Encode urls as pandas.Categorical to reduce memory footprint if the ratio
            of the number of unique urls to total number of refs for each variable
            is greater than or equal to this number. (default 10)
        engine: Literal["fastparquet","pyarrow"]
            Engine choice for reading parquet files. (default is "fastparquet")
        z/{field}/refs.{record}.parqNfiler   )	find_specri   z)engine choice `pyarrow` is not installed.)rootrM   out_root
cat_threshrg   
cache_sizeurlfsspecr	   fsimportlib.utilrl   ImportError)r   rm   rs   rn   rp   categorical_thresholdrg   rl   s           r   r   zLazyReferenceMapper.__init__k   s    B 	 -DII/$99<</1z&##F+r,;;)#	)(<(DIJJ )E#r   c                 ^    |dv r| j                          | j                  |   S t        |      )N)rF   record_sizerB   )setup__dict__AttributeErrorr   items     r   __getattr__zLazyReferenceMapper.__getattr__   s.    99JJL==&&T""r   c                 P    i  _          j                  j                  dj                   j                  dg             j                   d<   t        j                   j                   d         }|d    _        |d    _        t         j                         fd       }| _        y )N/
.zmetadatarx   metadata)maxsizec                 r   j                   j                  | |      }t        j                  j                  j                  |            }	 j                  j                  |j                        }|j                  D ci c]  }|||   j                          }}|S c c}w # t        $ r d}Y |S w xY w)zcached parquet file loader)rL   record)rg   N)rq   formatioBytesIOrs   cat_filerf   read_parquetrg   columnsto_numpyOSError)rL   r   r2   datadfcrefsr   s          r   	open_refsz,LazyReferenceMapper.setup.<locals>.open_refs   s     88??v?>D::dgg..t45DWW))$t{{)C57ZZ@2a5>>++@@ K A Ks$   5B' B"B' "B' 'B65B6)rF   rs   r   joinrm   rC   loadsrx   rB   r   rp   r   )r   metr   s   `  r   ry   zLazyReferenceMapper.setup   s    $(GG$4$4HHdii./%
L! jj\23}-Z 
4??	+		 
,		 #r   c                 n   i |d}|'t        j                  j                  | fi |xs i \  }} |j                  |       r|j	                  | d       |j                  | d       |j                  dj                  | dg      t        j                  |      j                                t        | |fi |S )aJ  Make empty parquet reference set

        First deletes the contents of the given directory, if it exists.

        Parameters
        ----------
        root: str
            Directory to contain the output; will be created
        storage_options: dict | None
            For making the filesystem to use for writing is fs is None
        fs: FileSystem | None
            Filesystem for writing
        record_size: int
            Number of references per parquet file
        kwargs: passed to __init__

        Returns
        -------
        LazyReferenceMapper instance
        r   rx   T	recursiveexist_okr   r   )rr   core	url_to_fsexistsrmmakedirspiper   rC   rD   rE   r`   )rm   storage_optionsrs   rx   kwargsr   s         r   createzLazyReferenceMapper.create   s    , k::{{,,TMo6KMHB99T?EE$$E'
D4(
$-.

30F0F0HI"46v66r   c                 >    d | j                   D        }t        |      S )zList top-level directoriesc              3   j   K   | ]+  }|j                  d       r|j                  dd      d    - yw).zr   rW   r   N)
startswithrsplit.0ps     r   	<genexpr>z.LazyReferenceMapper.listdir.<locals>.<genexpr>   s,     V1<<PTCUa #Vs   33)rB   set)r   dirss     r   rG   zLazyReferenceMapper.listdir   s     WT^^V4yr   c                 l   |j                  d      }|r|dz   nd}| j                         }|D cg c](  }|j                  |      rd|j                  |      vr|* }}|rt	        dgd | j
                  D        d | j                  D              D ch c](  }|j                  |      rd|j                  |      vr|* }}|du r|j                  |       t        |      S |D 	cg c]  }	|	ddd	
 }
}	|D 	cg c]N  }	|	d
t        |	| j
                  v r"t        j                  | j
                  |	         n| j                  |	         d	P }}	t        |
|z   d       S |}t        | j
                  D 	cg c]  }	|	j                  | d      s|	 c}	| j                  D 	cg c]  }	|	j                  | d      s|	 c}	z         }|D 	cg c]N  }	|	d
t        |	| j
                  v r"t        j                  | j
                  |	         n| j                  |	         d	P }}	| j                  |      }|du rt        |      t        |      z   S | j                  |      }t!        ||      D 	cg c]  \  }	}|d   r	|	d
|d   d	 }}	}||z   S c c}w c c}w c c}	w c c}	w c c}	w c c}	w c c}	w c c}}	w )zShortcut file listingsr    r   c              3       K   | ]  }|  y wr   rP   r   names     r   r   z)LazyReferenceMapper.ls.<locals>.<genexpr>   s     5dT5   c              3       K   | ]  }|  y wr   rP   r   s     r   r   z)LazyReferenceMapper.ls.<locals>.<genexpr>   s     2dT2r   F	directoryr   r   typesizerk   c                     | d   S )Nr   rP   )r]   s    r   <lambda>z(LazyReferenceMapper.ls.<locals>.<lambda>  s
    AfI r   keyrX   )rstriprG   r   lstripr   rB   rF   updatesortedrI   rC   rD   r   _keys_in_fieldr/   rJ   rT   )r   r2   detailpathdashdirnamesr)   r   fothersr   dirinfofileinforL   rU   recsrecrecinfos                    r   lszLazyReferenceMapper.ls   s   {{3!%4#:2<<> 
||H%#QXXh5G*G 
 

  !N5dnn52dkk2
 <<)c(9K.K F  d#f~%RVW$k1EWGW #  !"4>>1 

4>>$#78![[.H  'H,2EFF"nnLd4&
0KTL $Kt$qz/JtKL
 
  t~~- JJt~~d34T*
 
 ""5)U?<$t*,,))%0 !t_
c1v 63r7;
 

 '!!w
 X MK
"
s=   -J-J%J8AJ3J!J!!J&
;J&
AJ+-J0c                    || j                   v r| j                   |   S || j                  v r0t        j                  | j                  |         j	                         S d|vs| j                  |      rt        |      |j                  dd      \  }}| j                  |      \  }}}| j                   j                  ||fi       j                  |d      }|t        |r|S |dk(  ry	 | j                  ||      }g d}
|
D cg c]  }||v r||   |   nd }}|d   }||S |d   t        d	      |dd
 ddgk(  r|dd S |dd
 S # t        t        t        f$ r}	t        |      |	d}	~	ww xY wc c}w )zfGet the reference for one key

        Returns bytes, one-element list or three-element list.
        r   rW   FNr   r   r2   offsetr   rawrX   z1This reference does not exist or has been deleted   )rF   rB   rC   rD   rE   _is_metaKeyErrorr   _key_to_recordr-   r   
ValueError	TypeErrorFileNotFoundError)r   r   rL   _r   ri
chunk_sizemayber   excr   r   	selectionr   s                 r   _load_one_keyz!LazyReferenceMapper._load_one_key!  s   
 $++;;s##DNN"::dnnS1299;;^t}}S13-::c1%q!%!4!4S!9J488UC=NL1_	)>>%0D 4AHIAAIT!WR[47I	Im?JQ<NOOQq>aV#Ra= !} I'89 	)3-S(	) Js   'E E-E*E%%E*i   c                 4   |j                  dd      \  }}| j                  |      }t        |      dk(  ry|j                  d      D cg c]  }t	        |       }}t        ||      }|| j                  z  }|| j                  z  }	||	t        |      fS c c}w )z3Details needed to construct a reference for one keyr   rW   r   )r   r   r   .)r   rH   rI   splitintr^   rx   )
r   r   rL   chunkrM   r   	chunk_idxchunk_numberr   r   s
             r   r   z"LazyReferenceMapper._key_to_recordI  s     zz#q)u++E2{q %*[[%56SV6	6(K@!1!11D,,,r3{+++	 7s   	Bc                    || j                   vr`| j                  | d   }t        |d   |d         D cg c]  \  }}t        j                  ||z         }}}|xs dg| j                   |<   | j                   |   S c c}}w )z6The number of chunks along each axis for a given fieldz/.zarrayshapechunksrW   )rM   rB   rT   mathceil)r   rL   zarrayr]   r   
size_ratios         r   rH   z$LazyReferenceMapper._get_chunk_sizesV  s    (((^^ugX$67F-0&BR-S%)Q		!a% J  '1&7QCDU#&&	s   "A>c                     | j                  ||      }t        t        |j                                }t	        |      dk(  r	d |D        S t	        |      dk(  r|d   S d |D        S )z8The references for a given parquet file of a given fieldr   c              3   2   K   | ]  }t        |        y wr   r/   r   ts     r   r   z7LazyReferenceMapper._generate_record.<locals>.<genexpr>f  s     (DG(   rW   r   c              3   L   K   | ]  }|d    st        |dd        n|d      yw)r   Nr   r   s     r   r   z7LazyReferenceMapper._generate_record.<locals>.<genexpr>l  s)     B!qtD2AK15Bs   "$)r   r'   rT   r(   rI   )r   rL   r   r   its        r   _generate_recordz$LazyReferenceMapper._generate_record`  s`    ~~eV,#t{{}%&t9>(R((Y!^; CrBBr   c              #      K   d}| j                  |      D ]  }||z  }	 t        j                  || j                  z        }t	        |      D ]  }| j                  ||      E d{     y7 w)zJLoad all the references within a field by iterating over the parquet filesrW   N)rH   r   r   rx   ranger   )r   rL   nrecchr   s        r   rJ   z)LazyReferenceMapper._generate_all_recordsn  ss     ''. 	BBJD	yy 0 001Dk 	<F,,UF;;;	<;s   A#A/%A-&A/c                     t        |       S r   )r>   r   s    r   r(   zLazyReferenceMapper.valuesw  s    d##r   c                     t        |       S r   )rR   r   s    r   itemszLazyReferenceMapper.itemsz  s    T""r   c                     t        |       S r   )idr   s    r   __hash__zLazyReferenceMapper.__hash__}  s    $xr   c                 $    | j                  |      S r   )r   )r   r   s     r   __getitem__zLazyReferenceMapper.__getitem__  s    !!#&&r   c                    d|v r| j                  |      sy|j                  dd      \  }}| j                  |      \  }}}| j                  j	                  ||fi       }|||<   t        |      | j                  k(  r| j                  ||       y y || j                  |<   t        j                  t        |t              r|j                         n|      }	i | j                  j                  |i       |	| j                  |<   y Nr   rW   )r   r   r   rF   r7   rI   rx   writerC   r   r.   bytesdecoderB   r-   )
r   r   valuerL   r   r   r\   r   subdict	new_values
             r   __setitem__zLazyReferenceMapper.__setitem__  s    #:dmmC0::c1-LE5..s3LFAqkk,,eV_bAGGAJ7|t///

5&) 0  %DKK

",UE":I #OT^^%7%7R%@"NI"NDNN3r   c                 0    | j                  d      xs d| v S )Nr   z/.zr   r   s    r   r   zLazyReferenceMapper._is_meta  s    ~~d#3u|3r   c                    || j                   v r| j                   |= y || j                  v r| j                  |= y d|v r| j                  |      sy|j                  dd      \  }}| j	                  |      \  }}}| j                   j                  ||fi       }d ||<   t        |      | j                  k(  r| j                  ||       y y d | j                   |<   y r  )	rF   rB   r   r   r   r7   rI   rx   r  )r   r   rL   r   r   r\   r
  s          r   __delitem__zLazyReferenceMapper.__delitem__  s    $++C DNN"s#cz$--"4::c1-q#22371++00%"E!
w<4#3#33JJuf- 4 $(C r   c           	      t   dd l }dd l}dd l}| j                  ||f   }d}	t	        |      | j
                  k  r	 | j                  ||      }	|	r|	d   }
|	d   }|	d   }|	d   }n|j                  | j
                  |j                  d      }
|j                  | j
                  d	      }|j                  | j
                  d	      }|j                  | j
                  |j                  d      }|j                         D ]  \  }}t        |t              rot        |
j                        d
k(  r/|d   |
j                  j                   vr|
j#                  |d         }
|d   |
|<   t	        |      dkD  st|d   ||<   |d   ||<   |d |
|<   d||<   d||<   d ||<   |j$                  j'                  |      ||<    |j)                  |
|||dd      }|j*                  j-                         |j*                  j/                         xs dz  | j0                  kD  r|d   j3                  d
      |d<   ddd}ddg}|xs | j4                   d| d| d}| j6                  j9                  |xs | j4                   d| d       | j:                  dk(  rddi}n.| j:                  dk(  rd||d}nt=        | j:                   d       |j>                  |f| j:                  |xs tA        | j6                  dd       ddd| |jC                          | j                  jE                  ||f       y # t        $ r Y w xY w)Nr   Fr2   r   r   r   O)dtypeint64categoryrW      r   )copyr  utf8)r   r2   r   z/refs.z.parqTr   ri   write_statisticsrh   )statsobject_encoding	has_nullsz not supportedr   zstd)rg   r   compressionindex)#kerchunk.dfrb   re   rF   rI   rx   r   r   fullnanzerosr   r.   r/   r1   r  
categoriesadd_categoriesr   	_proc_raw	DataFramer2   countnuniquero   astypern   rs   mkdirsrg   NotImplementedError
to_parquetgetattrclearpop)r   rL   r   base_urlr   kerchunkrc   rf   	partitionoriginalr9   offsetsrZ   rawsjr   r   r  r  fndf_backend_kwargss                        r   r  zLazyReferenceMapper.write  sJ   KK0	y>D,,,>>%8 V$Ex(GV$EE?DGGD,,bffCG@Ehht//wh?GHHT--WH=E774++RVV37?D ( 	6GAt$%$
2Qu{{'='==!00a9E7at9q=!%aGAJ#AwE!Ha
aQ #++//5Q'	6* \\!	   
 77==?bggoo/415GF**:6BvJ")6:UO	)DMM*!E7&F(3dmm4AeW=M;;)#!3U ;[[M)#2&! &^&DEE	
;;+ 9tww 148	
  	
 	(S  s   L* *	L76L7c                    t        | j                        D ]-  }t        |t              s|\  }}| j	                  ||||       / t        | j                        D ]H  }|dk7  s	d|v st        j                  | j                  j                  |            | j                  |<   J | j                  | j                  d}| j                  j                          t        j                  |      j                         | j                  d<   | j                  j                  dj                  |xs | j                   dg      | j                  d          | j"                  j%                          y)zOutput any modified or deleted keys

        Parameters
        ----------
        base_url: str
            Location of the output
        )r1  r   r   r   r   r   N)r/   rF   r.   r0   r  rC   r   r0  rB   rx   r/  rD   rE   rs   r   r   rn   r   cache_clear)r   r1  r   thingrL   r   kr   s           r   flushzLazyReferenceMapper.flush  s'    $++& 	E%' %v

%$3	  	 dkk" 	CAL TQY$(JJt{{q/A$Bq!	C  >>$:J:JK$(JJsO$:$:$<L!HHh/$-->?KK%	
 	""$r   c                    d}| j                         D ]@  }|j                  d      r|dz  }|t        j                  | j	                  |            z  }B |t        | j                        z  }|t        d | j                  D              z  }|S )Nr   r   rW   c              3   B   K   | ]  }t        |t              rd   yw)rW   N)r.   r0   )r   r   s     r   r   z.LazyReferenceMapper.__len__.<locals>.<genexpr>+  s     H1:a3GQHs   )	rG   r   r   prodrH   rI   rB   sumrF   )r   r(  rL   s      r   __len__zLazyReferenceMapper.__len__!  s    \\^ 	AE$
4#8#8#?@@		A
 	T^^$$HHHHr   c              #     K   t        | j                        }|j                  | j                         |D ]  }t	        |t
              s|  | j                         D ]!  }| j                  |      D ]  }|| v s|  # y wr   )r   rB   r   rF   r.   r1   rG   r   )r   metasbitrL   r=  s        r   rN   zLazyReferenceMapper.__iter__.  s      DNN#T[[! 	C#s#		 \\^ 	E((/ 9G	s   AB	1B;	Bc                 F    	 | j                  |       y# t        $ r Y yw xY w)NTF)r   r   r|   s     r   __contains__z LazyReferenceMapper.__contains__;  s*    	t$ 		s    	  c           
   #     K   | j                  |      }t        |      dk(  r|dz    yt        j                  d |D         }|D ]2  }|dz   dj	                  |D cg c]  }t        |       c}      z    4 yc c}w w)z|List key names in given field

        Produces strings like "field/x.y" appropriate from the chunking of the array
        r   r@   Nc              3   2   K   | ]  }t        |        y wr   )r   )r   r\   s     r   r   z5LazyReferenceMapper._keys_in_field.<locals>.<genexpr>K  s     "A58"Ar   r   r   )rH   rI   	itertoolsproductr   r1   )r   rL   rM   indsindr   s         r   r   z"LazyReferenceMapper._keys_in_fieldB  s     
 ++E2{q $,  "A["AB 	@C#+#)>Q#a&)> ???	@)>s   AB A;-B )NN   
   rh   )NNi'  )r   TNN)"r    r!   r"   __doc__propertyrc   rf   r   r   r~   ry   staticmethodr   r   rG   r   r   r   rH   r   rJ   r(   r   r  r  r  r   r  r  r>  rC  rN   rH  r   rP   r   r   r`   r`   V   s   Y  
    4A.K 01.K`##0 7 7< [ 
@"D&P t_
, 
,'C<$#'O  4 4("T)l!%F@r   r`   c                       e Zd ZdZdZ	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zd dZd dZd dZd Z	d Z
efd	Zd!d
Zd"dZd#dZd Zd#dZd Zd Zd Zd$dZd%dZd Zd Zd Zd%dZd& fd	Zd Zd Zd Zd'dZd'dZ d Z! xZ"S )(ReferenceFileSystema  View byte ranges of some other file as a file system
    Initial version: single file system target, which must support
    async, and must allow start and end args in _cat_file. Later versions
    may allow multiple arbitrary URLs for the targets.
    This FileSystem is read-only. It is designed to be used with async
    targets (for now). This FileSystem only allows whole-file access, no
    ``open``. We do not get original file details from the target FS.
    Configuration is by passing a dict of references at init, or a URL to
    a JSON file containing the same; this dict
    can also contain concrete data for some set of paths.
    Reference dict format:
    {path0: bytes_data, path1: (target_url, offset, size)}
    https://github.com/fsspec/kerchunk/blob/main/README.md
    r   c                    t        |   di | || _        |	| _        |
| _        i | _        i | _        i | _        || _        || _	        t        |t              rt        di |xs |xs i d|i}t        j                  j                  |fi |\  }}|j!                  |      r^t        j"                  |dfi |5 }t$        j'                  d|       t)        j*                  |      }ddd       | j-                  |	       n<t$        j'                  d|       t/        |||      | _        n| j-                  ||	       t        |t              r|j3                         D ci c]A  \  }}|t        |t              r)t        j4                  |j7                  dd      d	   fi |n|C c}}| _        d| j                  vrt5        d
      | j                  d<   y|Dt        |j8                  t:              r|j8                  d	   n|j8                  }|| j                  |<   || j
                  j=                         D ]g  }t?        |      r |       }t        j                  jA                  |      \  }}|s:|| j                  vsIt5        |fi |xs i }|| j                  |<   i || j0                  j=                         D ]  }t?        |      r |       }t        |tB              s&|d	   s,t        j                  jA                  |d	         \  }}|| j                  vs`t5        |fi |xs i }|| j                  |<    n |r-|| j                  vrt5        |fi |xs i }|| j                  |<   |xs t5        d
      | j                  d<   | j                  j3                         D ]*  \  }}|jD                  rtG        |      | j                  |<   , y# 1 sw Y   xY wc c}}w )a  
        Parameters
        ----------
        fo : dict or str
            The set of references to use for this instance, with a structure as above.
            If str referencing a JSON file, will use fsspec.open, in conjunction
            with target_options and target_protocol to open and parse JSON at this
            location. If a directory, then assume references are a set of parquet
            files to be loaded lazily.
        target : str
            For any references having target_url as None, this is the default file
            target to use
        ref_storage_args : dict
            If references is a str, use these kwargs for loading the JSON file.
            Deprecated: use target_options instead.
        target_protocol : str
            Used for loading the reference file, if it is a path. If None, protocol
            will be derived from the given path
        target_options : dict
            Extra FS options for loading the reference file ``fo``, if given as a path
        remote_protocol : str
            The protocol of the filesystem on which the references will be evaluated
            (unless fs is provided). If not given, will be derived from the first
            URL that has a protocol in the templates or in the references, in that
            order.
        remote_options : dict
            kwargs to go with remote_protocol
        fs : AbstractFileSystem | dict(str, (AbstractFileSystem | dict))
            Directly provide a file system(s):
                - a single filesystem instance
                - a dict of protocol:filesystem, where each value is either a filesystem
                  instance, or a dict of kwargs that can be used to create in
                  instance for the given protocol

            If this is given, remote_options and remote_protocol are ignored.
        template_overrides : dict
            Swap out any templates in the references file with these - useful for
            testing.
        simple_templates: bool
            Whether templates can be processed with simple replace (True) or if
            jinja  is needed (False, much slower). All reference sets produced by
            ``kerchunk`` are simple in this sense, but the spec allows for complex.
        max_gap, max_block: int
            For merging multiple concurrent requests to the same remote file.
            Neighboring byte ranges will only be merged when their
            inter-range gap is <= ``max_gap``. Default is 64KB. Set to 0
            to only merge when it requires no extra bytes. Pass a negative
            number to disable merging, appropriate for local target files.
            Neighboring byte ranges will only be merged when the size of
            the aggregated range is <= ``max_block``. Default is 256MB.
        cache_size : int
            Maximum size of LRU cache, where cache_size*record_size denotes
            the total number of references that can be loaded in memory at once.
            Only used for lazily loaded references.
        kwargs : passed to parent class
        r;   rbzRead reference from URL %sNz$Open lazy reference dict from URL %s)rs   rp   :rW   r   rk   rP   )$r   r   r   template_overridessimple_templates	templatesfss	_dircachemax_gap	max_blockr.   r1   dictrr   r   r   isfiler
   loggerinforC   load_process_referencesr`   r3   r   r	   r   r;   r0   r(   callabler   r/   
async_implr   )r   for   ref_storage_argstarget_protocoltarget_optionsremote_protocolremote_optionsrs   rZ  r[  r_  r`  rp   r   dicref_fsfo2r   textr=  optsr4   r;   r   r   s                            r   r   zReferenceFileSystem.__init__b  s   R 	"6""4 0"b# #;~;GVC !++//:c:KFC}}S![[T1S1 (QKK <bA99Q<D( ((/AB BBG"5)# $$R);<b$  "xxz At !$- %%aggc1oa&8ADADH 488#!+F!3> #-R[[%"@Abkk  )+DHH_%"~~,,. ,C=%C$kk88=! 8#HG1E2GB)+DHHX&, " --/ 	C=%Cc4(SV"(++"<"<SV"DKHatxx/'K^5IrK-/*	 dhh>OF0D"FB(*DHH_%1z&1HHNN$ 	8DAq<<4Q7	8( ( s   ;,O!AO!Oc           
         | j                  |      }t        j                  d|        	 | j                  |   }t        |t              r|j                         }t        |t              rGt        j                  d| d       |j                  d      rt        j                  |dd        }|d d fS t        |      dk(  r%t        j                  d| d|        |d   }||}}nV|\  }}	}
t        j                  d| d	| d
|	 d|
        |	|
z   }||dk\  r|	|z   }n||z   }n|	}||dk\  r|	|z   }n||z   }n|}|| j                  }|||fS # t        $ r}t        |      |d }~ww xY w)Nzcat: zReference: z, type bytes   base64:   rW   z, whole file => r   z => z	, offset z, size )_strip_protocolrc  debugr3   r   r   r.   r1   rE   r  r   base64	b64decoderI   r   )r   r2   startendpartr   rq   start1end1start0r   end0s               r   _cat_commonzReferenceFileSystem._cat_common  s   ##D)uTF^$	3??4(D dC ;;=DdE"LL;tfL9:z*''QR1t##t9>LL;tf,<TFCDq'C #DF $CLL;tfDYvhgdVTUD=D A:#e^F!E\F!8!C<D#:D;++CFD  G  	3#D)s2	3s   E 	E'E""E'c                   K   | j                  |||      \  }}}t        |t              r||| S t        |      \  }}		 | j                  |   j                  |||       d {   S 7 # t        $ r}
t        ||      |
d }
~
ww xY wwN)r{  r|  )r  r.   r  r   r]  	_cat_file	Exceptionr   r   r2   r{  r|  r   part_or_urlr  r  r;   r   es              r   r  zReferenceFileSystem._cat_file(  s     $($4$4TC$4$P!VTk5)uS))$[1!	B(+556t 6      	B'k:A	Bs:   <B$A* #A($A* 'B(A* *	B3B  BBc                     | j                  |||      \  }}}t        |t              r||| S t        |      \  }}		 | j                  |   j                  |||      S # t        $ r}
t        ||      |
d }
~
ww xY wr  )r  r.   r  r   r]  r   r  r   r  s              r   r   zReferenceFileSystem.cat_file4  s    $($4$4TC$4$P!VTk5)uS))$[1!	B88H%..{&d.SS 	B'k:A	Bs    A 	A9'A44A9c                 "    || j                   |<   y)z2Temporarily add binary data or reference as a fileNr3   )r   r2   r	  r   s       r   	pipe_filezReferenceFileSystem.pipe_file>  s     %r   c                    K   | j                  |      rt        j                  |d      S | j                  |       d {   }t	        |d      5 }|j                  |       d d d        y 7 +# 1 sw Y   y xY wwNTr   wb)isdirosr   r  r
   r  )r   rpathlpathr   r   r   s         r   	_get_filezReferenceFileSystem._get_fileB  sj     ::e;;ut44^^E**% 	!GGDM	 	 +	 	s(   =A9A+ A9A-"
A9-A62A9c                    | j                  |      rt        j                  |d      S  | j                  |fi |}|j	                  t        |             t        |      r|j                  |       n&t        |d      5 }|j                  |       d d d        |j                  t        |             y # 1 sw Y   $xY wr  )
r  r  r   r   set_sizerI   r   r  r
   absolute_update)r   r  r  callbackr   r   r   s          r   get_filezReferenceFileSystem.get_fileI  s    ::e;;ut44t}}U-f-#d)$eKKeT" a  T+ s   ?B44B=c                    |r| j                  d       | j                  ||      }t        j                  dd      }t	        ||      }|r2| j                  |D cg c]  }| j                  |      r| c}      }n| j                  |      }t        ||      D ]  \  }	}
|	|v s|j                  |
||	          ! y c c}w )Nr   r   rk   T)
auto_mkdir)	r   expand_pathrr   r	   r   catr  rT   r  )r   r  r  r   r   rs   targetsrr   remotelocals              r   r-   zReferenceFileSystem.getU  s    GGBK  ) <v$7eU+88C1TZZ]QCDD88E?D 0 	2MFE~UDL1	2 Ds   B>2B>c           	         t        |t              r|rt        t        |t              r|st	        d |D              rt        t        || j                        }i }|j                         D ]  \  }}| j                  |   }	g g g g f\  }
}}}|D ]t  }	 | j                  |      \  }}}t        |t        t        f      s0	 |
j                  |       |j                  |       |j                  |       |j                  |       v g }g }g }g }t               }t        |
|||      D ]u  \  }}}}t        |t              r|||<   |!|j                  |       |j                  |       |j                  |       |j                  |       |j                  |       w t        |
|||      D ]S  \  }}}}|||vs|j                  |       |j                  |       |j                  |       |j                  |       U t!        t        |      t        |      t        |      d| j"                  | j$                        \  }}}|	j'                  |||      }t        |
|||      D ]  \  }}}}||v rt        ||||      D ]i  \  }}}}||k(  r#||t        |t(              r|||<   '||| ||<   0||k(  s6||k\  s<||k  sBt        |t(              r|||<   X|||z
  ||z
  xs d  ||<   k   |j+                         j                         D ]\  \  } }!t        |!t(              s| | j                  v s&||    }"t-        | | j                  |          }#|"|#_        |dk(  r|#|dk7  sX|#|| <   ^ t1        |      dk(  rt        |t              rd|vrt3        |      S |S # t        $ r}|dk(  r |dk7  r|||<   Y d }~/d }~ww xY w)Nc              3   $   K   | ]  }d |v  
 yw)*NrP   r   s     r   r   z*ReferenceFileSystem.cat.<locals>.<genexpr>g  s     7OQq7Os   raiseomitT)sortr_  r`  rW   r  )r.   r1   r,  r/   anyr<   r3   r   r]  r  r  r8   r   r   rT   addr   r_  r`  
cat_rangesr  r  r   	__cause__rI   r*   )$r   r2   r   on_errorr   
proto_dictr:   protor9   rs   urlsstartsendsvalid_pathsr   ur]   r  errurls2starts2ends2paths2whole_files	new_paths
new_startsnew_ends	bytes_outrc   nsnebr=  vexnew_exs$                                       r   r  zReferenceFileSystem.catd  s   dC Y%%dD!yC7O$7O4O%%%dDOO<
&,,. I	BLE5%B.0"b"n+D&$ **"..q1GAq!%a%6  7 KKNMM!$KKN&&q)#*( EGEF%K!$kB %
1aa'CFY  OOA&LLONN1%LLOMM!$% "$kB %
1a=Qk%9LLONN1%LLOMM!$% /BUWU../+Iz8 iXFI "$kB B
1a8%(J)%T 
BMBBQwBJ"*%a3%&CF%&qVCFqQ"Wb%a3%&CF%&q2vR0@D%ACF
BByI	BV HHJ$$& 		$DAq!Y'A,@V.q$//!2DE#% w& L'#CF		$ s8q=Zc2s$#;
[ ) %7*6)!$A	%s   +N	O'N==Oc                     |j                  dd       }|| j                  |       y |dk(  r| j                  ||       y t        d|       )NversionrW   )rZ  z Unknown reference spec version: )r-   _process_references0_process_references1r   )r   r3   rZ  verss       r   rf  z'ReferenceFileSystem._process_references  sS    ~~i.<%%j1QY%%jEW%X?vFGGr   c           	          t        |t              rH|j                         D ci c]-  \  }}|t        |t              rt        j                  |      n|/ }}}|| _        yc c}}w )z&Make reference dict for Spec Version 0N)r.   ra  r   rC   rD   r3   )r   r3   r   rK   s       r   r  z(ReferenceFileSystem._process_references0  s^    j$'
 !+ 0 0 2C 
3(=TZZ_3FJ  %	s   2A!c                      j                   r j                  rdd li  _         j	                  |j                  di              t        d       fd       }|j                  di       j                         D ]/  \  }}t        |t              rF|j                  d      r%t        j                  |dd         j                  |<   | j                  |<   ]t        |t              r#t        j                  |       j                  |<    j                  r|d   }d|v rQ j                   r= |j!                  dd	      j!                  d
d      j"                  di  j                  }n ||      }t%        |      dk(  r|gn
||d   |d   g j                  |<   !| j                  |<   2  j                  j'                   j)                  |j                  dg                    y )Nr   r\  i  c                 Z     j                  |       j                  di j                  S NrP   )Templaterenderr\  )r  jinja2r   s    r   _render_jinjaz?ReferenceFileSystem._process_references1.<locals>._render_jinja  s&    ,6??1%,,>t~~>>r   r   zbase64:rv  {{{z}}}rW   r  genrP   )r[  r\  r  r3   _process_templatesr-   r   r   r.   r1   r   ry  rz  ra  rC   rD   replacer   rI   r   _process_gen)r   r3   rZ  r  r=  r  r  r  s   `      @r   r  z(ReferenceFileSystem._process_references1  s   $$
{B ?@	4	? 
	? NN62.446 	'DAq!S!<<	*)/)9)9!AB%)@DOOA&%&"At$%)ZZ]"aD19,,$AIIdC0$WT3/#V6&*nn6  *!,,/FaKaSa1qt_"%&")	'* 	t00r1JKLr   c                     i | _         | j                  |j                  | j                         |j                         D ]1  \  }}d|v rdd l|ffd	| j                   |<   #|| j                   |<   3 y )Nr  r   c                 F     j                  |       j                  di |S r  )r  r  )tempr   r  s     r   r   z8ReferenceFileSystem._process_templates.<locals>.<lambda>  s*     >V__>&>#!># r   )r\  rZ  r   r   r  )r   tmpr=  r  r  s       @r   r  z&ReferenceFileSystem._process_templates  sn    "".JJt../IIK 	&DAqqy01 %#q! %&q!	&r   c                 ^   i }|D ]  }|d   j                         D ci c]H  \  }}|t        |t              r|n/t        |j	                  dd      |d   |j	                  dd            J c}}fdt        j                  j                          D        }|D ]  }dd l} |j                  |d         j                  di || j                  }	 |j                  |d	         j                  di || j                  }
d
|v rd|v r{t         |j                  |d
         j                  di || j                        }t         |j                  |d         j                  di || j                        }|
||g||	<   d
|v d|v z  rt        d      |
g||	<     |S c c}}w )N
dimensionsr{  r   stopsteprW   c              3   d   K   | ]'  }t        t        j                         |             ) y wr   )ra  rT   rU   )r   r(   	dimensions     r   r   z3ReferenceFileSystem._process_gen.<locals>.<genexpr>  s,       S)623s   -0r   rq   r   lengthz^Both 'offset' and 'length' are required for a reference generator entry if either is provided.rP   )r   r.   r/   r   r-   rK  rL  r(   r  r  r  r\  r   r   )r   gensr:   r  r=  r  productsprr  r   rq   r   r  r  s                @r   r  z ReferenceFileSystem._process_gen  s     	%C  -335 Aq !!T* quuWa0!F)QUU61=MNOI'//1A1A1CDH  %8fooc%j188P2PP8fooc%j188P2PPO(c/ =H6==UUdnnUF !=H6==UUdnnUF !$VV4CH#o(c/:$K 
 !$uCH'% 	%B 
As   AF)c                    dg i| _         | j                  j                         }|D ]<  \  }}t        |t        t
        f      rt        |      }nt        |      dk(  rd }n|\  }}}d|v r|j                  dd      d   nd}|}|g}|rA|| j                   vr3| j                  |      }|j                  |       |r|| j                   vr3|j                          t        ||dd        D ]V  \  }	}
|
| j                   vsJ |	| j                   v sJ | j                   |	   j                  |
ddd       g | j                   |
<   X | j                   |   j                  |d|d       ? y )Nr   rW   r   r   r   r   rk   )dircacher3   r   r.   r  r1   rI   r   _parentr8   reverserT   )r   r   r2   r}  r   r   parpar0subdirsparentchilds              r   _dircache_from_itemsz(ReferenceFileSystem._dircache_from_items0  sh   R__""$ 	TJD$$-4yTa!
1d,/4K$++c1%a(RCDfG4t}}4||D)t$ 4t}}4
 OO!$Wgabk!: *DMM111...f%,,"KC (*e$* MM#%%tVT&RS3	Tr   c                 N    | j                  |      }t        j                  |      S r   )r   r   r   )r   r2   mode
block_sizecache_optionsr   r   s          r   _openzReferenceFileSystem._openN  s    }}T"zz$r   c                 z   | j                  |      }t        | j                  t              r	 | j                  j	                  ||      S | j                  s| j                          | j                  |      }|t        |      |r|S |D cg c]  }|d   	 c}S # t
        $ r Y nw xY wt        d| d      c c}w )N'z' is not a known keyr   )
rw  r.   r3   r`   r   r   r   r  r  _ls_from_cache)r   r2   r   r   r:   os         r   r   zReferenceFileSystem.lsR  s    ##D)doo':;))$77 }}%%'!!$';#D))J#&'a&	''  #av-A$BCC (s   B B8	B&%B&c                 J    | j                  |      xs | j                  |      S r   )r  rb  r   r2   r   s      r   r   zReferenceFileSystem.existsc  s    zz$44;;t#44r   c                     | j                   r| j                   v S t        | j                  t              r| j                  j	                         v S t        fd| j                  D              S )Nc              3   F   K   | ]  }|j                   d         yw)r   Nr  )r   r   r2   s     r   r   z,ReferenceFileSystem.isdir.<locals>.<genexpr>n  s      IAq||tfAJ/Is   !)r  r.   r3   r`   rG   r  r   r2   s    `r   r  zReferenceFileSystem.isdirf  sW    ==4==(()<=4??22444 IIIIr   c                     || j                   v S r   r  r  s     r   rb  zReferenceFileSystem.isfilep  s    t&&r   c                 2   K    | j                   ||fi |S wr   )r   )r   r2   r   r   s       r   _lszReferenceFileSystem._lss  s     twwtV.v..r   c                 P   |rt        |   f|||d|S r0| j                        t        fd| j                  D              }nt        | j                        }|r?| j
                  s| j                          |D ci c]  }|| j                  |      d    c}S |S c c}w )N)maxdepthwithdirsr   c              3   F   K   | ]  }|j                        s|  y wr   r  )r   r=  r2   s     r   r   z+ReferenceFileSystem.find.<locals>.<genexpr>}  s     HQQ\\$5GqHs   !!r   )r   findrw  r   r3   r  r  r  )	r   r2   r  r  r   r   r  r=  r   s	    `      r   r  zReferenceFileSystem.findv  s    7<'(6MS  ''-DH$//HHAt'A==))+:;<QAt**1-a00<<H =s   B#c                    | j                   j                  |      }|Dt        |t        t        f      r|dt        |      dS t        |      dkD  r	|d|d   dS |dd dg}n2| j                  |d      }|D cg c]  }|d   |k(  s| }}|s|dddS |d   d	   Rt        | j                   |   d         \  }}| j                  |   j                  | j                   |   d         |d   d	<   |d   S c c}w )
Nrk   r   rW   r  Tr   r   r   r   )
r3   r-   r.   r1   r  rI   r   r   r]  r   )r   r2   r   r:   out0r  protr   s           r   rd  zReferenceFileSystem.info  s	   oo!!$'?#U|, $fc#hGGSA $fc!fEE!%vtDE''$%C"8!ai4&7A8D8 $k1EE76?"$T__T%:1%=>GD!"hhtn11$//$2G2JKDGFOAw 9s   8C3C3c                 ,   K   | j                  |      S wr   )rd  r  s      r   _infozReferenceFileSystem._info  s     yys   c                 x   K   | j                   j                  |d        | j                  j                          y wr   )r3   r0  r  r/  r  s      r   _rm_filezReferenceFileSystem._rm_file  s/     $	
 	s   8:c                    K   |dk(  r| j                  |      rt        || j                  |<   | j                  j	                          y w)Nr   )r   FileExistsErrorr3   r  r/  )r   r2   r   r  r   s        r   
_pipe_filezReferenceFileSystem._pipe_file  s=     8D 1!! $s   AA	c                    K   |dk(  r| j                  |      rt        t        |d      5 }|j                         | j                  |<   d d d        | j
                  j                          y # 1 sw Y   $xY ww)Nr   rX  )r   r  r
   readr3   r  r/  )r   r  r  r  r   r   s         r   	_put_filezReferenceFileSystem._put_file  sc     8E 2!!% 	.!%&VVXDOOE"	.	. 	.s   )A8A,	#A8,A51A8c                    i }| j                   j                         D ]0  \  }}t        |t              r	 |j	                  d      ||<   ,|||<   2 t        j                  |dfi |5 }|j                  t        j                  d|d      j                                ddd       y# t
        $ r, dt        j                  |      z   j	                         ||<   Y w xY w# 1 sw Y   yxY w)z+Write modified references into new locationasciiru  r  rW   )r  r   N)r3   r   r.   r  r  UnicodeDecodeErrorry  	b64encoderr   r
   r  rC   rD   rE   )r   rq   r   r:   r=  r  r   s          r   	save_jsonzReferenceFileSystem.save_json  s    OO))+ 	DAq!U#IXXg.CF A	 [[d6o6 	F!GGDJJ1c:;BBDE	F 	F	 * I(6+;+;A+>>FFHCFI	F 	Fs   B&'6C&2CCC')NNNNNNNNTi   i @BrO  rQ  )F)Fr  r   )rX  NN)T)NFF)	overwrite)#r    r!   r"   rR  r;   r   r  r  r   r  r  r   r  r-   r  rf  r  r  r  r  r  r  r   r   r  rb  r  r  rd  r  r  r  r  r  r#   r$   s   @r   rV  rV  P  s     H
 Z8x(!T
BB& /? 
,2`DH	%MB&#JT< ("5J'/"*Fr   rV  )/ry  collectionsr   rK  loggingr   r  	functoolsr   r   typingr   r   fsspec.corerr   ujsonrC   ru   fsspec.asynr   fsspec.callbacksr   r	   r
   r   #fsspec.implementations.asyn_wrapperr   fsspec.utilsr   r   r   	getLoggerrc  RuntimeErrorr   r*   r5   r<   abc
ValuesViewr>   	ItemsViewrR   r^   MutableMappingr`   rV  rP   r   r   <module>r*     s      	    	   ) 
 ( - 8 8 F E E			-	.TL T"?
B[__// 
BAKOO-- A
w@+//88 w@tp	F/ p	FA  s   C2 2D ?D 