o
    h?                     @   s  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mZ zd dlZW n eyO   esMd dlZY n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 d d
lmZmZm Z  e!dZ"G dd de#Z$dd Z%dd Z&dd Z'G dd dej(j)Z*G dd dej(j+Z,dd Z-G dd dej(j.Z/G dd deZ0G dd deZ1dS )     N)	lru_cache)chain)TYPE_CHECKINGLiteral)AbstractBufferedFile)AsyncFileSystem)DEFAULT_CALLBACK)
filesystemopensplit_protocol)AsyncFileSystemWrapper)
isfilelikemerge_offset_rangesother_pathszfsspec.referencec                       s$   e Zd Z fddZdd Z  ZS )ReferenceNotReachablec                    s   t  j|  || _|| _d S N)super__init__	referencetarget)selfr   r   args	__class__ t/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/fsspec/implementations/reference.pyr      s   
zReferenceNotReachable.__init__c                 C   s   d| j  d| j S )NzReference "z" failed to fetch target )r   r   r   r   r   r   __str__$      zReferenceNotReachable.__str__)__name__
__module____qualname__r   r   __classcell__r   r   r   r   r      s    r   c                 C   s   t t|  S r   )nextitervalues)dr   r   r   _first(   s   r'   c                 C   sN   | | }t|ttfr#t|d tr%|d rt|d d S |d S d S d S Nr   )get
isinstancelisttuplestrr   )path
referencesrefr   r   r   _prot_in_references,   s   
 r1   c                 C   sH   t | trt| || giS i }| D ]}t||}||g | q|S r   )r*   r-   r1   
setdefaultappend)pathsr/   outr.   protocolr   r   r   _protocol_groups2   s   

