B
    @\                 @   s   d Z ddlmZmZmZmZ ddlZee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ZddlmZ dZdd
dZdd Zd ddZdd Zdd Z dd Z!dd Z"dd Z#d!ddZ$d"ddZ%dS )#z> Helper functions for downloading and accessing sample data.

    )absolute_importdivisionprint_functionunicode_literalsN)mkdirremove)abspathdirnameexists
expanduserisdirisfilejoinsplitext)stdout)urljoin)downloadTc             C   s   t dd}td|  d}|df|df|df|df|d	f|d
f|df|df|df|df|df|df|df|df|df|df|df|df|df|dfg}x |D ]\}}t|||| d qW dS )z< Download larger data sets for various Bokeh examples.

    T)createzUsing data directory: %sz)https://bokeh-sampledata.s3.amazonaws.comzCGM.csvzUS_Counties.zipzus_cities.jsonzunemployment09.csvzAAPL.csvzFB.csvzGOOG.csvzIBM.csvzMSFT.csvz+WPP2012_SA_DB03_POPULATION_QUINQUENNIAL.zipzgapminder_fertility.csvzgapminder_population.csvzgapminder_life_expectancy.csvzgapminder_regions.csvzworld_cities.zipzairports.jsonzmovies.db.zipzairports.csvz
routes.csvz#haarcascade_frontalface_default.xml)progressN)external_data_dirprint_download_file)r   data_dirZs3filesbase_urlfilename r   4lib/python3.7/site-packages/bokeh/util/sampledata.pyr   1   s2    

r   c             K   s,   ddl m} |dd|  }|jt|f|S )z

       )import_requiredpandaszI%s sample data requires Pandas (http://pandas.pydata.org) to be installed)dependenciesr   read_csvexternal_path)modulenamekwr   pdr   r   r   external_csvW   s    r(   Fc          	   C   s   yddl }W n tk
r(   tdY nX t| d}t|d}y$|tt|d}t|d }W n tt	fk
rz   Y nX t
|s| stdtd	|  yt| W q tk
r   td
| Y qX nt|std| |S )z

    r   NzB'yaml' and 'pyyaml' are required to use bokeh.sampledata functions)r   dataconfigZsampledata_dirzVbokeh sample data directory does not exist, please execute bokeh.sampledata.download()zCreating %s directoryz+could not create bokeh data directory at %sz %s exists but is not a directory)yamlImportErrorRuntimeError
_bokeh_dirr   Z	safe_loadopenr   IOError	TypeErrorr
   r   r   OSErrorr   )r   r+   	bokeh_dirr   r*   r   r   r   r   _   s,    

r   c             C   s0   t  }t|| }t|s,t|r,td| |S )NzRCould not locate external data file %e. Please execute bokeh.sampledata.download())r   r   r
   r   r-   )r   r   fnr   r   r   r#      s
    
r#   c             K   s,   ddl m} |dd|  }|jt|f|S )z

    r   )r   r    zI%s sample data requires Pandas (http://pandas.pydata.org) to be installed)r!   r   r"   package_path)r$   r%   r&   r   r'   r   r   r   package_csv   s    r6   c               C   s   t tttdddS )z

    z..Z
sampledata_data)r   r   r	   __file__r   r   r   r   package_dir   s    r9   c             C   s   t t | S )z

    )r   r9   )r   r   r   r   r5      s    r5   c             C   s$   t jrt| dS t| ddddS dS )z

    rbr utf8)newlineencodingN)sixZPY2r/   )r   r   r   r   open_csv   s    
rA   c             C   sr   t tdd}t|sZ| s|S td|  yt| W qn tk
rV   td| Y qnX nt|sntd| |S )z

    ~z.bokehzCreating %s directoryz-could not create bokeh config directory at %sz %s exists but is not a directory)r   r   r
   r   r   r2   r-   r   )r   r3   r   r   r   r.      s     r.   c          	   C   s4  ddl m} ddlm} t| |}t||}||}t|d}	t|jd }
t	d||
f  d}d}xT|
|}|sxP |t|7 }|	| |rhd||d	 |
 f }t| t  qhW W d
Q R X |rt	  t|\}}|dkr0t|d s|d7 }t	d|  ||d}||| W d
Q R X t| d
S )z

    r   )urlopen)ZipFilewbzContent-LengthzDownloading: %s (%d bytes)i @  z%10d [%6.2f%%]g      Y@Nz.zipr   z.csvzUnpacking: %sr;   )Zsix.moves.urllib.requestrC   ZzipfilerD   r   r   r/   intZheadersr   readlenwriter   flushr   extractr   )r   r   r   r   rC   rD   Zfile_urlZ	file_pathZurlfileZ	file_sizeZ
fetch_sizeZ
block_sizer)   ZstatusZ	real_nameZextZzip_filer   r   r   r      s<    





r   )T)F)F)T)&__doc__Z
__future__r   r   r   r   ZloggingZ	getLogger__name__logosr   r   os.pathr   r	   r
   r   r   r   r   r   sysr   r@   Zsix.moves.urllib_parser   __all__r   r(   r   r#   r6   r9   r5   rA   r.   r   r   r   r   r   <module>	   s&   

(	
&
 	
