o
    Si                     @   s   d dl mZmZmZmZmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZ d	d
ddZdZG dd deZG dd dZdS )    )AnyDictIteratorOptionalTuple)ArrayTemporalArray)	Recording)CutSet)Features)Image)get_aistore_client)is_module_availableis_valid_url
memory_rawmemory_lilcommemory_pillow)numpy_fileslilcom_filespillow_files)z.tar.gzz.tarz.tgzc                   @   s   e Zd ZdZdS )AISBatchLoaderErrorz-Base exception for AISBatchLoader operations.N)__name__
__module____qualname____doc__ r   r   K/home/ubuntu/.local/lib/python3.10/site-packages/lhotse/ais/batch_loader.pyr      s    r   c                   @   s   e Zd ZdZdddZdedefddZd	ed
eddfddZde	d
eddfddZ
d	ededdfddZede	dee	 fddZdS )AISBatchLoadera  
    Loads all data referenced by a :class:`CutSet` in a single AIStore Get-Batch call.

    The loader optimizes I/O by aggregating all object URLs from a CutSet and requesting
    them together. It offloads archive extraction and data slicing to AIStore, avoiding
    redundant downloads and local decompression.

    Example:
        >>> loader = AISBatchLoader()
        >>> cuts_with_data = loader(cuts)
    returnNc                 C   s    t dstdt \| _}dS )zGInitialize the AISBatchLoader with an AIStore client and batch context.aistorez?Please run 'pip install aistore>=1.17.0' to use AISBatchLoader.N)r   ImportErrorr   client)self_r   r   r   __init__&   s
   zAISBatchLoader.__init__cutsc           
      C   s   |j rtd| j }g }|D ]}| D ]\}}| ||}|||f qq| }|D ]\}}|rAt|\}}	| 	||	 q/|S )a  
        Fetch all data referenced by a CutSet in one AIStore batch operation.

        Args:
            cuts: A non-lazy CutSet representing a single batch of data.

        Returns:
            The same CutSet object with all manifests updated to reference in-memory data.

        Raises:
            ValueError: If the input CutSet is lazy.
            AISBatchLoaderError: For invalid URLs or unsupported storage types.
        zgLazy CutSets cannot be used with AISBatchLoader. Convert to eager via `cuts.to_eager()` before loading.)
is_lazy
ValueErrorr!   batch	iter_data_collect_manifest_urlsappendgetnext_inject_data_into_manifest)
r"   r%   r(   manifest_listcutr#   manifesthas_urlbatch_resultcontentr   r   r   __call__.   s$   
zAISBatchLoader.__call__r1   r(   c                 C   s  t |tr|jD ]}|jdkr| |j|  dS qdS t |trO|j}|jt	vr8t
d|j dtt	  |j d|j }t|rM| || dS dS t |tttfr|jt	vrkt
d|j dtt	  |j d|j }t|r| || dS dS dS )z3Add all URLs referenced in a manifest to the batch.urlTFzUnsupported storage type 'z'. Supported types: /)
isinstancer	   sourcestype_add_url_to_batchsourcer   arraystorage_typeFILE_TO_MEMORY_TYPEr   listkeysstorage_pathstorage_keyr   r   r   r   )r"   r1   r(   r<   inner_arrayobj_pathr   r   r   r*   W   sB   









z%AISBatchLoader._collect_manifest_urlsr6   c                 C   s   ddl m} ||\}}}|r|r|std| d| |}d}|r:||v r:|| d\}	}
}|	| |}}| j||}|j|||d dS )z.Add a single AIStore URL to the batch request.r   )	parse_urlzInvalid object URL: ''Nr7   )archpath)	aistore.sdk.utilsrF   r   _get_archive_extension	partitionr!   bucketaddobject)r"   r6   r(   rF   providerbck_nameobj_namearch_extrH   prefixr#   suffixrL   r   r   r   r;   ~   s   
z AISBatchLoader._add_url_to_batchr4   c                 C   s   t |tr|jD ]}d|_||_qdS t |tr)|j}t|j |_d|_	||_
dS t |tttfr?t|j |_d|_	||_
dS dS )z;Replace manifest storage references with in-memory content.memory N)r8   r	   r9   r:   r<   r   r=   r?   r>   rB   rC   r   r   r   )r"   r1   r4   r<   rD   r   r   r   r.      s   




z)AISBatchLoader._inject_data_into_manifestrQ   c                 C   s   t D ]
}|| v r|  S qdS )zEReturn the supported archive extension if present in the object name.N)ARCHIVE_EXTENSIONS)rQ   extr   r   r   rJ      s
   z%AISBatchLoader._get_archive_extension)r   N)r   r   r   r   r$   r
   r5   r   r*   strr;   bytesr.   staticmethodr   rJ   r   r   r   r   r      s    
)'r   N)typingr   r   r   r   r   lhotse.arrayr   r   lhotse.audio.recordingr	   
lhotse.cutr
   lhotse.features.baser   lhotse.imager   lhotse.serializationr   lhotse.utilsr   r   r?   rW   	Exceptionr   r   r   r   r   r   <module>   s    