r7   c                   @      e Zd Zdd ZdS )RefsValuesViewc                 c   s    | j j D ]
}t| V  q| j j E d H  | j  D ] }| j |}t	|dkr7| j |d  V  q | j 
|E d H  q d S )Nr   /0)_mapping	zmetadatar%   jsondumpsencode_itemslistdir_get_chunk_sizeslen_generate_all_records)r   valfieldchunk_sizesr   r   r   __iter__=   s   zRefsValuesView.__iter__Nr   r    r!   rH   r   r   r   r   r9   <       r9   c                   @   r8   )RefsItemsViewc                 C   s   t | j | j S r   )zipr;   keysr%   r   r   r   r   rH   J   s   zRefsItemsView.__iter__NrI   r   r   r   r   rK   I   rJ   rK   c                 C   sH   d}d}t | d d d |d d d D ]\}}||| 7 }||9 }q|S )Nr      )rL   )idxsizesrE   multisr   r   r   ravel_multi_indexN   s   &
rU   c                   @   s  e Zd ZdZedd Zedd Z						d@d
ed fddZdd Z	dd Z
edAddZe dd ZdBddZdd Zeddd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zed0d1 Zd2d3 ZdCd4d5ZdCd6d7Zd8d9 Zd:d; Zd<d= Z d>d? Z!dS )D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                 C      dd l }|S r(   )numpy)r   npr   r   r   rY   `      zLazyReferenceMapper.npc                 C   rW   r(   )pandas)r   pdr   r   r   r\   f   rZ   zLazyReferenceMapper.pdN   
   fastparquetengine)r_   pyarrowc                 C   s   || _ i | _|| _|| _|| _| j d | _|du rtdn|| _| j	|p(| j | _
ddlm} | jdkr@|ddu rBtddS dS )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_specra   z)engine choice `pyarrow` is not installed.)rootrG   
cat_threshr`   
cache_sizeurlfsspecr	   fsunstrip_protocolout_rootimportlib.utilrc   ImportError)r   rd   ri   rk   rf   categorical_thresholdr`   rc   r   r   r   r   l   s   !zLazyReferenceMapper.__init__c                 C   s"   |dv r|    | j| S t|)N)r@   record_sizer<   )setup__dict__AttributeErrorr   itemr   r   r   __getattr__   s   
zLazyReferenceMapper.__getattr__c                    sj   i  _  jd jdg j d< t j d }|d  _|d  _t	 j
d fdd}| _d S )N/
.zmetadataro   metadata)maxsizec                    sh   j j| |d}tj|}zjj|jd  fdd j	D }W |S  t
y3   d}Y |S w )zcached parquet file loader)rF   record)r`   c                    s   i | ]	}| |   qS r   )to_numpy.0cdfr   r   
<dictcomp>       z@LazyReferenceMapper.setup.<locals>.open_refs.<locals>.<dictcomp>N)rg   formatioBytesIOri   cat_filer\   read_parquetr`   columnsOSError)rF   rz   r.   datarefsr   r   r   	open_refs   s   z,LazyReferenceMapper.setup.<locals>.open_refs)r@   ri   r   joinrd   r=   loadsro   r<   r   rf   r   )r   metr   r   r   r   rp      s   




zLazyReferenceMapper.setup'  c                 K   s   i |d}|du rt jj| fi |pi \}} || r#|j| dd |j| dd |d| dgt	|
  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
        rx   ro   NT	recursiveexist_okrv   rw   )rh   core	url_to_fsexistsrmmakedirspiper   r=   r>   r?   rV   )rd   storage_optionsri   ro   kwargsr   r   r   r   create   s   

 zLazyReferenceMapper.createc                 C   s   dd | j D }t|S )zList top-level directoriesc                 s   s*    | ]}| d s|ddd V  qdS ).zrv   rN   r   N)
startswithrsplitr}   pr   r   r   	<genexpr>   s   ( z.LazyReferenceMapper.listdir.<locals>.<genexpr>)r<   set)r   dirsr   r   r   rA      s   zLazyReferenceMapper.listdir Tc                    sD    d  r d nd }fdd|D }|r]fddtdgdd	 jD d
d	 jD D }|du rC|| t|S dd |D }fdd|D }t|| dd dS  }t fddjD  fddjD  }fdd|D }|}	|du rt	|t	|	 S 
|}
dd t|	|
D }|| S )zShortcut file listingsrv   r   c                    s(   g | ]}|  rd | vr|qS rv   r   lstrip)r}   r&   pathdashr   r   
<listcomp>   s    z*LazyReferenceMapper.ls.<locals>.<listcomp>c                    s(   h | ]}|  rd | vr|qS r   r   )r}   fr   r   r   	<setcomp>   s    z)LazyReferenceMapper.ls.<locals>.<setcomp>rw   c                 s       | ]}|V  qd S r   r   r}   namer   r   r   r          z)LazyReferenceMapper.ls.<locals>.<genexpr>c                 s   r   r   r   r   r   r   r   r      r   Fc                 S   s   g | ]}|d ddqS )	directoryr   r   typesizer   r   r   r   r   r      s    c              	      <   g | ]}|d t | jv rt j| n j| dqS rb   r   rC   r<   r=   r>   r@   r   r   r   r   r          

c                 S   s   | d S )Nr   r   )rT   r   r   r   <lambda>  s    z(LazyReferenceMapper.ls.<locals>.<lambda>keyc                        g | ]}|   d r|qS r   r   r   r.   r   r   r          c                    r   r   r   r   r   r   r   r     r   c              	      r   r   r   r   r   r   r   r   
  r   c                 S   s(   g | ]\}}|d  r|d|d dqS )r   rb   rO   r   r   )r}   r   recr   r   r   r     s    )rstriprA   r   r<   r@   updatesortedr   _keys_in_fieldr+   rD   rL   )r   r.   detaildirnamesr   othersdirinfofileinforF   rM   recsrecinfor   )r.   r   r   r   ls   sN   


	






zLazyReferenceMapper.lsc              
      sP  || j v r
| j | S || jv rt| j|  S d|vs"| |r&t||dd\}}| |\}}| j 	||fi 	d}|du rIt|rM|S |dkrSdS z| 
|| W n tttfyo } zt||d}~ww g d} fdd	|D }	|	d
 }
|
dur|
S |	d du rtd|	dd ddgkr|	dd S |	dd S )zfGet the reference for one key

        Returns bytes, one-element list or three-element list.
        rv   rN   FNr       r.   offsetr   rawc                    s$   g | ]}| v r |  nd qS r   r   r|   r   rir   r   r   >  s   $ z5LazyReferenceMapper._load_one_key.<locals>.<listcomp>rO   z1This reference does not exist or has been deleted   )r@   r<   r=   r>   r?   _is_metaKeyErrorr   _key_to_recordr)   r   
ValueError	TypeErrorFileNotFoundError)r   r   rF   _rz   
chunk_sizemaybeexcr   	selectionr   r   r   r   _load_one_key"  s>   



z!LazyReferenceMapper._load_one_keyi   c           	      C   sj   | dd\}}| |}t|dkrdS dd |dD }t||}|| j }|| j }||t|fS )z3Details needed to construct a reference for one keyrv   rN   r   )r   r   r   c                 S      g | ]}t |qS r   )intr|   r   r   r   r   Q      z6LazyReferenceMapper._key_to_record.<locals>.<listcomp>.)r   rB   rC   splitrU   ro   )	r   r   rF   chunkrG   	chunk_idxchunk_numberrz   r   r   r   r   r   J  s   



z"LazyReferenceMapper._key_to_recordc                 C   sP   || j vr#| j| d }dd t|d |d D }|pdg| j |< | j | S )z6The number of chunks along each axis for a given fieldz/.zarrayc                 S   s   g | ]\}}t || qS r   )mathceil)r}   rT   r~   r   r   r   r   [  s    z8LazyReferenceMapper._get_chunk_sizes.<locals>.<listcomp>shapechunksrN   )rG   r<   rL   )r   rF   zarray
size_ratior   r   r   rB   W  s   

z$LazyReferenceMapper._get_chunk_sizesc                 C   sX   |  ||}tt|  }t|dkrdd |D S t|dkr%|d S dd |D S )z8The references for a given parquet file of a given fieldr   c                 s       | ]}t |V  qd S r   r+   r}   tr   r   r   r   g      z7LazyReferenceMapper._generate_record.<locals>.<genexpr>rN   r   c                 s   s0    | ]}|d  st |dd  n|d  V  qdS )r   Nr   r   r   r   r   r   m  s   . )r   r$   rL   r%   rC   )r   rF   rz   r   itr   r   r   _generate_recorda  s   z$LazyReferenceMapper._generate_recordc                 c   sR    d}|  |D ]}||9 }qt|| j }t|D ]}| ||E dH  qdS )zJLoad all the references within a field by iterating over the parquet filesrN   N)rB   r   r   ro   ranger   )r   rF   nrecchrz   r   r   r   rD   o  s   
z)LazyReferenceMapper._generate_all_recordsc                 C      t | S r   )r9   r   r   r   r   r%   x     zLazyReferenceMapper.valuesc                 C   r   r   )rK   r   r   r   r   items{  r   zLazyReferenceMapper.itemsc                 C   r   r   )idr   r   r   r   __hash__~  r   zLazyReferenceMapper.__hash__c                 C   s
   |  |S r   )r   )r   r   r   r   r   __getitem__     
zLazyReferenceMapper.__getitem__c                 C   s   d|v r7|  |s7|dd\}}| |\}}}| j||fi }|||< t|| jkr5| || d S d S t|drC|	 
 }	nt|trM|
 }	n|}	|	| j|< t|	}
i | j|i |
| j|< d S )Nrv   rN   to_bytes)r   r   r   r@   r2   rC   ro   writehasattrr  decoder*   bytesr=   r   r<   r)   )r   r   valuerF   r   rz   rS   r   subdictrE   	new_valuer   r   r   __setitem__  s    




 zLazyReferenceMapper.__setitem__c                 C   s   |  dpd| v S )Nr   z/.zr   r   r   r   r   r     s   zLazyReferenceMapper._is_metac                 C   s   || j v r| j |= d S || jv r| j|= d S d|v rM| |sM|dd\}}| |\}}}| j ||fi }d ||< t|| jkrK| || d S d S d | j |< d S )Nrv   rN   )	r@   r<   r   r   r   r2   rC   ro   r  )r   r   rF   r   rz   rS   r  r   r   r   __delitem__  s   

zLazyReferenceMapper.__delitem__c                 C   s  dd l }dd l}dd l}| j||f }d}	t|| jk r.z| ||}	W n	 ty-   Y nw |	rA|	d }
|	d }|	d }|	d }n$|j| j|j	dd}
|j
| jd	d}|j
| jd	d}|j| j|j	dd}| D ]U\}}t|trt|
jd
kr|d |
jjvr|
|d }
|d |
|< t|dkr|d ||< |d ||< qi|d u rd |
|< d||< d||< d ||< qi|j|||< qi|j|
|||ddd}|j |j pd | jkr|d d
|d< ddd}ddg}|p| j d| d| d}| jj|p| j d| dd | jdkrddi}n| jdkr#d||d}nt| j d|j|f| j|p:t | jdd ddd| |!  | j"||f d S )Nr   Fr.   r   r   r   O)dtypeint64categoryrN      r   )copyr  utf8)r   r.   rv   z/refs.z.parqTr   ra   write_statisticsr_   )statsobject_encoding	has_nullsz not supportedr   zstd)r`   r   compressionindex)#kerchunk.dfrX   r[   r@   rC   ro   r   r   fullnanzerosr   r*   r+   r-   r  
categoriesadd_categoriesr   	_proc_raw	DataFramer.   countnuniquere   astyperk   ri   mkdirsr`   NotImplementedError
to_parquetgetattrclearpop)r   rF   rz   base_urlr   kerchunkrY   r\   	partitionoriginalr4   offsetsrQ   rawsjr   r   r  r  fndf_backend_kwargsr   r   r   r    s   


	
"

zLazyReferenceMapper.writec                 C   s   t | jD ]}t|tr|\}}| j||||d qt | jD ]}|dkr5d|v r5t| j|| j|< q| j| j	d}| j
  t| | jd< | jd|pU| jdg| jd  | j  dS )zOutput any modified or deleted keys

        Parameters
        ----------
        base_url: str
            Location of the output
        )r+  r   rw   r   r   rv   N)r+   r@   r*   r,   r  r=   r   r*  r<   ro   r)  r>   r?   ri   r   r   rk   r   cache_clear)r   r+  r   thingrF   rz   kr   r   r   r   flush  s,   


zLazyReferenceMapper.flushc                 C   sd   d}|   D ]}|dr|d7 }q|t| |7 }q|t| j7 }|tdd | jD 7 }|S )Nr   r   rN   c                 s   s    | ]
}t |tsd V  qdS )rN   N)r*   r,   r}   r   r   r   r   r   /      z.LazyReferenceMapper.__len__.<locals>.<genexpr>)	rA   r   r   prodrB   rC   r<   sumr@   )r   r"  rF   r   r   r   __len__%  s   

zLazyReferenceMapper.__len__c                 c   sb    t | j}|| j |D ]
}t|tr|V  q|  D ]}| |D ]	}|| v r-|V  q$qd S r   )r   r<   r   r@   r*   r-   rA   r   )r   metasbitrF   r6  r   r   r   rH   2  s   

zLazyReferenceMapper.__iter__c                 C   s&   z|  | W dS  ty   Y dS w )NTF)r   r   rs   r   r   r   __contains__?  s   
z LazyReferenceMapper.__contains__c                 c   sf    |  |}t|dkr|d V  dS tjdd |D  }|D ]}|d ddd	 |D  V  qdS )
z|List key names in given field

        Produces strings like "field/x.y" appropriate from the chunking of the array
        r   r:   Nc                 s   r   r   )r   )r}   rS   r   r   r   r   O  r   z5LazyReferenceMapper._keys_in_field.<locals>.<genexpr>rv   r   c                 S   r   r   )r-   r|   r   r   r   r   Q  r   z6LazyReferenceMapper._keys_in_field.<locals>.<listcomp>)rB   rC   	itertoolsproductr   )r   rF   rG   indsindr   r   r   r   F  s   

 z"LazyReferenceMapper._keys_in_field)NNr]   r^   r_   )NNr   )r   TNN)"r   r    r!   __doc__propertyrY   r\   r   r   ru   rp   staticmethodr   r   rA   r   r   r   rB   r   rD   r%   r   r   r   r
  r   r  r  r7  r<  rH   r?  r   r   r   r   r   rV   W   sR    


0

B(

	


U#rV   c                       s<  e Zd ZdZdZdZ												dD fd	d
	ZdEddZdEddZdEddZ	dd Z
dd ZefddZdFddZdGddZdHddZdd ZdHd d!Zd"d# Zd$d% Zd&d' ZdId)d*ZdJd+d,Zd-d. Zd/d0 Zd1d2 ZdJd3d4ZdK fd5d6	Zd7d8 Zd9d: Zd;d< ZdLd>d?Z dLd@dAZ!dBdC Z"  Z#S )M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). 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   FNT    @Br]   c                    s  t  jdi | || _|	| _|
| _i | _i | _i | _|| _|| _	t
|trtdi |p.|p.i d|i}tjj|fi |\}}||rrtj|dfi |}td| t|}W d   n1 sfw   Y  | ||	 ntd| t|||d| _n| ||	 t
|trdd | D | _d| jvrtd	| jd< dS |durt
|jtr|jd
 n|j}|| j|< |du r| j D ]'}t|r| }tj|\}}|r|| jvrt|fi |pi }|| j|< q|du r-| j D ]6}t|r| }t
|t r,|d
 r,tj|d
 \}}|| jvr,t|fi |p"i }|| j|<  nq|rF|| jvrFt|fi |p>i }|| j|< |pLtd	| jd< | j D ] \}}|j!sit"|| j#d| j|< qU| j#|j#A rtt$dqUd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   rbzRead reference from URL %sNz$Open lazy reference dict from URL %s)ri   rf   c                 S   s>   i | ]\}}|t |trtj|d dd fi |n|qS ):rN   r   )r*   dictrh   r	   r   )r}   r6  optsr   r   r   r     s     z0ReferenceFileSystem.__init__.<locals>.<dictcomp>rb   r   )asynchronouszDReference-FS's target filesystem must have same valueof asynchronousr   )%r   r   r   template_overridessimple_templates	templatesfss	_dircachemax_gap	max_blockr*   r-   rM  rh   r   r   isfiler
   loggerinfor=   load_process_referencesrV   r/   r   r	   r6   r,   r%   callabler   r+   
async_implr   rO  r   )r   for   ref_storage_argstarget_protocoltarget_optionsremote_protocolremote_optionsri   rP  rQ  rU  rV  rf   r   dicref_fsfo2r   textr0   r6   r   r6  r   r   r   r   f  s   I










zReferenceFileSystem.__init__c              
   C   s  |  |}td|  z| j| }W n ty% } zt||d }~ww t|tr/| }t	|dr8|
 }t|trYtd| d |drTt|dd  }|d d fS t|dkrttd| d|  |d	 }||}}nB|\}}	}
td| d
| d|	 d|
  |	|
 }|d ur|d	kr|	| }n|| }n|	}|d ur|d	kr|	| }n|| }n|}|d u r| j}|||fS )Nzcat: r  zReference: z, type bytes   base64:   rN   z, whole file => r   z => z	, offset z, size )_strip_protocolrX  debugr/   r   r   r*   r-   r?   r  r  r  r   base64	b64decoderC   r   )r   r.   startendpartr   rg   start1end1start0r   end0r   r   r   _cat_common  sJ   







"




zReferenceFileSystem._cat_commonc              
      s~   | j |||d\}}}t|tr||| S t|\}}	z| j| j|||dI d H W S  ty> }
 zt|||
d }
~
ww N)rn  ro  )ru  r*   r  r   rS  	_cat_file	Exceptionr   r   r.   rn  ro  r   part_or_urlrs  rt  r6   r   er   r   r   rw  3  s   

zReferenceFileSystem._cat_filec              
   K   sv   | j |||d\}}}t|tr||| S t|\}}	z| j| j|||dW S  ty: }
 zt|||
d }
~
ww rv  )ru  r*   r  r   rS  r   rx  r   ry  r   r   r   r   ?  s   
zReferenceFileSystem.cat_filec                 K   s   || j |< dS )z2Temporarily add binary data or reference as a fileNr/   )r   r.   r  r   r   r   r   	pipe_fileI  s   zReferenceFileSystem.pipe_filec                    sd   |  |rtj|ddS | |I d H }t|d}|| W d    d S 1 s+w   Y  d S NTr   wb)isdirosr   rw  r
   r  )r   rpathlpathr   r   r   r   r   r   	_get_fileM  s   
"zReferenceFileSystem._get_filec                 K   s   |  |rtj|ddS | j|fi |}|t| t|r&|| nt|d}|| W d    n1 s;w   Y  |	t| d S r~  )
r  r  r   r   set_sizerC   r   r  r
   absolute_update)r   r  r  callbackr   r   r   r   r   r   get_fileT  s   
zReferenceFileSystem.get_filec           
         s   |r  d  j||d}tjddd}t||}|r)  fdd|D }n |}t||D ]\}}	||v rC||	||  q3d S )Nr   r   rb   T)
auto_mkdirc                    s   g | ]	}  |s|qS r   )r  )r}   rr   r   r   r   h  r   z+ReferenceFileSystem.get.<locals>.<listcomp>)r   expand_pathrh   r	   r   catrL   r}  )
r   r  r  r   r   ri   targetsr   remotelocalr   r   r   r)   `  s   


