
    gu                     N    d Z ddlZddlZddlmZ ddlmZ  G d de          ZdS )zGit LFS related utilities    N)AbstractContextManager)BinaryIOc                   t    e Zd ZdZdededefdZd Zd Zdd	efd
Z	defdZ
ej        fdededefdZd ZdS )SliceFileObja  
    Utility context manager to read a *slice* of a seekable file-like object as a seekable, file-like object.

    This is NOT thread safe

    Inspired by stackoverflow.com/a/29838711/593036

    Credits to @julien-c

    Args:
        fileobj (`BinaryIO`):
            A file-like object to slice. MUST implement `tell()` and `seek()` (and `read()` of course).
            `fileobj` will be reset to its original position when exiting the context manager.
        seek_from (`int`):
            The start of the slice (offset from position 0 in bytes).
        read_limit (`int`):
            The maximum number of bytes to read from the slice.

    Attributes:
        previous_position (`int`):
            The previous position

    Examples:

    Reading 200 bytes with an offset of 128 bytes from a file (ie bytes 128 to 327):
    ```python
    >>> with open("path/to/file", "rb") as file:
    ...     with SliceFileObj(file, seek_from=128, read_limit=200) as fslice:
    ...         fslice.read(...)
    ```

    Reading a file in chunks of 512 bytes
    ```python
    >>> import os
    >>> chunk_size = 512
    >>> file_size = os.getsize("path/to/file")
    >>> with open("path/to/file", "rb") as file:
    ...     for chunk_idx in range(ceil(file_size / chunk_size)):
    ...         with SliceFileObj(file, seek_from=chunk_idx * chunk_size, read_limit=chunk_size) as fslice:
    ...             chunk = fslice.read(...)

    ```
    fileobj	seek_from
read_limitc                 0    || _         || _        || _        d S N)r   r   r	   )selfr   r   r	   s       V/var/www/html/ai-engine/env/lib/python3.11/site-packages/huggingface_hub/utils/_lfs.py__init__zSliceFileObj.__init__D   s    "$    c                 $   | j                                         | _        | j                             dt          j                  }t          | j        || j        z
            | _	        | j                             | j        t          j                   | S )Nr   )r   tell_previous_positionseekosSEEK_ENDminr	   r   _lenioSEEK_SET)r   end_of_streams     r   	__enter__zSliceFileObj.__enter__I   sl    "&,"3"3"5"5))!R[99)GHH	$."+666r   c                 Z    | j                             | j        t          j                   d S r   )r   r   r   r   r   )r   exc_type	exc_value	tracebacks       r   __exit__zSliceFileObj.__exit__Q   s%    $12;?????r   nc                     |                                  }|| j        k    rdS | j        |z
  }| j                            |dk     r|nt	          ||                    }|S )Nr   r   )r   r   r   readr   )r   r"   posremaining_amountdatas        r   r$   zSliceFileObj.readT   s`    iikk$)39s?|  QUU!1!1AGW@X@XYYr   returnc                 D    | j                                         | j        z
  S r   )r   r   r   r   s    r   r   zSliceFileObj.tell\   s    |  ""T^33r   offsetwhencec                    | j         }|| j        z   }|t          j        t          j        fv rE|t          j        k    r||z   n||z   }t          |t          ||                    }t          j        }na|t          j        k    r>| j        	                                }t          ||z
  t          |||z
                      }nt          d| d          | j                            ||          | j         z
  S )Nzwhence value z is not supported)r   r   r   r   r   maxr   SEEK_CURr   r   
ValueErrorr   )r   r+   r,   startendcur_poss         r   r   zSliceFileObj.seek_   s    dibk2;///'-'<'<UV^^#,FFC 0 011F[FFr{""l''))G#fcGm*D*DEEFFFVFFFGGG|  004>AAr   c              #   :   K   |                      d          V  d S )Ni  @ )r"   )r$   r*   s    r   __iter__zSliceFileObj.__iter__m   s&      ii/i*******r   N)r!   )__name__
__module____qualname____doc__r   intr   r   r    r$   r   r   r   r   r5    r   r   r   r      s        * *X% %S %c % % % %
  @ @ @ c    4c 4 4 4 4 /1k B B3 B Bc B B B B+ + + + +r   r   )r9   r   r   
contextlibr   typingr   r   r;   r   r   <module>r>      s       				 				 - - - - - -      W+ W+ W+ W+ W+) W+ W+ W+ W+ W+r   