
    .PhU                        d Z ddlmZ ddlZddlmZ ddlmZm	Z	m
Z
 	 ddlmZ  ed	          Zn# e$ r  e            ZY nw xY w	 d
gZddZddZ ed          eee ee          eeeeej        ej        ej        ej        hZ  G d de!          Z" G d d          Z# G d de#          Z$ G d de#          Z% G d de#          Z& G d de#          Z' G d de#          Z( G d d
          Z)dS )a	  If there is one recurring theme in ``boltons``, it is that Python
has excellent datastructures that constitute a good foundation for
most quick manipulations, as well as building applications. However,
Python usage has grown much faster than builtin data structure
power. Python has a growing need for more advanced general-purpose
data structures which behave intuitively.

The :class:`Table` class is one example. When handed one- or
two-dimensional data, it can provide useful, if basic, text and HTML
renditions of small to medium sized data. It also heuristically
handles recursive data of various formats (lists, dicts, namedtuples,
objects).

For more advanced :class:`Table`-style manipulation check out the
`pandas`_ DataFrame.

.. _pandas: http://pandas.pydata.org/

    )escapeN)islice)SequenceMappingMutableSequence   )make_sentinel_MISSING)var_nameTablec                 <   	 t          |           }ne# t          $ rX 	 t          t          |                     }n7# t          $ r* t          t                              |                     }Y nw xY wY nw xY w|r#t          |          |k    r|d |dz
           dz   }|S )N   z...)str	Exceptionreprobject__repr__lenobjmaxlentexts      R/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/boltons/tableutils.pyto_textr   T   s    -3xx - - -	-tCyy>>DD 	- 	- 	-vs++,,DDD	--
  )#d))f$$KVaZK 5(Ks/    
A4:A41A.+A4-A..A43A4c                 F    t          | |          }t          |d          S )Nr   T)quote)r   html_escaper   s      r   escape_htmlr   b   s'    3v&&&Dt4((((    c                       e Zd ZdS )UnsupportedDataN)__name__
__module____qualname__ r    r   r"   r"   m   s        Dr    r"   c                       e Zd Zd Zd ZdS )	InputTypec                     d S Nr&   )selfakws      r   __init__zInputType.__init__r   s    r    c                 $      fd|D             S )Nc                 <    g | ]}                     |          S r&   )	get_entry).0entryheadersr+   s     r   
<listcomp>z+InputType.get_entry_seq.<locals>.<listcomp>v   s'    EEE5ug..EEEr    r&   )r+   data_seqr4   s   ` `r   get_entry_seqzInputType.get_entry_sequ   s!    EEEEEHEEEEr    N)r#   r$   r%   r.   r7   r&   r    r   r(   r(   q   s7          F F F F Fr    r(   c                   &    e Zd Zd Zd Zd Zd ZdS )DictInputTypec                 ,    t          |t                    S r*   )
isinstancer   r+   r   s     r   
check_typezDictInputType.check_typez   s    #w'''r    c                 D    t          |                                          S r*   )sortedkeysr<   s     r   guess_headerszDictInputType.guess_headers}   s    chhjj!!!r    c                      fd|D             S )Nc                 :    g | ]}                     |          S r&   getr2   hr   s     r   r5   z+DictInputType.get_entry.<locals>.<listcomp>   s#    ,,,q