zReferenceFileSystem.getraisec           $      K   s  t |tr	|r	tt |tr|stdd |D rtt|| j}i }| D ]Y\}}| j| }	g g g g f\}
}}}|D ]I}z| 	|\}}}t |t
tfsQW q=W n tyq } z|dkr_ |dkrg|||< W Y d }~q=d }~ww |
| || || || q=g }g }g }g }t }t|
|||D ]-\}}}}t |t
r|||< q|d u r|| || || || || qt|
|||D ]"\}}}}|d ur||vr|| || || || qtt|t|t|d| j| jd\}}}|	|||}t|
|||D ]l\}}}}||v r!qt||||D ]V\}}}}||krS|d u s=|d u rSt |trI|||< q(||| ||< q(||kr}||kr}||kr}t |trn|||< q(||| || pxd  ||< q(qq'|  D ]0\} }!t |!tr| | jv r||  }"t| | j|  }#|"|#_|dkr|#|dkr|#|| < qt|dkrt |trd|vrt|S |S )	Nc                 s   s    | ]}d |v V  qdS )*Nr   r   r   r   r   r   r  r   z*ReferenceFileSystem.cat.<locals>.<genexpr>r  omitT)sortrU  rV  rN   r  )r*   r-   r&  r+   anyr7   r/   r   rS  ru  r  r   r3   r   rL   addr   rU  rV  
cat_rangesrx  r  r   	__cause__rC   r'   )$r   r.   r   on_errorr   
proto_dictr5   protor4   ri   urlsstartsendsvalid_pathsr   urT   r{  errurls2starts2ends2paths2whole_files	new_paths
new_startsnew_ends	bytes_outrY   nsnebr6  vexnew_exr   r   r   r  o  s    

















$zReferenceFileSystem.catc                 C   sJ   | dd }|d u r| | d S |dkr| j||d d S td| )NversionrN   )rP  z Unknown reference spec version: )r)   _process_references0_process_references1r   )r   r/   rP  versr   r   r   r[    s   z'ReferenceFileSystem._process_referencesc                 C   s&   t |trdd | D }|| _dS )z&Make reference dict for Spec Version 0c                 S   s*   i | ]\}}|t |trt|n|qS r   )r*   rM  r=   r>   )r}   r   rE   r   r   r   r     s    z<ReferenceFileSystem._process_references0.<locals>.<dictcomp>N)r*   rM  r   r/   )r   r/   r   r   r   r    s
   

z(ReferenceFileSystem._process_references0c                    sL  j rjr
dd l i _|di  td fdd}|di  D ]m\}}t|t	rI|
drCt|dd  j|< |j|< q)t|trWt|j|< q)jr|d }d	|v r{j rw|d	d
ddjdi j}n||}t|dkr|gn||d |d gj|< q)|j|< q)j|dg  d S )Nr   rR  i  c                    s     | jdi jS Nr   )TemplaterenderrR  )r  jinja2r   r   r   _render_jinja  s   z?ReferenceFileSystem._process_references1.<locals>._render_jinjar   zbase64:ri  {{{z}}}rN   r  genr   )rQ  rR  r  r/   _process_templatesr)   r   r   r*   r-   r   rl  rm  rM  r=   r>   replacer   rC   r   _process_gen)r   r/   rP  r  r6  r  r  r   r  r   r    s8   



