a
    IDg6                     @   s   d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
mZ dZedZdd ZdddZdd ZdddZdddZdd ZdS )a  
Functions that render ASCII tables.

Some generic notes about the table formatting functions in this module:

- These functions were not written with performance in mind (*at all*) because
  they're intended to format tabular data to be presented on a terminal. If
  someone were to run into a performance problem using these functions, they'd
  be printing so much tabular data to the terminal that a human wouldn't be
  able to digest the tabular data anyway, so the point is moot :-).

- These functions ignore ANSI escape sequences (at least the ones generated by
  the :mod:`~humanfriendly.terminal` module) in the calculation of columns
  widths. On reason for this is that column names are highlighted in color when
  connected to a terminal. It also means that you can use ANSI escape sequences
  to highlight certain column's values if you feel like it (for example to
  highlight deviations from the norm in an overview of calculated values).
    N)coerce_string)
ansi_strip
ansi_width	ansi_wrapterminal_supports_colorsfind_terminal_sizeHIGHLIGHT_COLOR)format_pretty_tableformat_robust_tableformat_rst_tableformat_smart_tablez^\d+(\.\d+)?$c                 C   sd   dd | D } t |}tdd | D sZt| |}ttt| }t \}}||krZ|S t| |S )a7  
    Render tabular data using the most appropriate representation.

    :param data: An iterable (e.g. a :func:`tuple` or :class:`list`)
                 containing the rows of the table, where each row is an
                 iterable containing the columns of the table (strings).
    :param column_names: An iterable of column names (strings).
    :returns: The rendered table (a string).

    If you want an easy way to render tabular data on a terminal in a human
    friendly format then this function is for you! It works as follows:

    - If the input data doesn't contain any line breaks the function
      :func:`format_pretty_table()` is used to render a pretty table. If the
      resulting table fits in the terminal without wrapping the rendered pretty
      table is returned.

    - If the input data does contain line breaks or if a pretty table would
      wrap (given the width of the terminal) then the function
      :func:`format_robust_table()` is used to render a more robust table that
      can deal with data containing line breaks and long text.
    c                 S   s   g | ]}t |qS  normalize_columns.0rr   r   e/mounts/lovelace/software/anaconda3/envs/paleomix/lib/python3.9/site-packages/humanfriendly/tables.py
<listcomp>W       z&format_smart_table.<locals>.<listcomp>c                 s   s    | ]}t d d |D V  qdS )c                 s   s   | ]}d |v V  qdS 
Nr   )r   cr   r   r   	<genexpr>[   r   z/format_smart_table.<locals>.<genexpr>.<genexpr>N)anyr   r   r   r   r   [   r   z%format_smart_table.<locals>.<genexpr>)	r   r   r	   maxmapr   
splitlinesr   r
   )datacolumn_namesZpretty_tabletable_widthnum_rowsnum_columnsr   r   r   r   >   s    

r   -|c              	   C   s  dd | D } |durBt |}|rBt r6dd |D }| d| tt}tt}t| D ]X\}}t|D ]F\}}	t|| t	|	||< |r|dksn|| 
ttt|	 qnq^|t| t|d  d  }
|
g}t| D ]\}}|g}t|D ]b\}}	d|| t	|	  }t|| r>|
d| |	 d  n|
d|	 | d  |
| q|
d	| |r|dkr|
|
 q|
|
 d
|S )af  
    Render a table using characters like dashes and vertical bars to emulate borders.

    :param data: An iterable (e.g. a :func:`tuple` or :class:`list`)
                 containing the rows of the table, where each row is an
                 iterable containing the columns of the table (strings).
    :param column_names: An iterable of column names (strings).
    :param horizontal_bar: The character used to represent a horizontal bar (a
                           string).
    :param vertical_bar: The character used to represent a vertical bar (a
                         string).
    :returns: The rendered table (a string).

    Here's an example:

    >>> from humanfriendly.tables import format_pretty_table
    >>> column_names = ['Version', 'Uploaded on', 'Downloads']
    >>> humanfriendly_releases = [
    ... ['1.23', '2015-05-25', '218'],
    ... ['1.23.1', '2015-05-26', '1354'],
    ... ['1.24', '2015-05-26', '223'],
    ... ['1.25', '2015-05-26', '4319'],
    ... ['1.25.1', '2015-06-02', '197'],
    ... ]
    >>> print(format_pretty_table(humanfriendly_releases, column_names))
    -------------------------------------
    | Version | Uploaded on | Downloads |
    -------------------------------------
    | 1.23    | 2015-05-25  |       218 |
    | 1.23.1  | 2015-05-26  |      1354 |
    | 1.24    | 2015-05-26  |       223 |
    | 1.25    | 2015-05-26  |      4319 |
    | 1.25.1  | 2015-06-02  |       197 |
    -------------------------------------

    Notes about the resulting table:

    - If a column contains numeric data (integer and/or floating point
      numbers) in all rows (ignoring column names of course) then the content
      of that column is right-aligned, as can be seen in the example above. The
      idea here is to make it easier to compare the numbers in different
      columns to each other.

    - The column names are highlighted in color so they stand out a bit more
      (see also :data:`.HIGHLIGHT_COLOR`). The following screen shot shows what
      that looks like (my terminals are always set to white text on a black
      background):

      .. image:: images/pretty-table.png
    c                 S   s   g | ]}t |d dqS )T)
