o
    2wÖi  ã                   @   sP   d dl Z d dlZd dlZG dd„ dƒZG dd„ de jƒZ					d
dd	„ZdS )é    Nc                   @   s.   e Zd ZdZd
deddfdd„Zddd	„ZdS )ÚHdf5MemoryIssueFixa£  
    Use this class to limit the growing memory use when reading from HDF5 files.

    It should be instantiated within the dataloading worker, i.e., the best place
    is likely inside the PyTorch Dataset class.

    Every time a new batch/example is returned, call ``.update()``.
    Once per ``reset_interval`` updates, this object will close all open HDF5 file
    handles, which seems to limit the memory use.
    éd   Úreset_intervalÚreturnNc                 C   s   d| _ || _d S )Nr   )Úcounterr   )Úselfr   © r   úO/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/lhotse/workarounds.pyÚ__init__   s   
zHdf5MemoryIssueFix.__init__c                 C   sD   ddl m} | jdkr| j| j dkr|ƒ  d| _|  jd7  _d S )Nr   )Úclose_cached_file_handlesé   )Úlhotser   r   r   )r   r   r   r   r	   Úupdate   s
   zHdf5MemoryIssueFix.update)r   )r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úintr
   r   r   r   r   r	   r      s    r   c                   @   s   e Zd ZdZddd„ZdS )ÚAltGzipFilea-  
    This is a workaround for Python's stdlib gzip module
    not implementing gzip decompression correctly...
    Command-line gzip is able to discard "trailing garbage" in gzipped files,
    but Python's gzip is not.

    Original source: https://gist.github.com/nczeczulin/474ffbf6a0ab67276a62
    éÿÿÿÿc              
   C   s¨   g }z0|dk r	 |   ¡ }|sn| |¡ qn|dkr1|   |¡}|s"n|t|ƒ8 }| |¡ |dksW n tyN } z|rCt|ƒ d¡sD‚ W Y d }~nd }~ww d |¡S )Nr   TzNot a gzipped fileó    )Úread1ÚappendÚlenÚOSErrorÚstrÚ
startswithÚjoin)r   ÚsizeÚchunksÚchunkÚer   r   r	   Úread)   s2   
ü

û€ÿ€ÿ
zAltGzipFile.readN)r   )r   r   r   r   r"   r   r   r   r	   r      s    	r   Úrbé	   c                 C   sÄ   d|v rd|v rt d|f ƒ‚n|durt dƒ‚|dur t dƒ‚|dur(t dƒ‚| dd¡}t| tttjfƒr>t| ||ƒ}nt| d	ƒsHt| d
ƒrPtd||| ƒ}nt	dƒ‚d|v r`t
 ||||¡S |S )a  Open a gzip-compressed file in binary or text mode.

    The filename argument can be an actual filename (a str or bytes object), or
    an existing file object to read from or write to.

    The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or "ab" for
    binary mode, or "rt", "wt", "xt" or "at" for text mode. The default mode is
    "rb", and the default compresslevel is 9.

    For binary mode, this function is equivalent to the GzipFile constructor:
    GzipFile(filename, mode, compresslevel). In this case, the encoding, errors
    and newline arguments must not be provided.

    For text mode, a GzipFile object is created, and wrapped in an
    io.TextIOWrapper instance with the specified encoding, error handling
    behavior, and line ending(s).

    Note: This method is copied from Python's 3.7 stdlib, and patched to handle
    "trailing garbage" in gzip files. We could monkey-patch the stdlib version,
    but we imagine that some users prefer third-party libraries like Lhotse
    not to do such things.
    ÚtÚbzInvalid mode: %rNz0Argument 'encoding' not supported in binary modez.Argument 'errors' not supported in binary modez/Argument 'newline' not supported in binary modeÚ r"   Úwritez1filename must be a str or bytes object, or a file)Ú
ValueErrorÚreplaceÚ
isinstancer   ÚbytesÚosÚPathLiker   ÚhasattrÚ	TypeErrorÚioÚTextIOWrapper)ÚfilenameÚmodeÚcompresslevelÚencodingÚerrorsÚnewlineÚgz_modeÚbinary_filer   r   r	   Úgzip_open_robustA   s&   ÿr;   )r#   r$   NNN)Úgzipr1   r-   r   ÚGzipFiler   r;   r   r   r   r	   Ú<module>   s    $ú