,z(ReferenceFileSystem._process_references1c                    sd   i | _ | jd ur|| j | D ]\}}d|v r*dd l |f fdd	| j |< q|| j |< qd S )Nr  r   c                    s     | jdi |S r  )r  r  )tempr   r  r   r   r     s    z8ReferenceFileSystem._process_templates.<locals>.<lambda>)rR  rP  r   r   r  )r   tmpr6  r  r   r  r   r    s   
z&ReferenceFileSystem._process_templatesc                    s   i }|D ]}dd |d   D   fddtj   D }|D ]k}dd l}||d jdi || j}||d jdi || j}d	|v r{d
|v r{t||d	 jdi || j}	t||d
 jdi || j}
||	|
g||< q!d	|v d
|v A rt	d|g||< q!q|S )Nc              
   S   s@   i | ]\}}|t |tr|nt|d d|d |ddqS )rn  r   stopsteprN   )r*   r+   r   r)   )r}   r6  r  r   r   r   r     s    z4ReferenceFileSystem._process_gen.<locals>.<dictcomp>
dimensionsc                 3   s"    | ]}t t  |V  qd S r   )rM  rL   rM   )r}   r%   	dimensionr   r   r   !  s
    
z3ReferenceFileSystem._process_gen.<locals>.<genexpr>r   r   rg   r   lengthz^Both 'offset' and 'length' are required for a reference generator entry if either is provided.r   )
r   r@  rA  r%   r  r  r  rR  r   r   )r   gensr5   r  productsprr  r   rg   r   r  r   r  r   r    s6   

  z ReferenceFileSystem._process_genc                 C   s4  dg i| _ | j }|D ]\}}t|ttfst|dr!t|}nt|dkr*d }n|\}}}d|v r;|ddd nd}|}|g}|rZ|| j vrZ| 	|}|