expandtabsr   r   r   r   r   r      r   z'format_pretty_table.<locals>.<listcomp>Nc                 S   s   g | ]}t |qS r   highlight_column_namer   nr   r   r   r      r   r           r   )r   r   insertcollectionsdefaultdictintlist	enumerater   r   appendboolNUMERIC_DATA_PATTERNmatchr   sumvalueslenalljoin)r   r   Zhorizontal_barZvertical_barwidthsZnumeric_dataZ	row_indexrowcolumn_indexcolumnZline_delimiterlineslinepaddingr   r   r   r	   h   s:    4

  
r	   c                 C   s   g }dd t |D }t r*dd |D }| D ]p}g }tt |D ]P\}}| }d|vrr|d|| |f  qB|||  ||   qB|| q.t \}}	t	dd |D }
dd	t
|
|	  }|d
d |d |dd |D  S )a  
    Render tabular data with one column per line (allowing columns with line breaks).

    :param data: An iterable (e.g. a :func:`tuple` or :class:`list`)
                 containing the rows of the table, where each row is an
                 iterable containing the columns of the table (strings).
    :param column_names: An iterable of column names (strings).
    :returns: The rendered table (a string).

    Here's an example:

    >>> from humanfriendly.tables import format_robust_table
    >>> column_names = ['Version', 'Uploaded on', 'Downloads']
    >>> humanfriendly_releases = [
    ... ['1.23', '2015-05-25', '218'],
    ... ['1.23.1', '2015-05-26', '1354'],
    ... ['1.24', '2015-05-26', '223'],
    ... ['1.25', '2015-05-26', '4319'],
    ... ['1.25.1', '2015-06-02', '197'],
    ... ]
    >>> print(format_robust_table(humanfriendly_releases, column_names))
    -----------------------
    Version: 1.23
    Uploaded on: 2015-05-25
    Downloads: 218
    -----------------------
    Version: 1.23.1
    Uploaded on: 2015-05-26
    Downloads: 1354
    -----------------------
    Version: 1.24
    Uploaded on: 2015-05-26
    Downloads: 223
    -----------------------
    Version: 1.25
    Uploaded on: 2015-05-26
    Downloads: 4319
    -----------------------
    Version: 1.25.1
    Uploaded on: 2015-06-02
    Downloads: 197
    -----------------------

    The column names are highlighted in bold font and color so they stand out a
    bit more (see :data:`.HIGHLIGHT_COLOR`).
    c                 S   s   g | ]}d | qS )z%s:r   r(   r   r   r   r      r   z'format_robust_table.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   r&   r(   r   r   r   r      r   r   z%s %sc                 s   s   | ]}t tt|V  qd S N)r   r   r   )r   rA   r   r   r   r     r   z&format_robust_table.<locals>.<genexpr>z
%s
r#   r   r-   c                 s   s   | ]}d  |V  qdS r   r<   )r   br   r   r   r     r   )r   r   r3   stripr4   extendrstripr   r   r   minr.   r<   )r   r   blocksr>   rA   r?   Zcolumn_textZstripped_columnr!   r"   Zlongest_line	delimiterr   r   r   r
      s&    /

r
   c                 C   s   dd | D } |r"|  dt| tt}| D ],}t|D ]\}}t|| t|||< q<q0| D ]8}t|D ]*\}}|t|d k rn||| ||< qnqbdd t	|
 D }|r|  d| |  d| | | ddd | D S )	a  
    Render a table in reStructuredText_ format.

    :param data: An iterable (e.g. a :func:`tuple` or :class:`list`)
                 containing the rows of the table, where each row is an
                 iterable containing the columns of the table (strings).
    :param column_names: An iterable of column names (strings).
    :returns: The rendered table (a string).

    Here's an example:

    >>> from humanfriendly.tables import format_rst_table
    >>> column_names = ['Version', 'Uploaded on', 'Downloads']
    >>> humanfriendly_releases = [
    ... ['1.23', '2015-05-25', '218'],
    ... ['1.23.1', '2015-05-26', '1354'],
    ... ['1.24', '2015-05-26', '223'],
    ... ['1.25', '2015-05-26', '4319'],
    ... ['1.25.1', '2015-06-02', '197'],
    ... ]
    >>> print(format_rst_table(humanfriendly_releases, column_names))
    =======  ===========  =========
    Version  Uploaded on  Downloads
    =======  ===========  =========
    1.23     2015-05-25   218
    1.23.1   2015-05-26   1354
    1.24     2015-05-26   223
    1.25     2015-05-26   4319
    1.25.1   2015-06-02   197
    =======  ===========  =========

    .. _reStructuredText: https://en.wikipedia.org/wiki/ReStructuredText
    c                 S   s   g | ]}t |qS r   r   r   r   r   r   r   3  r   z$format_rst_table.<locals>.<listcomp>r   r+   c                 S   s   g | ]\}}d | qS )=r   )r   iwr   r   r   r   A  r   r   c                 s   s   | ]}d  |V  qdS )z  NrE   r   r   r   r   r   G  r   z#format_rst_table.<locals>.<genexpr>)r.   r   r/   r0   r1   r3   r   r:   ljustsorteditemsr4   r<   )r   r   r=   r>   indexr@   rL   r   r   r   r     s"    "

r   Fc                 C   s0   g }| D ]"}t |}|r | }|| q|S rD   )r   r%   r4   )r>   r%   resultsvaluetextr   r   r   r   J  s    r   c                 C   s   t | dtdS )NT)boldcolor)r   r   )namer   r   r   r'   T  s    r'   )Nr#   r$   )N)F)__doc__r/   reZhumanfriendly.compatr   Zhumanfriendly.terminalr   r   r   r   r   r   __all__compiler6   r   r	   r
   r   r   r'   r   r   r   r   <module>   s    

*
ZO
9

