o
    W+ i"                     @   s.  d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	 d dl
mZmZmZ dd Zdd Zd	efd
dZd	efddZd	efddZded	efddZded	efddZdd ZddeddfddZdeeeeef d	efddZ			 d&deeeeef d!ee d"ee d#ee d	ef
d$d%ZdS )'    N)Path)Errorcopy2copystat)BinaryIOOptionalUnionc                 C   sx   t | }|j}|j}|du r|du sdS |jsg n|j}|r*|d dv r*|dn| t|dkr:|d dv r:dS dS )zto decide if a func could receive dict inputs or not

    Args:
        func (class): the target function to be inspected

    Returns:
        bool: if func only has one arg ``input`` or ``inputs``, return True, else return False
    NFr   )selfcls   )inputZinputsT)inspectgetfullargspecvarargsvarkwargspoplen)funcZfull_args_specr   r   r    r   g/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/modelscope/utils/file_utils.pyfunc_receive_dict_inputs   s   
	r   c                  C   s   t jt ddd} | S )z1
    default base dir: '~/.cache/modelscope'
    z.cacheZ
modelscopeZhub)ospath
expanduserr   homejoinpath)Zdefault_cache_dirr   r   r    get_default_modelscope_cache_dir$   s   r   returnc                   C   s   t jt dt S )zGet modelscope cache dir, default location or
       setting with MODELSCOPE_CACHE

    Returns:
        str: the modelscope cache root.
    ZMODELSCOPE_CACHE)r   r   r   getenvr   r   r   r   r   get_modelscope_cache_dir-   s   r    c                   C      t jt dS )z[Get model cache root path.

    Returns:
        str: the modelscope model cache root.
    modelsr   r   joinr    r   r   r   r   get_model_cache_root8   s   r%   c                   C   r!   )zGet dataset raw file cache root path.
    if `MODELSCOPE_CACHE` is set, return `MODELSCOPE_CACHE/datasets`,
    else return `~/.cache/modelscope/hub/datasets`

    Returns:
        str: the modelscope dataset raw file cache root.
    Zdatasetsr#   r   r   r   r   get_dataset_cache_rootA   s   r&   
dataset_idc                 C   $   t  }| du r	|S tj|| d S )zGet the dataset_id's path.
       dataset_cache_root/dataset_id.

    Args:
        dataset_id (str): The dataset id.

    Returns:
        str: The dataset_id's cache root path.
    N/)r&   r   r   r$   )r'   Zdataset_rootr   r   r   get_dataset_cache_dirL      
r*   model_idc                 C   r(   )zcache dir precedence:
        function parameter > environment > ~/.cache/modelscope/hub/model_id

    Args:
        model_id (str, optional): The model id.

    Returns:
        str: the model_id dir if model_id not None, otherwise cache root dir.
    Nr)   )r%   r   r   r$   )r,   	root_pathr   r   r   get_model_cache_dir[   r+   r.   c                 C   s8   t | d}| }W d    |S 1 sw   Y  |S )Nr)openread)r   ftextr   r   r   	read_filej   s   

r4   Fc                 C   s  t | }|dur|| |}nt }t j||d g }	|D ]}
|
|v r$qt j| |
}t j||
}zUt j|rpt |}|rOt || t	||| d n7t j
|sY|rYW qt j|rjt||||||d n||| nt j|rt||||||d n||| W q ty } z|	|jd  W Y d}~qd}~w ty } z|	||t|f W Y d}~qd}~ww zt	| | W n$ ty } zt|dddu r|	| |t|f W Y d}~nd}~ww |	rt|	|S )z7copy from py37 shutil. add the parameter dirs_exist_ok.N)exist_ok)follow_symlinks)dirs_exist_okr   winerror)r   listdirsetmakedirsr   r$   islinkreadlinksymlinkr   existsisdircopytree_py37r   extendr   OSErrorappendstrgetattr)srcdstsymlinksignorecopy_functionignore_dangling_symlinksr7   namesignored_nameserrorsnamesrcnamedstnamelinktoerrwhyr   r   r   rA   q   sr   


	 rA   file_path_or_objc                 C   sr   t | ttfrt| }| jS t | trt| S t | tjr5| 	 }| 
dtj | 	 }| 
| |S td)Nr   zCUnsupported type: must be string, Path, bytes, or io.BufferedIOBase)
isinstancerE   r   statst_sizebytesr   ioBufferedIOBasetellseekr   SEEK_END	TypeError)rV   	file_pathcurrent_positionsizer   r   r   get_file_size   s   


rd   r   [Calculating]Tbuffer_size_mb	tqdm_descdisable_tqdmc              	   C   s  ddl m} t| }|dkr-d}d}t| ttfr't| tr | nt| }|j}d| d}|d d }t }	g }
||dd	d	d
||d}t| ttfrt	| d,}|
| }ru|
t|  |	| |t| |
| }sXW d    n1 sw   Y  |	 }	|}ndt| tr|	|  |	 }	|
|	 t| }|| nGt| tjr| dtj | 
| }r|
t|  |	| |t| | 
| }s|	 }	|}| dtj n|  td|  | |	||t|
|
dS )Nr   )tqdmi   @Fz
Large Filez[Validating Hash for ]i   TB)totalinitialZ
unit_scaleZdynamic_ncolsunitZdescdisablerbz5Input must be str, Path, bytes or a io.BufferedIOBase)rV   	file_hash	file_size
chunk_sizeZ
chunk_numschunk_hash_list)Z	tqdm.autori   rd   rW   rE   r   rP   hashlibsha256r0   r1   rD   	hexdigestupdater   rZ   r[   r\   r^   r   SEEK_SETclose
ValueError)rV   rf   rg   rh   ri   rr   rP   r   buffer_sizerq   rt   progressr2   Z
byte_chunkZfinal_chunk_sizer   r   r   get_file_hash   s   





r~   )r   re   T)ru   r   r[   r   pathlibr   shutilr   r   r   typingr   r   r   r   r   rE   r    r%   r&   r*   r.   r4   rA   rZ   intrd   booldictr~   r   r   r   r   <module>   sH   			
I