3
dw*                 @   s   d Z ddlZddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlmZ dgZejeZdd	 Zd
ddd dfddZejdZejdZdZdZejdfddZdS )zI
Module version for monitoring CLI pipes (`... | python -m tqdm | ...`).
    N)literal_eval   )TqdmKeyErrorTqdmTypeErrortqdm)__version__mainc             C   s   t j| |f d|krZx2|jdD ]$}y
t| |S  tk
rD   Y q"X q"W t| d | |dkr| dksr| dkrvdS | dkrdS t| d | yt|d	 |  d
 S  tk
r   |dkrtttd|  d j	 S t| d | Y nX d S )Nz or z : boolTrue TFalseFz("z")chr")
logdebugsplitcastr   eval	Exceptionr   ordencode)valtypt r   (/tmp/pip-build-v9q4h5k9/tqdm/tqdm/cli.pyr      s(    

r   s   \n   c             C   s   d S )Nr   )floatr   r   r   <lambda>,   s    r   Tc             C   s2  |j }|sHx<| j|}|s0t|ddd   dS || |t| qW d}t|}	x| j|}|s|r|| |r|d|j|  nx|j|D ]}
||
 qW t|ddd   dS xn|j|}
|
dk r||7 }P |||d|
t|    ||r dn||d|
   d}||
|	 d }qW qVW dS )	aZ  
    Params
    ------
    fin  : binary file with `read(buf_size : int)` method
    fout  : binary file with `write` (and optionally `flush`) methods.
    callback  : function(float), e.g.: `tqdm.update`
    callback_len  : If (default: True) do `callback(len(buffer))`.
      Otherwise, do `callback(data) for data in buffer.split(delim)`.
    flushc               S   s   d S )Nr   r   r   r   r   r   >   s    zposix_pipe.<locals>.<lambda>N    r   c               S   s   d S )Nr   r   r   r   r   r   U   s    r   )writereadgetattrlencountr   find)ZfinZfoutdelimbuf_sizecallbackcallback_lenfp_writetmpbufZ	len_delimir   r   r   
posix_pipe+   s>    


r/   z\n {8}(\S+)\s{2,}:\s*([^,]+)z\s*(?<!\S)--?([^\s=]+)(\s+|=|$)iterableguioutfilea  
        Extra CLI Options
        -----------------
        name  : type, optional
            TODO: find out why this is needed.
        delim  : chr, optional
            Delimiting character [default: '\n']. Use '\0' for null.
            N.B.: on Windows systems, Python converts '\n' to '\r\n'.
        buf_size  : int, optional
            String buffer size in bytes [default: 256]
            used when `delim` is specified.
        bytes  : bool, optional
            If true, will count bytes, ignore `delim`, and default
            `unit_scale` to True, `unit_divisor` to 1024, and `unit` to 'B'.
        tee  : bool, optional
            If true, passes `stdin` to both `stderr` and `stdout`.
        update  : bool, optional
            If true, will treat input as newly elapsed iterations,
            i.e. numbers to pass to `update()`. Note that this is slow
            (~2e5 it/s) since every input must be decoded as a number.
        update_to  : bool, optional
            If true, will treat input as total elapsed iterations,
            i.e. numbers to assign to `self.n`. Note that this is slow
            (~2e5 it/s) since every input must be decoded as a number.
        null  : bool, optional
            If true, will discard input (no stdout).
        manpath  : str, optional
            Directory in which to install tqdm man pages.
        comppath  : str, optional
            Directory in which to place tqdm completion.
        log  : str, optional
            CRITICAL|FATAL|ERROR|WARN(ING)|[default: 'INFO']|DEBUG|NOTSET.
c          &      s   dkrt jdd  y jd}W nB tk
rf   x, D ] }|jdr:|tdd }P q:W d}Y nX  |d  }tjtt|dd t	j
jt }ttj|}xtD ]}|j| qW tjt|j  tj|}t|ddd |d	dd |ddd }	d
jdd |	D }d}
|
d |jd d }t fdddED rbt jjtd  t jd nht fdddFD rt jj|d  t jd n4 rʈ d dd	 dkrt jjdj  d |
 t!jdjdg   tt ddd  ddd }tj| |jdd di}yxj|j D ]^\}}|j"dd}yt#||| ||< W n0 t$k
r } zt%t&|W Y dd}~X nX q,W tjd t&|  |jd!d"}|jd#d"}|jd$d"}t'|||fdkrt%d%W nP t(k
r2   jd&|
  t j)t jj }x|D ]}| qW  Y nX |jd'd(}|jd)d*}|jd+d"}|jd,d}|jd-d}|jd.d"rG d/d0 d0t*}nt j}t|d1|}tt j)d1t j)}|s|rVdd2l+m,} dd3l-m. yddl/W n" t0k
