
    c\                        d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
 ddlmZ ddlZdZ G d de      Z G d	 d
      Z ej$                  d      Z G d d      Z G d d      Z G d de      Z G d d      Z G d de      Zy)zTable management module.    )CodeType)AnyListOptionalTupleUnionDictIterator)StringIONzrestructuredtext enc                      e Zd ZdZ	 	 	 d@dedeee      dee   ddfdZ	defdZ
defd	Zd
edefdZej                   Zd ZdefdZdee   ddfdZdee   ddfdZdAdeddfdZdeddfdZdBdededdfdZdBdededdfdZdededeeeeed f   f   eed f   f   fdZdededd fdZd Zdeded eddfd!Zd"eded eeef   ddfd#Zded$eee    ee   ee   f   ddfd%Z!d"ed$ee   ddfd&Z"	 dAd$eeee ef      ee   f   dee   defd'Z#dAd(ed$ee   deddfd)Z$d(edee   fd*Z%d"eddfd+Z&ded,eee   e'f   ddfd-Z(ded,eee   e'f   ddfd.Z)d,e'deddfd/Z*d(ed,e'deddfd0Z+d(edee   fd1Z,deddfd2Z-de.eef   fd3Z/ e0e/      Z1d4ee.eee2ef   eeef   f   ee2f   defd5Z3d6 Z4d7 Z5dCd8Z6deee      fd9Z7dCd:Z8dDd;Z9dEd<Z:defd=Z;defd>Z<d? Z=y)FTablezTable defines a data table with column and row names.
    inv::

        len(self.data) <= len(self.row_names)
        forall(self.data, lambda x: len(x) <= len(self.col_names))
    Ndefault_value	col_names	row_namesreturnc                     g | _         g | _        g | _        || _        |r| j	                  |       |r| j                  |       y y N)r   r   datar   create_columnscreate_rows)selfr   r   r   s       6/usr/lib/python3/dist-packages/logilab/common/table.py__init__zTable.__init__%   sH      "!	"/	*Y'     c                 8    dt        | j                        dz   z  S )Nzrow%s   lenr   r   s    r   _next_row_namezTable._next_row_name4   s    #dnn-122r   c                 ,    t        | j                        S r   )iterr   r   s    r   __iter__zTable.__iter__7       DIIr   otherc                 h    dt         dt        fd}|y ||      rt        |       t        |      k(  S y)Nvariabler   c                 :    	 t        |        y# t        $ r Y yw xY w)NTF)r"   	TypeError)r'   s    r   is_iterablez!Table.__eq__.<locals>.is_iterable<   s'    X   s    	F)r   boollist)r   r%   r*   s      r   __eq__zTable.__eq__;   s?    	# 	$ 	 = :e,,r   c                     | |k(   S r    )r   r%   s     r   __ne__zTable.__ne__O   s    5=  r   c                 ,    t        | j                        S r   r   r   s    r   __len__zTable.__len__R   s    4>>""r   c                     | j                   j                  |       |D ]>  }| j                  j                  | j                  gt        | j                        z         @ y)z.Appends row_names to the list of existing rowsN)r   extendr   appendr   r   r   )r   r   row_names      r   r   zTable.create_rowsV   sN    i(! 	IHIId001C4GGH	Ir   c                 4    |D ]  }| j                  |        y)z1Appends col_names to the list of existing columnsN)create_column)r   r   col_names      r   r   zTable.create_columns\   s    ! 	)Hx(	)r   r6   c                     |xs | j                         }| j                  j                  |       | j                  j                  | j                  gt        | j                        z         y)z'Creates a rowname to the row_names listN)r    r   r5   r   r   r   r   )r   r6   s     r   
