o
    Ä]Ü[V  ã                   @   s¾  d Z ddlmZ ddgZdZdZdZdZdd	lZdd	l	Z	z
dd	l
Z
d
d„ ZW n" eyH   z
dd	lZdd„ ZW n eyE   ej d¡ ‚ w Y nw ddlmZ ejdkrYeZeZneZeZdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZG dd„ dƒZedkrÝeƒ Ze  g d¢¡ e !g d¢¡ e "g d¢g d¢g d¢g d¢g¡ e#e $¡ d ƒ eƒ Ze %ej&¡ e 'g d ¢¡ e  g d!¢¡ e "g d"¢g d#¢g d$¢g d%¢g d&¢g¡ e#e $¡ ƒ d	S d	S )'a¢  module for creating simple ASCII tables


Example:

    table = Texttable()
    table.set_cols_align(["l", "r", "c"])
    table.set_cols_valign(["t", "m", "b"])
    table.add_rows([["Name", "Age", "Nickname"],
                    ["Mr\nXavier\nHuon", 32, "Xav'"],
                    ["Mr\nBaptiste\nClement", 1, "Baby"],
                    ["Mme\nLouise\nBourgeau", 28, "Lou\n\nLoue"]])
    print table.draw() + "\n"

    table = Texttable()
    table.set_deco(Texttable.HEADER)
    table.set_cols_dtype(['t',  # text
                          'f',  # float (decimal)
                          'e',  # float (exponent)
                          'i',  # integer
                          'a']) # automatic
    table.set_cols_align(["l", "r", "r", "r", "l"])
    table.add_rows([["text",    "float", "exp", "int", "auto"],
                    ["abcd",    "67",    654,   89,    128.001],
                    ["efghijk", 67.5434, .654,  89.6,  12800000000000000000000.00023],
                    ["lmn",     5e-78,   5e-78, 89.4,  .000000000000128],
                    ["opqrstu", .023,    5e+78, 92.,   12800000000000000000000]])
    print table.draw()

Result:

    +----------+-----+----------+
    |   Name   | Age | Nickname |
    +==========+=====+==========+
    | Mr       |     |          |
    | Xavier   |  32 |          |
    | Huon     |     |   Xav'   |
    +----------+-----+----------+
    | Mr       |     |          |
    | Baptiste |   1 |          |
    | Clement  |     |   Baby   |
    +----------+-----+----------+
    | Mme      |     |   Lou    |
    | Louise   |  28 |          |
    | Bourgeau |     |   Loue   |
    +----------+-----+----------+

    text   float       exp      int     auto
    ===========================================
    abcd   67.000   6.540e+02   89    128.001
    efgh   67.543   6.540e-01   90    1.280e+22
    ijkl   0.000    5.000e-78   89    0.000
    mnop   0.023    5.000e+78   92    1.280e+22
é    )ÚdivisionÚ	TexttableÚArraySizeErrorz%Gerome Fournier <jef(at)foutaise.org>ÚMITz1.5.0añ  Jeff Kowalczyk:
    - textwrap improved import
    - comment concerning header output

Anonymous:
    - add_rows method, for adding rows in one go

Sergey Simonenko:
    - redefined len() function to deal with non-ASCII characters

Roger Lew:
    - columns datatype specifications

Brian Peterson:
    - better handling of unicode errors

Frank Sachsenheim:
    - add Python 2/3-compatibility

Maximilian Hils:
    - fix minor bug for Python 3 compatibility

frinkelpi:
    - preserve empty lines
Nc                 C   ó   t  | |¡S ©N)ÚcjkwrapÚwrap©ÚtxtÚwidth© r   ú;/var/www/html/env/lib/python3.10/site-packages/texttable.pyÚtextwrapperf   ó   r   c                 C   r   r   )Útextwrapr	   r
   r   r   r   r   k   r   zCan't import textwrap module!
)Úreduce)é   r   c              
   C   sr   t | tƒr| S t | tƒr5zt| dƒW S  ty4 } ztj d| |f ¡ t| ddƒW  Y d}~S d}~ww t| ƒS )z7Return a unicode representation of a python object
    zutf-8z1UnicodeDecodeError exception for string '%s': %s
