
    $g
$                     j    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dS )z# Tablib - reStructuredText Support
    )zip_longest)median)TextWrapperleftcenterrightc                 t    t          | t                    r|                     d          S t          |           S )Nzutf-8)
isinstancebytesdecodestr)values    S/var/www/html/netbox-4.1.3/venv/lib/python3.11/site-packages/tablib/formats/_rst.pyto_strr      s2    % %||G$$$u::    c                 d    | r-t          d |                                 D             d          ndS )z}
    Return the length of the longest word in `text`.

    >>> _max_word_len('Python Module for Tabular Datasets')
    8
    c              3   4   K   | ]}t          |          V  d S Nlen).0words     r   	<genexpr>z _max_word_len.<locals>.<genexpr>   s(      33dD		333333r   r   )default)maxsplit)texts    r   _max_word_lenr      s:     DHN333djjll333Q????Q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d	S )
ReSTFormatrst)r!   P   c                 4   |j         r#d |j         D             }d |j         D             }n<d t          |j                  D             }d t          |j                  D             }|j        D ]}t	          t          |d          r|                                n|          }t          |          D ]b\  }}t          |          }||         	                    t          |                     t          ||         t          |                    ||<   c||fS )zn
        Returns a list of string lengths of each column, and a list of
        maximum word lengths.
        c                 .    g | ]}t          |          gS  r   r   hs     r   
<listcomp>z9ReSTFormat._get_column_string_lengths.<locals>.<listcomp>+   s     @@@1s1vvh@@@r   c                 ,    g | ]}t          |          S r%   )r   r&   s     r   r(   z9ReSTFormat._get_column_string_lengths.<locals>.<listcomp>,   s     CCCaq))CCCr   c                     g | ]}g S r%   r%   r   _s     r   r(   z9ReSTFormat._get_column_string_lengths.<locals>.<listcomp>.   s    ???Qb???r   c                     g | ]}d S )r   r%   r+   s     r   r(   z9ReSTFormat._get_column_string_lengths.<locals>.<listcomp>/   s    999q999r   values)headersrangewidthdictiterhasattrr.   	enumerater   appendr   r   r   )	clsdatasetcolumn_lengths	word_lensrowr.   ivalr   s	            r   _get_column_string_lengthsz%ReSTFormat._get_column_string_lengths$   s*    ? 	:@@@@@NCC7?CCCII??%*>*>???N99E'-$8$8999I< 	F 	FC'#x*@*@I#**,,,cJJF#F++ F F3c{{q!((T333"9Q<t1D1DEE	!F y((r   |c                    t           vr:t          d                    d                    t                                         fd|r|dz   nd|rd|z   ndd|z   dz   g }t	          |          D ]E\  }}||_        t          |          }	|                    |	          }
|                    |
           Ft          |ddi}fd|D             }f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                     t           k    r|                     |          S t          k    r|                     |          S |                     |          S r   )JUSTIFY_LEFTljustJUSTIFY_CENTERr   rjust)text_width_justifys     r   justz&ReSTFormat._row_to_lines.<locals>.justB   sO    ,&&{{6***N**||F+++{{6***r     	fillvaluec              3   P   K   | ] }fd t          |          D             V  !dS )c              3   B   K   | ]\  }} ||                   V  d S r   r%   )r   r<   	cell_linerI   widthss      r   r   z5ReSTFormat._row_to_lines.<locals>.<genexpr>.<genexpr>T   s7      LLLAyTT)VAY''LLLLLLr   N)r5   )r   linerI   rP   s     r   r   z+ReSTFormat._row_to_lines.<locals>.<genexpr>S   sU       
 
 MLLLLIdOOLLL
 
 
 
 
 
r   c                 f    g | ]-}d                                           |          f          .S )rK   )join)r   rQ   lpadpadrpads     r   r(   z,ReSTFormat._row_to_lines.<locals>.<listcomp>W   s6    III4$566IIIr   )
JUSTIFY_VALUES
ValueErrorformatrS   zipr1   r   wrapr6   r   )r7   r.   rP   wrapperseprH   cellsr   r1   r   celllinesrI   rT   rU   rV   s     `  `      @@@@r   _row_to_lineszReSTFormat._row_to_lines8   sU   
 .((ELLN++    	+ 	+ 	+ 	+ 	+  'sSyyR'sSyyRCi#o// 	 	LE5!GM%==D<<%%DLLU1b11
 
 
 
 

 
 
 JIIIII5IIIr      c                 ,   |                      |          \  }}d |D             }t          |          t          |          z  z
  k    rfd|D             }nd |D             }f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                 F    g | ]}t          t          |                    S r%   )intr   r   lenss     r   r(   z1ReSTFormat._get_column_widths.<locals>.<listcomp>a   s&    >>>Ts6$<<((>>>r   c              3   (   K   | ]}|z  z  V  d S r   r%   )r   rg   max_table_widthtotals     r   r   z0ReSTFormat._get_column_widths.<locals>.<genexpr>d   s-      UU_t3u<UUUUUUr   c              3      K   | ]}|V  d S r   r%   rf   s     r   r   z0ReSTFormat._get_column_widths.<locals>.<genexpr>f   s"      ::dT::::::r   c              3   2   K   | ]}|k    r|z
  n|V  d S r   r%   )r   wpad_lens     r   r   z0ReSTFormat._get_column_widths.<locals>.<genexpr>h   s3      RRqGWRRRRRRr   c                 4    g | ]\  }}t          ||          S r%   )r   )r   rm   ls      r   r(   z1ReSTFormat._get_column_widths.<locals>.<listcomp>j   s$    MMMtq!QMMMr   )r>   sumr   rZ   )	r7   r8   ri   rn   str_lensr:   median_lenscolumn_widthsrj   s	     ``    @r   _get_column_widthszReSTFormat._get_column_widthsZ   s     "<<WEE)>>X>>>K  ?gK0@0@&@AAAUUUUUUUUMM::k:::MRRRRMRRRMMs=)/L/LMMMr   Nc           	         g }t                      }||                     |d          }d                    d |D                       }|                    |           |j        rL|                    |                     |j        ||dt                               |                    |           |j        D ]`}t          t          |d          r|                                n|          }|                    |                     |||d                     a|                    |           d	                    |          S )