create_rowzTable.create_rowa   sP    4t224h'		$,,-DNN0CCDr   r9   c                     | j                   j                  |       | j                  D ]  }|j                  | j                          y)z'Creates a colname to the col_names listN)r   r5   r   r   )r   r9   rows      r   r8   zTable.create_columng   s9    h'99 	+CJJt))*	+r   col_idmethodc                     	 | j                   j                  |      }| j                  ||       y# t        $ r t	        d|z        w xY w)z=Sorts the table (in-place) according to data stored in col_idzCol (%s) not found in tableN)r   indexsort_by_column_index
ValueErrorKeyError)r   r>   r?   	col_indexs       r   sort_by_column_idzTable.sort_by_column_idn   sN    	E,,V4I%%i8 	E8FCDD	E	   -0 ArE   c           	      x   t        t        | j                  | j                        D cg c]  \  }}||   ||f c}}      }|j	                         dk(  r|j                          g | _        g | _        |D ]<  \  }}}| j                  j                  |       | j                  j                  |       > yc c}}w )zvSorts the table 'in-place' according to data stored in col_index

        method should be in ('asc', 'desc')
        descN)sortedzipr   r   lowerreverser5   )r   rE   r?   r=   r6   	sort_listvals          r   rB   zTable.sort_by_column_indexv   s    
 BEdiiQUQ_Q_B`ahc)nc8,a
	
 <<>V# 	"+ 	,ChIIS!NN!!(+	, bs   B6
colnameothersc                 v   i }|f|z   }|D cg c]  }| j                   j                  |       }}| j                  D ]t  }|}|dd D ]  }	|j                  ||	   i       } |j                  ||d      t	        | j
                  | j                               }
|
j                  t        |             v |S c c}w )z]builds indexes of data
        :returns: nested dictionaries pointing to actual rows
        N)r   r   )r   rA   r   
setdefaultr   r   
append_rowtuple)r   rP   rQ   groupscolnamesr>   col_indexesr=   ptrrE   tables              r   groupbyzTable.groupby   s     :&BJKt~~++F3KK99 	)CC("- 9	nnS^R89NNKO$D$6$6$..QE U3Z(	)  Ls   "B6valuec                 b    | j                  |      }	 ||   S # t        $ r t               cY S w xY wr   )r\   rD   r   )r   rP   r]   groupeds       r   selectzTable.select   s7    ,,w'	 5>! 	7N	s    ..c                     | j                   j                  |      }| j                  d d  D ]&  }||   |k(  s| j                  j                  |       ( y r   )r   rA   r   remove)r   rP   r]   rE   r=   s        r   rb   zTable.remove   sK    NN((1	99Q< 	&C9~&		  %	&r   	row_indexr   c                 (    || j                   |   |<   y)z3sets value of cell 'row_indew', 'col_index' to dataNr   )r   rc   rE   r   s       r   set_cellzTable.set_cell   s    *.		)Y'r   row_idc                    	 | j                   j                  |      }	 | j                  j                  |      }|| j                  |   |<   y# t        $ r t        d|z        w xY w# t        $ r t        d|z        w xY w)zsets value of cell mapped by row_id and col_id to data
        Raises a KeyError if row_id or col_id are not found in the table
        Column (%s) not found in tableRow (%s) not found in tableN)r   rA   r   r   rC   rD   )r   rg   r>   r   rc   rE   s         r   set_cell_by_idszTable.set_cell_by_ids   s    		L,,V4IL NN008	26		)$Y/ L?6JKKL  	E8FCDD	Es   A' -A A$'A?row_datac                 "    || j                   |<   y)zsets the 'row_index' row
        pre::

            type(row_data) == types.ListType
            len(row_data) == len(self.col_names)
        Nre   )r   rc   rl   s      r   set_rowzTable.set_row   s      (		)r   c                     	 | j                   j                  |      }| j                  ||       y# t        $ r t	        d|z        w xY w)zsets the 'row_id' column
        pre::

            type(row_data) == types.ListType
            len(row_data) == len(self.row_names)

        Raises a KeyError if row_id is not found
        rj   N)r   rA   rn   rC   rD   )r   rg   rl   rc   s       r   set_row_by_idzTable.set_row_by_id   sL    	E,,V4ILLH- 	E8FCDD	ErG   c                     |xs | j                         }| j                  j                  |       | j                  j                  |       t	        | j                        dz
  S )zAppends a row to the table
        pre::

            type(row_data) == types.ListType
            len(row_data) == len(self.col_names)
        r   )r    r   r5   r   r   )r   rl   r6   s      r   rU   zTable.append_row   sN     4t224h'		"499~!!r   rA   c                     |xs | j                         }| j                  j                  ||       | j                  j                  ||       y)a.  Appends row_data before 'index' in the table. To make 'insert'
        behave like 'list.insert', inserting in an out of range index will
        insert row_data to the end of the list
        pre::

            type(row_data) == types.ListType
            len(row_data) == len(self.col_names)
        N)r    r   insertr   )r   rA   rl   r6   s       r   
insert_rowzTable.insert_row   s?     4t224eX.		)r   c                 n    | j                   j                  |       | j                  j                  |      S )ztDeletes the 'index' row in the table, and returns it.
        Raises an IndexError if index is out of range
        )r   popr   r   rA   s     r   
delete_rowzTable.delete_row   s)     	5!yy}}U##r   c                     	 | j                   j                  |      }| j                  |       y# t        $ r t	        d|z        w xY w)zbDeletes the 'row_id' row in the table.
        Raises a KeyError if row_id was not found.
        rj   N)r   rA   rx   rC   rD   r   rg   rc   s      r   delete_row_by_idzTable.delete_row_by_id   sJ    	E,,V4IOOI& 	E8FCDD	E	   ,/ Acol_datac                 N    t        |      D ]  \  }}|| j                  |   |<    y)zsets the 'col_index' column
        pre::

            type(col_data) == types.ListType
            len(col_data) == len(self.row_names)
        N)	enumerater   )r   rE   r}   rc   	cell_datas        r   
set_columnzTable.set_column  s0     %.h$7 	8 Iy.7DIIi +	8r   c                     	 | j                   j                  |      }| j                  ||       y# t        $ r t	        d|z        w xY w)zsets the 'col_id' column
        pre::

            type(col_data) == types.ListType
            len(col_data) == len(self.col_names)

        Raises a KeyError if col_id is not found
        ri   N)r   rA   r   rC   rD   )r   r>   r}   rE   s       r   set_column_by_idzTable.set_column_by_id  sL    	H,,V4IOOIx0 	H;vFGG	HrG   c                     | j                   j                  |       t        |      D ]#  \  }}| j                  |   j                  |       % y)zAppends the 'col_index' column
        pre::

            type(col_data) == types.ListType
            len(col_data) == len(self.row_names)
        N)r   r5   r   r   )r   r}   r9   rc   r   s        r   append_columnzTable.append_column   sF     	h'$-h$7 	3 IyIIi ''	2	3r   c                     | j                   j                  ||       t        |      D ]$  \  }}| j                  |   j                  ||       & y)a.  Appends col_data before 'index' in the table. To make 'insert'
        behave like 'list.insert', inserting in an out of range index will
        insert col_data to the end of the list
        pre::

            type(col_data) == types.ListType
            len(col_data) == len(self.row_names)
        N)r   rs   r   r   )r   rA   r}   r9   rc   r   s         r   insert_columnzTable.insert_column+  sJ     	eX.$-h$7 	: IyIIi ''y9	:r   c                     | j                   j                  |       | j                  D cg c]  }|j                  |       c}S c c}w )zwDeletes the 'index' column in the table, and returns it.
        Raises an IndexError if index is out of range
        )r   rv   r   )r   rA   r=   s      r   delete_columnzTable.delete_column8  s6     	5!*.))43444s   Ac                     	 | j                   j                  |      }| j                  |       y# t        $ r t	        d|z        w xY w)zbDeletes the 'col_id' col in the table.
        Raises a KeyError if col_id was not found.
        ri   N)r   rA   r   rC   rD   )r   r>   rE   s      r   delete_column_by_idzTable.delete_column_by_id?  sL    	H,,V4Iy) 	H;vFGG	Hr|   c                 V    t        | j                        t        | j                        fS )z2Returns a tuple which represents the table's shape)r   r   r   r   s    r   	get_shapezTable.get_shapeK  s    4>>"C$777r   indicesc                    d}d}d}t        |t              r|d   }t        |      dkD  r|d   }n|}t        |t              r	 | j                  j                  |      }t        |t              rt        ||dz         }d}nt        d      }d}t        |t              r	 | j                  j                  |      }t        |t              rt        ||dz         }d}nt        d      }d}t               }| j                  |_        |j                  | j                  |          |j                  | j                  |          t        | j                   |         D ]  \  }	}
|j#                  |	|
|           |r!|r|S |j                   D cg c]  }|d   	 c}S |r|j                   d   S |j                   d   d   S # t        $ r t        d|z        w xY w# t        $ r t        d|z        w xY wc c}w )zprovided for convenienceFNr   r   rj   Tri   )
isinstancerV   r   strr   rA   rC   rD   intslicer   r   r   r   r   r   r   rn   )r   r   	multirows	multicolscols_indicerows_indicerowscolstabidxr=   items               r   __getitem__zTable.__getitem__Q  s     		 .2gu%!!*K7|a%aj!K k3'N"nn22;? k3'kAo6DI;DI k3'Q"nn22;? k3'kAo6DI;DI g ..t,-4>>$/0!$))D/2 	(HCKKSY'	( 
,/HH5DQ55xx{"xx{1~%W  N<LMMN  Q?;OPPQ0 6s   F: G G0:GG-c                    	 | j                   j                  |      }	 | j                  j                  |      }| j
                  |   |   S # t        $ r t	        d|z        w xY w# t        $ r t	        d|z        w xY w)z'Returns the element at [row_id][col_id]ri   rj   )r   rA   r   rC   rD   r   )r   rg   r>   rc   rE   s        r   get_cell_by_idszTable.get_cell_by_ids  s    	L,,V4IL NN008	 yy#I..  L?6JKKL  	E8FCDD	Es   A& A A#&A>c                     	 | j                   j                  |      }| j                  |   S # t        $ r t        d|z        w xY w)zReturns the 'row_id' rowrj   )r   rA   rC   rD   r   rz   s      r   get_row_by_idzTable.get_row_by_id  sQ    	E,,V4I yy##  	E8FCDD	Es	   , Ac                     	 | j                   j                  |      }| j	                  ||      S # t        $ r t        d|z        w xY w)zReturns the 'col_id' colri   )r   rA   rC   rD   
get_column)r   r>   distinctrE   s       r   get_column_by_idzTable.get_column_by_id  sS    	H,,V4I y(33  	H;vFGG	Hs	   / Ac                 r    t        t        | j                              D cg c]  }| dd|f    c}S c c}w )z$Returns all the columns in the tableN)ranger   r   rw   s     r   get_columnszTable.get_columns  s,    ,1#dnn2E,FG5QXGGGs   4c                 t    | j                   D cg c]  }||   	 }}|rt        t        |            }|S c c}w )zget a column by index)r   r,   set)r   rE   r   r=   cols        r   r   zTable.get_column  s7    )-3#s9~33s3x.C
 4s   5c                 <    |j                   D ]  }t        |        y)z$Applies the stylesheet to this tableN)instructionseval)r   
stylesheetinstructions      r   apply_stylesheetzTable.apply_stylesheet  s    %22 	K	r   c                     t               }|j                  | j                         |j                  | j                         t        | j                               D ]  \  }}|j                  ||        |S )zZKeeps the self object intact, and returns the transposed (rotated)
        table.
        )r   r   r   r   r   r   r   rn   )r   
transposedrE   columns       r   	transposezTable.transpose  sg     W
t~~.!!$..1!*4+;+;+=!> 	2Ivy&1	2r   c           
         d}| j                   D ]  }t        |      |kD  st        |      } |dz   }g }d|z  g}| j                  D ]  }|j                  |dz           |j                  ddj	                  |      z   dz          t        |d         }t        | j                        D ]  \  }}	g }
| j                   |   }|
j                  |d|t        |      z
  z  z          t        |	      D ]N  \  }}t        | j                  |         dz   }t        |      }|
j                  |d|t        |      z
  z  z          P |j                  ddj	                  |
      z   dz          t        |d         |kD  st        |d         } |j                  dd|z         |j                  d|z         dj	                  |      S )	z[returns a string representing the table in a pretty
        printed 'text' format.
        r       z     |rS   -
)	r   r   r   r5   joinr   r   r   rs   )r   max_row_namer6   	col_startlinescol_names_liner9   max_line_lengthrc   r=   linerE   cellcol_name_lengthr   s                  r   pprintzTable.pprint  s   
  	-H8}|+"8}	- !1$	 	/* 	6H!!(W"45	6S388N33c9:eAh- (		2 	1NIsD~~i0HKK3)c(m*C#DDE $-S> H	4"%dnnY&?"@1"D4yD3/CI*E#FFGH LLsxx~-3459~/"%eBi.	1  	Qo-.S?*+yyr   c                 ,    t        | j                        S r   )reprr   r   s    r   __repr__zTable.__repr__  r$   r   c           	          g }| j                   D ]*  }|j                  |D cg c]  }t        |       c}       , |D cg c]  }dj                  |       }}dj                  |      S c c}w c c}w )N	r   )r   r5   r   r   )r   r   r=   r   r   s        r   as_textzTable.as_text  sg    99 	5CKKs3tT34	5+/0C300yy 40s   A+
 A0)r   NNr   )asc)F)r   TableStyleSheetr   N)r   r   )>__name__
__module____qualname____doc__r   r   r   r   r   r   r    r
   r#   objectr+   r-   __hash__r0   r2   r   r   r;   r8   rF   rB   r   r	   r\   r`   rb   rf   rk   floatrn   rp   rU   rt   rx   r{   r   r   r   r   r   r   r   r   r   propertyshaper   r   r   r   r   r   r   r   r   r   r   r   r/   r   r   r   r      s5    )-#'	(( DI&( C=	(
 
