
    ?wg
$                     b    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 Zd	 Z G d
 d      Zy)z# Tablib - reStructuredText Support
    )zip_longest)median)TextWrapperleftcenterrightc                 Z    t        | t              r| j                  d      S t        |       S )Nzutf-8)
isinstancebytesdecodestr)values    J/var/www/horilla/myenv/lib/python3.12/site-packages/tablib/formats/_rst.pyto_strr      s%    %||G$$u:    c                 N    | r"t        d | j                         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              3   2   K   | ]  }t        |        y wN)len).0words     r   	<genexpr>z _max_word_len.<locals>.<genexpr>   s     3dD	3s   r   )default)maxsplit)texts    r   _max_word_lenr      s&     DH33djjl3Q?NQNr   c                       e Zd ZdZdZdZed        Zedefd       Z	eedfd       Z
edd
       Zedd       Zed        Zed        Zed        Zy	)
ReSTFormatrst)r    P   c                 z   |j                   rF|j                   D cg c]  }t        |      g }}|j                   D cg c]  }t        |       }}nDt        |j                        D cg c]  }g  }}t        |j                        D cg c]  }d }}|j
                  D ]  }t        t        |d      r|j                         n|      }t        |      D ]H  \  }}	t        |	      }
||   j                  t        |
             t        ||   t        |
            ||<   J  ||fS c c}w c c}w c c}w c c}w )zn
        Returns a list of string lengths of each column, and a list of
        maximum word lengths.
        r   values)headersr   r   rangewidthdictiterhasattrr#   	enumerater   appendr   )clsdatasethcolumn_lengths	word_lens_rowr#   ivalr   s              r   _get_column_string_lengthsz%ReSTFormat._get_column_string_lengths$   s    ??07@1s1vh@N@3:??Caq)CIC*/*>?Qb?N?$)'--$89q9I9<< 	FC'#x*@#**,cJF#F+ F3c{q!((T3"9Q<t1DE	!F	F y(( AC?9s   D)D.*	D3	D8|c           	         t         vr-t        dj                  dj                  t                           fd|r|dz   nd}|rd|z   nd}d|z   dz   }g }	t	        |      D ]9  \  }
}||_        t        |
      }|j                  |      }|	j                  |       ; t        |	ddi}fd|D        }|D cg c]%  }dj                  ||j                  |      |f      ' }}|S c c}w )zJ
        Returns a table row of wrapped values as a list of lines
        z&Value of "justify" must be one of "{}"z", "c                     t         k(  r| j                  |      S t        k(  r| j                  |      S | j	                  |      S r   )JUSTIFY_LEFTljustJUSTIFY_CENTERr   rjust)text_width_justifys     r   justz&ReSTFormat._row_to_lines.<locals>.justB   s@    ,&{{6**N*||F++{{6**r     	fillvaluec              3   H   K   | ]  }fd t        |      D          yw)c              3   <   K   | ]  \  }} ||           y wr    )r   r3   	cell_liner@   widthss      r   r   z5ReSTFormat._row_to_lines.<locals>.<genexpr>.<genexpr>T   s      LLAyT)VAY'Ls   N)r*   )r   liner@   rH   s     r   r   z+ReSTFormat._row_to_lines.<locals>.<genexpr>S   s&      
 MIdOLL
s   ")
JUSTIFY_VALUES
ValueErrorformatjoinzipr&   r   wrapr+   r   )r,   r#   rH   wrappersepr?   lpadrpadpadcellsr   r&   r   celllinesrI   r@   s     `  `          @r   _row_to_lineszReSTFormat._row_to_lines8   s   
 .(ELLN+  	+  sSyRsSyRCi#o/ 	LE5!GM%=D<<%DLL		
 U1b1

 DII4$56II Js   =*C+   c                 \   | j                  |      \  }}|D cg c]  }t        t        |             }}t        |      t	        |      z  z
  kD  rfd|D        }n	d |D        }fd|D        }t        ||      D 	
cg c]  \  }	}
t        |	|
       }}	}