,,,r    r&   r+   r   r4   s    ` r   r1   zDictInputType.get_entry   s    ,,,,G,,,,r    c                      fd|D             S )Nc                 .    g | ]fd D             S )c                 :    g | ]}                     |          S r&   rD   )r2   rG   cis     r   r5   z:DictInputType.get_entry_seq.<locals>.<listcomp>.<listcomp>   s#    ,,,q,,,r    r&   )r2   rL   r4   s    @r   r5   z/DictInputType.get_entry_seq.<locals>.<listcomp>   s/    ;;;,,,,G,,,;;;r    r&   rH   s     `r   r7   zDictInputType.get_entry_seq   s    ;;;;s;;;;r    Nr#   r$   r%   r=   rA   r1   r7   r&   r    r   r9   r9   y   sP        ( ( (" " "- - -< < < < <r    r9   c                        e Zd Zd Zd Zd ZdS )ObjectInputTypec                 N    t          |          t          vot          |d          S )N	__class__)type_DNRhasattrr<   s     r   r=   zObjectInputType.check_type   s"    Cyy$Bk)B)BBr    c                     g }t          |          D ]I}	 t          ||          }n# t          $ r Y  w xY wt          |          r4|                    |           J|S r*   )dirgetattrr   callableappend)r+   r   r4   attrvals        r   rA   zObjectInputType.guess_headers   s    HH 
	! 
	!Dc4((     }} NN4    s   &
33c                     g }|D ]K}	 |                     t          ||                     '# t          $ r |                     d            Y Hw xY w|S r*   )rY   rW   r   )r+   r   r4   valuesrG   s        r   r1   zObjectInputType.get_entry   sp     	$ 	$A$gc1oo.... $ $ $d#####$s   #,AAN)r#   r$   r%   r=   rA   r1   r&   r    r   rO   rO      sD        C C C      r    rO   c                   &    e Zd Zd Zd Zd Zd ZdS )ListInputTypec                 ,    t          |t                    S r*   )r;   r   r<   s     r   r=   zListInputType.check_type   s    #///r    c                     d S r*   r&   r<   s     r   rA   zListInputType.guess_headers       tr    c                     |S r*   r&   rH   s      r   r1   zListInputType.get_entry   s    
r    c                     |S r*   r&   r+   obj_seqr4   s      r   r7   zListInputType.get_entry_seq   s    r    NrM   r&   r    r   r_   r_      sP        0 0 0        r    r_   c                   &    e Zd Zd Zd Zd Zd ZdS )TupleInputTypec                 ,    t          |t                    S r*   )r;   tupler<   s     r   r=   zTupleInputType.check_type   s    #u%%%r    c                     d S r*   r&   r<   s     r   rA   zTupleInputType.guess_headers   rb   r    c                      t          |          S r*   listrH   s      r   r1   zTupleInputType.get_entry   s    Cyyr    c                     d |D             S )Nc                 ,    g | ]}t          |          S r&   rm   )r2   ts     r   r5   z0TupleInputType.get_entry_seq.<locals>.<listcomp>   s    )))AQ)))r    r&   re   s      r   r7   zTupleInputType.get_entry_seq   s    ))))))r    NrM   r&   r    r   rh   rh      sP        & & &    * * * * *r    rh   c                   &    e Zd Zd Zd Zd Zd ZdS )NamedTupleInputTypec                 L    t          |d          ot          |t                    S )N_fields)rT   r;   rj   r<   s     r   r=   zNamedTupleInputType.check_type   s!    sI&&A:c5+A+AAr    c                 *    t          |j                  S r*   )rn   ru   r<   s     r   rA   z!NamedTupleInputType.guess_headers   s    CK   r    c                      fd|D             S )Nc                 2    g | ]}t          |d           S r*   rW   rF   s     r   r5   z1NamedTupleInputType.get_entry.<locals>.<listcomp>   s%    777!Q%%777r    r&   rH   s    ` r   r1   zNamedTupleInputType.get_entry   s    7777w7777r    c                      fd|D             S )Nc                 .    g | ]fd D             S )c                 2    g | ]}t          |d           S r*   ry   rF   s     r   r5   z@NamedTupleInputType.get_entry_seq.<locals>.<listcomp>.<listcomp>   s%    8881a&&888r    r&   )r2   r   r4   s    @r   r5   z5NamedTupleInputType.get_entry_seq.<locals>.<listcomp>   s/    LLLS8888888LLLr    r&   re   s     `r   r7   z!NamedTupleInputType.get_entry_seq   s    LLLLGLLLLr    NrM   r&   r    r   rs   rs      sX        B B B! ! !8 8 8M M M M Mr    rs   c                   ~   e Zd ZdZ e             e             e             e             e            gZ	d\  Z
Zd\  ZZd\  ZZd\  ZZd\  ZZd\  ZZdedfd	Zd
 ZddZd Zeeddfd            Zeeddfd            Zeeddfd            Zeedfd            Zd Z d Z!d Z"	 	 	 ddZ#d Z$d Z%d Z&ddZ'dS )r   aM  
    This Table class is meant to be simple, low-overhead, and extensible. Its
    most common use would be for translation between in-memory data
    structures and serialization formats, such as HTML and console-ready text.

    As such, it stores data in list-of-lists format, and *does not* copy
    lists passed in. It also reserves the right to modify those lists in a
    "filling" process, whereby short lists are extended to the width of
    the table (usually determined by number of headers). This greatly
    reduces overhead and processing/validation that would have to occur
    otherwise.

    General description of headers behavior:

    Headers describe the columns, but are not part of the data, however,
    if the *headers* argument is omitted, Table tries to infer header
    names from the data. It is possible to have a table with no headers,
    just pass in ``headers=None``.

    Supported inputs:

    * :class:`list` of :class:`list` objects
    * :class:`dict` (list/single)
    * :class:`object` (list/single)
    * :class:`collections.namedtuple` (list/single)
    * TODO: DB API cursor?
    * TODO: json

    Supported outputs:

    * HTML
    * Pretty text (also usable as GF Markdown)
    * TODO: CSV
    * TODO: json
    * TODO: json lines

    To minimize resident size, the Table data is stored as a list of lists.
    )z<tr>z</tr>)z<th>z</th>)z<td>z</td>)z<thead>z</thead>)z<tbody>z</tbody>)z<table>z</table>Nc                     |t           u r*g }|r&t          |d                   t          |dd           }}|pg | _        |pi | _        g | _        d| _        |                     |           d S )Nr   r   )r
   rn   r   r4   metadata_data_widthextend)r+   datar4   r   s       r   r.   zTable.__init__  sv    hG E $T!WvdAt/D/D}" B