(3 3( F t $ H!# #IT#Y I4 I)S	 )d )
E3 E$ E+c +d +E ES ET E,c ,3 ,4 ,(%(	tCc7l++,d3<.@@	A(c # ' &/# /# /S /T /Lc L3 LeCHo LRV L( (d5k49dSVi6W0X (]a (EC E49 E E  ^b"d5#45tCy@A"MUVY]"	"* *tCy *C *SW *$ $S	 $Es Et E	8C 	85cE9I3J 	8t 	8Hs HeDIu<L6M HRV H	3e 	3s 	3t 	3:3 :% :3 :4 :53 549 5H# H$ H85c? 8 YEC&U5eS#95c?#JKSRWWXC&	C&J/$4HT$s)_ H
	'  ' R#  r   r   c                       e Zd ZdZdeddfdZdededdfdZded	eddfd
Z	dededdfdZ
ded	eddfdZdededdfdZded	eddfdZdedefdZd	edefdZdedefdZd	edefdZdedefdZd	edefdZy)
TableStylezDefines a table's styler[   r   Nc                    || _         t        |j                  D cg c]  }|df c}      | _        d| j                  d<   t        |j                  D cg c]  }|df c}      | _        d| j                  d<   t        |j                  D cg c]  }|df c}      | _        d| j
                  d<   y c c}w c c}w c c}w )Nz1*__row_column__right )_tabledictr   size	alignmentunits)r   r[   r9   s      r   r   zTableStyle.__init__  s    5??Kx8T*KL	 '+		"#5??Sx'2ST+2'( %//JhHb>JK
')

#$ L T
 Ks   B4B9B>r]   r>   c                 "    || j                   |<   y)z.sets the size of the specified col_id to valueNr   r   r]   r>   s      r   set_sizezTableStyle.set_size  s    !		&r   rE   c                 j    |dk(  rd}n| j                   j                  |dz
     }|| j                  |<   y)zAllows to set the size according to the column index rather than
        using the column's id.
        BE CAREFUL : the '0' column is the '__row_column__' one !
        r   r   r   Nr   r   r   r   r]   rE   r>   s       r   set_size_by_indexzTableStyle.set_size_by_index  s6    
 >%F[[**9q=9F!		&r   c                 "    || j                   |<   y)z3sets the alignment of the specified col_id to valueNr   r   s      r   set_alignmentzTableStyle.set_alignment%  s    !&vr   c                 j    |dk(  rd}n| j                   j                  |dz
     }|| j                  |<   y)zAllows to set the alignment according to the column index rather than
        using the column's id.
        BE CAREFUL : the '0' column is the '__row_column__' one !
        r   r   r   Nr   r   r   r   s       r   set_alignment_by_indexz!TableStyle.set_alignment_by_index)  s6    
 >%F[[**9q=9F!&vr   c                 "    || j                   |<   y)z.sets the unit of the specified col_id to valueNr   r   s      r   set_unitzTableStyle.set_unit5  s    "

6r   c                 j    |dk(  rd}n| j                   j                  |dz
     }|| j                  |<   y)a)  Allows to set the unit according to the column index rather than
        using the column's id.
        BE CAREFUL :  the '0' column is the '__row_column__' one !
        (Note that in the 'unit' case, you shouldn't have to set a unit
        for the 1st column (the __row__column__ one))
        r   r   r   Nr   r   r   r   s       r   set_unit_by_indexzTableStyle.set_unit_by_index9  s6     >%F[[**9q=9F"

6r   c                      | j                   |   S )z(Returns the size of the specified col_idr   r   r>   s     r   get_sizezTableStyle.get_sizeG  s    yy  r   c                 h    |dk(  rd}n| j                   j                  |dz
     }| j                  |   S )zAllows to get the size  according to the column index rather than
        using the column's id.
        BE CAREFUL : the '0' column is the '__row_column__' one !
        r   r   r   r   r   rE   r>   s      r   get_size_by_indexzTableStyle.get_size_by_indexK  s7    
 >%F[[**9q=9Fyy  r   c                      | j                   |   S )z-Returns the alignment of the specified col_idr   r   s     r   get_alignmentzTableStyle.get_alignmentW  s    ~~f%%r   c                 h    |dk(  rd}n| j                   j                  |dz
     }| j                  |   S )zAllors to get the alignment according to the column index rather than
        using the column's id.
        BE CAREFUL : the '0' column is the '__row_column__' one !
        r   r   r   r   r   s      r   get_alignment_by_indexz!TableStyle.get_alignment_by_index[  s7    
 >%F[[**9q=9F~~f%%r   c                      | j                   |   S )z(Returns the unit of the specified col_idr   r   s     r   get_unitzTableStyle.get_unitg  s    zz&!!r   c                 h    |dk(  rd}n| j                   j                  |dz
     }| j                  |   S )zAllors to get the unit according to the column index rather than
        using the column's id.
        BE CAREFUL : the '0' column is the '__row_column__' one !
        r   r   r   r   r   s      r   get_unit_by_indexzTableStyle.get_unit_by_indexk  s7    
 >%F[[**9q=9Fzz&!!r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r/   r   r   r   r     s&   !*e * *"c "3 "4 "
"s 
"s 
"t 
"'3 ' ' '
'C 
'C 
'D 
'#c #3 #4 ##s #s #t #!s !s !
!3 
!3 
!&C &C &
& 
& 
&"s "s "
"3 
"3 
"r   r   z([0-9]+)_([0-9]+)c            
           e Zd ZdZddeee      ddfdZdeddfdZde	e
e
f   d	e
d
e
de
ddf
dZde	e
e
f   d	e
d
e
de
ddf
dZde	e
e
f   de
de
de
ddf
dZde	e
e
f   de
de
de
ddf
dZy)r   a  A simple Table stylesheet
    Rules are expressions where cells are defined by the row_index
    and col_index separated by an underscore ('_').
    For example, suppose you want to say that the (2,5) cell must be
    the sum of its two preceding cells in the row, you would create
    the following rule ::

        2_5 = 2_3 + 2_4

    You can also use all the math.* operations you want. For example::

        2_5 = sqrt(2_3**2 + 2_4**2)
    Nrulesr   c                 \    |xs g }g | _         g | _        |D ]  }| j                  |        y r   )r  r   add_rule)r   r  rules      r   r   zTableStyleSheet.__init__  s5     "