ÚreplaceN)Ú
isinstanceÚunicode_typeÚ
bytes_typeÚUnicodeDecodeErrorÚsysÚstderrÚwrite)ÚobjÚstrerrorr   r   r   Úobj2unicode{   s   

€þr   c                    s@   t | tƒs
t | tƒrt| ƒ}tj‰ t‡ fdd„|D ƒƒS |  ¡ S )zMRedefining len here so it will be able to work with non-ASCII characters
    c                    s.   g | ]}ˆ |ƒd v rdpt  |¡rdnd‘qS )ÚWFé   r   é   )ÚunicodedataÚ	combining)Ú.0Úc©Úwr   r   Ú
<listcomp>�   s   . zlen.<locals>.<listcomp>)r   r   r   r   r"   Úeast_asian_widthÚsumÚ__len__)ÚiterableÚunicode_datar   r&   r   ÚlenŠ   s
   r.   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r   zEException raised when specified rows don't fit the required size
    c                 C   s   || _ t | |d¡ d S )NÚ )ÚmsgÚ	ExceptionÚ__init__)Úselfr0   r   r   r   r2   ™   s   zArraySizeError.__init__c                 C   s   | j S r   )r0   ©r3   r   r   r   Ú__str__�   s   zArraySizeError.__str__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   r5   r   r   r   r   r   •   s    c                   @   s   e Zd ZdZdS )ÚFallbackToTextz#Used for failed conversion to floatN)r6   r7   r8   r9   r   r   r   r   r:   ¡   s    r:   c                   @   sT  e Zd ZdZdZdZdZdOdd„Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdPd!d"„Zd#d$„ Zed%d&„ ƒZed'd(„ ƒZed)d*„ ƒZed+d,„ ƒZed-d.„ ƒZed/d0„ ƒZd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Z d9d:„ Z!d;d<„ Z"d=d>„ Z#d?d@„ Z$dQdBdC„Z%dDdE„ Z&dFdG„ Z'dHdI„ Z(dQdJdK„Z)dLdM„ Z*dNS )Rr   r!   r    é   é   éP   c                 C   sD   |   |¡ d| _tjtjB tjB tjB | _|  g d¢¡ |  	¡  dS )z­Constructor

        - max_width is an integer, specifying the maximum width of the table
        - if set to 0, size is unlimited, therefore cells won't be wrapped
        r   )ú-ú|ú+ú=N)
Úset_max_widthÚ
_precisionr   ÚVLINESÚHLINESÚBORDERÚHEADERÚ_decoÚ	set_charsÚreset©r3   Ú	max_widthr   r   r   r2   ­   s   
ÿzTexttable.__init__c                 C   s   d| _ d| _g | _g | _| S )z<Reset the instance

        - reset rows and header
        N)Ú_hline_stringÚ	_row_sizeÚ_headerÚ_rowsr4   r   r   r   rJ   ¼   s
   zTexttable.resetc                 C   s   |dkr	|| _ | S d| _ | S )zÄSet the maximum width of the table

        - max_width is an integer, specifying the maximum width of the table
        - if set to 0, size is unlimited, therefore cells won't be wrapped
        r   F)Ú