| |rZ|| j vsI|  t||dd  D ]#\}	}
|
| j vsrJ |	| j v syJ | j |	 
|
ddd g | j |
< qg| j | 
|d|d qd S )	Nr   r  rN   rv   r   r   r   rb   )dircacher/   r   r*   r  r-   r  rC   r   _parentr3   reverserL   )r   r   r.   rp  r   r   parpar0subdirsparentchildr   r   r   _dircache_from_items;  s2   







z(ReferenceFileSystem._dircache_from_itemsrK  c                 K   s   |  |\}}}t|trt||| S t|\}	}
|d u r4|d u r4| j|	 j||f||d|S t| ||f||d|S )N)
block_sizecache_options)	ru  r*   r  r   r   r   rS  _openReferenceFile)r   r.   moder  r  r   rz  rs  rt  r6   r   r   r   r   r  Y  s0   

zReferenceFileSystem._openc                 K   s   t d| | |}t| jtr+z| j||W S  ty"   Y nw td| d| j	s2| 
  | |}|d u r?t||rC|S dd |D S )Nzlist %s'z' is not a known keyc                 S   s   g | ]}|d  qS r   r   r}   or   r   r   r     r   z*ReferenceFileSystem.ls.<locals>.<listcomp>)rX  rk  rj  r*   r/   rV   r   r   r   r  r  _ls_from_cache)r   r.   r   r   r5   r   r   r   r   s  s"   