,. 	 DMM$	 r   r  c                 0   	 dg}|j                  t        j                  d|             | j                  j                  t	        dj                  |      dd             | j                  j                  |       y# t        $ r t        d|z         Y yw xY w)z#Adds a rule to the stylesheet ruleszfrom math import *zself.data[\1][\2]r   ztable.pyexecz"Bad Stylesheet Rule : %s [skipped]N)	r5   	CELL_PROGsubr   compiler   r  SyntaxErrorprint)r   r  source_codes      r   r  zTableStyleSheet.add_rule  s    	?/0Ky}}-A4HI$$WTYY{-CZQW%XYJJd# 	?6=>	?s   A8A; ;BB	dest_cellrc   	start_colend_colc                     t        ||dz         D cg c]	  }d||fz   }}d|z  dj                  |      z   }| j                  |       yc c}w )a&  Creates and adds a rule to sum over the row at row_index from
        start_col to end_col.
        dest_cell is a tuple of two elements (x,y) of the destination cell
        No check is done for indexes ranges.
        pre::

            start_col >= 0
            end_col > start_col
        r   %d_%d%d_%d=+Nr   r   r  )r   r  rc   r  r  rA   	cell_listr  s           r   add_rowsum_rulezTableStyleSheet.add_rowsum_rule  s[     @EYPWZ[P[?\]eW	511]	])#chhy&99d ^   Ac                     t        ||dz         D cg c]	  }d||fz   }}||z
  dz   }d|z  dz   dj                  |      z   d|z  z   }| j                  |       yc c}w )a  Creates and adds a rule to make the row average (from start_col
        to end_col)
        dest_cell is a tuple of two elements (x,y) of the destination cell
        No check is done for indexes ranges.
        pre::

            start_col >= 0
            end_col > start_col
        r   r  r  (r  )/%fNr  )	r   r  rc   r  r  rA   r  numr  s	            r   add_rowavg_rulezTableStyleSheet.add_rowavg_rule  sx     @EYPWZ[P[?\]eW	511]	]	!A%)#c)CHHY,??&3,Nd ^   ArE   	start_rowend_rowc                     t        ||dz         D cg c]	  }d||fz   }}d|z  dj                  |      z   }| j                  |       yc c}w )a&  Creates and adds a rule to sum over the col at col_index from
        start_row to end_row.
        dest_cell is a tuple of two elements (x,y) of the destination cell
        No check is done for indexes ranges.
        pre::

            start_row >= 0
            end_row > start_row
        r   r  r  r  Nr  )r   r  rE   r"  r#  rA   r  r  s           r   add_colsum_rulezTableStyleSheet.add_colsum_rule  s[     @EYPWZ[P[?\]eWy11]	])#chhy&99d ^r  c                     t        ||dz         D cg c]	  }d||fz   }}||z
  dz   }d|z  dz   dj                  |      z   d|z  z   }| j                  |       yc c}w )a  Creates and adds a rule to make the col average (from start_row
        to end_row)
        dest_cell is a tuple of two elements (x,y) of the destination cell
        No check is done for indexes ranges.
        pre::

            start_row >= 0
            end_row > start_row
        r   r  r  r  r  r  Nr  )	r   r  rE   r"  r#  rA   r  r  r  s	            r   add_colavg_rulezTableStyleSheet.add_colavg_rule  sx     @EYPWZ[P[?\]eWy11]	]	!A%)#c)CHHY,??&3,Nd ^r!  r   )r   r   r   r   r   r   r   r   r  r   r   r  r   r%  r'  r/   r   r   r   r   {  s
    htCy1  T  ?S ?T ?sCx58EHSV	 sCx58EHSV	"sCx58EHSV	 sCx58EHSV	r   r   c            
           e Zd ZdZdeddfdZdeeef   dede	de
