U
    g
$                     @   s`   d Z ddlmZ ddlmZ ddlmZ dZdZdZ	eee	fZ
dd	 Zd
d ZG dd dZdS )z# Tablib - reStructuredText Support
    )zip_longest)median)TextWrapperleftcenterrightc                 C   s   t | tr| dS t| S )Nzutf-8)
isinstancebytesdecodestr)value r   7/tmp/pip-unpacked-wheel-2jt74g4_/tablib/formats/_rst.pyto_str   s    

r   c                 C   s"   | rt dd |  D ddS dS )z}
    Return the length of the longest word in `text`.

    >>> _max_word_len('Python Module for Tabular Datasets')
    8
    c                 s   s   | ]}t |V  qd S Nlen).0wordr   r   r   	<genexpr>   s     z _max_word_len.<locals>.<genexpr>r   )default)maxsplit)textr   r   r   _max_word_len   s    r   c                   @   s   e Zd ZdZdZdZedd ZedefddZ	eed	fd
dZ
edddZedddZedd Zedd Zedd ZdS )
ReSTFormatrst)r   P   c           	      C   s   |j r(dd |j D }dd |j D }n(dd t|jD }dd t|jD }|jD ]`}tt|drn| n|}t|D ]8\}}t|}|| 	t
| t|| t|||< q|qV||fS )zn
        Returns a list of string lengths of each column, and a list of
        maximum word lengths.
        c                 S   s   g | ]}t |gqS r   r   r   hr   r   r   
<listcomp>+   s     z9ReSTFormat._get_column_string_lengths.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   )r   r   r   r   r   r    ,   s     c                 S   s   g | ]}g qS r   r   r   _r   r   r   r    .   s     c                 S   s   g | ]}d qS )r   r   r!   r   r   r   r    /   s     values)headersrangewidthdictiterhasattrr#   	enumerater   appendr   r   r   )	clsdatasetZcolumn_lengths	word_lensrowr#   ivalr   r   r   r   _get_column_string_lengths$   s    
z%ReSTFormat._get_column_string_lengths|c                    s   t krtddt fdd |r4|d nd|rDd| ndd| d g }t|D ]*\}}||_t|}	||	}
||
 qbt	|ddi} fdd	|D }fd
d|D }|S )zJ
        Returns a table row of wrapped values as a list of lines
        z&Value of "justify" must be one of "{}"z", "c                    s2    t kr| |S  tkr$| |S | |S d S r   )JUSTIFY_LEFTljustJUSTIFY_CENTERr   rjust)Ztext_Zwidth_justifyr   r   justB   s
    

z&ReSTFormat._row_to_lines.<locals>.just  	fillvaluec                 3   s&   | ]} fd dt |D V  qdS )c                 3   s    | ]\}} || V  qd S r   r   )r   r0   Z	cell_liner:   widthsr   r   r   T   s     z5ReSTFormat._row_to_lines.<locals>.<genexpr>.<genexpr>N)r*   r   liner>   r   r   r   S   s   z+ReSTFormat._row_to_lines.<locals>.<genexpr>c                    s"   g | ]}d    |fqS )r<   )joinr@   )lpadpadrpadr   r   r    W   s     z,ReSTFormat._row_to_lines.<locals>.<listcomp>)
JUSTIFY_VALUES
ValueErrorformatrB   zipr&   r   wrapr+   r   )r,   r#   r?   wrappersepr9   cellsr   r&   r   celllinesr   )r:   r9   rC   rD   rE   r?   r   _row_to_lines8   s(    
zReSTFormat._row_to_lines   c                    s   |  |\}}dd |D }t| t|  krN fdd|D }ndd |D }fdd|D }dd t||D }|S )zw
        Returns a list of column widths proportional to the median length
        of the text in their cells.
        c                 S   s   g | ]}t t|qS r   )intr   r   Zlensr   r   r   r    a   s     z1ReSTFormat._get_column_widths.<locals>.<listcomp>c                 3   s   | ]} |  V  qd S r   r   rS   )max_table_widthtotalr   r   r   d   s     z0ReSTFormat._get_column_widths.<locals>.<genexpr>c                 s   s   | ]
}|V  qd S r   r   rS   r   r   r   r   f   s     c                 3   s"   | ]}| kr|  n|V  qd S r   r   r   wpad_lenr   r   r   h   s     c                 S   s   g | ]\}}t ||qS r   )r   )r   rW   lr   r   r   r    j   s     )r2   sumr   rI   )r,   r-   rT   rY   Zstr_lensr.   Zmedian_lenscolumn_widthsr   )rT   rY   rU   r   _get_column_widthsZ   s    zReSTFormat._get_column_widthsNc              	   C   s   g }t  }|dkr | j|dd}ddd |D }|| |jrj|| j|j||dtd || |jD ]4}t	t
|d	r| n|}|| |||d qp|| d
|S )zP
        Returns reStructuredText grid table representation of dataset.
        N   rX   z  c                 S   s   g | ]}d | qS =r   rV   r   r   r   r    v   s     z9ReSTFormat.export_set_as_simple_table.<locals>.<listcomp>r<   )rL   r9   r#   
