
    uj(                     @   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                  Z G d	 d
e          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Zdej        fd Zd! Zdej        fd"Z d# Z!	 d,dej        d%ee"         d&e#fd'Z$d( Z%d)ej        fd*Z&d+ Z'dS )-    N)
HTTPStatus)Path)OptionalUnion)	HTTPError)MODELSCOPE_REQUEST_ID)
get_logger)	log_levelc                       e Zd ZdS )NotSupportErrorN__name__
__module____qualname__     e/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/modelscope/hub/errors.pyr   r              Dr   r   c                       e Zd ZdS )NoValidRevisionErrorNr   r   r   r   r   r      r   r   r   c                       e Zd ZdS )NotExistErrorNr   r   r   r   r   r      r   r   r   c                       e Zd ZdS )RequestErrorNr   r   r   r   r   r      r   r   r   c                       e Zd ZdS )GitErrorNr   r   r   r   r   r   !   r   r   r   c                       e Zd ZdS )InvalidParameterNr   r   r   r   r   r   %   r   r   r   c                       e Zd ZdS )NotLoginExceptionNr   r   r   r   r    r    )   r   r   r    c                       e Zd ZdS )FileIntegrityErrorNr   r   r   r   r"   r"   -   r   r   r"   c                       e Zd ZdS )FileDownloadErrorNr   r   r   r   r$   r$   1   r   r   r$   c                   X     e Zd ZU dZeeef         ed<   dedeeef         f fdZ xZ	S )CacheNotFoundz8Exception thrown when the ModelScope cache is not found.	cache_dirmsgc                 P     t                      j        |g|R i | || _        d S )N)super__init__r'   )selfr(   r'   argskwargs	__class__s        r   r+   zCacheNotFound.__init__:   s5    .t...v..."r   )
r   r   r   __doc__r   strr   __annotations__r+   __classcell__)r/   s   @r   r&   r&   5   sq         BBS$Y#C #E#t),< # # # # # # # # # #r   r&   c                       e Zd ZdZdS )CorruptedCacheExceptionzFException for any unexpected structure in the ModelScope cache-system.N)r   r   r   r0   r   r   r   r5   r5   ?   s        PPPPr   r5   responsec                 Z    t           | j        j        v r| j        j        t                    S dS )N )r   requestheadersr6   s    r   get_request_idr<   C   s*     0 888'(=>>rr   c                 >    | 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`.
    CodeSuccess)r   OKrsps    r   is_okrC   J   s     v;*-':C	N:r   c                     d| j                             dd          v r|                                 }n| j                            d          }|S )Nzapplication/jsonzcontent-typer8   utf-8)r:   getjsoncontentdecode)r6   messages     r   _decode_response_errorrK   V   sJ    X-11."EEEE--//"))'22Nr   c                     	 |                                   d S # t          $ r=}t          |           }t          d|d|d|dt          |                     |d }~ww xY w)NzRequest z with body: z exception, Response details: z, request id: )raise_for_statusr   rK   r<   )r6   urlrequest_bodyerrorrJ   s        r   handle_http_post_errorrQ   ^   s    O!!##### O O O(22i SS,,,)A)A)ACD D JO	OOs    
A8AATraise_on_errorreturnc                 ^   d}t          | j        t                    rF	 | j                            d          }n1# t          $ r | j                            d          }Y nw xY w| j        }t          |           }d| j        k    rd|z  }ntd| j        k    r|d| d}n`d	| d
}nYd| j        cxk    rdk     rn n| j        d|d|d| j        }n,d| j        cxk    rdk     rn n| j        d|d|d| j        }|r(|r&|                    |           t          ||           | j        S )Nr8   rE   
iso-8859-1  z%The request model: %s does not exist!  z<Authentication token does not exist, failed to access model z; which may not exist or may be private. Please login first.z<The authentication token is invalid, failed to access model .     Client Error: , Request id: 
 for url: X   Server Error: , for url: r;   )

isinstancereasonbytesrI   UnicodeDecodeErrorr<   status_coderN   rP   r   )r6   loggercookiesmodel_idrR   http_error_msgrb   
request_ids           r   handle_http_responserk   i   s   
 N(/5)) !	:_++G44FF! 	: 	: 	:_++L99FFF	: ))J
h"""@HM	$	$	$?Mx M M M N
 h\dgggNN	$	*	*	*	*s	*	*	*	*	*   &&&***hllD 
$	*	*	*	*s	*	*	*	*	*   &&&***hllD $. $^$$$::::##   9 $A A c                 \    | 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.
    r>   TMessage)r   r@   r   rA   s    r   rR   rR      s,     6{jm##t3y>***r   http_responsec                     |                     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.
    r>   T	RequestIdzUrl = z, Request id=z Code = z Message = rn   zC,                Please specify correct dataset_name and namespace.)rF   r   r@   r   )rN   rB   ro   rj   s       r   datahub_raise_on_errorrr      s     wwv*-''t%
DS D Dz D D3v; D DSVW`Sa D D DE E 	Er   c                 \   d}t          | j        t                    rF	 | j                            d          }n1# t          $ r | j                            d          }Y nw xY w| j        }t          |           }d| j        k    rd}d| j        d| ngd| j        k    rd	}nYd
| j        cxk    rdk     rn n| j        d|d|d| j        }n,d| j        cxk    rdk     rn n| j        d|d|d| j        }|r/| j        }|j	        dk    r|d|j
        }t          ||           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.
    r8   rE   rU   rV   z7The request resource(model or dataset) does not exist!,zurl: z
, reason: rW   z/Authentication token does not exist or invalid.rY   rZ   r[   r\   r]   r^   r_   r`   POSTz, body: r;   N)ra   rb   rc   rI   rd   r<   re   rN   r9   methodbodyr   )rB   ri   rb   rj   reqs        r   raise_for_http_statusrx      s    N#*e$$ 	5Z&&w//FF! 	5 	5 	5Z&&|44FFF	5 $$J
coR!$&&111			J		%	%	%	%#	%	%	%	%	%OOOVVVZZZ: 
	%	%	%	%#	%	%	%	%	%OOOVVVZZZ:  6k:0>IN5555	6 6rl   )T)(logginghttpr   pathlibr   typingr   r   requestsrequests.exceptionsr   modelscope.hub.constantsr   modelscope.utils.loggerr	   WARNINGrf   	Exceptionr   r   r   r   r   r   r    r"   r$   r&   r5   Responser<   rC   rK   rQ   boolintrk   rR   rr   rx   r   r   r   <module>r      s`                " " " " " " " "  ) ) ) ) ) ) : : : : : : . . . . . .	go	.	.	.	 	 	 	 	i 	 	 		 	 	 	 	9 	 	 		 	 	 	 	I 	 	 		 	 	 	 	9 	 	 		 	 	 	 	y 	 	 		 	 	 	 	y 	 	 		 	 	 	 		 	 	 		 	 	 	 	 	 	 		 	 	 	 		 	 	 	# # # # #I # # #Q Q Q Q Qi Q Q QX.    	; 	; 	;X%6    O O O ;?	#$ #$8#4 #$ *2$	#$ DG	#$ #$ #$ #$L+ + +$EH4E E E E E.%6 %6 %6 %6 %6r   