
    OOId                        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Z		 d dl
Zn# e$ r d dlZY nw xY wddlmZ ddlmZ ddlmZmZ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j         j!                  Z" G d dej         j#                  Z$ G d dej         j%                  Z& G d de          Z'dS )    N)	lru_cache   )AsyncFileSystem)_DEFAULT_CALLBACK)
filesystemopensplit_protocol)
isfilelikemerge_offset_rangesother_pathszfsspec.referencec                   $     e Zd Z fdZd Z xZS )ReferenceNotReachablec                 P     t                      j        |  || _        || _        d S N)super__init__	referencetarget)selfr   r   args	__class__s       @lib/python3.11/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    ST^SSdkSSSr   )__name__
__module____qualname__r   r   __classcell__r   s   @r   r   r      sN            
T T T T T T Tr   r   c                 P    t          |                                           d         S Nr   )listvalues)ds    r   _firstr'   "   s    

Ar   c                     |                     |           }t          |t          t          f          r+|d         rt	          |d                   d         n|d         S d S r#   )get
isinstancer$   tupler	   )path
referencesrefs      r   _prot_in_referencesr/   &   s\    
..

C#e}%% ?,/F>~c!f%%a((A>? ?r   c                     t          | t                    rt          | |          | giS i }| D ];}t          ||          }|                    |g                               |           <|S r   )r*   strr/   
setdefaultappend)pathsr-   outr,   protocols        r   _protocol_groupsr7   ,   sy    % A#E:66@@
C 2 2&tZ88x$$++D1111Jr   c                       e Zd Zd ZdS )RefsValuesViewc              #     K   | j         j                                        D ]*}t          j        |                                          V  +| j         j                                        E d {V  | j                                         D ]Z}| j                             |          }|j	        dk    r| j         |dz            V  :| j         
                    |          E d {V  [d S )Nr   /0)_mapping	zmetadatar%   jsondumpsencode_itemslistdir_get_chunk_sizessize_generate_all_records)r   valfieldchunk_sizess       r   __iter__zRefsValuesView.__iter__7   s     =*1133 	+ 	+C*S//((******='..000000000]**,, 	B 	BE-88??K1$$mEDL1111}::5AAAAAAAAAA	B 	Br   Nr   r   r   rI    r   r   r9   r9   6   s(        	B 	B 	B 	B 	Br   r9   c                       e Zd Zd ZdS )RefsItemsViewc                 ~    t          | j                                        | j                                                  S r   )zipr<   keysr%   r   s    r   rI   zRefsItemsView.__iter__D   s.    4=%%'')=)=)?)?@@@r   NrJ   rK   r   r   rM   rM   C   s(        A A A A Ar   rM   c                       e Zd ZdZed             Zed             Z	 	 ddZddZd	 Z	d
 Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )LazyReferenceMapperzVInterface to read parquet store as if it were a standard kerchunk
    references dict.c                     dd l }|S r#   )numpy)r   nps     r   rU   zLazyReferenceMapper.npM   s    	r   c                     dd l }|S r#   )pandas)r   pds     r   rX   zLazyReferenceMapper.pdS   s    	r   N   c                 >    | _         i  _        i  _        d _        |t	          j        d          n| _         j                            d                     j         dg          d          5 }|	                                 j        d<   ddd           n# 1 swxY w Y   t          j         j        d                   }|d          _        |d          _         j         r j         dz    _        nd	 _        t          |
           fd            }| _        dS )a  
        Parameters
        ----------
        root : str
            Root of parquet store
        fs : fsspec.AbstractFileSystem
            fsspec filesystem object, default is local filesystem.
        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.
        Nfile/z
