U
    d#                     @   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-_3ph8cfy/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 |tkr.dd  n|tkr@dd  ndd  |rT|d nd|rdd| ndd| d g }t|D ]*\}}||_t|}	|	|	}
|
|
 qt|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                 S   s
   |  |S r   )ljustr   r&   r   r   r   <lambda>B       z*ReSTFormat._row_to_lines.<locals>.<lambda>c                 S   s
   |  |S r   )r   r5   r   r   r   r6   D   r7   c                 S   s
   |  |S r   )rjustr5   r   r   r   r6   F   r7     	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justwidthsr   r   r   R   s     z5ReSTFormat._row_to_lines.<locals>.<genexpr>.<genexpr>N)r*   r   liner<   r   r   r   Q   s   z+ReSTFormat._row_to_lines.<locals>.<genexpr>c                    s"   g | ]}d    |fqS )r:   )joinr?   )lpadpadrpadr   r   r    U   s     z,ReSTFormat._row_to_lines.<locals>.<listcomp>)JUSTIFY_VALUES
ValueErrorformatrA   JUSTIFY_LEFTJUSTIFY_CENTERzipr&   r   wrapr+   r   )r,   r#   r>   wrappersepjustifycellsr   r&   r   celllinesr   )r=   rB   rC   rD   r>   r   _row_to_lines8   s0    


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    _   s     z1ReSTFormat._get_column_widths.<locals>.<listcomp>c                 3   s   | ]} |  V  qd S r   r   r   l)max_table_widthtotalr   r   r   b   s     z0ReSTFormat._get_column_widths.<locals>.<genexpr>c                 s   s   | ]
}|V  qd S r   r   rU   r   r   r   r   d   s     c                 3   s"   | ]}| kr|  n|V  qd S r   r   r   wpad_lenr   r   r   f   s     c                 S   s   g | ]\}}t ||qS r   )r   )r   rZ   rV   r   r   r   r    h   s     )r2   sumr   rJ   )r,   r-   rW   r\   Zstr_lensr.   Zmedian_lenscolumn_widthsr   )rW   r\   rX   r   _get_column_widthsX   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   r[   z  c                 S   s   g | ]}d | qS =r   rY   r   r   r   r    t   s     z9ReSTFormat.export_set_as_simple_table.<locals>.<listcomp>r:   )rM   rN   r#   
r   r_   rA   r+   r$   extendrR   rI   r'   r(   r)   r#   )r,   r-   r^   rQ   rL   Zborderr/   r#   r   r   r   export_set_as_simple_tablek   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 ra   r   rY   r   r   r   r       s     z7ReSTFormat.export_set_as_grid_table.<locals>.<listcomp>z=+z+-z-+-c                 S   s   g | ]}d | qS )-r   rY   r   r   r   r       s     z-+)rN   r#   rc   rd   )	r,   r-   r^   rQ   rL   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width0rP   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_gridFrW   r   N)	r'   getMAX_TABLE_WIDTHr_   ri   r$   Zget_colrf   rh   )r,   r-   kwargsrj   rW   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)rj   N)rn   )r   r-   r,   r   r   r     s   z)ReSTFormat.export_book.<locals>.<genexpr>)rA   Z	_datasets)r,   Zdatabookr   ro   r   export_book   s    zReSTFormat.export_book)N)N)__name__
__module____qualname__title
extensionsrl   classmethodr2   rH   rR   r_   rf   rh   ri   rn   rp   r   r   r   r   r      s&   
3

-r   N)__doc__	itertoolsr   Z
statisticsr   textwrapr   rH   rI   ZJUSTIFY_RIGHTrE   r   r   r   r   r   r   r   <module>   s   

