
    }cSX                     L   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	 dd	lZd Z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!jE                  g d       e!jG                  g d       e!jI                  g dg dg dg dg        e%e!jM                                 e%         e       Z!e!jO                  ejP                         e!jS                  g d       e!jE                  g d       e!jI                  g d g d!g d"g d#g d$g        e%e!jM                                y	y	# e$ r2 	 dd	lZd Zn%# e$ r ej                  j                  d        w xY wY nw xY w# e$ r d ZY uw xY w)%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.6.7a  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     +/usr/lib/python3/dist-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                 @    t        dt        j                  |             S ):Return the rendering width of a unicode character
        r   )maxwcwidthcs    r   uchar_widthr   w   s     1gooa())r   c                 `    t        j                  |       dv ryt        j                  |       ryy)r   WF   r      )unicodedataeast_asian_width	combiningr   s    r   r   r   |   s.     ''*d2""1%r   )reduce)   r   c                    t        | t              r| S t        | t              r	 t        | d      S t        |       S # t        $ r=}t        j
                  j                  d| d|d       t        | dd      cY d}~S d}~ww xY w)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/      sv     #|$
	C	$	9W--
 C  	 " 	9JJUXZbcdWi88	9s   ; 	B2A<6B<Bc                     t        | t              st        | t              r+t        t	        |       D cg c]  }t        |       c}      S | j                         S c c}w )zMRedefining len here so it will be able to work with non-ASCII characters
    )r&   r(   r'   sumr/   r   __len__)iterabler   s     r   lenr4      sK     (J':h+MK,ABqKNBCC!! Cs   Ac                       e Zd ZdZd Zd Zy)r   zEException raised when specified rows don't fit the required size
    c                 @    || _         t        j                  | |d       y )N )msg	Exception__init__)selfr8   s     r   r:   zArraySizeError.__init__   s    4b)r   c                     | j                   S r   )r8   r;   s    r   __str__zArraySizeError.__str__   s    xxr   N)__name__
__module____qualname____doc__r:   r>    r   r   r   r      s    *r   c                       e Zd ZdZy)FallbackToTextz#Used for failed conversion to floatN)r?   r@   rA   rB   rC   r   r   rE   rE      s    -r   rE   c                   2   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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*y(),r   r   r         c                 
   | j                  |       d| _        t        j                  t        j                  z  t        j
                  z  t        j                  z  | _        | j                  g d       | j                          y)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__   s`     	9%%%	(8(889;K;KK
+,

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

        - reset rows and header
        N)_hline_string	_row_size_header_rowsr=   s    r   rV   zTexttable.reset   s&     "
r   c                 0    |dkD  r	|| _         | S 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_widthrW   s     r   rN   zTexttable.set_max_width   s%     (11}) ;@r   c                     t        |      dk7  rt        d      |D cg c]  }t        |       c}D cg c]  }|dd 	 }}|\  | _        | _        | _        | _        | S c c}w c c}w )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:

            ['-', '|', '+', '=']
        rG   z!array should contain 4 charactersNr   )r4   r   str_char_horiz
_char_vert_char_corner_char_header)r;   arraysxs       r   rU   zTexttable.set_chars   sl     u:? !DEE38!:a3q6!:<A!BQ%<<49	2	4?t0 ";<s
   A A%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)rT   rZ   )r;   decos     r   set_decozTexttable.set_deco   s    " 
!r   c                 6    | j                  |       || _        | 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_alignr;   rf   s     r   set_header_alignzTexttable.set_header_align  s     	U#"r   c                 6    | j                  |       || _        | 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
        )rm   _alignro   s     r   set_cols_alignzTexttable.set_cols_align  s     	U#r   c                 6    | j                  |       || _        | 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
        )rm   _valignro   s     r   set_cols_valignzTexttable.set_cols_valign(  s     	U#r   c                 6    | j                  |       || _        | S )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
        )rm   _dtypero   s     r   set_cols_dtypezTexttable.set_cols_dtype6  s      	U#r   c                     | j                  |       	 t        t        t        |            }t	        t
        |      dk  rt        	 || _
        | S # t        $ r! t        j                  j                  d        w xY w)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
)rm   listmapintr!   min
ValueErrorr*   r+   r,   _widthro   s     r   set_cols_widthzTexttable.set_cols_widthJ  sv     	U#	S%)Ec5!Q&   '
 	  	JJMN	s   2A *A9c                 V    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   rO   )r;   r   s     r   set_precisionzTexttable.set_precision^  s.     E{c!UQYFGGr   c                 d    | j                  |       t        t        t        |            | _        | S )z(Specify the header of the table
        )rm   r{   r|   r/   r\   ro   s     r   headerzTexttable.headerk  s*     	U#CU34r   c                    | j                  |       t        | d      sdg| j                  z  | _        g }t	        |      D ]&  \  }}|j                  | j                  ||             ( | j                  j                  |       | S )zSAdd a row in the rows stack

        - cells can contain newlines and tabs
        rx   a)rm   hasattrr[   rx   	enumerateappend_strr]   )r;   rf   cellsirh   s        r   add_rowzTexttable.add_rows  sx     	U#tX&%$..0DKe$ 	*DAqLL1a)	*