zReferenceFileSystem.lsc                 K   s   |  |p	| |S r   )r  rW  r   r.   r   r   r   r   r     r   zReferenceFileSystem.existsc                    sB   | j r | j v S t| jtr | j v S t fdd| jD S )Nc                 3   s     | ]}|   d V  qdS )rv   Nr   r8  r   r   r   r     s    z,ReferenceFileSystem.isdir.<locals>.<genexpr>)r  r*   r/   rV   rA   r  r   r.   r   r   r   r    s
   
zReferenceFileSystem.isdirc                 C   s
   || j v S r   r|  r  r   r   r   rW    r  zReferenceFileSystem.isfilec                    s   | j ||fi |S r   )r   )r   r.   r   r   r   r   r   _ls  s   zReferenceFileSystem._lsc                    sz   |rt  j f|||d|S  r$  t fddjD }ntj}|r;js2  fdd|D S |S )N)maxdepthwithdirsr   c                 3   s    | ]
}|  r|V  qd S r   r   r}   r6  r   r   r   r     r9  z+ReferenceFileSystem.find.<locals>.<genexpr>c                    s   i | ]
}|  |d  qS )r   )r  r  r   r   r   r         z,ReferenceFileSystem.find.<locals>.<dictcomp>)r   findrj  r   r/   r  r  )r   r.   r  r  r   r   r  r   )r.   r   r   r    s"   

