ó
    pyüif  ã                   ó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       ÚW/home/mande/repo/quber/.venv/lib/python3.13/site-packages/huggingface_hub/utils/_lfs.pyÚ__init__ÚSliceFileObj.__init__C   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__H   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__P   s    € Ø�‰×Ñ˜$×1Ñ1´2·;±;Õ?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.readS   s^   € Ø�i‰i‹kˆØ—)‘)ÓØØŸ9™9 s™?ÐØ�|‰|× Ñ °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Ä   € Ø—‘ˆØ—i‘iÑˆØ”b—k‘k¤2§;¡;Ð/Ó/Ø'-´·±Ó'<�U’^À#Á,ˆFÜ˜¤ FÓ 0Ó1ˆFÜ—[‘[‰FØ”r—{‘{Ó"Ø—l‘l×'Ñ'Ó)ˆGÜ˜™¬#¨f¸G±mÓ*DÓE‰Fä˜}¨V¨HÐ4EÐFÓGÐGØ�|‰|× Ñ  Ó0°4·>±>ÑAÐAr   c              #   ó.   #   • U R                  SS9v •  g 7f)Ni  @ )r%   )r'   r.   s    r   Ú__iter__ÚSliceFileObj.__iter__l   s   é € Ø�i‰i˜/ˆ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õ ð4�cô 4ð /1¯k©kñ B˜3ð B¨ð B¸cõ Bõ+r   r   )rB   r   r   Ú
contextlibr   Útypingr   r   rE   r   r   Ú<module>rH      s%   ðñ  ã 	Û 	Ý -Ý ôW+Ð)õ W+r   