eef   fd	Zd
edede	de
eef   fdZdedede	de
eef   fdZde
eef   de	dede
eef   fdZdede	dede
eef   fdZdede	dedefdZy)TableCellRendererzDefines a simple text renderer
propertiesr   Nc                     || _         y)a/  keywords should be properties with an associated boolean as value.
        For example :
            renderer = TableCellRenderer(units = True, alignment = False)
        An unspecified property will have a 'False' value by default.
        Possible properties are :
            alignment, unit
        N)r*  r   r*  s     r   r   zTableCellRenderer.__init__  s     %r   
cell_coordr[   table_stylec                     |\  }}|j                   |   |   }| j                  |||dz         }| j                  |||dz         S )z@Renders the cell at 'cell_coord' in the table, using table_styler   )r   _make_cell_content_render_cell_content)r   r-  r[   r.  rc   rE   
cell_valuefinal_contents           r   render_cellzTableCellRenderer.render_cell  sT      *	9ZZ	*95
//
KUVW((YQR]SSr   r6   c                 ,    |}| j                  ||d      S )z!Renders the cell for 'row_id' rowr   )r1  )r   r6   r[   r.  r2  s        r   render_row_cellz!TableCellRenderer.render_row_cell  s     
(([!DDr   r9   c                 h    |}|j                   j                  |      }| j                  |||dz         S )z!Renders the cell for 'col_id' rowr   )r   rA   r1  )r   r9   r[   r.  r2  rE   s         r   render_col_cellz!TableCellRenderer.render_col_cell  s6     
OO))(3	(([)a-PPr   contentrE   c                     |S )a   Makes the appropriate rendering for this cell content.
        Rendering properties will be searched using the
        *table_style.get_xxx_by_index(col_index)' methods

        **This method should be overridden in the derived renderer classes.**
        r/   )r   r9  r.  rE   s       r   r1  z&TableCellRenderer._render_cell_content  s	     r   cell_contentc                     |}d| j                   v r| j                   d   }nd}|r|dk(  r|S 	 | j                   d   }|r| j                  |||      }|S # t        $ r Y |S w xY w)zVMakes the cell content (adds decoration data, like units for
        example)
        	skip_zeror   r   )r*  	_add_unitrD   )r   r;  r.  rE   r3  replacement_charunits_ons          r   r0  z$TableCellRenderer._make_cell_content  s     *6$//)#{;  2##	w/H $|[) T   		s   $A 	A A c                 F    |j                  |      }t        |      dz   |z   S )z'Adds unit to the cell_content if neededr   )r  r   )r   r;  r.  rE   units        r   r>  zTableCellRenderer._add_unit(  s'    ,,Y7< 3&--r   )r   r   r   r   r   r   r   r   r   r   r   r   r4  r6  r8  r1  r0  r>  r/   r   r   r)  r)    s:   (%S %T %TS/T27TFPT	sCxTEE$)E8BE	sCxEQQ$)Q8BQ	sCxQ	S#X	5?	LO		sCx	.8EH	sCx0.c .
 .s .WZ .r   r)  c                   F    e Zd ZdZdededefdZdeeef   dededefdZ	y)	DocbookRendererz1Defines how to render a cell for a docboook tablerE   r.  r   c                 2    |j                  |      }d||fz  S )z3Computes the colspec element according to the stylez'<colspec colname="c%d" colwidth="%s"/>
)r   )r   rE   r.  r   s       r   define_col_headerz!DocbookRenderer.define_col_header1  s"    ,,Y79Y<MMMr   r;  c                     	 | j                   d   }|j                  |      }|r	d|d|dS 	 y# t        $ r d|z  cY S w xY w)zMakes the appropriate rendering for this cell content.
        Rendering properties will be searched using the
        table_style.get_xxx_by_index(col_index)' methods.
        r   z<entry align='z'>z	</entry>