Dr    c                     |sdS | j                             |           |                                  |                                  dS )z@
        Append the given data to the end of the Table.
        N)r   r   
_set_width_fill)r+   r   s     r   r   zTable.extend  sH      	F
$

r    Fc                     |rd| _         | j         rd S | j        rt          | j                  | _         d S t          d | j        D                       | _         d S )Nr   c                 ,    g | ]}t          |          S r&   r   )r2   ds     r   r5   z$Table._set_width.<locals>.<listcomp>%  s    666a3q66666r    )r   r4   r   maxr   )r+   resets     r   r   zTable._set_width  sd     	DK; 	F< 	dl++DKF664:66677r    c                     | j         d g}}|sd S | j        D ]2}|t          |          z
  }|dk    r|                    ||z             3d S )Nr   )r   r   r   r   )r+   widthfillerr   rems        r   r   zTable._fill'  sa    dVv 	F 	' 	'A#a&&.CQww#&&&r    r   c                 N    |                      |||t                      |          S )zCreate a Table from a :class:`dict`. Operates the same as
        :meth:`from_data`, but forces interpretation of the data as a
        Mapping.
        r   r4   	max_depth
_data_typer   )	from_datar9   clsr   r4   r   r   s        r   	from_dictzTable.from_dict1  /     }}$'0]__&.  0 0 	0r    c                 N    |                      |||t                      |          S )zCreate a Table from a :class:`list`. Operates the same as
        :meth:`from_data`, but forces the interpretation of the data
        as a Sequence.
        r   )r   r_   r   s        r   	from_listzTable.from_list;  r   r    c                 N    |                      |||t                      |          S )zCreate a Table from an :class:`object`. Operates the same as
        :meth:`from_data`, but forces the interpretation of the data
        as an object. May be useful for some :class:`dict` and
        :class:`list` subtypes.
        r   )r   rO   r   s        r   from_objectzTable.from_objectE  s1     }}$'0_=N=N&.  0 0 	0r    c                    |                     dd          }|                     dd          }|dk     r | ||          S t          |t                    }|rA|s | ||          S |d         }|s'| j        D ]}	|	                    |          r|	} nd}|}n(t          |          t          v r | |gg||          S |}|sB| j        D ]}	|	                    |          r|	} n t          dt          |          z            |t          u r|	                    |          }|r|
                    ||          }