zReferenceFileSystem.findc                    s   | j  }|d ur/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|D }|sF dd	dS |d	 d
 d u rjt| j   d	 \}}| j| 	| j   d	 |d	 d
< |d	 S )Nrb   r   rN   r  Tc                    s   g | ]
}|d   kr|qS r  r   r  r   r   r   r     r  z,ReferenceFileSystem.info.<locals>.<listcomp>r   r   r   )
r/   r)   r*   r-   r  rC   r   r   rS  r   )r   r.   r   r5   out0protr   r   r   r   rY    s   "zReferenceFileSystem.infoc                    s   |  |S r   )rY  r  r   r   r   _info  s   
zReferenceFileSystem._infoc                    s   | j |d  | j  d S r   )r/   r*  r  r)  r  r   r   r   _rm_file  s
   zReferenceFileSystem._rm_file	overwritec                    s0   |dkr|  |rt|| j|< | j  d S )Nr   )r   FileExistsErrorr/   r  r)  )r   r.   r   r  r   r   r   r   
_pipe_file  s
   
zReferenceFileSystem._pipe_filec                    s^   |dkr|  |rtt|d}| | j|< W d    n1 s#w   Y  | j  d S )Nr   rK  )r   r  r
   readr/   r  r)  )r   r  r  r  r   r   r   r   r   	_put_file  s   zReferenceFileSystem._put_filec              	   K   s   i }| j  D ]+\}}t|tr.z	|d||< W q ty-   dt|  ||< Y qw |||< qtj	|dfi |}|
