a
    ]Df                     @  s~  d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZmZmZ ddlmZ dd	lmZ er|ddlZd
Zd;dddddZdddddZddddZdddddddZdddd d!d"d#Zdd d$d%d&Zdddddd'd(Zd dd)d*Zd+d d,d-d.Zd+dd,d/d0Zd+d1d,d2d3Z d<dd d!d4d5Z!d=ddd ddd6d7d8Z"e#d9krzedd: dS )>z> Helper functions for downloading and accessing sample data.

    )annotationsNsplitext)Path)stdout)TYPE_CHECKINGAnyTextIO)urljoin)urlopen)downloadTboolNone)progressreturnc           	   	   C  s   t dd}td|  d}t  D ]\}}|t| }| rt|d}| }W d   n1 sj0    Y  t	|
 }||krtd|d q&td	| t|||| d
 q&dS )z< Download larger data sets for various Bokeh examples.

    TcreatezUsing data directory: zhttp://sampledata.bokeh.orgrbNz	Skipping z (checksum match)z	Fetching r   )external_data_dirprintmetadataitems	real_nameexistsopenreadhashlibmd5	hexdigest_download_file)	r   data_dirZs3	file_namer   	real_pathfiledata	local_md5 r'   b/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/bokeh/util/sampledata.pyr   2   s    
&r   str)namer   c                 C  s8   t | \}}|dkr0t |d s*| dS |S n| S d S )N.zip   .csvr   )r*   r   extr'   r'   r(   r   N   s    
r   zdict[str, str])r   c                  C  sF   t tjd d} tt| W  d    S 1 s80    Y  d S )Nzsampledata.jsonr   )r   __file__parentr   dictjsonload)fr'   r'   r(   r   X   s    r   r   zpd.DataFrame)moduler*   kwr   c                 K  s   dd l }|jt|fi |S Nr   )pandasread_csvexternal_pathr5   r*   r6   pdr'   r'   r(   external_csv\   s    r=   Fr   r   )r   r   c              	   C  s   zdd l }W n ty&   tdY n0 t| d}|d }z$|t|d }t|d }W n tt	fyt   Y n0 |
 s| stdtd| d	 z|  W q ty   td
| Y q0 n| st| d|S )Nr   zB'yaml' and 'pyyaml' are required to use bokeh.sampledata functionsr   r%   configZsampledata_dirzVbokeh sample data directory does not exist, please execute bokeh.sampledata.download()	Creating 
 directoryz)could not create bokeh data directory at  exists but is not a directory)yamlImportErrorRuntimeError
_bokeh_dirZ	safe_loadr   r   
expanduserOSError	TypeErrorr   r   mkdiris_dir)r   rB   	bokeh_dirr!   r>   r'   r'   r(   r   `   s,    
r   )r"   r   c                 C  s   t  }||  }| r| s.td| dt|d}t }|| pp||  dpp|t| d  d}|d u rtd|  t	|
  }||krtd| dW d    n1 s0    Y  |S )	Nz$Could not locate external data file z,. Please execute bokeh.sampledata.download()r   r+   r   zUnknown external data file zExternal data file z8 is outdated. Please execute bokeh.sampledata.download())r   r   is_filerD   r   r   getr   r   r   r   r   )r"   r!   	file_pathr$   metaZ	known_md5r&   r'   r'   r(   r:   }   s"    
.r:   c                 K  s   dd l }|jt|fi |S r7   )r8   r9   package_pathr;   r'   r'   r(   package_csv   s    rQ   c                   C  s   t tjd dd S )Nr,   Z
sampledata_data)r   r/   parentsjoinpathresolver'   r'   r'   r(   package_dir   s    rV   z
str | Path)filenamer   c                 C  s
   t  |  S )N)rV   rW   r'   r'   r(   rP      s    rP   c                 C  s8   t | d}t|W  d    S 1 s*0    Y  d S )Nr   )r   r2   r3   )rW   r4   r'   r'   r(   	load_json   s    rY   r	   c                 C  s   t | dddS )N utf8)newlineencoding)r   rX   r'   r'   r(   open_csv   s    r^   c                 C  sz   t d d }| s`| s |S td| d z|  W qv ty\   td| Y qv0 n| svt| d|S )N~z.bokehr?   r@   z+could not create bokeh config directory at rA   )r   rF   r   r   rI   rG   rD   rJ   )r   rK   r'   r'   r(   rE      s    rE   )base_urlrW   r!   r   r   c                 C  s`  ddl m} t| |}|| }t|}t|d}t|jd }	td| d|	 d d}
d}||}|snq|
t	|7 }
|
| |r^d	|
d
d|
d |	 dd}t
| t  q^W d    n1 s0    Y  |rt  t|\}}|dkr\t|d s|d7 }td|  ||d}||| W d    n1 sJ0    Y  |  d S )Nr   )ZipFilewbzContent-LengthzDownloading: z (z bytes)i @  z< 10dz [g      Y@z6.2fz%%]r+   r,   r-   zUnpacking: r)zipfilera   r
   r   r   intheadersr   r   lenwriter   flushr   extractunlink)r`   rW   r!   r   ra   Zfile_urlrN   urlr$   	file_sizeZ
fetch_size
block_sizer%   statusr   r.   zip_filer'   r'   r(   r       s8    



(
,r    __main__r   )T)F)T)$__doc__
__future__r   r   r2   os.pathr   pathlibr   sysr   typingr   r   r	   urllib.parser
   urllib.requestr   r8   r<   __all__r   r   r   r=   r   r:   rQ   rV   rP   rY   r^   rE   r    __name__r'   r'   r'   r(   <module>   s6   	
5
