o
    MeG                     @   s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Zd dlm  mZ d dlZd dlZd dlZg ZejdZeeejsS	 e ZejedddZdd Zee d	d
 ZdddZdd ZdejfddZ ej!fddZ"dddZ#dS )    )print_functionN~z.cachepaddledatasetc              
   C   sF   zt t W d S  ty" } z|jtjkr W Y d }~d S d }~ww N)osmakedirs	DATA_HOMEOSErrorerrnoEEXIST)pathexc r   ED:\Projects\ConvertPro\env\Lib\site-packages\paddle/dataset/common.pymust_mkdirs6   s   r   c                    sD   t  }t| d t fdddD ]}|| q   | S )Nrbc                      s
     dS )N   )readr   fr   r   <lambda>E   s   
 zmd5file.<locals>.<lambda>    )hashlibmd5openiterupdateclose	hexdigest)fnameZhash_md5chunkr   r   r   md5fileB   s   
r"   c              
   C   sX  t jt|}t j|st | t j||d u r!| dd n|}t j|r2t||kr2|S d}d}t j|rCt||kst j|rUtj	
dt||f  ||k r^|d7 }ntd| |tj	
d|| f  tj	
d	 ztj| d
d}|jd}	|	d u rt|d}
t|j|
 W d    n1 sw   Y  nYt|dL}
d}t|	}	|	| d }|dkr|d nd}d}tjjj|dd}|j|dD ]}|

| |d7 }||i  || dkr|| qW d    n1 sw   Y  W n ty } zW Y d }~q6d }~ww t j|rCt||krCtj	
d tj  |S )N/r      zfile %s  md5 %s
   z*Cannot download {0} within retry limit {1}z)Cache file %s not found, downloading %s 
zBegin to download
T)streamzcontent-lengthwbr      item)name)
chunk_sizez
Download finished
)r   r   joinr	   existsr   splitr"   sysstderrwriteRuntimeErrorformatrequestsgetheadersr   shutilcopyfileobjrawintr   ZhapiZprogressbarProgressBariter_contentr   	Exceptionstdoutflush)urlmodule_nameZmd5sumZ	save_namedirnamefilenameretryZretry_limitrtotal_lengthr   r,   Z
total_iterZlog_intervalZ	log_indexbardataer   r   r   downloadK   sx   




&
rK   c                  C   sJ   dd t tjD D ]} dt td|  v r"ttd|  d  q
d S )Nc                 S   s   g | ]	}| d s|qS )__)
startswith).0xr   r   r   
<listcomp>   s
    
zfetch_all.<locals>.<listcomp>fetchzpaddle.dataset.%s)dirr   r   	importlibimport_modulegetattr)rB   r   r   r   	fetch_all   s   
rV   z%05d.picklec           	   	   C   s   t |stdg }d}t|  D ]5\}}|| ||krF|| dkrFt|| d}||| g }|d7 }W d   n1 sAw   Y  q|rht|| d}||| W d   dS 1 saw   Y  dS dS )a  
    you can call the function as:

    split(paddle.dataset.cifar.train10(), line_count=1000,
        suffix="imikolov-train-%05d.pickle")

    the output files as:

    |-imikolov-train-00000.pickle
    |-imikolov-train-00001.pickle
    |- ...
    |-imikolov-train-00480.pickle

    :param reader: is a reader creator
    :param line_count: line count for each file
    :param suffix: the suffix for the output files, should contain "%d"
                means the id for each file. Default is "%05d.pickle"
    :param dumper: is a callable function that dump object to file, this
                function will be called as dumper(obj, f) and obj is the object
                will be dumped, f is a file object. Default is cPickle.dump.
    zdumper should be callable.r   wr&   N)callable	TypeError	enumerateappendr   )	reader
line_countsuffixZdumperlinesZindx_fidr   r   r   r   r/      s$   


"r/   c                    s    fdd}|S )a  
    Create a reader that yield element from the given files, select
    a file set according trainer count and trainer_id

    :param files_pattern: the files which generating by split(...)
    :param trainer_count: total trainer count
    :param trainer_id: the trainer rank id
    :param loader: is a callable function that load object from file, this
                function will be called as loader(f) and f is a file object.
                Default is cPickle.load
    c               	   3   s    t s	tdt } |   g }t| D ]\}}| kr-td|  || q|D ]#}t|d}|}|D ]}|V  q>W d    n1 sNw   Y  q0d S )Nzloader should be callable.zappend file: %srF   )rX   rY   globsortrZ   printr[   r   )	file_listZmy_file_listidxfnr   r_   linefiles_patternloadertrainer_count
trainer_idr   r   r\      s(   

z$cluster_files_reader.<locals>.readerr   )rj   rl   rm   rk   r\   r   ri   r   cluster_files_reader   s   rn   Tc                 C   s8   | r
t j| r
| S |rtjj|||S td| )Nz({} not exists and auto download disabled)	r   r   r.   r   r   commonrK   
ValueErrorr4   )r   rA   r   rB   rK   r   r   r   _check_exists_and_download   s   rq   r   )T)$
__future__r   r5   r   r   r   r8   sixr0   rS   Zpaddle.datasetr   Zsix.moves.cPicklemovescPicklepickletempfilerb   __all__r   
expanduserHOMEaccessW_OK
gettempdirr-   r	   r   r"   rK   rV   dumpr/   loadrn   rq   r   r   r   r   <module>   s:   	
	9
)
#