
    EfCY                        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		 dd	l
Z
d
 Zn8# e$ r0 	 dd	lZd Zn$# e$ r ej                            d            w xY wY nw xY w	 dd	lZd Zn# e$ r d ZY nw xY wddlmZ ej        dk    reZeZneZeZd Zd Z G d de          Z G d de          Z G 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!&                                            e%              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 to create 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())
    print()

    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.7.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                 ,    t          j        | |          S N)cjkwrapwraptxtwidths     K/var/www/ffwdstore.be/apps/py-env/lib/python3.11/site-packages/texttable.pytextwrapperr   g   s    |C'''    c                 ,    t          j        | |          S r   )textwrapr
   r   s     r   r   r   l   s    =e,,,r   zCan't import textwrap module!
c                 F    t          dt          j        |                     S ):Return the rendering width of a unicode character
        r   )maxwcwidthcs    r   uchar_widthr   w   s     1goa(()))r   c                 b    t          j        |           dv rdS t          j        |           rdS dS )r   WF   r      )unicodedataeast_asian_width	combiningr   s    r   r   r   |   s<     '**d221"1%% 	11r   )reduce)   r   c                 6   t          | t                    r| S t          | t                    r_	 t          | d          S # t          $ rA}t          j                            d| d|d           t          | dd          cY d}~S d}~ww xY wt          |           S )z7Return a unicode representation of a python object
    zutf-8z)UnicodeDecodeError exception for string 'z': 
replaceN)
isinstanceunicode_type
bytes_typeUnicodeDecodeErrorsysstderrwrite)objstrerrors     r   obj2unicoder/      s     #|$$ 	!
	C	$	$ !	9W---! 	9 	9 	9JUXUXUXZbZbZbcdddWi88888888	9 C   s   > 
B	6B>B	B	c                     t          | t                    st          | t                    r&t          d t	          |           D                       S |                                 S )zMRedefining len here so it will be able to work with non-ASCII characters
    c                 ,    g | ]}t          |          S  )r   ).0r   s     r   
<listcomp>zlen.<locals>.<listcomp>   s    BBBqKNNBBBr   )r&   r(   r'   sumr/   __len__)iterables    r   lenr8      s^     (J'' ":h+M+M "BBK,A,ABBBCCC!!!r   c                       e Zd ZdZd Zd ZdS )r   zEException raised when specified rows don't fit the required size
    c                 L    || _         t                              | |d           d S )N )msg	Exception__init__)selfr<   s     r   r>   zArraySizeError.__init__   s'    4b)))))r   c                     | j         S r   )r<   r?   s    r   __str__zArraySizeError.__str__   s	    xr   N)__name__
__module____qualname____doc__r>   rB   r2   r   r   r   r      s<         * * *    r   c                       e Zd ZdZdS )FallbackToTextz#Used for failed conversion to floatN)rC   rD   rE   rF   r2   r   r   rH   rH      s        --Dr   rH   c                   n   e Zd ZdZdZdZdZd-dZd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd.dZd Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zd Zd Zd Z d  Z!d! Z"d" Z#d# Z$d$ Z%d/d&Z&d' Z'd( Z(d) Z)d/d*Z*d+ Z+d,S )0r   r   r         P   c                    |                      |           d| _        t          j        t          j        z  t          j        z  t          j        z  | _        |                     g d           | 	                                 dS )zConstructor

        - 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r?   	max_widths     r   r>   zTexttable.__init__   sn     	9%%%%	(889;KK
+++,,,

r   c                 >    d| _         d| _        g | _        g | _        | S )z<Reset the instance

        - reset rows and header
        N)_hline_string	_row_size_header_rowsrA   s    r   rZ   zTexttable.reset   s&     "
r   c                 $    |dk    r|nd| _         | S )zSet 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_widthr[   s     r   rR   zTexttable.set_max_width   s     (11}}))%r   c                     t          |          dk    rt          d          d d |D             D             }|\  | _        | _        | _        | _        | S )zSet the characters used to draw lines between rows and columns

        - the array should contain 4 fields:

            [horizontal, vertical, corner, header]

        - default is set to:

            ['-', '|', '+', '=']
        rJ   z!array should contain 4 charactersc                 "    g | ]}|d d         S )Nr   r2   )r3   xs     r   r4   z'Texttable.set_chars.<locals>.<listcomp>   s     <<<A!BQB%<<<r   c                 ,    g | ]}t          |          S r2   )str)r3   ss     r   r4   z'Texttable.set_chars.<locals>.<listcomp>   s    !:!:!:a3q66!:!:!:r   )r8   r   _char_horiz
_char_vert_char_corner_char_headerr?   arrays     r   rY   zTexttable.set_chars   se     u::?? !DEEE<<!:!:5!:!:!:<<<49	2	4?t0r   c                 "    || _         d| _        | S )a  Set the table decoration

        - 'deco' can be a combination 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
        N)rX   r^   )r?   decos     r   set_decozTexttable.set_deco   s    " 
!r   c                 >    |                      |           || _        | S )zSet 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_alignrn   s     r   set_header_alignzTexttable.set_header_align  s%     	U###"r   c                 >    |                      |           || _        | S )zSet 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
        )rt   _alignrn   s     r   set_cols_alignzTexttable.set_cols_align  s$     	U###r   c                 >    |                      |           || _        | S )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
        )rt   _valignrn   s     r   set_cols_valignzTexttable.set_cols_valign(  s$     	U###r   c                 >    |                      |           || _        | S )aW  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", "i" or "b":

            * "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
            * "b": treat as boolean
            * a callable: should return formatted string for any value given

        - by default, automatic datatyping is used for each column
        )rt   _dtypern   s     r   set_cols_dtypezTexttable.set_cols_dtype6  s$    " 	U###r   c                 "   |                      |           	 t          t          t          |                    }t	          t
          |          dk    rt          n.# t          $ r! t          j        	                    d            w xY w|| _
        | S )zSet 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
)rt   listmapintr!   min
ValueErrorr*   r+   r,   _widthrn   s     r   set_cols_widthzTexttable.set_cols_widthK  s     	U###	S%))Ec5!!Q&&   ' 	 	 	JMNNN	 s   AA +Bc                 j    t          |          t          us|dk     rt          d          || _        | S )zSet 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)typer   r   rS   )r?   r   s     r   set_precisionzTexttable.set_precision_  s8     E{{c!!UQYYFGGGr   c                 ~    |                      |           t          t          t          |                    | _        | S )z(Specify the header of the table
        )rt   r   r   r/   r`   rn   s     r   headerzTexttable.headerl  s6     	U###CU3344r   c                 $   |                      |           t          | d          sdg| j        z  | _        g }t	          |          D ].\  }}|                    |                     ||                     /| j                            |           | S )zSAdd a row in the rows stack

        - cells can contain newlines and tabs
        r~   a)rt   hasattrr_   r~   	enumerateappend_strra   )r?   ro   cellsirf   s        r   add_rowzTexttable.add_rowt  s     	U###tX&& 	1%$.0DKe$$ 	* 	*DAqLL1a))))
%   r   Tc                    |rmt          |d          r8t          |d          r(|                     |                                           n%|                     |d                    |dd         }|D ]}|                     |           | S )zAdd 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)r   r   r   r   )r?   rowsr   rows       r   add_rowszTexttable.add_rows  s      	 tZ((  WT6-B-B  DIIKK((((DG$$$ABBx 	 	CLLr   c                    | j         s	| j        sdS |                                  |                                  d}|                                 r||                                 z  }| j         rJ||                     | j         d          z  }|                                 r||                                 z  }d}| j        D ]b}|dz  }||                     |          z  }| 	                                r/|t          | j                  k     r||                                 z  }c|                                 r||                                 z  }|dd         S )zJDraw the table

        - the table is returned as a whole string
        Nr;   T)isheaderr   r   )r`   ra   _compute_cols_width_check_align_has_border_hline
_draw_line_has_header_hline_header_has_hlinesr8   )r?   outlengthr   s       r   drawzTexttable.draw  s\    | 	DJ 	F  """ 	!4;;== C< 	,4??4<$????C!! ,t))+++: 	% 	%CaKF4??3'''C!! %fs4:&>&>t{{}}$ 	!4;;== C3B3xr   c                     |t                      	 t          |          S # t          t          f$ r t                      w xY wr   )rH   float	TypeErrorr   )clsrf   s     r   	_to_floatzTexttable._to_float  sO    9 """	#88O:& 	# 	# 	# """	#s	   !  Ac           	          t          |          t          k    rt          |          S t          t          t          |                     |                                        S )z)Integer formatting class-method.
        )r   r   rh   roundr   r   rf   kws      r   _fmt_intzTexttable._fmt_int  sH     77c>>q66Ms5q!1!12233444r   c                 `    |                     d          }d||                     |          fz  S )zFloat 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   rf   r   r   s       r   
_fmt_floatzTexttable._fmt_float  /     FF3KKCMM!,,---r   c                 `    |                     d          }d||                     |          fz  S )zExponential 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   s       r   _fmt_expzTexttable._fmt_exp  r   r   c                      t          |          S )zString formatting class-method.)r/   r   s      r   	_fmt_textzTexttable._fmt_text  s     1~~r   c                 :    t          t          |                    S )zBoolean formatting class-method)rh   boolr   s      r   	_fmt_boolzTexttable._fmt_bool  s     477||r   c                    |                      |          }t          |          dk    r| j        }nO||k    r| j        }nA|t	          |          z
  dk    r$t          |t                    r| j        n| j        }n| j	        } ||fi |S )zauto formatting class-method.g    חAr   )
