o
    e
$                     @   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   F/var/www/html/venv/lib/python3.10/site-packages/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 ]0}tt|dr7| n|}t|D ]\}}t|}|| 	t
| t|| t|||< q>q+||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    ,       c                 S   s   g | ]}g qS r   r   r   _r   r   r   r    .       c                 S   s   g | ]}d qS )r   r   r"   r   r   r   r    /   r$   values)headersrangewidthdictiterhasattrr%   	enumerater   appendr   r   r   )	clsdatasetcolumn_lengths	word_lensrowr%   ivalr   r   r   r   _get_column_string_lengths$   s   
z%ReSTFormat._get_column_string_lengths|c                    s   t vrtddt fdd |r|d nd|r"d| ndd| d g }t|D ]\}}||_t|}	||	}
||
 q1t	|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.    t kr	| |S  tkr| |S | |S r   )JUSTIFY_LEFTljustJUSTIFY_CENTERr   rjust)text_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   r3   	cell_liner?   widthsr   r   r   T   s     z5ReSTFormat._row_to_lines.<locals>.<genexpr>.<genexpr>N)r,   r   linerD   r   r   r   S   s
    
z+ReSTFormat._row_to_lines.<locals>.<genexpr>c                    s"   g | ]}d    |fqS )rA   )joinrF   )lpadpadrpadr   r   r    W   s   " z,ReSTFormat._row_to_lines.<locals>.<listcomp>)
JUSTIFY_VALUES
ValueErrorformatrH   zipr(   r   wrapr-   r   )r.   r%   rE   wrappersepr>   cellsr   r(   r   celllinesr   )r?   r>   rI   rJ   rK   rE   r   _row_to_lines8   s(   
zReSTFormat._row_to_lines   c                    s   |  |\}}dd |D }t| t|  kr' 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   lensr   r   r   r    a   s    z1ReSTFormat._get_column_widths.<locals>.<listcomp>c                 3   s    | ]	} |  V  qd S r   r   rY   )max_table_widthtotalr   r   r   d   s    z0ReSTFormat._get_column_widths.<locals>.<genexpr>c                 s   s    | ]}|V  qd S r   r   rY   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   r^   lr   r   r   r    j   s    )r5   sumr   rO   )r.   r/   r[   r`   str_lensr1   median_lenscolumn_widthsr   )r[   r`   r\   r   _get_column_widthsZ   s   zReSTFormat._get_column_widthsNc              	   C   s   g }t  }|du r| j|dd}ddd |D }|| |jr5|| j|j||dtd || |jD ]}t	t
|d	rD| n|}|| |||d q8|| d
|S )zP
        Returns reStructuredText grid table representation of dataset.
        N   r_   z  c                 S      g | ]}d | qS =r   r]   r   r   r   r    v   r!   z9ReSTFormat.export_set_as_simple_table.<locals>.<listcomp>rA   )rR   r>   r%   
r   rf   rH   r-   r&   extendrV   r9   r)   r*   r+   r%   )r.   r/   re   rU   rQ   borderr2   r%   r   r   r   export_set_as_simple_tablem   s(   




z%ReSTFormat.export_set_as_simple_tablec           	      C   s   g }t  }|du r| |}dddd |D  d }ddd	d |D  d
 }|| |jrD|| j|j||td || |jD ]}t	t
|drS| n|}|| ||| || qGd|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   rh   ri   r   r]   r   r   r   r       r!   z7ReSTFormat.export_set_as_grid_table.<locals>.<listcomp>z=+z+-z-+-c                 S   rh   )-r   r]   r   r   r   r       r!   z-+r=   r%   rk   rl   )	r.   r/   re   rU   rQ   
header_seprow_sepr2   r%   r   r   r   export_set_as_grid_table   s(   




z#ReSTFormat.export_set_as_grid_tablec                 C   sH   |du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.   head0col0width0rT   r   r   r   _use_simple_table   s   zReSTFormat._use_simple_tablec                 K   sx   |j sdS |dd}|d| j}| ||}| |jr"|jd nd|d|d }|r6|s6| ||S | ||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

        rA   
force_gridFr[   r   N)	r)   getMAX_TABLE_WIDTHrf   rw   r&   get_colro   rs   )r.   r/   kwargsrx   r[   re   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)rx   N)r~   )r   r/   r.   r   r   r   
  s    z)ReSTFormat.export_book.<locals>.<genexpr>)rH   	_datasets)r.   databookr   r   r   export_book  s   zReSTFormat.export_bookr   )__name__
__module____qualname__title
extensionsrz   classmethodr5   r7   rV   rf   ro   rs   rw   r~   r   r   r   r   r   r      s(    
!3

-r   N)__doc__	itertoolsr   
statisticsr   textwrapr   r7   r9   JUSTIFY_RIGHTrL   r   r   r   r   r   r   r   <module>   s    