td|d  W d   dS 1 sVw   Y  dS )z+Write modified references into new locationasciirh  r  rN   )r  r   N)r/   r   r*   r  r  UnicodeDecodeErrorrl  	b64encoderh   r
   r  r=   r>   r?   )r   rg   r   r5   r6  r  r   r   r   r   	save_json  s   

"zReferenceFileSystem.save_json)NNNNNNNNTrI  rJ  r]   rD  )F)Fr  r   )rK  NN)T)NFF)r  )$r   r    r!   rE  r6   cachabler   ru  rw  r   r}  r  r   r  r)   r  r[  r  r  r  r  r  r  r   r   r  rW  r  r  rY  r  r  r  r  r  r"   r   r   r   r   rH  T  sZ     
"
,




b
!%





rH  c                       sJ   e Zd Z						d fdd	Zedd	 Z fd
dZdd Z  ZS )r  rK  defaultT	readaheadNc	              	      sf   t  j||f||||||d|	 | j| j\}
| _| _t|
\}}| jj| | _	|
| _
d | _d S )N)r  r  
autocommitr   
cache_typer  )r   r   ri   ru  r.   rn  ro  r   rS  src_fssrc_path_f)r   ri   r.   r  r  r  r  r  r   r   rz  r6   r   r   r   r   r     s$   	
zReferenceFile.__init__c                 C   sB   | j d u s	| j jr| jj| jf| j| j| jdd| j| _ | j S )Nnone)r  r  r  r  )	r  closedr  r  r  r  	blocksizer  r   r   r   r   r   r     s   zReferenceFile.fc                    s   | j d ur
| j   t  S r   )r  closer   r   r   r   r   r     s   


zReferenceFile.closec                 C   s8   || j  }t|| j  | j}| j| | j|| S r   )rn  minro  r   seekr  )r   rn  ro  r   r   r   _fetch_range  s   
zReferenceFile._fetch_range)rK  r  Tr  NN)	r   r    r!   r   rF  r   r   r  r"   r   r   r   r   r    s    
r  )2rl  collectionsr   r@  loggingr   r  	functoolsr   r   typingr   r   fsspec.corerh   fsspec.specr   ujsonr=   rm   fsspec.asynr   fsspec.callbacksr   r	   r
   r   #fsspec.implementations.asyn_wrapperr   fsspec.utilsr   r   r   	getLoggerrX  RuntimeErrorr   r'   r1   r7   abc
ValuesViewr9   	ItemsViewrK   rU   MutableMappingrV   rH  r  r   r   r   r   <module>   sX    


	         