_max_widthrK   r   r   r   rB   È   s   ÿzTexttable.set_max_widthc                 C   sD   t |ƒdkr
tdƒ‚dd„ dd„ |D ƒD ƒ}|\| _| _| _| _| S )zéSet the characters used to draw lines between rows and columns

        - the array should contain 4 fields:

            [horizontal, vertical, corner, header]

        - default is set to:

            ['-', '|', '+', '=']
        r;   z!array should contain 4 charactersc                 S   s   g | ]}|d d… ‘qS )Nr!   r   ©r$   Úxr   r   r   r(   ß   s    z'Texttable.set_chars.<locals>.<listcomp>c                 S   s   g | ]}t |ƒ‘qS r   )Ústr)r$   Úsr   r   r   r(   ß   ó    )r.   r   Ú_char_horizÚ
_char_vertÚ_char_cornerÚ_char_header©r3   Úarrayr   r   r   rI   Ñ   s   
ÿzTexttable.set_charsc                 C   s
   || _ | S )a¯  Set the table decoration

        - 'deco' can be a combinaison of:

            Texttable.BORDER: Border around the table
            Texttable.HEADER: Horizontal line below the header
            Texttable.HLINES: Horizontal lines between rows
            Texttable.VLINES: Vertical lines between columns

           All of them are enabled by default

        - example:

            Texttable.BORDER | Texttable.HEADER
        )rH   )r3   Údecor   r   r   Úset_decoä   s   zTexttable.set_decoc                 C   ó   |   |¡ || _| S )zãSet the desired header alignment

        - the elements of the array should be either "l", "c" or "r":

            * "l": column flushed left
            * "c": column centered
            * "r": column flushed right
        )Ú_check_row_sizeÚ_header_alignr[   r   r   r   Úset_header_alignø   ó   

zTexttable.set_header_alignc                 C   r_   )zäSet the desired columns alignment

        - the elements of the array should be either "l", "c" or "r":

            * "l": column flushed left
            * "c": column centered
            * "r": column flushed right
        )r`   Ú_alignr[   r   r   r   Úset_cols_align  rc   zTexttable.set_cols_alignc                 C   r_   )a,  Set the desired columns vertical alignment

        - the elements of the array should be either "t", "m" or "b":

            * "t": column aligned on the top of the cell
            * "m": column aligned on the middle of the cell
            * "b": column aligned on the bottom of the cell
        )r`   Ú_valignr[   r   r   r   Úset_cols_valign  rc   zTexttable.set_cols_valignc                 C   r_   )a.  Set the desired columns datatype for the cols.

        - the elements of the array should be either a callable or any of
          "a", "t", "f", "e" or "i":

            * "a": automatic (try to use the most appropriate datatype)
            * "t": treat as text
            * "f": treat as float in decimal format
            * "e": treat as float in exponential format
            * "i": treat as int
            * a callable: should return formatted string for any value given

        - by default, automatic datatyping is used for each column
        )r`   Ú_dtyper[   r   r   r   Úset_cols_dtype"  s   
zTexttable.set_cols_dtypec                 C   sV   |   |¡ zttt|ƒƒ}tt|ƒdkrt‚W n ty%   tj 	d¡ ‚ w || _
| S )z¸Set the desired columns width

        - the elements of the array should be integers, specifying the
          width of each column. For example:

                [10, 20, 5]
        r   z-Wrong argument in column width specification
)r`   ÚlistÚmapÚintr   ÚminÚ
ValueErrorr   r   r   Ú_widthr[   r   r   r   Úset_cols_width6  s   
	ÿþzTexttable.set_cols_widthc                 C   s&   t |ƒtus
|dk rtdƒ‚|| _| S )zŽSet the desired precision for float/exponential formats

        - width must be an integer >= 0

        - default value is set to 3
        r   z'width must be an integer greater then 0)Útyperl   rn   rC   )r3   r   r   r   r   Úset_precisionJ  s   zTexttable.set_precisionc                 C   s   |   |¡ ttt|ƒƒ| _| S )z(Specify the header of the table
        )r`   rj   rk   r   rO   r[   r   r   r   ÚheaderW  s   
zTexttable.headerc                 C   sZ   |   |¡ t| dƒsdg| j | _g }t|ƒD ]\}}| |  ||¡¡ q| j |¡ | S )zSAdd a row in the rows stack

        - cells can contain newlines and tabs
        rh   Úa)r`   ÚhasattrrN   rh   Ú	enumerateÚappendÚ_strrP   )r3   r\   ÚcellsÚirS   r   r   r   Úadd_row_  s   

