U
    ڶdd^&                     @   s   d 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ZmZm	Z	m
Z
mZmZm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mZmZ ddlmZ dZdd	 Zd
d Z dd Z!dd Z"dd Z#dd Z$dd Z%dd Z&dd Z'dd Z(dddZ)dS )aN  
Usage: humanfriendly [OPTIONS]

Human friendly input/output (text formatting) on the command
line based on the Python package with the same name.

Supported options:

  -c, --run-command

    Execute an external command (given as the positional arguments) and render
    a spinner and timer while the command is running. The exit status of the
    command is propagated.

  --format-table

    Read tabular data from standard input (each line is a row and each
    whitespace separated field is a column), format the data as a table and
    print the resulting table to standard output. See also the --delimiter
    option.

  -d, --delimiter=VALUE

    Change the delimiter used by --format-table to VALUE (a string). By default
    all whitespace is treated as a delimiter.

  -l, --format-length=LENGTH

    Convert a length count (given as the integer or float LENGTH) into a human
    readable string and print that string to standard output.

  -n, --format-number=VALUE

    Format a number (given as the integer or floating point number VALUE) with
    thousands separators and two decimal places (if needed) and print the
    formatted number to standard output.

  -s, --format-size=BYTES

    Convert a byte count (given as the integer BYTES) into a human readable
    string and print that string to standard output.

  -b, --binary

    Change the output of -s, --format-size to use binary multiples of bytes
    (base-2) instead of the default decimal multiples of bytes (base-10).

  -t, --format-timespan=SECONDS

    Convert a number of seconds (given as the floating point number SECONDS)
    into a human readable timespan and print that string to standard output.

  --parse-length=VALUE

    Parse a human readable length (given as the string VALUE) and print the
    number of metres to standard output.

  --parse-size=VALUE

    Parse a human readable data size (given as the string VALUE) and print the
    number of bytes to standard output.

  --demo

    Demonstrate changing the style and color of the terminal font using ANSI
    escape sequences.

  -h, --help

    Show this message and exit.
    N)Timerformat_lengthformat_numberformat_sizeformat_timespanparse_length
parse_size)format_pretty_tableformat_smart_table)
ANSI_COLOR_CODESANSI_TEXT_STYLESHIGHLIGHT_COLOR
ansi_strip	ansi_wrapenable_ansi_supportfind_terminal_sizeoutputusagewarning)Spinner)demonstrate_256_colorsdemonstrate_ansi_formattingmainprint_formatted_lengthprint_formatted_numberprint_formatted_sizeprint_formatted_tableprint_formatted_timespanprint_parsed_lengthprint_parsed_sizerun_commandc            
      C   s  t   z8ttjdd ddddddd	d
dddddg\} }W n6 tk
rt } ztd| td W 5 d}~X Y nX g }d}d}tdd | D }| D ]"\}}|dkr|}q|dkr|t	
t| q|dkr|t	
t| q|dkr|t	
t| q|dkr$|t	
t| q|dkrB|t	
t| q|dkrb|t	
t|| q|dkrrd}q|dkr|t	
t| q|dkr|t q|dkrtt  dS q|r|t	
t| |stt dS |D ]}	|	  qdS ) z9Command line interface for the ``humanfriendly`` program.   Nzcd:l:n:s:bt:hzrun-commandzformat-tablez
delimiter=zformat-length=zformat-number=zformat-size=binaryzformat-timespan=zparse-length=zparse-size=Zdemohelpz	Error: %sFc                 s   s   | ]\}}|d kV  qdS ))z-bz--binaryN ).0ovr$   r$   5/tmp/pip-unpacked-wheel-u0wxwz4h/humanfriendly/cli.py	<genexpr>   s     zmain.<locals>.<genexpr>)z-dz--delimiterz--parse-sizez--parse-length)z-cz--run-command)z-lz--format-length)z-nz--format-number)z-sz--format-sizez--format-tableT)z-tz--format-timespanz--demo)z-hz--help)r   getoptsysargv	Exceptionr   exitanyappend	functoolspartialr   r   r    r   r   r   r   r   r   __doc__r   )
options	argumentseactions	delimiterZshould_format_tabler"   optionvaluer2   r$   r$   r(   r      sl             







r   c              	   C   sn   t  }ddttj|  }t||d0}t| }|  |	  |
 dk	r4qTq4W 5 Q R X t|j dS )zHRun an external command and show a spinner while the command is running.zWaiting for command: %s )labeltimerN)r   joinmappipesquoter   