% r   c                     |rQt        |d      r,t        |d      r | j                  |j                                n| j                  |d          |dd }|D ]  }| j                  |        | 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  sg     tZ(WT6-BDIIK(DG$ABx 	CLL	r   c                 ~   | j                   s| j                  sy| j                          | j                          d}| j	                         r|| j                         z  }| j                   rC|| j                  | j                   d      z  }| j                         r|| j                         z  }d}| j                  D ]X  }|dz  }|| j                  |      z  }| j                         s-|t        | j                        k  sF|| j                         z  }Z | j	                         r|| j                         z  }|dd S )zJDraw the table

        - the table is returned as a whole string
        Nr7   T)isheaderr   r   )r\   r]   _compute_cols_width_check_align_has_border_hline
_draw_line_has_header_hline_header_has_hlinesr4   )r;   outlengthr   s       r   drawzTexttable.draw  s    ||DJJ  "4;;= C<<4??4<<$???C!t))++:: 	%CaKF4??3''C!fs4::&>t{{}$		%
 4;;= C3Bxr   c                 l    |
t               	 t        |      S # t        t        f$ r t               w xY wr   )rE   float	TypeErrorr   )clsrh   s     r   	_to_floatzTexttable._to_float  s;    9 ""	#8O:& 	# ""	#s   
 3c           	          t        |      t        k(  rt        |      S t        t        t        | j	                  |                        S )z)Integer formatting class-method.
        )r   r}   ra   roundr   r   rh   kws      r   _fmt_intzTexttable._fmt_int  s6     7c>q6Ms5q!12344r   c                 P    |j                  d      }d|| j                  |      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   rh   r   r   s       r   
_fmt_floatzTexttable._fmt_float  *     FF3KCMM!,---r   c                 P    |j                  d      }d|| j                  |      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                     | j                  |      }t        |      dkD  r| j                  }n<||k7  r| j                  }n*|t	        |      z
  dk(  r| j
                  }n| j                  } ||fi |S )zauto formatting class-method.g    חAr   )r   absr   r   r   r   r   )r   rh   r   ffns        r   	_fmt_autozTexttable._fmt_auto  sf     MM!q6C<B!VBq\QBB!{r{r   c                 .   | j                   | j                  | j                  | j                  | j                  d}| j
                  }| j                  |   }	 t        |      r ||      S  ||   ||      S # t        $ r | j	                  |      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   r   et)r   )	r   r   r   r   r   rO   rx   callablerE   )r;   r   rh   FMTr   dtypes         r   r   zTexttable._str  s      OOA	%Qx!s5z!q)) 	%>>!$$	%s   A7 *A7 7BBc                     | j                   st        |      | _         y| j                   t        |      k7  rt        d| j                   z        y)zCCheck that the specified array fits the previous rows size
        z array should contain %d elementsN)r[   r4   r   ro   s     r   rm   zTexttable._check_row_size	  sG     ~~ ZDN^^s5z) !C.."! " " *r   c                 B    | j                   t        j                  z  dkD  S )z8Return a boolean, if vlines are required or not
        r   )rT   r   rP   r=   s    r   _has_vlineszTexttable._has_vlines       zzI,,,q00r   c                 B    | j                   t        j                  z  dkD  S )z8Return a boolean, if hlines are required or not
        r   )rT   r   rQ   r=   s    r   r   zTexttable._has_hlines  r   r   c                 B    | j                   t        j                  z  dkD  S )z7Return a boolean, if border is required or not
        r   )rT   r   rR   r=   s    r   r   zTexttable._has_border  r   r   c                 B    | j                   t        j                  z  dkD  S )z<Return a boolean, if header line is required or not
        r   )rT   r   rS   r=   s    r   r   zTexttable._has_header%  r   r   c                 $    | j                  d      S )z'Print header's horizontal line
        T)_build_hliner=   s    r   r   zTexttable._hline_header+  s       &&r   c                 \    | j                   s| j                         | _         | j                   S )z!Print an horizontal line
        )rZ   r   r=   s    r   r   zTexttable._hline1  s*     !!!%!2!2!4D!!!r   c                 V   | j                   }|r| j                  }||| j                  g| j                            |}|j	                  | j
                  D cg c]  }||z  	 c}      }| j                         r#| j                  |||| j                  d}|S |dz  }|S c c}w )zTReturn a string used to separated rows or separate header from
        rows
        r$   )rb   re   rd   r   joinr   r   )r;   	is_headerhorizrg   r   ls         r   r   zTexttable._build_hline9  s       %%Et'8'89$:J:J:LM FFt{{3!EAI34"&"3"3UAu!!#A  IA 4s   B&c                 .   |j                  d      }d}|D ]|  }d}|j                  d      }t        |t        t        dt	        |      dz                     D ]-  \  }}|t	        |      z   }|t	        |      k  s#|dz  dz   dz  }/ t        ||      }~ |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   rH   )splitzipr{   ranger4   r   )	r;   cell
cell_linesmaxiliner   partspartr   s	            r   	_len_cellzTexttable._len_cellM  s     ZZ%
 	%DFJJt$Eud5CJN+C&DE 1a#d)+s5z>$ai!mq0F1 tV$D	% r   c                 D   t        | d      ryg }| j                  r(| j                  D cg c]  }| j                  |       }}| j                  D ]S  }t	        |t        t        t        |                        D ]'  \  }}	 t        ||   | j                  |            ||<   ) U t        |      }t        |      }d|dz
  z  ddg| j                            z   }| j                  ry||z   | j                  kD  rg	 | j                  ||z   k  rt        d      | j                  |z
  }	dg|z  }