|S c c}w c c}
}	w )zw
        Returns a list of column widths proportional to the median length
        of the text in their cells.
        c              3   .   K   | ]  }|z  z    y wr   rF   )r   lensmax_table_widthtotals     r   r   z0ReSTFormat._get_column_widths.<locals>.<genexpr>d   s     U_t3u<Us   c              3       K   | ]  }|  y wr   rF   )r   r\   s     r   r   z0ReSTFormat._get_column_widths.<locals>.<genexpr>f   s     :dT:s   c              3   6   K   | ]  }|kD  r|z
  n|  y wr   rF   )r   wpad_lens     r   r   z0ReSTFormat._get_column_widths.<locals>.<genexpr>h   s      RqGW:Rs   )r5   intr   sumr   rN   r   )r,   r-   r]   rb   str_lensr0   r\   median_lenscolumn_widthsra   lr^   s     ``       @r   _get_column_widthszReSTFormat._get_column_widthsZ   s     "<<WE)5=>Ts6$<(>>K ?gK0@&@AAUUM:k:MRMR/2=)/LMtq!QMM ? Ns   B#B(Nc           	      N   g }t               }|| j                  |d      }dj                  |D cg c]  }d|z  	 c}      }|j                  |       |j                  rD|j                  | j                  |j                  ||dt                     |j                  |       |j                  D ]L  }t        t        |d      r|j                         n|      }|j                  | j                  |||d             N |j                  |       dj                  |      S c c}w )	zP
        Returns reStructuredText grid table representation of dataset.
           )rb   z  =rB   )rQ   r?   r#   
r   ri   rM   r+   r$   extendrX   r;   r'   r(   r)   r#   )	r,   r-   rg   rW   rP   ra   borderr2   r#   s	            r   export_set_as_simple_tablez%ReSTFormat.export_set_as_simple_tablem   s	   
 - 227A2FM];C!G;<V??LL**& +   LL << 	PC'#x*@#**,cJFLL**6='2NO	P 	Vyy! <s   D"c                    g }t               }|| j                  |      }ddj                  |D cg c]  }d|z  	 c}      z   dz   }ddj                  |D cg c]  }d|z  	 c}      z   dz   }|j                  |       |j                  rC|j                  | j                  |j                  ||t        	             |j                  |       |j                  D ]\  }t        t        |d
      r|j                         n|      }	|j                  | j                  |	||             |j                  |       ^ dj                  |      S c c}w c c}w )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  |
        +-------+-------+-------+

        z+=z=+=rl   z=+z+-z-+--z-+)r?   r#   rm   rn   )
r,   r-   rg   rW   rP   ra   
header_seprow_sepr2   r#   s
             r   export_set_as_grid_tablez#ReSTFormat.export_set_as_grid_table   s2   : - 227;MEJJ'GAa'GHH4O
m$DS1W$DEELW??LL**&	 +   LL$<< 	"C'#x*@#**,cJFLL**6='JKLL!	" yy# (H$Ds   E
E
c                     |t        |      }t        |      |kD  ry|D ]  }t        |      }t        |      |kD  s y y)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

        FT)r   r   )r,   head0col0width0rV   s        r   _use_simple_tablezReSTFormat._use_simple_table   sN     5ME5zF" 	D$<D4y6!	 r   c                 l   |j                   sy|j                  dd      }|j                  d| j                        }| j                  ||      }| j	                  |j
                  r|j
                  d   nd|j                  d      |d         }|r|s| j                  ||      S | j                  ||      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

        rB   
force_gridFr]   r   N)	r'   getMAX_TABLE_WIDTHri   r{   r$   get_colrq   rv   )r,   r-   kwargsr}   r]   rg   use_simple_tables          r   
export_setzReSTFormat.export_set   s    < ||ZZe4
 **%68K8KL..wH00")//GOOAtOOA!

 J11'=II//GGr   c                 L     dj                   f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   D   K   | ]  }j                  |d         yw)T)r}   N)r   )r   r-   r,   s     r   r   z)ReSTFormat.export_book.<locals>.<genexpr>
  s&      >& >>'d>C >s    )rM   	_datasets)r,   databooks   ` r   export_bookzReSTFormat.export_book  s)     {{ >*2*<*<> > 	>r   r   )__name__
__module____qualname__title
extensionsr   classmethodr5   r9   rX   ri   rq   rv   r{   r   r   rF   r   r   r   r      s    EJO) )& 8;\  B 9HRS  $    4 1  1 f  . +H +HZ > >r   r   N)__doc__	itertoolsr   
statisticsr   textwrapr   r9   r;   JUSTIFY_RIGHTrJ   r   r   r   rF   r   r   <module>r      sG    "   >Om> m>r   