r   absr   r   r   r&   r   r   r   r   )r   rf   r   ffns        r   	_fmt_autozTexttable._fmt_auto  s     MM!q66C<<BB!VVBBq\Q",Q"5"5G3<BBBr!{{r{{r   c                    | j         | j        | j        | j        | j        | j        d}| j        }| j        |         }	 t          |          r ||          S  ||         ||          S # t          $ r |                     |          cY S w xY w)zHandles string formatting of cell data

            i - index of the cell datatype in self._dtype
            x - cell data to format
        )r   r   br   et)r   )
r   r   r   r   r   r   rS   r~   callablerH   )r?   r   rf   FMTr   dtypes         r   r   zTexttable._str  s       OA	% *uQxx!s5z!q)))) 	% 	% 	%>>!$$$$$	%s   A* A* *BBc                     | j         st          |          | _         dS | j         t          |          k    rt          d| j         z            dS )zCCheck that the specified array fits the previous rows size
        z array should contain %d elementsN)r_   r8   r   rn   s     r   rt   zTexttable._check_row_size  sY     ~ 	" ZZDNNN^s5zz)) !C."! " " " *)r   c                 2    | j         t          j        z  dk    S )z8Return a boolean, if vlines are required or not
        r   )rX   r   rT   rA   s    r   _has_vlineszTexttable._has_vlines       zI,,q00r   c                 2    | j         t          j        z  dk    S )z8Return a boolean, if hlines are required or not
        r   )rX   r   rU   rA   s    r   r   zTexttable._has_hlines   r   r   c                 2    | j         t          j        z  dk    S )z7Return a boolean, if border is required or not
        r   )rX   r   rV   rA   s    r   r   zTexttable._has_border&  r   r   c                 2    | j         t          j        z  dk    S )z<Return a boolean, if header line is required or not
        r   )rX   r   rW   rA   s    r   r   zTexttable._has_header,  r   r   c                 ,    |                      d          S )z'Print header's horizontal line
        T)_build_hlinerA   s    r   r   zTexttable._hline_header2  s       &&&r   c                 P    | j         s|                                 | _         | j         S )z!Print an horizontal line
        )r^   r   rA   s    r   r   zTexttable._hline8  s,     ! 	5!%!2!2!4!4D!!r   Fc                 $   | j         |r| j        | j        g|                                          }|                    fd| j        D                       }|                                 r| j        || j        d}n|dz  }|S )zTReturn a string used to separated rows or separate header from
        rows
        c                     g | ]}|z  S r2   r2   )r3   r   horizs     r   r4   z*Texttable._build_hline.<locals>.<listcomp>K  s    333!EAI333r   r$   )rj   rm   rl   r   joinr   r   )r?   	is_headerri   lr   s       @r   r   zTexttable._build_hline@  s       	&%Et'89$:J:J:L:LMME FF3333t{33344 	"&"3"3UUAAuu!!!#AA IAr   c                 n   |                     d          }d}|D ]}d}|                     d          }t          |t          t          dt	          |          dz                                 D ]5\  }}|t	          |          z   }|t	          |          k     r|dz  dz   dz  }6t          ||          }|S )zReturn 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   rK   )splitzipr   ranger8   r   )	r?   cell