subprocessPopenstepsleeppollr+   r.   
returncode)Zcommand_liner=   Zspinner_labelspinnerprocessr$   r$   r(   r       s    
r    c                 C   s.   d| krt tt|  nt tt|  dS )zPrint a human readable length..N)r   r   floatintr:   r$   r$   r(   r      s    r   c                 C   s   t tt|  dS )z/Print large numbers in a human readable format.N)r   r   rK   rM   r$   r$   r(   r      s    r   c                 C   s   t tt| |d dS )zPrint a human readable size.)r"   N)r   r   rL   )r:   r"   r$   r$   r(   r      s    r   c                 C   s8   g }t jD ]}| }|||  q
tt| dS )z8Read tabular data from standard input and print a table.N)r+   stdinrstripr0   splitr   r	   )r8   dataliner$   r$   r(   r      s
    
r   c                 C   s   t tt|  dS )z Print a human readable timespan.N)r   r   rK   rM   r$   r$   r(   r      s    r   c                 C   s   t t|  dS )z=Parse a human readable length and print the number of metres.N)r   r   rM   r$   r$   r(   r      s    r   c                 C   s   t t|  dS )z?Parse a human readable data size and print the number of bytes.N)r   r   rM   r$   r$   r(   r      s    r   c                     s:  t dtddd ddg} | t  t| D ]@}ttd}|dkrNd||< |dd		 }t d
t|f| q0dD ]\ }dt fdtddfg dkr
ddtddf t dtd| dd  t t fddtt D dgdd D  d qvtddd tddd tddd td d!d" d#S )$z-Demonstrate the use of ANSI escape sequences.z%szText styles:TZboldnormalbrightcolor_r;   z - %s))rW   zForeground colors)
backgroundzBackground colors)rU   rY   r   faint)rZ   z
%s%s:c                    s&   g | ]  g fd dD  qS )c                    s>   g | ]6\}}t d krdndftt|  fg qS )rY   ZXXXXXXz      )r   dictlistitemsr%   r<   kw)
color_name
color_typer$   r(   
<listcomp>   s   z:demonstrate_ansi_formatting.<locals>.<listcomp>.<listcomp>r$   )r%   rb   Zintensities)ra   r(   rc      s   z/demonstrate_ansi_formatting.<locals>.<listcomp>ZColorc                 S   s   g | ]\}}|  qS r$   )
capitalizer_   r$   r$   r(   rc     s    )Zcolumn_names   zstandard colors      zhigh-intensity colors      z
216 colors      zgray scale colorsN)r   r   extendr   keyssortedr\   r   replacere   insertr
   r   r   )ZstylesZ
style_namer4   Zstyle_labelZcolor_labelr$   rd   r(   r      s6    



r   c                    s   d}|r|d| 7 }t dtd| dd  ddd	 t| |d
 D }t \}}|tt|krnt | nptt|d
 t| fddt| |d
 D   fddtdt D }t ddd	 |D  dS )z#Demonstrate 256 color mode support.z256 color modez (%s)
r[   TrS    c                 s   s"   | ]}d t t||d V  qdS )r;   rV   N)r   strr%   nr$   r$   r(   r)     s     z)demonstrate_256_colors.<locals>.<genexpr>r!   c                    s"   g | ]}t t| |d qS )rV   )r   rt   rjustru   )widthr$   r(   rc   !  s     z*demonstrate_256_colors.<locals>.<listcomp>c                    s   g | ]} ||  qS r$   r$   ru   )colorscolors_per_liner$   r(   rc   "  s     r   c                 s   s   | ]}d  |V  qdS )rs   N)r>   )r%   br$   r$   r(   r)   #  s     N)	r   r   r>   ranger   lenr   rt   rL   )ijgroupr<   Zsingle_linelinescolumnsblocksr$   )ry   rz   rx   r(   r     s    

 r   )N)*r3   r1   r*   r@   rB   r+   Zhumanfriendlyr   r   r   r   r   r   r   Zhumanfriendly.tablesr	   r
   Zhumanfriendly.terminalr   r   r   r   r   r   r   r   r   r   Zhumanfriendly.terminal.spinnersr   __all__r   r    r   r   r   r   r   r   r   r   r   r$   r$   r$   r(   <module>   s*   I$	01	'