U
    /e                     @  sb  d Z ddlmZ ddlZeeZddlZddlZddl	m
Z
mZ ddlmZmZmZmZmZmZmZ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 d
ZeZ d-dddddZ!dddddddZ"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&d'Z(d/dddd(d)Z)d0dddddd*d+d,Z*dS )1z> Helper functions for downloading and accessing sample data.

    )annotationsN)mkdirremove)abspathdirnameexists
expanduserisdirisfilejoinsplitext)stdout)AnyTextIOcast)urljoin)urlopen)downloadTboolNone)progressreturnc           
      C  s   t dd}td|  d}tttttd}|D ]\}}t|\}}|dkrht|d sp|d7 }n||7 }t||}t	|rt
t|d	  }	|	|krtd
|d q6ntd|d t|||| d q6dS )z< Download larger data sets for various Bokeh examples.

    TcreatezUsing data directory: %szhttp://sampledata.bokeh.orgzsampledata.json.zip   .csvrbz	Skipping z (checksum match)zRe-fetching z (checksum mismatch))r   N)external_data_dirprintjsonloadopenr   r   __file__r   r   hashlibmd5read	hexdigest_download_file)
r   data_dirZs3filesfilenamer%   	real_nameext	real_pathZ	local_md5 r/   9/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/util/sampledata.pyr   ;   s$    


r   strr   	DataFrame)modulenamekwr   c                 K  s2   ddl m} |dd|  }tt|jt|f|S 

    r   )import_requiredZpandaszI%s sample data requires Pandas (http://pandas.pydata.org) to be installed)dependenciesr8   r   r   read_csvexternal_pathr3   r4   r5   r8   pdr/   r/   r0   external_csv]   s    r>   F)r   r   c              	   C  s   zddl }W n tk
r(   tdY nX t| d}t|d}z$|tt|d}t|d }W n tt	fk
rz   Y nX t
|s| stdtd	|  zt| W q tk
r   td
| Y qX nt|std| |S )r7   r   NzB'yaml' and 'pyyaml' are required to use bokeh.sampledata functionsr   dataconfigZsampledata_dirzVbokeh sample data directory does not exist, please execute bokeh.sampledata.download()Creating %s directoryz+could not create bokeh data directory at %s %s exists but is not a directory)yamlImportErrorRuntimeError
_bokeh_dirr   Z	safe_loadr"   r   OSError	TypeErrorr   r   r   r	   )r   rC   	bokeh_dirr)   r@   r/   r/   r0   r   e   s,    

r   )r+   r   c                 C  s0   t  }t|| }t|s,t|r,td| |S )NzRCould not locate external data file %s. Please execute bokeh.sampledata.download())r   r   r   r
   rE   )r+   r)   fnr/   r/   r0   r;      s
    
r;   c                 K  s2   ddl m} |dd|  }tt|jt|f|S r6   )r9   r8   r   r   r:   package_pathr<   r/   r/   r0   package_csv   s    rL   )r   c                   C  s   t tttdddS )r7   z..Z
sampledata_data)r   r   r   r#   r/   r/   r/   r0   package_dir   s    rN   c                 C  s   t t | S )r7   )r   rN   r+   r/   r/   r0   rK      s    rK   r   c                 C  s   t | ddddS )r7   r utf8)newlineencoding)r"   rO   r/   r/   r0   open_csv   s    rU   c                 C  sr   t tdd}t|sZ| s|S td|  zt| W qn tk
rV   td| Y qnX nt|sntd| |S )r7   ~z.bokehrA   z-could not create bokeh config directory at %srB   )r   r   r   r   r   rG   rE   r	   )r   rI   r/   r/   r0   rF      s     rF   )base_urlr+   r)   r   r   c              	   C  s$  ddl m} t| |}t||}t|}t|d~}t|jd }	td||	f  d}
d}|	|}|sjq|
t
|7 }
|| |rZd|
|
d |	 f }t| t  qZW 5 Q R X |rt  t|\}}|d	kr t|d
 s|d7 }td|  ||d}||| W 5 Q R X t| dS )r7   r   )ZipFilewbzContent-LengthzDownloading: %s (%d bytes)i @  z%10d [%6.2f%%]g      Y@r   r   r   zUnpacking: %srP   N)zipfilerX   r   r   r   r"   intheadersr   r&   lenwriter   flushr   extractr   )rW   r+   r)   r   rX   Zfile_url	file_pathurlfile	file_sizeZ
fetch_size
block_sizer?   statusr,   r-   zip_filer/   r/   r0   r(      s8    





r(   )T)F)F)T)+__doc__
__future__r   logging	getLogger__name__logr$   r    osr   r   os.pathr   r   r   r   r	   r
   r   r   sysr   typingr   r   r   urllib.parser   urllib.requestr   __all__r2   r   r>   r   r;   rL   rN   rK   rU   rF   r(   r/   r/   r/   r0   <module>   s.   

(
" 	
