o
    i                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlZd dl	Zzd dl
Z
dZW n ey5   dZY nw zd dlZedu rHG dd dejZdZW n eyV   dZY nw ddlmZ d d	lmZ ee jZdZd
ejdefddZdejfddZejejjejjfddded
ejddfddZ dejfddZ!e j"de#eef fddZ$dedefddZ%				d'dede&de&dedB de&dejfdd Z'G d!d" d"e(Z)dedefd#d$Z*dede&fd%d&Z+dS )(    N)castTFc                	   @   s.   e Zd Z	d	dedededB ddfddZdS )
DownloadProgressBar   Nbbsizetsizereturnc                 C   s&   |d ur|| _ | || | j  d S )N)totalupdaten)selfr   r   r    r   K/mnt/sdb/aimis/docanh/lib/python3.10/site-packages/pydicom/data/download.py	update_to   s   zDownloadProgressBar.update_to)r   r   N)__name__
__module____qualname__intr   r   r   r   r   r      s    r   r   )retry)warn_and_logfpathr   c                 C   s   d}t  }t| d0}||}t|dkr1|| ||}t|dksW d   | S W d   | S 1 s>w   Y  | S )zReturn the SHA256 checksum for the file at `fpath`.

    Parameters
    ----------
    fpath : pathlib.Path
        The absolute path to the file that is to be checksummed.

    Returns
    -------
    str
        The SHA256 checksum of the file.
    i   rbr   N)hashlibsha256openreadlenr
   	hexdigest)r   	BLOCKSIZEhasherfbufr   r   r   calculate_file_hash/   s   




r"   c                  C   s   t j d } | jdd | S )zReturn the path to the pydicom config directory, creating it if required

    The config directory will be named ``.pydicom`` and will be created in the
    local user's home directory.
    z.pydicomTexist_ok)pathlibPathhomemkdir)
config_dirr   r   r   get_config_dirG   s   r*   z*Installing the `requests` package may help)exc_msgurlc           	   
   C   sL  t |}trqtrNtj| dd}t|jdd}t|d$}t	j	|j
dd|ddd	| d
d dD ]}|| q3W d   n
1 sEw   Y  dS dS t| }t|d}||j W d   dS 1 sjw   Y  dS trtddd	| d
d d}tjj| ||jd W d   dS 1 sw   Y  dS tj| | dS )zDownload the file at `url` to `fpath` with a progress bar.

    Parameters
    ----------
    url : str
        The URL to download the file from.
    fpath : pathlib.Path
        The absolute path where the file will be written to.
    T)streamzcontent-lengthr   wbi   )
chunk_sizeBr   /)r	   unit
unit_scaleminitersdescN)r3   r4   r5   r6   )
reporthook)osfspathHAVE_REQUESTSUSE_PROGRESS_BARrequestsgetr   headersr   tqdmiter_contentsplitwritecontentr   urllibrequesturlretriever   )	r,   r   filenamertotal_size_in_bytesfiledatar    tr   r   r   download_with_progressS   s:   


$
""rM   c                  C   s   t  d } | jdd | S )z@Return the path to the cache directory, creating it if required.rK   Tr#   )r*   r(   )data_dirr   r   r   get_data_dir   s   
rO   c                  C   sH   t td } ttttf t| W  d   S 1 sw   Y  dS )z>Return a dict containing the URL mappings from ``urls.json```.z	urls.jsonN)r   HEREr   dictstrjsonload)url_filer   r   r   get_url_map   s   $rV   rG   c                 C   s:   dd t   D }z||   W S  ty   tdw )a  Return the download URL corresponding to `filename`.

    The filename:URL mappings are located in the ``urls.json`` file.

    Parameters
    ----------
    filename : str
        The filename of the file to get the corresponding URL for.

    Returns
    -------
    str
        The download URL corresponding to `filename`.

    Raises
    ------
    ValueError
        If `filename` is not in the ``urls.json`` record.
    c                 S      i | ]	\}}|  |qS r   lower.0kvr   r   r   
<dictcomp>       zget_url.<locals>.<dictcomp>z:The file provided isn't within pydicom's urls.json record.)rV   itemsrY   KeyError
ValueError)rG   urlsr   r   r   get_url   s   rd   
check_hashredownload_on_hash_mismatchquietc                 C   s   t rtdt | }|r&| r&zt|  W n ty%   |  Y nw | s7|du r2t| }t	|| |r_zt
| }W n tyL   |  Y S w |s_|r[|  t| ddS td| S )a  Return the absolute path to the cached file with `filename`.

    If the file isn't available in the cache then it will be downloaded.

    Parameters
    ----------
    filename : str
        The filename of the file to return the path to.
    check_hash : bool, optional
        ``True`` to perform a SHA256 checksum on the file, ``False`` otherwise.
    redownload_on_hash_mismatch : bool, optional
        ``True`` to redownload the file on checksum failure, ``False``
        otherwise.
    url : str, optional
        The file's corresponding download URL

    Returns
    -------
    pathlib.Path
        The absolute path to the file.
    zNo network!NF)rf   z2The file on disk does not match the recorded hash.)_SIMULATE_NETWORK_OUTAGERuntimeErrorrO   joinpathexistsget_cached_filehashNoHashFoundunlinkrd   rM   data_file_hash_checkresolvedata_path_with_downloadrb   )rG   re   rf   r,   rg   filepathhash_agreesr   r   r   rq      s6   
rq   c                   @   s   e Zd ZdS )rm   N)r   r   r   r   r   r   r   rm      s    rm   c                 C   sx   t td }ttttf t|}dd | D }W d   n1 s&w   Y  z||   W S  t	y;   t
w )zReturn the SHA256 checksum of a cached file.

    Parameters
    ----------
    filename : str
        The filename of the cached file to calculate the checksum for.

    Returns
    -------
    str
        The SHA256 checksum of the cached file.
    hashes.jsonc                 S   rW   r   rX   rZ   r   r   r   r^      r_   z'get_cached_filehash.<locals>.<dictcomp>N)r   rP   r   rQ   rR   rS   rT   r`   rY   ra   rm   )rG   	hash_filehashesr   r   r   rl      s   rl   c                 C   s   t | } t | }t|}z	t| }W ||kS  tyc   td tt	d }t
|}W d   n1 s9w   Y  ||| < tt	d d}t
j||ddd W d    1 s]w   Y   w )a+  Return ``True`` if the SHA256 checksum of the cached file is correct.

    Parameters
    ----------
    filename : str
        The filename of the cached file to check.

    Returns
    -------
    bool
        ``True`` if the cached file has the correct checksum, ``False``
        otherwise.
    z4Hash not found in hashes.json. File will be updated.rt   Nw   T)indent	sort_keys)r8   r9   rO   rj   r"   rl   rm   r   r   rP   rS   rT   dump)rG   rr   calculated_filehashcached_filehashru   rv   r   r   r   ro     s&   


ro   )TTNT),	functoolsr   rS   r8   r%   typingr   urllib.requestrD   urllib.errorr<   r:   ImportErrorr?   r   r;    r   pydicom.miscr   r&   __file__rp   parentrP   rh   rR   r"   r*   error	HTTPErrorURLErrorrM   rO   	lru_cacherQ   rV   rd   boolrq   ra   rm   rl   ro   r   r   r   r   <module>   sr   (
?