n|                    ||          g}
|dk    rw|dz
  }t          |
          D ]b\  }}t          |          D ]M\  }}t          |          t          v r	 |                     ||	          |
|         |<   ># t          $ r Y Jw xY wc | |
||          S )
a  Create a Table from any supported data, heuristically
        selecting how to represent the data in Table format.

        Args:
            data (object): Any object or iterable with data to be
                imported to the Table.

            headers (iterable): An iterable of headers to be matched
                to the data. If not explicitly passed, headers will be
                guessed for certain datatypes.

            max_depth (int): The level to which nested Tables should
                be created (default: 1).

            _data_type (InputType subclass): For advanced use cases,
                do not guess the type of the input data, use this data
                type instead.
        r   Nr   r   )r4   r   r   Fzunsupported data type %rr   )popr;   r   _input_typesr=   rR   rS   r"   r
   rA   r7   r1   	enumerater   )r   r   r4   r   kwargsr   r   is_seqto_checkitentriesnew_max_depthir3   jcells                   r   r   zTable.from_dataP  s   0 ::j$//ZZd33
q==3w::::D(++ 	 ?s7X>>>>AwH $* $ $B}}X.. %'

 #F#HDzzT!! sTF8WxHHHHH 	4& 4 4==** !#JE &&@(,T

'3 4 4 4h ..x88G 	< ..tW==GG!++D'::;Gq==%MM%g.. 	! 	!5(// ! !GAtDzzT)) !(+d@M )6 )O )O
1* ! ! ! !! s7Gh????s    F>>
G
Gc                 *    t          | j                  S r*   )r   r   )r+   s    r   __len__zTable.__len__  s    4:r    c                     | j         |         S r*   )r   )r+   idxs     r   __getitem__zTable.__getitem__  s    z#r    c                 l    | j         j        }| j        r| d| j        d| j        dS | d| j        dS )Nz	(headers=z, data=)()rQ   r#   r4   r   )r+   cns     r   r   zTable.__repr__  sR    ^$< 	+II4<II$*IIII**4:****r    Tc                    g }g }|rq| j         rjt                              | j         |          }	|	                    d|d|          }
|dk    r*|                    |
           |                    d           |rT| j        rM|                    | j                   |                    dg| j        t          | j                  z
  z             |r|                    | j	                   |pd}|d	         
                                }|d
k    rt          |           dk    rdnd}|dk    r|                     |||           n1|dk    r|                     |||           nt          d|z            |r7| j         r0|dk    r*|                    d           |                    |
           |r|                    | j                   |rdnd}|                    |          S )a  Render this Table to HTML. Configure the structure of Table
        HTML by subclassing and overriding ``_html_*`` class
        attributes.

        Args:
            orientation (str): one of 'auto', 'horizontal', or
                'vertical' (or the first letter of any of
                those). Default 'auto'.
            wrapped (bool): whether or not to include the wrapping
                '<table></table>' tags. Default ``True``, set to
                ``False`` if appending multiple Table outputs or an
                otherwise customized HTML wrapping tag is needed.
            with_newlines (bool): Set to ``True`` if output should
                include added newlines to make the HTML more
                readable. Default ``False``.
            with_metadata (bool/str): Set to ``True`` if output should
                be preceded with a Table of preset metadata, if it
                exists. Set to special value ``'bottom'`` if the
                metadata Table HTML should come *after* the main HTML output.
            max_depth (int): Indicate how deeply to nest HTML tables
                before simply reverting to :func:`repr`-ing the nested
                data.

        Returns:
            A text string of the HTML of the rendered table.

        r   TF)with_headerswith_newlineswith_metadatar   bottomz<br />Nautor   r,   r   rG   v)r4   r   z;expected one of 'auto', 'vertical', or 'horizontal', not %r
 )r   r   r   to_htmlrY   r4   r   r   r   _html_table_taglower_add_horizontal_html_lines_add_vertical_html_lines