.zmetadatarbrecord_sizemetadataz/{field}/refs.{record}.parqz{field}/refs.{record}.parq)maxsizec                    j                             | |          }j                            |          5 }j                            |d          d d d            n# 1 swxY w Y   fdj        D             }|fS )N)rG   recordfastparquet)enginec                 ,    i | ]}||         j         S rK   )r%   ).0cdfs     r   
<dictcomp>zCLazyReferenceMapper.__init__.<locals>.open_refs.<locals>.<dictcomp>   s!    888Ar!u|888r   )urlformatfsr   rX   read_parquetcolumns)rG   rb   r,   frefsrh   r   s        @r   	open_refsz/LazyReferenceMapper.__init__.<locals>.open_refsz   s    8??v?>>Dd## CqW))!M)BBC C C C C C C C C C C C C C C8888RZ888D t8Os   A""A&)A&)rootrH   rA   dirsfsspecr   rl   r   joinreadr>   loadsr^   r=   rj   r   rq   )r   rr   rl   
cache_sizero   metrq   s   `      r   r   zLazyReferenceMapper.__init__Y   sZ   " 		/1z&#F+++rW\\#((DI|#<==tDD 	1()DK%	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1j\233}-Z9 	4y#@@DHH3DH 
:	&	&	&	 	 	 	 
'	&	 #s   0BB BTc                     | j         <d | j        D             }t          t          d |D                                 | _         | j         }|rd |D             }|S )Nc                 F    g | ]}|                     d d          d         S )r\      r   )splitrf   ps     r   
<listcomp>z/LazyReferenceMapper.listdir.<locals>.<listcomp>   s)    ???1AGGCOOA&???r   c              3   H   K   | ]}||                     d           |V  dS ).N
startswithr~   s     r   	<genexpr>z.LazyReferenceMapper.listdir.<locals>.<genexpr>   s8      "R"Ra"RS@Q@Q"R1"R"R"R"R"R"Rr   c                 L    g | ]!}t           j                            |          "S rK   )osr,   basename)rf   r,   s     r   r   z/LazyReferenceMapper.listdir.<locals>.<listcomp>   s(    BBB$rw''--BBBr   )rs   r=   setsorted)r   r   rs   listings       r   rB   zLazyReferenceMapper.listdir   sm    9?????DF"R"Rd"R"R"RRRSSDI) 	CBB'BBBGr   c                 R   || j         v r| j         |         S || j        v r1t          j        | j        |                                                   S d|vrt          |          |                    d          \  }}	 |                     |          \  }}|dk    rdS |                     ||          \  }n*# t          t          t          f$ r t          |          w xY wg d}fd|D             }|d         }	|	|	S |d d         }
|
dd          ddgk    r
|
d d         }
|
S )Nr\   r   r   )r,   offsetrD   rawc                 8    g | ]}|v r|                  nd S r   rK   )rf   rg   rp   ris     r   r   z5LazyReferenceMapper._load_one_key.<locals>.<listcomp>   s-    IIIAAIIT!WR[[4IIIr   r|   )rA   r=   r>   r?   r@   KeyErrorr}   _key_to_recordrq   
ValueError	TypeErrorFileNotFoundError)r   keyrG   sub_keyrb   
chunk_size_rn   	selectionr   datarp   r   s              @@r   _load_one_keyz!LazyReferenceMapper._load_one_key   s[   $+;s##DN"":dnS12299;;;^^3--3w	 %)%8%8%=%="FB
QsnnUF33GAttI'89 	  	  	 3--	 333IIIIIIII	m?J"~81v8Ds   ?B:  B: :'C!c                 V   |                     d          \  }}|                     |          }|j        dk    rdS | j                            d |                     d          D                       }| j                            ||          }|| j        z  }|| j        z  }|||j        fS )Nr\   r   )r   r   r   c                 ,    g | ]}t          |          S rK   )intrf   rg   s     r   r   z6LazyReferenceMapper._key_to_record.<locals>.<listcomp>   s    "D"D"Da3q66"D"D"Dr   r   )r}   rC   rD   rU   arrayravel_multi_indexr^   )	r   r   rG   chunkrH   	chunk_idxchunk_numberrb   r   s	            r   r   z"LazyReferenceMapper._key_to_record   s    yy~~u++E22q  7GMM"D"D5;;s3C3C"D"D"DEE	w00KHH!11D,,r;+++r   c                 D   || j         vr| j        | d         }| j                            |d                   | j                            |d                   z  }| j                            |                              t                    | j         |<   | j         |         S )Nz/.zarrayshapechunks)rH   r=   rU   r   ceilastyper   )r   rG   zarray
size_ratios       r   rC   z$LazyReferenceMapper._get_chunk_sizes   s    (((^u$6$6$67Fvg77$'--x ; ; J '+gll:&>&>&E&Ec&J&JDU#&&r   c                     |                      ||          \  }}|                    d d          }|j        j        dk    rd |D             S |j        j        dk    rd |D             S d |D             S )NF)nameindex   c              3   4   K   | ]}t          |          V  d S r   r$   rf   ts     r   r   z7LazyReferenceMapper._generate_record.<locals>.<genexpr>   s(      ((DGG((((((r   r|   c              3   &   K   | ]}|d          V  dS )r   NrK   r   s     r   r   z7LazyReferenceMapper._generate_record.<locals>.<genexpr>   s&      %%QAaD%%%%%%r   c              3   d   K   | ]+}|d          st          |dd                    n|d          V  ,dS )r   Nr   r   s     r   r   z7LazyReferenceMapper._generate_record.<locals>.<genexpr>   sA      BB!qt5D2A2KKK1BBBBBBr   )rq   
itertuplesrn   rD   )r   rG   rb   rh   r   its         r   _generate_recordz$LazyReferenceMapper._generate_record   s    uf--A]]E]22:?a((R((((Z_!!%%"%%%% CBrBBBBr   c              #     K   |                      |          }t          | j                            | j                            |          | j        z                      }t          |          D ]}|                     ||          E d {V  d S r   )rC   r   rU   r   productr^   ranger   )r   rG   r   nrecrb   s        r   rE   z)LazyReferenceMapper._generate_all_records   s      **511
47<<
 ; ;d>N NOOPPDkk 	< 	<F,,UF;;;;;;;;;;	< 	<r   c                      t          |           S r   )r9   r   s    r   r%   zLazyReferenceMapper.values   s    d###r   c                      t          |           S r   )rM   r   s    r   itemszLazyReferenceMapper.items   s    T"""r   c                     || j         v r| j         |         }|t          || j        v r1t          j        | j        |                                                   S |                     |          S r   )rA   r   r=   r>   r?   r@   r   )r   r   rF   s      r   __getitem__zLazyReferenceMapper.__getitem__   si    $++c"C{$.  :dnS12299;;;!!#&&&r   c                     || j         |<   d S r   )rA   )r   r   values      r   __setitem__zLazyReferenceMapper.__setitem__   s     Cr   c                 f    || j         v r
| j         |= d S || j        v r
| j        |= d S d | j         |<   d S r   )rA   r=   )r   r   s     r   __delitem__zLazyReferenceMapper.__delitem__   sP    $+C   DN""s###  $DKr   c                 2   d}|                                  D ]Q}|                    d          r|dz  }|                     |          }| j                            |          }||z  }R|t          | j                  z  }|t          | j                  z  }|S )Nr   r   r|   )rB   r   rC   rU   r   lenr=   rA   )r   countrG   rH   nchunkss        r   __len__zLazyReferenceMapper.__len__   s    \\^^ 	! 	!E$$ !
"33E::'//+66 T^$$$T[!!!r   c              #   2  K   | j         E d {V  | j        E d {V  |                                 D ]e}|                     |          }|j        dk    r|dz   V  * | j        j        | }|D ])}|dz   d                    d |D                       z   V  *fd S )Nr   r;   r\   r   c                 ,    g | ]}t          |          S rK   )r1   r   s     r   r   z0LazyReferenceMapper.__iter__.<locals>.<listcomp>  s    -B-B-Bc!ff-B-B-Br   )r=   rA   rB   rC   rD   rU   ndindexru   )r   rG   rH   indsinds        r   rI   zLazyReferenceMapper.__iter__   s       >!!!!!!!;\\^^ 	D 	DE//66K1$$dl""""47?K0D D DckCHH-B-Bc-B-B-B$C$CCCCCCD	D 	Dr   )NrY   T)r   r   r   __doc__propertyrU   rX   r   rB   r   r   rC   r   rE   r%   r   r   r   r   r   rI   rK   r   r   rR   rR   H   sA           X
   X 	,# ,# ,# ,#\     4	, 	, 	,' ' 'C C C< < <$ $ $# # #' ' '! ! !$ $ $  D D D D Dr   rR   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$ Z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   NT    @BrY   c                 z    t                      j        d	i | || _        |	| _        |
| _        i | _        i | _        i | _        || _        || _	        t          |t                    rt          d	i |p|pi d|i}t          j        j        |fi |\  }}|                    |          rt|                    |d          5 }t$                              d|           t)          j        |          }ddd           n# 1 swxY w Y   |                     ||	           n.t/          |||          | _        n|                     ||	           t          |t                    rEd |                                D             | _        d| j        vrt5          d          | j        d<   dS |8t          |j        t8                    r|j        d         n|j        }|| j        |<   |{| j                                        D ]a}t=          |          r
 |            }t          j                            |          \  }}|r"|| j        vrt5          |fi |pi }|| j        |<   b|| j                                        D ]}t=          |          r
 |            }t          |t@                    rT|d         rLt          j                            |d                   \  }}|r"|| j        vrt5          |fi |pi }|| j        |<   |r"|| j        vrt5          |fi |pi }|| j        |<   |pt5          d          | j        d<   dS )
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
        r6   r]   zRead reference from URL %sN)rl   rx   c           	          i | ]I\  }}|t          |t                    r,t          j        |                    d d          d         fi |n|JS ):r|   r   )r*   dictrt   r   r}   )rf   koptss      r   ri   z0ReferenceFileSystem.__init__.<locals>.<dictcomp>  sl        At !$--F%aggc1ooa&8AADAAA	  r   r[   r   rK   )!r   r   r   template_overridessimple_templates	templatesfss	_dircachemax_gap	max_blockr*   r1   r   rt   core	url_to_fsisfiler   loggerinfor>   load_process_referencesrR   r-   r   r   r6   r+   r%   callabler	   r$   )r   for   ref_storage_argstarget_protocoltarget_optionsremote_protocolremote_optionsrl   r   r   r   r   rx   kwargsdicref_fsro   textr.   r6   r   r   s                         r   r   zReferenceFileSystem.__init__  s   R 	""6""""4 0"b# 	=  #;~; GV  C  .r99S99JFB}}R   [[T** (aKK <bAAA9Q<<D( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ((/ABBBB #6)# # # $$R);<<<b$ 	   "xxzz  DH 48##!+F!3!3F> #-R[%"@"@QAbk  )+DH_%"~,,.. , ,C==  #%%C$k88==! , 8 8#HGG1E2GGB)+DHX&"--// 0 0C==  #%%Cc4(( 0SV 0"(+"<"<SV"D"DKHa 0HDH$<$<'KK^5IrKK-/* 	+dh>>OFF0D"FFB(*DH_%1z&11s   0C==DDc           
      8   |                      |          }t                              d|            	 | j        |         }n# t          $ r t          |          w xY wt          |t                    r|                                }t          |t                    rTt                              d| d           |
                    d          rt          j        |dd                    }|d d fS t          |          dk    r-t                              d| d|            |d         }||}}n]|\  }}}	t                              d| d	| d
| d|	            ||	z   }
||dk    r||z   }n|
|z   }n|}||dk    r||z   }n|
|z   }n|
}|| j        }|||fS )Nzcat: zReference: z, type bytes   base64:   r|   z, whole file => r   z => z	, offset z, size )_strip_protocolr   debugr-   r   r   r*   r1   r@   bytesr   base64	b64decoder   r   )r   r,   startendpartrj   start1end1start0rD   end0s              r   _cat_commonzReferenceFileSystem._cat_common  s   ##D))^T^^$$$	*?4(DD 	* 	* 	*#D)))	*dC   	!;;==DdE"" 	$LL9t999:::z** 2'QRR11t##t99>>LLCtCCTCCDDDq'C #DFF $CLLTtTTTTvTTdTTUUUD=D A::#e^FF!E\FF!88!C<DD#:DD;+CFD  s   A Ac                 >  K   |                      |||          \  }}}t          |t                    r
|||         S t          |          \  }}		 | j        |                             |||           d {V  d S # t          $ r}
t          ||          |
d }
~
ww xY wN)r  r  )r  r*   r   r	   r   	_cat_file	Exceptionr   r   r,   r  r  r   part_or_urlr  r  r6   r   es              r   r  zReferenceFileSystem._cat_file  s      $($4$4TC$4$P$P!VTk5)) 	*uSy))$[11!	B(8$..{%S.QQQQQQQQQQQ 	B 	B 	B'k::A	Bs   )A< <
BBBc                 *   |                      |||          \  }}}t          |t                    r
|||         S t          |          \  }}		 | j        |                             |||          S # t          $ r}
t          ||          |
d }
~
ww xY wr
  )r  r*   r   r	   r   cat_filer  r   r  s              r   r  zReferenceFileSystem.cat_file  s    $($4$4TC$4$P$P!VTk5)) 	*uSy))$[11!	B8H%..{&d.SSS 	B 	B 	B'k::A	Bs   "A2 2
B<BBc                     || j         |<   dS )z2Temporarily add binary data or reference as a fileNr-   )r   r,   r   r   s       r   	pipe_filezReferenceFileSystem.pipe_file  s     %r   c                   K   |                      |          rt          j        |d          S |                     |           d {V }t	          |d          5 }|                    |           d d d            d S # 1 swxY w Y   d S NT)exist_okwb)isdirr   makedirsr  r   write)r   rpathlpathr   r   ro   s         r   	_get_filezReferenceFileSystem._get_file  s      ::e 	5;ut4444^^E********% 	!GGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A<<B B c                    |                      |          rt          j        |d          S  | j        |fi |}|                    t          |                     t          |          r|                    |           n=t          |d          5 }|                    |           d d d            n# 1 swxY w Y   |	                    t          |                     d S r  )
r  r   r  r  set_sizer   r
   r  r   absolute_update)r   r  r  callbackr   r   ro   s          r   get_filezReferenceFileSystem.get_file  s   ::e 	5;ut4444t}U--f--#d))$$$e 	KKeT"" a                T+++++s   B33B7:B7Fc                     |r                      d                                ||          }t          j        dd          }t	          ||          }|r"                      fd|D                       }n                     |          }t          ||          D ]%\  }}	||v r|                    |	||                    &d S )N )	recursiver[   T)
auto_mkdirc                 >    g | ]}                     |          |S rK   )r  )rf   rr   s     r   r   z+ReferenceFileSystem.get.<locals>.<listcomp>  s(    CCC1TZZ]]CQCCCr   )lsexpand_pathrt   r   r   catrO   r  )
r   r  r  r&  r   rl   targetsr   remotelocals
   `         r   r)   zReferenceFileSystem.get  s     	GGBKKK  ) <<v$777eU++ 	#88CCCCCCCDDDD88E??D 00 	2 	2MFE~~UDL111	2 	2r   raisec           	      @   t          |t                    r	|rt          t          |t                    r"|st	          d |D                       rt          t          || j                  }i }|                                D ]\  }}| j        |         }	g g g }}}