cell_linesmaxiliner   partspartr   s	            r   	_len_cellzTexttable._len_cellT  s     ZZ%%
 	% 	%DFJJt$$Eud5CJJN+C+C&D&DEE 1 1a#d))+s5zz>>$ai!mq0FtV$$DDr   c                 b    t           d          rdS g } j        r fd j        D             } j        D ]}t          |t	          t          t          |                                        D ]q\  }}	 t          ||                              |                    ||<   3# t          t          f$ r+ |                                         |                     Y nw xY wt          |          }t          |          }d|dz
  z  ddg                                          z   } j        r{||z    j        k    rm	  j        ||z   k     rt          d           j        |z
  }dg|z  }	d}|dk    r5|	|         ||         k     r|	|xx         dz  cc<   |dz  }|dz   |z  }|dk    5|	}| _        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.
        r   Nc                 :    g | ]}                     |          S r2   )r   )r3   rf   r?   s     r   r4   z1Texttable._compute_cols_width.<locals>.<listcomp>s  s%    >>>1T^^A&&>>>r   r"   r   r   rJ   z max_width too low to render data)r   r`   ra   r   r   r   r8   r   r   r   
IndexErrorr   r5   r   rc   r   r   )
r?   r   r   r   r   ncolscontent_width
deco_widthavailable_widthnewmaxis
   `         r   r   zTexttable._compute_cols_widthg  s    4"" 	F< 	?>>>>>>>D: 	6 	6Cc4c#hh#8#899 6 6Q6!$q'4>>$+?+?@@DGG!:. 6 6 6KKt 4 45555566 D		D		a[Aa5)9)9););#<<
? 	
 :doMM %*"455 !CDDD"o
:OcEkGA!A%%1:Q''AJJJ!OJJJ#q(OUeO	 "A%%
 Ds   6,B##9CCc                     t          | d          sdg| j        z  | _        t          | d          sdg| j        z  | _        t          | d          sdg| j        z  | _        dS dS )zFCheck if alignment has been specified, set default one if not
        ru   r   rx   r   r{   r   N)r   r_   ru   rx   r{   rA   s    r   r   zTexttable._check_align  sy     t_-- 	8"%!7DtX&& 	1%$.0DKtY'' 	254>1DLLL	2 	2r   c           	         |                      ||          }d}d}t          t          |d                             D ]8}|                                 r|d| j        z  z  }d}t          || j        | j                  D ]\  }}}	|dz  }||         }
|t          |
          z
  }|r| j        |dz
           }	|	dk    r|||z  |
z   z  }nI|	dk    r8|t          |dz            |z  |
z   t          |dz  |dz  z             |z  z   z  }n||
||z  z   z  }|t          |          k     r'|d	|| j        g| 
                                         z  z  }|d
d|| j        z   g|                                          z  z  }:|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   r8   r   rk   r   r   rx   ru   r   r   )r?   r   r   spacer   r   r   r   r   align	cell_linefills               r   r   zTexttable._draw_line  s    }}T8,,s47||$$ 	N 	NA!! /ut..F&)$T[&I&I Q Q"eU! G	s9~~- ; .vz:EC<<4%<)33CCc\\CQKK%/);!$q&46/22U:; <CC 9te|33CCII%%6UDO$<T=M=M=O=O$PPPC6R!89$:J:J:L:LMMMCC
r   c           	      D   g }t          || j                  D ]\  }}g }|                    d          D ]S}|                                dk    r|                    d           0|                    t          ||                     T|                    |           t          t          t          t          t          |                              }t          || j                  D ]\  }}	|rd}	|	dk    r]|t          |          z
  }
dgt          |
dz            z  |dd<   |                    dgt          |
dz  |
dz  z             z             l|	dk    rdg|t          |          z
  z  |dd<   |                    dg|t          |          z
  z             |S )	zSplit 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   r   )r   r   r   stripr   extendr   r!   r   r   r   r8   r{   r   )r?   r   r   line_wrappedr   r   ro   r   max_cell_linesvalignmissings              r   r   zTexttable._splitit  s    tT[11 	' 	'KD%EZZ%% 8 87799??LL$$$$LLQ!6!67777&&&&T#c<*@*@%A%ABBdl;; 
	A 
	ALD& }}(3t9944#gk"2"22RaRRD3w{Wq['@#A#AABBBB34>CII#=>RaRRDNSYY$>?@@@@r   N)rL   )T)F),rC   rD   rE   rV   rW   rU   rT   r>   rZ   rR   rY   rr   rv   ry   r|   r   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r   rt   r   r   r   r   r   r   r   r   r   r   r   r   r2   r   r   r   r      s       FFFF   
 
 
    &  *        *  (      "   *  6 # # [# 5 5 [5 	. 	. [	. 	. 	. [	.   [   [   [% % %2" " "1 1 11 1 11 1 11 1 1' ' '" " "   (  && & &P	2 	2 	2   >    r   __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
 
Loue)r   r   r   r   r   )r   r   r   r   r   )textr   expr   auto)abcd67i  Y   gy&1 `@)efghijkg_)P@g!rh?gffffffV@g@xD)lmnܐ؆/r  gYV@gV瞯B=)opqrstugZd;O?g/'%EPg      W@l      Zx^+)*rF   
__future__r   __all__
__author____license____version____credits__r*   r   r	   r   ImportErrorr   r+   r,   r   r   	functoolsr!   version_inforh   r'   bytesr(   unicoder/   r8   r=   r   rH   r   rC   tablery   r|   r   printr   rr   rW   r   r2   r   r   <module>r"     s  6 6p       (
)4
6 


    NNN( ( ( (   	- 	- 	- 	-   
:;;;	- 	-NNN* * * *  	 	 	    	      vLJJLJ! ! !" " "	 	 	 	 	Y 	 	 		 	 	 	 	Y 	 	 	
\ \ \ \ \ \ \ \~ zIKKE	)))	///***	NN///444888AAAC D D D 
E%**,,	EGGGIKKE	NN9#$$$	         
 
222333	NN>>>???UUUHHHOOO	Q R R R
 
E%**,,1 s5   & A4A!AAAA' 'A21A2