zTexttable.add_rowTc                 C   sZ   |r!t |dƒrt |dƒr|  | ¡ ¡ n|  |d ¡ |dd… }|D ]}|  |¡ q#| S )züAdd several rows in the rows stack

        - The 'rows' argument can be either an iterator returning arrays,
          or a by-dimensional array
        - 'header' specifies if the first row should be used as the header
          of the table
        Ú__iter__Únextr   r!   N)ru   rs   r}   r{   )r3   Úrowsrs   Úrowr   r   r   Úadd_rowsp  s   zTexttable.add_rowsc                 C   sÎ   | j s| jsdS |  ¡  |  ¡  d}|  ¡ r||  ¡ 7 }| j r3|| j| j dd�7 }|  ¡ r3||  ¡ 7 }d}| jD ]}|d7 }||  |¡7 }|  	¡ rV|t
| jƒk rV||  ¡ 7 }q8|  ¡ ra||  ¡ 7 }|dd… S )zJDraw the table

        - the table is returned as a whole string
        Nr/   T)Úisheaderr   r!   éÿÿÿÿ)rO   rP   Ú_compute_cols_widthÚ_check_alignÚ_has_borderÚ_hlineÚ
_draw_lineÚ_has_headerÚ_hline_headerÚ_has_hlinesr.   )r3   ÚoutÚlengthr   r   r   r   Údraw…  s*   
€zTexttable.drawc              	   C   s2   |d u rt ƒ ‚zt|ƒW S  ttfy   t ƒ ‚w r   )r:   ÚfloatÚ	TypeErrorrn   )ÚclsrS   r   r   r   Ú	_to_float   s   
ÿzTexttable._to_floatc                 K   s   t tt|  |¡ƒƒƒS )z]Integer formatting class-method.

        - x will be float-converted and then used.
        )rT   rl   Úroundr‘   ©r�   rS   Úkwr   r   r   Ú_fmt_int©  s   zTexttable._fmt_intc                 K   ó   |  d¡}d||  |¡f S )zÉFloat formatting class-method.

        - x parameter is ignored. Instead kw-argument f being x float-converted
          will be used.

        - precision will be taken from `n` kw-argument.
        Únz%.*f©Úgetr‘   ©r�   rS   r”   r—   r   r   r   Ú
_fmt_float±  ó   
	zTexttable._fmt_floatc                 K   r–   )zÏExponential formatting class-method.

        - x parameter is ignored. Instead kw-argument f being x float-converted
          will be used.

        - precision will be taken from `n` kw-argument.
        r—   z%.*er˜   rš   r   r   r   Ú_fmt_exp½  rœ   zTexttable._fmt_expc                 K   s   t |ƒS )zString formatting class-method.)r   r“   r   r   r   Ú	_fmt_textÉ  s   zTexttable._fmt_textc                 K   sL   |   |¡}t|ƒdkr| j}n|t|ƒ dkr| j}n| j}||fi |¤ŽS )zauto formatting class-method.g    „×—Ar   )r‘   Úabsr�   r’   r•   r›   )r�   rS   r”   ÚfÚfnr   r   r   Ú	_fmt_autoÎ  s   
zTexttable._fmt_autoc                 C   sl   | j | j| j| j| jdœ}| j}| j| }zt|ƒr||ƒW S || ||d�W S  ty5   |  |¡ Y S w )zŽHandles string formatting of cell data

            i - index of the cell datatype in self._dtype
            x - cell data to format
        )rt   rz   r    ÚeÚt)r—   )	r¢   r•   r›   r�   rž   rC   rh   Úcallabler:   )r3   rz   rS   ÚFMTr—   Údtyper   r   r   rx   Û  s   û