zP
        Returns reStructuredText grid table representation of dataset.
        N   )rn   z  c                     g | ]}d |z  S =r%   r   rm   s     r   r(   z9ReSTFormat.export_set_as_simple_table.<locals>.<listcomp>v   s    ;;;C!G;;;r   rK   )r]   rH   r.   
r   ru   rS   r6   r/   extendra   rD   r2   r3   r4   r.   )r7   r8   rt   r`   r\   borderr;   r.   s           r   export_set_as_simple_tablez%ReSTFormat.export_set_as_simple_tablem   sF   
 -- 227A2FFM;;];;;<<V? 	!LL**& +      LL   < 	P 	PC'#x*@*@I#**,,,cJJFLL**6='2NNOOOOVyyr   c                    g }t                      }||                     |          }dd                    d |D                       z   dz   }dd                    d |D                       z   d	z   }|                    |           |j        rK|                    |                     |j        ||t          
                     |                    |           |j        D ]t}t          t          |d          r|                                n|          }|                    |                     |||                     |                    |           ud                    |          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                     g | ]}d |z  S ry   r%   r{   s     r   r(   z7ReSTFormat.export_set_as_grid_table.<locals>.<listcomp>   s    'G'G'GAa'G'G'Gr   z=+z+-z-+-c                     g | ]}d |z  S )-r%   r{   s     r   r(   z7ReSTFormat.export_set_as_grid_table.<locals>.<listcomp>   s    $D$D$DS1W$D$D$Dr   z-+)rH   r.   r|   r}   )	r7   r8   rt   r`   r\   
header_seprow_sepr;   r.   s	            r   export_set_as_grid_tablez#ReSTFormat.export_set_as_grid_table   sn   : -- 227;;MEJJ'G'G'G'G'GHHH4O
$D$Dm$D$D$DEEELW? 	%LL**&	 +      LL$$$< 	" 	"C'#x*@*@I#**,,,cJJFLL**6='JJKKKLL!!!!yyr   c                     |$t          |          }t          |          |k    rdS |D ]'}t          |          }t          |          |k    r dS (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   )r7   head0col0width0r_   s        r   _use_simple_tablezReSTFormat._use_simple_table   sg     5MME5zzF""u 	 	D$<<D4yy6!!uu "tr   c                    |j         sdS |                    dd          }|                    d| j                  }|                     ||          }|                     |j        r|j        d         nd|                    d          |d                   }|r|s|                     ||          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

        rK   
force_gridFri   r   N)	r2   getMAX_TABLE_WIDTHru   r   r/   get_colr   r   )r7   r8   kwargsr   ri   rt   use_simple_tables          r   
export_setzReSTFormat.export_set   s    < | 	2ZZe44
 **%68KLL..wHH00")/;GOAtOOA!
 

  	HJ 	H11'=III//GGGr   c                 P     d                      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   F   K   | ]}                     |d           V  dS )T)r   N)r   )r   r8   r7   s     r   r   z)ReSTFormat.export_book.<locals>.<genexpr>
  sG       > >& >>'d>CC > > > > > >r   )rS   	_datasets)r7   databooks   ` r   export_bookzReSTFormat.export_book  sD     {{ > > > >*2*<> > > > > 	>r   r   )__name__
__module____qualname__title
extensionsr   classmethodr>   rB   ra   ru   r   r   r   r   r   r%   r   r   r    r       s       EJO) ) [)& 8;\    [B 9HRS    [$       [ 4 1  1  1  [1 f   [. +H +H [+HZ > > [> > >r   r    N)__doc__	itertoolsr   
statisticsr   textwrapr   rB   rD   JUSTIFY_RIGHTrW   r   r   r    r%   r   r   <module>r      s     " ! ! ! ! !                  >  O O Om> m> m> m> m> m> m> m> m> m>r   