|D ]}	 | 	                    |          \  }}}|

                    |           |
                    |           |
                    |           \# t          $ r}|dk    r |dk    r|||<   Y d }~}d }~ww xY wg }g }g }g }t                      }t          |
|||          D ]\  }}}}t          |t                    r|||<   "|i|                    |           |
                    |           |
                    |           |
                    |           |
                    |           t          |
|||          D ]a\  }}}}|X||vrT|
                    |           |
                    |           |
                    |           |
                    |           bt!          t          |          t          |          t          |          d| j        | j                  \  }}}|	                    |||          }t          |
|||          D ]\  }}}}||v rt          ||||          D ]|\  }}}}||k    r-||)t          |t(                    r|||<   ,|||         ||<   :||k    r<||k    r6||k    r0t          |t(                    r|||<   g|||z
  ||z
  pd          ||<   }|                                                                D ]`\  }}t          |t(                    rF|| j        v r=||         } t-          || j        |                   }!| |!_        |dk    r|!|dk    r|!||<   at1          |          dk    r(t          |t                    rd|vrt3          |          S |S )Nc              3      K   | ]}d |v V  	dS )*NrK   r~   s     r   r   z*ReferenceFileSystem.cat.<locals>.<genexpr>  s&      7O7OQq7O7O7O7O7O7Or   r0  omitT)sortr   r   r|   r3  )r*   r1   NotImplementedErrorr$   anyr7   r-   r   r   r  r3   r   r   rO   r   addr   r   r   
cat_rangesr  copyr   	__cause__r   r'   )"r   r,   r&  on_errorr   
proto_dictr5   protor4   rl   urlsstartsendsr   usr  urls2starts2ends2paths2whole_files	new_paths
new_startsnew_ends	bytes_outrU   nsnebr   vexnew_exs"                                     r   r,  zReferenceFileSystem.cat  s   dC   	&Y 	&%%dD!! 	&y 	&C7O7O$7O7O7O4O4O 	&%%%dDO<<
&,,.. D	B D	BLE5%B!#R$&D # #	#"..q11GAq!KKNNNMM!$$$KKNNNN( # # #7**6))!"A	# EGEF%%K!$e<< % %
1aa'' 
%CFFY  OOA&&&LLOOONN1%%%LLOOOMM!$$$!$e<< % %
1a=Qk%9%9LLOOONN1%%%LLOOOMM!$$$ /BUWU./ / /+Iz8 iXFFI "$e<< B B
1a88%(J)%T%T 
B 
BMBBQwwBJ"*%a33 ,%&CFF%&qsVCFFqQ"WWb%a33 B%&CFF%&q2vR0@D'@%ACF
BB HHJJ$$&& 		$ 		$DAq!Y'' $A,@,@V.q$/!2DEE#% w&& L''#CFs88q==Zc22=s$#;;
s   AC88
DDDc                     |                     dd           }||                     |           d S |dk    r|                     ||           d S t          d|           )Nversionr|   )r   z Unknown reference spec version: )r)   _process_references0_process_references1r   )r   r-   r   verss       r   r   z'ReferenceFileSystem._process_referencesq  st    ~~i..<%%j11111QYY%%jEW%XXXXXFFFGGGr   c                     || _         dS )z&Make reference dict for Spec Version 0Nr  )r   r-   s     r   rU  z(ReferenceFileSystem._process_references0|  s    $r   c                      j         r j        rdd li  _                             |                    di                      t          d           fd            }|                    di                                           D ]\  }}t          |t                    rD|
                    d          r$t          j        |dd                     j        |<   | j        |<   ^ j        r|d         }d|v rM j         r; |                    dd	                              d
d          j        di  j        }n ||          }t          |          dk    r|gn||d         |d         g j        |<   | j        |<    j                                                 |                    dg                                d S )Nr   r   i  c                 N                          |           j        di j        S NrK   )Templaterenderr   )rB  jinja2r   s    r   _render_jinjaz?ReferenceFileSystem._process_references1.<locals>._render_jinja  s*    ,6??1%%,>>t~>>>r   rp   zbase64:r   {{{z}}}r|   r   genrK   )r   r   r^  r-   _process_templatesr)   r   r   r*   r1   r   r   r   replacerk   r   update_process_gen)r   r-   r   r_  r   rP  rB  r^  s   `      @r   rV  z(ReferenceFileSystem._process_references1  s   $ 	 	MMM
{B ? ?@@@	4	? 	? 	? 	? 	? 
	? NN62..4466 	' 	'DAq!S!! '<<	** A)/)9!ABB%)@)@DOA&%&"" 'aD199, -$AIIdC00$WT3//#6 6&*n6 6  *M!,,,/FFaKKaSSa1qt_""%&""t00r1J1JKKLLLLLr   c                     i | _         | j        |                    | j                   |                                D ]'\  }}d|v rdd l|ffd	| j         |<   || j         |<   (d S )Nr`  r   c                 D                          |           j        di |S r[  )r\  r]  )tempr   r^  s     r   <lambda>z8ReferenceFileSystem._process_templates.<locals>.<lambda>  s7     >V__> >># >#!># ># r   )r   r   rf  r   r^  )r   tmpr   rP  r^  s       @r   rd  z&ReferenceFileSystem._process_templates  s    ".JJt.///IIKK 	& 	&DAqqyy01 %# %# %# %# %#q!! %&q!!	& 	&r   c           	         i }|D ][}d |d                                          D             fdt          j                                         D             }|D ]}dd l} |                    |d                   j        d
i || j        } |                    |d                   j        d
i || j        }d|v rd|v r}t           |                    |d                   j        d
i || j                  }	t           |                    |d                   j        d
i || j                  }
||	|
g||<   d|v d|v z  rt          d	          |g||<   ]|S )Nc                     i | ]\\  }}|t          |t                    r|n>t          |                    d d          |d         |                    dd                    ]S )r  r   stopstepr|   )r*   r$   r   r)   )rf   r   rP  s      r   ri   z4ReferenceFileSystem._process_gen.<locals>.<dictcomp>  st        Aq a&&K11155!,,aivq9I9IJJ  r   
dimensionsc              3   v   K   | ]3}t          t                                          |                    V  4d S r   )r   rO   rP   )rf   r%   	dimensions     r   r   z3ReferenceFileSystem._process_gen.<locals>.<genexpr>  sS         S))62233     r   r   r   rj   r   lengthz^Both 'offset' and 'length' are required for a reference generator entry if either is provided.rK   )
r   	itertoolsr   r%   r^  r\  r]  r   r   r   )r   gensr5   rc  productsprr^  r   rj   r   rt  rs  s              @r   rg  z ReferenceFileSystem._process_gen  s    	% 	%C   -3355	  I   '/1A1A1C1CD  H  % %8fooc%j118PP2PPP8fooc%j118PP2PPPOO(c// =H66=UUUdnUU F !=H66=UUUdnUU F !$VV4CHH#o(c/: %$K  
 !$uCHH'%( 