ValueError_html_table_tag_closejoin)r+   orientationwrappedr   r   r   r   linesr4   metadata_tablemetadata_htmlolseps                r   r   zTable.to_html  sL   <  		'T] 		'"__T]7@ - B BN*22ANAF=F 3 H HM ((]+++X&&& 	GDL 	GNN4<(((NND6T[3t|3D3D%DEFFF 	/LL-...!+V^!!##99D		A3B99++E76? , A A A A3YY))%4= * ? ? ? ?  57BC D D D 	(T] 	(}/H/HLL"""LL''' 	5LL3444#+ddxxr    c                      t          |          S )zCalled on each value in an HTML table. By default it simply escapes
        the HTML. Override this method to add additional conditions
        and behaviors, but take care to ensure the final output is
        HTML escaped.
        )r   )r+   values     r   get_cell_htmlzTable.get_cell_html  s     5!!!r    c           	         | j         |dk    r|dz
  n|}|dk    r|dz
  }|r| j        | j        z   }|                    | j                   |                    | j        | j        z   |                    fd|D                       z   | j        z   | j        z              |                    | j                   | j        | j	        z   | j
        | j	        z   | j
        | j        z   }}}|                    | j                   | j        D ]}	|dk    reg }
|	D ]_}t          |t                    r*|
                    |                    |                     A|
                     |                     `nfd|	D             }
|                    d                    ||                    |
          |g                     |                    | j                   d S )Nr   c                 &    g | ]} |          S r&   r&   )r2   rG   escs     r   r5   z4Table._add_horizontal_html_lines.<locals>.<listcomp>  s!    $=$=$=SSVV$=$=$=r    r   c                 &    g | ]} |          S r&   r&   )r2   cr   s     r   r5   z4Table._add_horizontal_html_lines.<locals>.<listcomp>  s!    333!ss1vv333r    r   )r   _html_th_close_html_thrY   _html_thead_html_trr   _html_tr_close_html_thead_close_html_td_html_td_close_html_tbodyr   r;   r   r   _html_tbody_close)r+   r   r4   r   	new_depth_ththtrtd_tdtd_td_trrow_fill_partsr   r   s               @r   r   z Table._add_horizontal_html_lines  s    %.]]IMM		q==!AI 	1'$-7ELL)***LL6$=$=$=$=W$=$=$=>>?,-/3/BC D D D LL/000#}t}<#2T]B#2T5HH e 	T%&&&: 
	K 
	KC1}}  6 6D!$.. 6#**4<<)<+L+LMMMM#**33t995555	6 4333s333LL$

;(?(?!HIIJJJJT+,,,,,r    c                    | j         |dk    r|dz
  n|}| j        | j        | j        }}}| j        | j        | j        z   }	}| j        | j        z   }
t          | j                  D ]|g}|r'|	                    | |                   |g           |dk    r}|dz
  }g }| j
        D ]m}|         }t          |t                    r*|                    |                    |                     I|                     |                              nnfd| j
        D             }|	                    ||	                    |          |
g           |                    d                    |                     d S )Nr   r   c                 2    g | ]} |                   S r&   r&   )r2   r   r   r   s     r   r5   z2Table._add_vertical_html_lines.<locals>.<listcomp>*  s%    AAAsss3q6{{AAAr    r   )r   r   r   r   r   r   r   ranger   r   r   r;   r   rY   r   r   )r+   r   r4   r   r   trth_thtdr   r   
line_partsr   r   r   r   r   s                  @@r   r   zTable._add_vertical_html_lines  s    %.]]IMM		mT]D4GBM4#6#FE$t'::t{## 	. 	.AJ >!!2ss71:"<===1}}%M	 : 8 8Cq6D!$.. 8#**4<<)<+L+LMMMM#**33s1v;;77778 BAAAAdjAAAr5::k#:#:FCDDDLL,,----!	. 	.r    c           	        	 g }g 	t          | j                  }fd| j        D             }t          | j                  D ]i}d |D             }|r7|                    t          t          ||                                        	                    t          |                     j|rs|                    d	                    	fdt          |          D                                  |                    d	                    d 	D                                  |D ]C}|                    d	                    	fdt          |          D                                  Dd		                    |          S )
a  Get the Table's textual representation. Only works well
        for Tables with non-recursive data.

        Args:
            with_headers (bool): Whether to include a header row at the top.
            maxlen (int): Max length of data in each cell.
        c                 ,    g | ]}fd |D             S )c                 2    g | ]}t          |           S )r   )r   )r2   r   r   s     r   r5   z,Table.to_text.<locals>.<listcomp>.<listcomp>9  s&    CCCtgd6222CCCr    r&   )r2   r   r   s     r   r5   z!Table.to_text.<locals>.<listcomp>9  s>     , , , DCCCsCCC , , ,r    c                 ,    g | ]}t          |          S r&   r   )r2   curs     r   r5   z!Table.to_text.<locals>.<listcomp><  s    888s#c((888r    r   z | c                 L    g | ] \  }}|                     |                   !S r&   center)r2   r   rG   widthss      r   r5   z!Table.to_text.<locals>.<listcomp>A  sC     %E %E %E)-A &'XXfQi%8%8 %E %E %Er    z-|-c                     g | ]}d |z  S )-r&   )r2   ws     r   r5   z!Table.to_text.<locals>.<listcomp>C  s    $=$=$=S1W$=$=$=r    c                 L    g | ] \  }}|                     |                   !S r&   r   )r2   r   r   r   s      r   r5   z!Table.to_text.<locals>.<listcomp>E  sC     %D %D %D)0D &*[[%;%; %D %D %Dr    r   )rn   r4   r   r   r   rY   r   r   r   r   r   )
r+   r   r   r   r4   	text_datar   
cur_widthsr   r   s
     `      @r   r   zTable.to_text.  s    t|$$, , , , $
, , ,	%% 	+ 	+C88i888J M!!#ggcl6&J&J&J"K"KLLLMM#j//**** 	@LL %E %E %E %E1:71C1C%E %E %E F F G G GLL$=$=f$=$=$=>>??? 	F 	FCLL %D %D %D %D4=cNN%D %D %D E E F F F Fyyr    )F)NTTTFr   )TN)(r#   r$   r%   __doc__r9   r_   rs   rh   rO   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r.   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r&   r    r   r   r      s        % %P "MOO]]__''))>>+;+;#O%%'L  /Hn.Hn.Hn%:"K"%:"K" .C*O* (T 
 
 
 
  8 8 8 8   %-T 0 0 0 [0 %-T 0 0 0 [0 '/1t 0 0 0 [0 %- K@ K@ K@ [K@Z    + + + 1515/0D D D DL" " "- - -:. . .0           r    r*   )*r   htmlr   r   types	itertoolsr   collections.abcr   r   r   	typeutilsr	   r
   ImportErrorr   __all__r   r   rR   boolcomplexfloatNotImplementedslicer   bytesintFunctionType
MethodTypeBuiltinFunctionTypeGeneratorTyperS   	TypeErrorr"   r(   r9   rO   r_   rh   rs   r   r&   r    r   <module>r     s  > * ' & & & & &        > > > > > > > > > >((((((}j111HH   vxxHHH$ )   ) ) ) )
 	T

D'5$$~*>*>UCE,!5#68	 	 	 	 	i 	 	 	F F F F F F F F< < < < <I < < <    i   @    I   * * * * *Y * * *M M M M M) M M Mv  v  v  v  v  v  v  v  v  v s   1 AA