
    IThu                     F    S r SSKrSSKrSSKJr  SSKJr   " S S\5      rg)zGit LFS related utilities    N)AbstractContextManager)BinaryIOc                       \ rS rSrSrS\S\S\4S jrS rS r	SS	\4S
 jjr
S\4S jr\R                  4S\S\S\4S jjrS rSrg)SliceFileObj   a!  
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                 (    Xl         X l        X0l        g N)r   r	   r
   )selfr   r	   r
   s       R/var/www/auris/envauris/lib/python3.13/site-packages/huggingface_hub/utils/_lfs.py__init__SliceFileObj.__init__D   s    "$    c                 N   U R                   R                  5       U l        U R                   R                  S[        R
                  5      n[        U R                  XR                  -
  5      U l	        U R                   R                  U R                  [        R                  5        U $ )Nr   )r   tell_previous_positionseekosSEEK_ENDminr
   r	   _lenioSEEK_SET)r   end_of_streams     r   	__enter__SliceFileObj.__enter__I   sk    "&,,"3"3"5))!R[[9)GH	$.."++6r   c                 l    U R                   R                  U R                  [        R                  5        g r   )r   r   r   r   r   )r   exc_type	exc_value	tracebacks       r   __exit__SliceFileObj.__exit__Q   s     $112;;?r   nc                     U R                  5       nX R                  :  a  gU R                  U-
  nU R                  R                  US:  a  U5      nU$ [	        X5      5      nU$ )Nr   r   )r   r   r   readr   )r   r%   posremaining_amountdatas        r   r'   SliceFileObj.readT   s^    iik))99s?||  QU!1Y ADA@XYr   returnc                 P    U R                   R                  5       U R                  -
  $ r   )r   r   r	   r   s    r   r   SliceFileObj.tell\   s    ||  "T^^33r   offsetwhencec                    U R                   nX0R                  -   nU[        R                  [        R                  4;   aB  U[        R                  :X  a  X1-   OXA-   n[        U[        X5      5      n[        R                  nOXU[        R                  :X  a5  U R                  R                  5       n[        X5-
  [        XU-
  5      5      nO[        SU S35      eU R                  R                  X5      U R                   -
  $ )Nzwhence value z is not supported)r	   r   r   r   r   maxr   SEEK_CURr   r   
ValueErrorr   )r   r0   r1   startendcur_poss         r   r   SliceFileObj.seek_   s    iibkk2;;//'-'<U^#,FF 01F[[Fr{{"ll'')G#fGm*DEF}VH4EFGG||  04>>AAr   c              #   .   #    U R                  SS9v   g 7f)Ni  @ )r%   )r'   r.   s    r   __iter__SliceFileObj.__iter__m   s     ii/i**s   )r   r   r   r
   r	   N))__name__
__module____qualname____firstlineno____doc__r   intr   r   r#   r'   r   r   r   r   r;   __static_attributes__ r   r   r   r      sq    *X% %S %c %
@c 4c 4 /1kk B3 B Bc B+r   r   )rB   r   r   
contextlibr   typingr   r   rE   r   r   <module>rH      s%      	 	 - W+) W+r   