d}|	dkD  r+|
|   ||   k  r|
|xx   dz  cc<   |	dz  }	|dz   |z  }|	dkD  r+|
}|| _        yc c}w # t        t        f$ r$ |j                  | j                  |             Y $w xY w)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   Nr"   r   r   rG   z max_width too low to render data)r   r\   r   r]   r   r{   r   r4   r   r   
IndexErrorr   r1   r   r_   r   r   )r;   r   rh   r   r   r   ncolscontent_width
deco_widthavailable_widthnewmaxis              r   r   zTexttable._compute_cols_width`  s    4"<<04>1T^^A&>D>:: 	6Cc4c#h#89 6Q6!$q'4>>$+?@DG6	6 D	D	a[Aa5)9)9);#<<
??
 :dooM %*"45 !CDD"oo
:OcEkGA!A%1:Q'AJ!OJ#q(OUeO	 "A%
 D5 ?
 ":. 6KKt 456s   E'!E,,/FFc                     t        | d      sdg| j                  z  | _        t        | d      sdg| j                  z  | _        t        | d      sdg| j                  z  | _        yy)zFCheck if alignment has been specified, set default one if not
        rn   r   rr   r   ru   r   N)r   r[   rn   rr   ru   r=   s    r   r   zTexttable._check_align  s^     t_-"%!7DtX&%$..0DKtY'54>>1DL (r   c           	         | j                  ||      }d}d}t        t        |d               D ]*  }| j                         r|d| j                  z  z  }d}t        || j                  | j                        D ]  \  }}}	|dz  }||   }
|t        |
      z
  }|r| j                  |dz
     }	|	dk(  r|||z  |
z   z  }n@|	dk(  r0|t        |dz        |z  |
z   t        |dz  |dz  z         |z  z   z  }n||
||z  z   z  }|t        |      k  s|d	|| j                  g| j                            z  z  } |d
d|| j                  z   g| j                            z  z  }- |S )zPDraw a line

        Loop over a single cell length, over all the cells
         r7   r   z%s r   rr   r   z %s z%s
)_splititr   r4   r   rc   r   r   rr   rn   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A!ut..F&)$T[[&I Q"eU! G	s9~- ..vz:EC<4%<)33Cc\CQK%/);!$q&46/2U:; <C 9te|33CCI%6UDOO$<T=M=M=O$PPPCQ 6R!89$:J:J:LMMMC'	N( 
r   c           	         g }t        || j                        D ]n  \  }}g }|j                  d      D ]B  }|j                         dk(  r|j	                  d       (|j                  t        ||             D |j	                  |       p t        t        t        t        t        |                  }t        || j                        D ]  \  }}	|rd}	|	dk(  rK|t        |      z
  }
dgt        |
dz        z  |dd |j                  dgt        |
dz  |
dz  z         z         Z|	dk(  rdg|t        |      z
  z  |dd u|j                  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$   r7   r   mr   Nr   b)r   r   r   stripr   extendr   r!   r   r{   r|   r4   ru   r}   )r;   r   r   line_wrappedr   r   rf   r   max_cell_linesvalignmissings              r   r   zTexttable._splitit  s]    tT[[1 	'KD%EZZ% 8779?LL$LLQ!67	8
 &	'  T#c<*@%ABdll; 
	ALD&}(3t944#gk"22RaRD3w{Wq['@#AAB34>CI#=>RaRDNSY$>?@
	A r   N)P   )T)F)+r?   r@   rA   rR   rS   rQ   rP   r:   rV   rN   rU   rk   rp   rs   rv   ry   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r   rm   r   r   r   r   r   r   r   r   r   r   r   r   rC   r   r   r   r      s    FFFF
&*(("*6 # # 5 5 	. 	. 	. 	.    %0"1111'"(&&P	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^+)*rB   
__future__r   __all__
__author____license____version____credits__r*   r   r	   r   ImportErrorr   r+   r,   r   r   	functoolsr!   version_infora   r'   bytesr(   unicoder/   r4   r9   r   rE   r   r?   tablers   rv   r   printr   rk   rS   ry   rC   r   r   <module>r     s  6p  (
)4
6  (* vLJLJ!"	Y 		Y 	
U Up zKE	)	/*	NN/48AC D 
%**,	GKE	NN9##$	   
 
23	NN>?UHO	Q R
 
%**,1 Q  	- 

:;	-$  		s:   E F F#E+*F+"FFFF#"F#