r   dd4l1m2 Y nX fd5d6}|dk	r0|d7|j|d7 |dk	rL|d8|j|d9 t jd |r|jtd1jG fd:d0d0t*}|r|j3d;d< |j3d=d |j3d>d? tj| t	f |t4||d
|j5 W dQ R X n|d*krtj| |j}|s|rft	f |F|r.fd@dA}nfdBdA}x|D ]}|| || q@W W dQ R X n xt	|f|D ]}|| qtW nhtj| t	f |Nd"}|rfdCdA}n|rʇfdDdA}n
j5}d}t4|||||| W dQ R X dS )Gz
    Parameters (internal use only)
    ---------
    fp  : file-like object for tqdm
    argv  : list (default: sys.argv[1:])
    Nr   z--logz--log=INFOz/%(levelname)s:%(module)s:%(lineno)d:%(message)s)levelformat      r   c             s   sT   | ]L}|d  t kr|d dkr"dndj|d  jdd|d  f|dd  V  qdS )r   r   r	   z
  --{0}  : {2}{3}z
  --{0}=<{1}>  : {2}{3}_-N)UNSUPPORTED_OPTSr6   replace).0Zotdr   r   r   	<genexpr>   s   zmain.<locals>.<genexpr>z!Usage:
  tqdm [--help | options]
z^
Options:
  -h, --help     Print this help and exit.
  -v, --version  Print version and exit.

c             3   s   | ]}| kV  qd S )Nr   )r=   v)argvr   r   r>      s    -v	--versionr   c             3   s   | ]}| kV  qd S )Nr   )r=   r@   )rA   r   r   r>      s    -h--helpz--zError:Unknown argument:{0}
{1} r   r   Tr3   r:   r9   zargs:bytesFupdate	update_toz1Can only have one of --bytes --update --update_toz
Error:
r(   r   r'   s   \nteemanpathcomppathnullc               @   s   e Zd Zedd ZdS )zmain.<locals>.stdoutc             S   s   d S )Nr   )r9   r   r   r   r!      s    zmain.<locals>.stdout.writeN)__name__
__module____qualname__staticmethodr!   r   r   r   r   stdout   s   rR   buffer)path)copyfile)	resourcesc          
      sZ   t dr$ tjd|  | n&jd| } t|| W dQ R X tjd| dS )zcopy resource `name` to `dst`filesr   Nz
written:%s)hasattrstrrW   rT   r   info)namedstsrc)rU   rV   r   r   cp   s
    
zmain.<locals>.cpztqdm.1zcompletion.shztqdm_completion.shc                   s    e Zd Ze fddZdS )zmain.<locals>.stdoutc          	      s,   t j d |  W d Q R X |  d S )N)r3   )r   Zexternal_write_mode)x)fpr+   stdout_writer   r   r!     s    zmain.<locals>.stdout.writeN)rN   rO   rP   rQ   r!   r   )r`   r+   ra   r   r   rR     s   unitBZ
unit_scaleZunit_divisori   c                s    j t| j  d S )N)rH   numericdecode)r.   )r   r   r   r)   #  s    zmain.<locals>.callbackc                s    j t| j  j  d S )N)rH   rd   re   n)r.   )r   r   r   r)   &  s    c                s    j t| j  d S )N)rH   rd   re   )r.   )r   r   r   r)   3  s    c                s    j t| j  j  d S )N)rH   rd   re   rf   )r.   )r   r   r   r)   6  s    )rB   rC   )rD   rE   )6sysrA   index
ValueError
startswithr$   loggingbasicConfigr#   r   __init____doc__CLI_EXTRA_DOCdictRE_OPTSfindallr;   popr   r   sorteditemsr   zipjoinstripanyrR   r!   r   exitstderrr6   RE_SHLEXr<   r   KeyErrorr   rY   sumr   stdinobjectosrT   shutilrU   Zimportlib_resourcesImportError	importlibrV   
setdefaultr/   rH   )r`   rA   Zlog_idxr.   ZlogLeveldZ	opt_typesor   Zopt_types_descZ
help_shortoptsZ	tqdm_argsr@   eZdelim_per_charrH   rI   r   r(   r'   rJ   rK   rL   rR   rT   r^   r!   r)   r*   r   )rA   rU   r`   r+   rV   ra   r   r   r      s    




*

"
$





 



)r0   r1   r2   r3   )rn   rk   rerg   Zastr   rd   Zstdr   r   r   versionr   __all__	getLoggerrN   r   r   r/   compilerq   r|   r;   ro   r{   r   r   r   r   r   <module>   s    
9

#