ÿzTexttable._strc                 C   s4   | j s
t|ƒ| _ dS | j t|ƒkrtd| j  ƒ‚dS )zCCheck that the specified array fits the previous rows size
        z array should contain %d elementsN)rN   r.   r   r[   r   r   r   r`   ó  s   ÿÿzTexttable._check_row_sizec                 C   ó   | j tj@ dkS )z8Return a boolean, if vlines are required or not
        r   )rH   r   rD   r4   r   r   r   Ú_has_vlinesý  ó   zTexttable._has_vlinesc                 C   r¨   )z8Return a boolean, if hlines are required or not
        r   )rH   r   rE   r4   r   r   r   rŠ     rª   zTexttable._has_hlinesc                 C   r¨   )z7Return a boolean, if border is required or not
        r   )rH   r   rF   r4   r   r   r   r…   	  rª   zTexttable._has_borderc                 C   r¨   )z<Return a boolean, if header line is required or not
        r   )rH   r   rG   r4   r   r   r   rˆ     rª   zTexttable._has_headerc                 C   s
   |   d¡S )z'Print header's horizontal line
        T)Ú_build_hliner4   r   r   r   r‰     s   
zTexttable._hline_headerc                 C   s   | j s|  ¡ | _ | j S )z!Print an horizontal line
        )rM   r«   r4   r   r   r   r†     s   
zTexttable._hlineFc                    st   | j ‰ |r| j‰ dˆ ˆ | jg|  ¡  ˆ f }| ‡ fdd„| jD ƒ¡}|  ¡ r4d| jˆ |ˆ | jf }|S |d7 }|S )zTReturn a string used to separated rows or separate header from
        rows
        z%s%s%sc                    s   g | ]}ˆ | ‘qS r   r   )r$   r—   ©Úhorizr   r   r(   .  rV   z*Texttable._build_hline.<locals>.<listcomp>z%s%s%s%s%s
Ú
)rW   rZ   rY   r©   Újoinro   r…   )r3   Ú	is_headerrU   Úlr   r¬   r   r«   #  s   ÿÿÿzTexttable._build_hlinec           	      C   s‚   |  d¡}d}|D ]5}d}|  d¡}t|ttdt|ƒd ƒƒƒD ]\}}|t|ƒ }|t|ƒk r8|d d d }q t||ƒ}q	|S )z�Return the width of the cell

        Special characters are taken into account to return the width of the
        cell, such like newlines and tabs
        r®   r   ú	r!   r<   )ÚsplitÚziprj   Úranger.   Úmax)	r3   ÚcellÚ
cell_linesÚmaxiÚlinerŒ   ÚpartsÚpartrz   r   r   r   Ú	_len_cell7  s   

$€zTexttable._len_cellc           
         s\  t ˆ dƒrdS g }ˆ jr‡ fdd„ˆ jD ƒ}ˆ jD ]3}t|ttt|ƒƒƒƒD ]%\}}zt|| ˆ  |¡ƒ||< W q& t	t
fyK   | ˆ  |¡¡ Y q&w qt|ƒ}t|ƒ}d|d  ddgˆ  ¡   }ˆ jr©|| ˆ jkr©	 ˆ j|| k rytd	ƒ‚ˆ j| }dg| }	d}|dkr§|	| || k r�|	|  d7  < |d8 }|d | }|dks‰|	}|ˆ _dS )
a  Return an array with the width of each column

        If a specific width has been specified, exit. If the total of the
        columns width exceed the table desired width, another width will be
        computed to fit, and cells will be wrapped.
        ro   Nc                    s   g | ]}ˆ   |¡‘qS r   )r½   rR   r4   r   r   r(   V  s    z1Texttable._compute_cols_width.<locals>.<listcomp>r   r!   r   r;   z max_width too low to render data)ru   rO   rP   r´   rj   rµ   r.   r¶   r½   r�   Ú
IndexErrorrw   r*   r…   rQ   rn   ro   )
r3   r¹   r   r·   rz   ÚncolsÚcontent_widthÚ
deco_widthÚavailable_widthÚnewmaxir   r4   r   rƒ   J  s>   

ÿý

ü
zTexttable._compute_cols_widthc                 C   sP   t | dƒsdg| j | _t | dƒsdg| j | _t | dƒs&dg| j | _dS dS )zFCheck if alignment has been specified, set default one if not
        ra   r%   rd   r±   rf   r¤   N)ru   rN   ra   rd   rf   r4   r   r   r   r„   r  s   


ÿzTexttable._check_alignc                 C   s:  |   ||¡}d}d}tt|d ƒƒD ]ˆ}|  ¡ r|d| j 7 }d}t|| j| jƒD ]a\}}}	|d7 }|| }
|t|
ƒ }|rE| j|d  }	|	dkrR||| |
 7 }n%|	dkro|t	|d ƒ| |
 t	|d |d  ƒ|  7 }n||
||  7 }|t|ƒk rŠ|d	|| jg|  
¡   7 }q)|d
d|| j g|  ¡   7 }q|S )zPDraw a line

        Loop over a single cell length, over all the cells
        ú r/   r   z%s r!   Úrr%   r    z %s z%s
)Ú_splititrµ   r.   r…   rX   r´   ro   rd   ra   rl   r©   )r3   rº   r�   Úspacer‹   rz   rŒ   r·   r   ÚalignÚ	cell_lineÚfillr   r   r   r‡   }  s2   ÿ€ zTexttable._draw_linec                 C   s"  g }t || jƒD ]'\}}g }| d¡D ]}| ¡ dkr!| d¡ q| t||ƒ¡ q| |¡ qttt	t
t|ƒƒƒ}t || jƒD ]N\}}	|rHd}	|	dkrp|t|ƒ }
dgt|
d ƒ |dd…< | dgt|
d |
d  ƒ ¡ q@|	dkr‚dg|t|ƒ  |dd…< q@| dg|t|ƒ  ¡ q@|S )	zªSplit each element of line to fit the column width

        Each element is turned into a list, result of the wrapping of the
        string to the desired width
        r®   r/   r¤   Úmr    Nr   Úb)r´   ro   r³   Ústriprw   Úextendr   r   r¶   rj   rk   r.   rf   rl   )r3   rº   r�   Úline_wrappedr·   r   r\   r%   Úmax_cell_linesÚvalignÚmissingr   r   r   rÆ   œ  s(   "zTexttable._splititN)r=   )T)F)+r6   r7   r8   rF   rG   rE   rD   r2   rJ   rB   rI   r^   rb   re   rg   ri   rp   rr   rs   r{   r€   r�   Úclassmethodr‘   r•   r›   r�   rž   r¢   rx   r`   r©   rŠ   r…   rˆ   r‰   r†   r«   r½   rƒ   r„   r‡   rÆ   r   r   r   r   r   ¦   s\    
	








(
Ú__main__)r±   rÅ   r%   )r¤   rË   rÌ   )ÚNameÚAgeÚNickname)zMr
Xavier
Huoné    zXav')zMr
Baptiste
Clementr!   ÚBaby)zMme
Louise
Bourgeaué   z
Lou
 
Louer®   )r¤   r    r£   rz   rt   )r±   rÅ   rÅ   rÅ   r±   )ÚtextrŽ   Úexprl   Úauto)ÚabcdÚ67iŽ  éY   gyé&1 `@)Úefghijkg_)ËÇâP@g!°rh‘íä?gffffffV@g@Œµx¯…D)ÚlmnçÜ�ÁØ†â/rã   gš™™™™YV@gVçž¯B=)ÚopqrstugZd;ßO�—?g/'Û%—EPg      W@l      ÆZx^+)(r9   Ú
__future__r   Ú__all__Ú
__author__Ú__license__Ú__version__Ú__credits__r   r"   r   r   ÚImportErrorr   r   r   Ú	functoolsr   Úversion_inforT   r   Úbytesr   Úunicoder   r.   r1   r   r:   r   r6   Útablere   rg   r€   Úprintr�   r^   rG   ri   r   r   r   r   Ú<module>   sv   7þþý	
    
ý
üé