r   r]   rB   r+   r$   extendrP   r6   r'   r(   r)   r#   )r,   r-   r\   rO   rK   Zborderr/   r#   r   r   r   export_set_as_simple_tablem   s(    



z%ReSTFormat.export_set_as_simple_tablec           	      C   s   g }t  }|dkr| |}dddd |D  d }ddd	d |D  d
 }|| |jr|| j|j||td || |jD ]<}t	t
|dr| n|}|| ||| || qd|S )a  
        Returns reStructuredText grid table representation of dataset.


        >>> from tablib import Dataset
        >>> from tablib.formats import registry
        >>> bits = ((0, 0), (1, 0), (0, 1), (1, 1))
        >>> data = Dataset()
        >>> data.headers = ['A', 'B', 'A and B']
        >>> for a, b in bits:
        ...     data.append([bool(a), bool(b), bool(a * b)])
        >>> rst = registry.get_format('rst')
        >>> print(rst.export_set(data, force_grid=True))
        +-------+-------+-------+
        |   A   |   B   | A and |
        |       |       |   B   |
        +=======+=======+=======+
        | False | False | False |
        +-------+-------+-------+
        | True  | False | False |
        +-------+-------+-------+
        | False | True  | False |
        +-------+-------+-------+
        | True  | True  | True  |
        +-------+-------+-------+

        Nz+=z=+=c                 S   s   g | ]}d | qS r_   r   rV   r   r   r   r       s     z7ReSTFormat.export_set_as_grid_table.<locals>.<listcomp>z=+z+-z-+-c                 S   s   g | ]}d | qS )-r   rV   r   r   r   r       s     z-+r8   r#   ra   rb   )	r,   r-   r\   rO   rK   Z
header_sepZrow_sepr/   r#   r   r   r   export_set_as_grid_table   s(    



z#ReSTFormat.export_set_as_grid_tablec                 C   sH   |dk	r t |}t||kr dS |D ]}t |}t||kr$ dS q$dS )a[  
        Use a simple table if the text in the first column is never wrapped


        >>> from tablib.formats import registry
        >>> rst = registry.get_format('rst')
        >>> rst._use_simple_table('menu', ['egg', 'bacon'], 10)
        True
        >>> rst._use_simple_table(None, ['lobster thermidor', 'spam'], 10)
        False

        NFT)r   r   )r,   Zhead0Zcol0Zwidth0rN   r   r   r   _use_simple_table   s    zReSTFormat._use_simple_tablec                 K   s|   |j s
dS |dd}|d| j}| ||}| |jrD|jd nd|d|d }|rl|sl| ||S | ||S dS )a  
        Returns reStructuredText table representation of dataset.

        Returns a simple table if the text in the first column is never
        wrapped, otherwise returns a grid table.


        >>> from tablib import Dataset
        >>> bits = ((0, 0), (1, 0), (0, 1), (1, 1))
        >>> data = Dataset()
        >>> data.headers = ['A', 'B', 'A and B']
        >>> for a, b in bits:
        ...     data.append([bool(a), bool(b), bool(a * b)])
        >>> table = data.rst
        >>> table.split('\n') == [
        ...     '=====  =====  =====',
        ...     '  A      B    A and',
        ...     '                B  ',
        ...     '=====  =====  =====',
        ...     'False  False  False',
        ...     'True   False  False',
        ...     'False  True   False',
        ...     'True   True   True ',
        ...     '=====  =====  =====',
        ... ]
        True

        r<   
force_gridFrT   r   N)	r'   getMAX_TABLE_WIDTHr]   rg   r$   Zget_colrd   rf   )r,   r-   kwargsrh   rT   r\   Zuse_simple_tabler   r   r   
export_set   s    zReSTFormat.export_setc                    s   d  fdd|jD S )z
        reStructuredText representation of a Databook.

        Tables are separated by a blank line. All tables use the grid
        format.
        z

c                 3   s   | ]} j |d dV  qdS )T)rh   N)rl   )r   r-   r,   r   r   r   
  s   z)ReSTFormat.export_book.<locals>.<genexpr>)rB   Z	_datasets)r,   Zdatabookr   rm   r   export_book  s    zReSTFormat.export_book)N)N)__name__
__module____qualname__title
extensionsrj   classmethodr2   r4   rP   r]   rd   rf   rg   rl   rn   r   r   r   r   r      s&   
!3

-r   N)__doc__	itertoolsr   Z
statisticsr   textwrapr   r4   r6   ZJUSTIFY_RIGHTrF   r   r   r   r   r   r   r   <module>   s   

