
    j                    r    d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
  G d d          Z	 	 	 	 dddZdS )zBUpload utilities for Ultralytics, mirroring downloads.py patterns.    )annotationsN)Path)sleep)LOGGERTQDMc                  ,    e Zd ZdZd ZddZd Zd ZdS )	_ProgressReaderz>File wrapper that reports read progress for upload monitoring.c                    t          |d          | _        || _        t          j                            |          | _        d S )Nrb)openfilepbarospathgetsize_size)self	file_pathr   s      ^/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/utils/uploads.py__init__z_ProgressReader.__init__   s3    D))		W__Y//


    c                    | j                             |          }|r.| j        r'| j                            t	          |                     |S )z"Read data and update progress bar.)r   readr   updatelen)r   sizedatas      r   r   z_ProgressReader.read   sG    y~~d## 	(DI 	(ISYY'''r   c                    | j         S )z+Return file size for Content-Length header.)r   r   s    r   __len__z_ProgressReader.__len__   s
    zr   c                8    | j                                          dS )zClose the file.N)r   closer    s    r   r#   z_ProgressReader.close    s    	r   N)r   )__name__
__module____qualname____doc__r   r   r!   r#    r   r   r	   r	      s[        HH0 0 0
         r   r	      X  Fr   
str | Pathurlstrheadersdict | Noneretryinttimeoutprogressboolreturnc                   ddl }t          |           } |                                 st          d|            |                                 j        }d| j         }ddi}	|r|	                    |           d}
t          |dz             D ]L}d}d}	 |rt          ||dd	d
          }t          | |          }|                    |||	|          }|                                 |                                 d}|r|                                 d}t          j        d| j         d           	 |r|                                 |r|                                  d	S  d	S # |j        j        $ r}|j        |j        j        nd}d|cxk    rdk     rmn nj|dvrft          j        | d| dt+          |j        dd                      Y d}~|r|                                 |r|                                  dS  dS d| }
Y d}~n)d}~wt,          $ r}t/          |          }
Y d}~nd}~ww xY w|r|                                 |r|                                 n2# |r|                                 |r|                                 w w xY w||k     r=d|dz   z  }t          j        | d|
 d|dz    d| d| d
           t1          |           Nt          j        | d|dz    d|
            dS ) a  Upload a file to a URL with retry logic and optional progress bar.

    Args:
        file (str | Path): Path to the file to upload.
        url (str): The URL endpoint to upload the file to (e.g., signed GCS URL).
        headers (dict, optional): Additional headers to include in the request.
        retry (int, optional): Number of retry attempts on failure (default: 2 for 3 total attempts).
        timeout (int, optional): Request timeout in seconds.
        progress (bool, optional): Whether to display a progress bar during upload.

    Returns:
        (bool): True if upload succeeded, False otherwise.

    Examples:
        >>> from ultralytics.utils.uploads import safe_upload
        >>> success = safe_upload("model.pt", "https://storage.googleapis.com/...", progress=True)
    r   NzFile not found: z
Uploading zContent-Typezapplication/octet-stream   BTi   )totaldescunit
unit_scaleunit_divisor)r   r.   r2   z	Uploaded u    ✅i  i  >       z	 failed:  reason FzHTTP r)   z	 failed (z), retrying /z in zs...z failed after z attempts: )requestsr   existsFileNotFoundErrorstatst_sizenamer   ranger   r	   putraise_for_statusr#   r   info
exceptions	HTTPErrorresponsestatus_codewarninggetattr	Exceptionr-   r   )r   r,   r.   r0   r2   r3   rD   	file_sizer:   upload_headers
last_errorattemptr   readerrestatus	wait_times                     r   safe_uploadr^   %   s   2 OOO::D;;== ; 94 9 9:::		#I#	##D %&@AN 'g&&&J## # #	 f)$ST`deee$T400FSv~wWWA   LLNNNF 

K3DI333444   

   ", 	* 	* 	*/0z/EQZ++1Ff""""s"""""vZ'?'?$]]]]'!*hXZ:[:[]]^^^uuu
   

   *))JJJJJJ 	  	  	 QJJJJJJ	    

   

 U??gk*INdmmZmmWq[mmSXmm^gmmmnnn)
NdLL%!)LL
LLMMM5sJ   BE%%H34AHI#HI#H3H.)I#.H33I##/J)Nr)   r*   F)r   r+   r,   r-   r.   r/   r0   r1   r2   r1   r3   r4   r5   r4   )r'   
__future__r   r   pathlibr   timer   ultralytics.utilsr   r   r	   r^   r(   r   r   <module>rc      s    H H " " " " " " 				             * * * * * * * *       6  N N N N N N Nr   