z<entry>%s</entry>
r   )r*  r   rD   )r   r;  r.  rE   align_onr   s         r   r1  z$DocbookRenderer._render_cell_content6  sY    	8{3H#::9EI;DlSS    	8(<77	8s   */ A A N)
r   r   r   r   r   r   r   rF  r   r1  r/   r   r   rD  rD  .  sP    ;N3 NZ NC N
!#s(O:DQT	r   rD  c            
       `    e Zd ZdZdededee   deddf
dZd	 Z	d
e
ddfdZd ZddeddfdZy)TableWriterzA class to write tablesstreamr[   styler*  r   Nc                 d    || _         |xs t        |      | _        || _        || _        d | _        y r   )_streamr   rL  r   r*  renderer)r   rK  r[   rL  r*  s        r   r   zTableWriter.__init__M  s1     /j/
$37r   c                     || _         y)z!sets the table's associated styleN)rL  )r   rL  s     r   	set_stylezTableWriter.set_styleV  s	    
r   rO  c                     || _         y)zsets the way to render cellN)rO  )r   rO  s     r   set_rendererzTableWriter.set_rendererZ  s	     r   c                 :    | j                   j                  |       y)z0Updates writer's properties (for cell rendering)N)r*  updater,  s     r   update_propertieszTableWriter.update_properties^  s    z*r   titlec                     t        d      )Writes the tablez!write_table must be implemented !)NotImplementedErrorr   rW  s     r   write_tablezTableWriter.write_tableb  s    !"EFFr   r   )r   r   r   r   r   r   r   r   r   rQ  rD  rS  rV  r   r\  r/   r   r   rJ  rJ  J  si    !88',85=c]8RU8	8!_ ! !+G Gd Gr   rJ  c                   2    e Zd ZdZddZddZd	deddfdZy)
DocbookTableWriterzDDefines an implementation of TableWriter to write a table in Docbookr   Nc                 j   | j                   J t        t        | j                  j                        dz         D ]A  }| j
                  j                  | j                   j                  || j                               C | j
                  j                  d       | j
                  j                  d       | j                  j                  D ]L  }| j
                  j                  | j                   j                  || j                  | j                               N | j
                  j                  d       y)zWrites col headersNr   z<thead>
<row>
z<entry></entry>
z</row>
</thead>
)
rO  r   r   r   r   rN  writerF  rL  r8  )r   rE   r9   s      r   _write_headersz!DocbookTableWriter._write_headersj  s    }}((( s4;;#8#89A=> 	WILLt}}>>y$**UV	W 	-../-- 	aHLLt}}<<Xt{{TXT^T^_`	a 	/0r   c           	         | j                   J | j                  j                  d       t        | j                  j
                        D ]  \  }}| j                  j                  d       | j                  j                  |   }| j                  j                  | j                   j                  || j                  | j                               t        |      D ]Q  \  }}| j                  j                  | j                   j                  ||f| j                  | j                               S | j                  j                  d        | j                  j                  d       y)zWrites the table bodyNz<tbody>