r   c                    dg i| _         | j                                        }|D ]\  }}t          |t          t
          f          rt          |          }nt          |          dk    rd }n|\  }}}d|v r|                    dd          d         nd}|}|r|| j         vr|g | j         |<   | j                             d|v r|                    dd          d         ndg           	                    |ddd           | 
                    |          }|r	|| j         v|| j         |         	                    |d|d           d S )Nr%  r|   r\   r   	directoryr   typerD   r[   )dircacher-   r   r*   r   r1   r   rsplitr2   r3   _parent)	r   r   r,   r  rD   r   r  parpar0s	            r   _dircache_from_itemsz(ReferenceFileSystem._dircache_from_items  st   R_""$$ 	T 	TJD$$-- &4yyTa!%5$,/4KK$++c1%%a((RCD *4t}44&(d#((.1TkkDKKQ''**r2 &$QGGHHH||D))  *4t}44 M#%%tVT&R&RSSSS#	T 	Tr   r]   c                 T    |                      |          }t          j        |          S r   )r  ioBytesIO)r   r,   mode
block_sizecache_optionsr   r   s          r   _openzReferenceFileSystem._open  s#    }}T""z$r   c                     |                      |          }| j        s|                                  |                     |          }|t	          |          |r|S d |D             S )Nc                     g | ]
}|d          S r   rK   )rf   os     r   r   z*ReferenceFileSystem.ls.<locals>.<listcomp>  s    '''a&	'''r   )r   r}  r  _ls_from_cacher   )r   r,   detailr   r5   s        r   r*  zReferenceFileSystem.ls  sx    ##D))} 	(%%'''!!$'';#D))) 	J''3''''r   c                 V    |                      |          p|                     |          S r   )r  r   r   r,   r   s      r   existszReferenceFileSystem.exists  s%    zz$44;;t#4#44r   c                 d    | j         r	| j         v S t          fd| j        D                       S )Nc              3   H   K   | ]}|                      d           V  dS )r\   Nr   )rf   r   r,   s     r   r   z,ReferenceFileSystem.isdir.<locals>.<genexpr>   s5      IIAq||tJJJ//IIIIIIr   )r}  r7  r-   r   r,   s    `r   r  zReferenceFileSystem.isdir  sB    = 	J4=(( IIIIIIIIIIr   c                     || j         v S r   r  r  s     r   r   zReferenceFileSystem.isfile  s    t&&r   c                 $   K    | j         ||fi |S r   )r*  )r   r,   r  r   s       r   _lszReferenceFileSystem._ls  s"      twtV..v...r   c                 :    |r t                      j        f|||d|S r6                               t          fd j        D                       }nt           j                  }|r) j        s                                   fd|D             S |S )N)maxdepthwithdirsr  c              3   F   K   | ]}|                               |V  d S r   r   )rf   r   r,   s     r   r   z+ReferenceFileSystem.find.<locals>.<genexpr>  s4      HHQQ\\$5G5GHqHHHHHHr   c                 H    i | ]}|                     |          d          S )r   )r  )rf   r   r   s     r   ri   z,ReferenceFileSystem.find.<locals>.<dictcomp>  s.    <<<QAt**1--a0<<<r   )r   findr   r   r-   r}  r  )r   r,   r  r  r  r   r)  r   s   ``     r   r  zReferenceFileSystem.find  s     	577<'(6 MS    	(''--DHHHH$/HHHHHAAt''A 	= ,))+++<<<<!<<<<Hr   c                    | j         v rc| j                  }t          |t          t          f          rdt	          |          dS t	          |          dk    rd|d         dS dd dg}n,|                     d          }fd|D             }|sdddS |d         d	         ]t          | j                  d                   \  }}| j        |                             | j                  d                   |d         d	<   |d         S )
Nr[   r{  r|   r   Tc                 ,    g | ]}|d          k    |S r  rK   )rf   r  r,   s     r   r   z,ReferenceFileSystem.info.<locals>.<listcomp>&  s'    888!ai4&7&7A&7&7&7r   rz  r   rD   )	r-   r*   r1   r   r   r*  r	   r   rD   )r   r,   r   r5   out0protr   s    `     r   r   zReferenceFileSystem.info  s&   4?""/$'C#U|,, F $fc#hhGGGSA $fc!fEEE!%vtDDE''$%%C8888s888D F $k1EEE76?"$T_T%:1%=>>GD!"htn11$/$2G2JKKDGFOAwr   c                 0   K   |                      |          S r   )r   r  s      r   _infozReferenceFileSystem._info/  s      yyr   c                 r   K   | j                             |d            | j                                         d S r   )r-   popr}  clearr  s      r   _rm_filezReferenceFileSystem._rm_file2  sA      $	
 	
 	
 	r   c                 P   K   || j         |<   | j                                         d S r   )r-   r}  r  )r   r,   r   s      r   
