o
    W+ i&                     @   s  d dl Z d dlmZ d dlmZ d dlmZmZ d dlZd dl	m
Z
 d dlmZ d dlmZ ee jdZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdejfd d!Zd"d# Zdejfd$d%Z d&d' Z!	(d3dejd)ee" d*e#fd+d,Z$d-d) Z%d.ejfd/d0Z&d1d2 Z'dS )4    N)
HTTPStatus)Path)OptionalUnion)	HTTPError)MODELSCOPE_REQUEST_ID)
get_logger)	log_levelc                   @      e Zd ZdS )NotSupportErrorN__name__
__module____qualname__ r   r   a/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/modelscope/hub/errors.pyr          r   c                   @   r
   )NoValidRevisionErrorNr   r   r   r   r   r      r   r   c                   @   r
   )NotExistErrorNr   r   r   r   r   r      r   r   c                   @   r
   )RequestErrorNr   r   r   r   r   r      r   r   c                   @   r
   )GitErrorNr   r   r   r   r   r   !   r   r   c                   @   r
   )InvalidParameterNr   r   r   r   r   r   %   r   r   c                   @   r
   )NotLoginExceptionNr   r   r   r   r   r   )   r   r   c                   @   r
   )FileIntegrityErrorNr   r   r   r   r   r   -   r   r   c                   @   r
   )FileDownloadErrorNr   r   r   r   r   r   1   r   r   c                       sD   e Zd ZU dZeeef ed< dedeeef f fddZ  Z	S )CacheNotFoundz8Exception thrown when the ModelScope cache is not found.	cache_dirmsgc                    s$   t  j|g|R i | || _d S )N)super__init__r   )selfr   r   argskwargs	__class__r   r   r   :   s   
zCacheNotFound.__init__)
r   r   r   __doc__r   strr   __annotations__r   __classcell__r   r   r#   r   r   5   s   
 &r   c                   @   s   e Zd ZdZdS )CorruptedCacheExceptionzFException for any unexpected structure in the ModelScope cache-system.N)r   r   r   r%   r   r   r   r   r)   ?   s    r)   responsec                 C   s   t | jjv r| jjt  S dS )N )r   requestheadersr*   r   r   r   get_request_idC   s   r/   c                 C   s   | d t jko
| d S )z Check the request is ok

    Args:
        rsp (Response): The request response body

    Returns:
       bool: `True` if success otherwise `False`.
    CodeZSuccess)r   OKrspr   r   r   is_okJ   s   	r4   c                 C   s.   d| j ddv r|  }|S | jd}|S )Nzapplication/jsonzcontent-typer+   utf-8)r-   getjsoncontentdecode)r*   messager   r   r   _decode_response_errorV   s
   r;   c              
   C   sJ   z|    W d S  ty$ } zt| }td|||t| f |d }~ww )NzHRequest %s with body: %s exception, Response details: %s, request id: %s)raise_for_statusr   r;   r/   )r*   urlrequest_bodyerrorr:   r   r   r   handle_http_post_error^   s   r@   Traise_on_errorreturnc                 C   s  d}t | jtr z| jd}W n ty   | jd}Y nw | j}t| }d| jkr1d| }nFd| jkrH|d u rAd| d}n6d	| d
}n/d| j  krSdk r`n nd| j||| jf }nd| j  krkdk rwn n
d| j||| jf }|r|r|| t	|| d| jS )Nr+   r5   
iso-8859-1  z%The request model: %s does not exist!  zLAuthentication token does not exist,                 failed to access model zK which may not exist                 or may be private. Please login first.z<The authentication token is invalid, failed to access model .    /%s Client Error: %s, Request id: %s for url: %sX  0%s Server Error: %s, Request id: %s, for url: %sr.   )

isinstancereasonbytesr9   UnicodeDecodeErrorr/   status_coder=   r?   r   )r*   loggercookiesZmodel_idrA   http_error_msgrM   
request_idr   r   r   handle_http_responsei   s:   




rU   c                 C   s   | d t jkr	dS t| d )zIf response error, raise exception

    Args:
        rsp (_type_): The server response

    Raises:
        RequestError: the response error message.

    Returns:
        bool: True if request is OK, otherwise raise `RequestError` exception.
    r0   TMessage)r   r1   r   r2   r   r   r   rA      s   http_responsec              
   C   sF   | dtjkr
dS |d }td|  d| d|d  d|d  d		)
aQ  If response error, raise exception

    Args:
        url (str): The request url
        rsp (HTTPResponse): The server response.
        http_response: the origin http response.

    Raises:
        RequestError: the http request error.

    Returns:
        bool: `True` if request is OK, otherwise raise `RequestError` exception.
    r0   TZ	RequestIdzUrl = z, Request id=z Code = z Message = rV   zC,                Please specify correct dataset_name and namespace.)r6   r   r1   r   )r=   r3   rW   rT   r   r   r   datahub_raise_on_error   s   $rX   c                 C   s  d}t | jtr z| jd}W n ty   | jd}Y nw | j}t| }d| jkr6d}d| j|f  n7d| jkr>d}n/d	| j  krId
k rVn nd| j||| jf }nd
| j  kradk rmn n
d| j||| jf }|r| j}|j	dkr~d||j
f }t|| ddS )zAttempt to decode utf-8 first since some servers
    localize reason strings, for invalid utf-8, fall back
    to decoding with iso-8859-1.

    Args:
        rsp: The http response.

    Raises:
        HTTPError: The http error info.
    r+   r5   rC   rD   z7The request resource(model or dataset) does not exist!,zurl: %s, reason: %srE   z/Authentication token does not exist or invalid.rG   rH   rI   rJ   rK   POSTz%s, body: %sr.   N)rL   rM   rN   r9   rO   r/   rP   r=   r,   methodbodyr   )r3   rS   rM   rT   reqr   r   r   raise_for_http_status   s8   


r]   )T)(logginghttpr   pathlibr   typingr   r   requestsZrequests.exceptionsr   Zmodelscope.hub.constantsr   Zmodelscope.utils.loggerr   WARNINGrQ   	Exceptionr   r   r   r   r   r   r   r   r   r   r)   Responser/   r4   r;   r@   boolintrU   rA   rX   r]   r   r   r   r   <module>   sB   


&