z<row>
z</row>
z	</tbody>
)
rO  rN  ra  r   r   r   r   r6  rL  r4  )r   rc   r=   r6   rE   r   s         r   _write_bodyzDocbookTableWriter._write_bodyz  s   }}(((;''(8(89 	+NIsLLy){{,,Y7HLLt}}<<Xt{{TXT^T^_`#,S> 	4""MM--y).DdkkSWS]S]^
 LLz*	+ 	<(r   rW  c                 2   | j                   j                  d|z         | j                   j                  dt        | j                  j                        dz   z         | j                          | j                          | j                   j                  d       y)rY  z<table>
<title>%s></title>
z6<tgroup cols="%d" align="left" colsep="1" rowsep="1">
r   z</tgroup>
</table>
N)rN  ra  r   r   r   rb  rd  r[  s     r   r\  zDocbookTableWriter.write_table  sx    :eDEE4;;(()A-/	
 	23r   )r   Nr]  )r   r   r   r   rb  rd  r   r\  r/   r   r   r_  r_  g  s"    N1 )*
4 
4d 
4r   r_  )r   typesr   typingr   r   r   r   r   r	   r
   _ior   re__docformat__r   r   r   r  r  r   r)  rD  rJ  r_  r/   r   r   <module>rk     s   $   D D D  	%c F c Lr" r"j BJJ*+	b bJK. K.\' 8G G:24 24r   