_pipe_filezReferenceFileSystem._pipe_file8  s,       $r   c                    K   t          |d          5 }|                                | j        |<   d d d            n# 1 swxY w Y   | j                                         d S )Nr]   )r   rv   r-   r}  r  )r   r  r  ro   s       r   	_put_filezReferenceFileSystem._put_file=  s      % 	.!%&VVXXDOE"	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   <A A c                    i }| j                                         D ]u\  }}t          |t                    rV	 |                    d          ||<   4# t
          $ r/ dt          j        |          z                                   ||<   Y lw xY w|||<   vt          j	        |dfi |5 }|
                    t          j        d|d                                                     ddd           dS # 1 swxY w Y   dS )z+Write modified references into new locationasciir   r  r|   )rT  rp   N)r-   r   r*   r   decodeUnicodeDecodeErrorr   	b64encodert   r   r  r>   r?   r@   )r   rj   storage_optionsr5   r   rP  ro   s          r   	save_jsonzReferenceFileSystem.save_jsonC  sj   O))++ 	 	DAq!U## IXXg..CFF) I I I(6+;A+>+>>FFHHCFFFI A[d66o66 	F!GGDJ1c::;;BBDDEEE	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	Fs#   A6B	B	%=C//C36C3)NNNNNNNNTr   r   rY   )NN)F)Fr0  r   )r]   NNr   )NFF)#r   r   r   r   r6   r   r  r  r  r  r  r   r#  r)   r,  r   rU  rV  rd  rg  r  r  r*  r  r  r   r  r  r   r  r  r  r  r  r    r!   s   @r   r   r     s         H
 R2 R2 R2 R2 R2 R2h(! (! (! (!TB B B BB B B B& & &   /@ 
, 
, 
, 
,2 2 2 2Z Z Z ZxH H H H% % %M M M M>& & &" " "HT T T,       	( 	( 	( 	(5 5 5J J J' ' '/ / / /     $  *      
  F F F F F F Fr   r   )(r   collectionsr  ru  loggingr   	functoolsr   fsspec.corert   ujsonr>   ImportErrorasynr   	callbacksr   r   r   r   r	   utilsr
   r   r   	getLoggerr   RuntimeErrorr   r'   r/   r7   abc
ValuesViewr9   	ItemsViewrM   MutableMappingrR   r   rK   r   r   <module>r     se        				      				             KKKKK # " " " " " ) ) ) ) ) ) 3 3 3 3 3 3 3 3 3 3 @ @ @ @ @ @ @ @ @ @		-	.	.T T T T TL T T T  ? ? ?  
B 
B 
B 
B 
B[_/ 
B 
B 
BA A A A AKO- A A A
@D @D @D @D @D+/8 @D @D @DFD	F D	F D	F D	F D	F/ D	F D	F D	F D	F D	Fs   ) 	55