o
    MEgՆ                     @   s  d dl mZmZmZ d dlmZmZmZmZm	Z	m
Z
mZmZ ddlmZmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z% ddl&m'Z'm(Z( er~ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ eG dd dZ0eG dd dZ1G dd de	Z2G dd deZ3e4dkrvd dl5m*Z* d dl6m7Z7 d dl8m3Z3 e3ddddZ9e9j:d d!d"d#d$ e9j:d%d&d' e9j:d(dd)d* e9;d+d,d- e9;d.d/d0 e9j;d1d2d3d4d#d5 e9;d6d7d8 d9e<d:d;fd<d=Z=e* Z)e7 Z>e=d> e)j?e9d?d@ d#e9_@e=dA e)?e9 dBe9_Ae=dC e)j?e9d?d@ d;e9_AdDe9_@dEdFge9_Be=dG e)j?e9d?d@ d;e9_AdDe9_@dEdFge9_Bde9_Ce=dH e)j?e9d?d@ d;e9_AdDe9_@dEdFge9_Bd#e9_Dd e9_Ce=dI e)j?e9d?d@ d;S d;S )J    )	dataclassfieldreplace)DictTYPE_CHECKINGIterableList
NamedTupleOptionalTupleUnion   )boxerrors)loop_first_last	loop_last)	pick_bool)ratio_distributeratio_reduce)JupyterMixin)Measurement)PaddingPaddingDimensions)is_renderable)Segment)Style	StyleType)TextTextType)ConsoleConsoleOptionsJustifyMethodOverflowMethodRenderableTypeRenderResultc                   @   s   e Zd ZU dZdZded< 	 dZded< 	 dZeed< 	 dZ	eed< 	 dZ
eed< 	 d	Zd
ed< 	 dZded< 	 dZee ed< 	 dZee ed< 	 dZee ed< 	 dZee ed< 	 dZeed< 	 dZeed< 	 eedZed ed< d!ddZeded fddZedefdd ZdS )"ColumnzDefines a column in a table. r#   headerfooterheader_stylefooter_stylestyleleftr!   justifyellipsisr"   overflowNwidth	min_width	max_widthratioFno_wrapr   _index)default_factory_cellsreturnc                 C   s   t | g dS )zReturn a copy of this Column.r7   )r   self r<   d/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/rich/table.pycopyQ   s   zColumn.copyc                 c   s    | j E dH  dS )z2Get all cells in the column, not including header.Nr9   r:   r<   r<   r=   cellsU   s   zColumn.cellsc                 C   s
   | j duS )z!Check if this column is flexible.N)r3   r:   r<   r<   r=   flexibleZ      
zColumn.flexible)r8   r%   )__name__
__module____qualname____doc__r'   __annotations__r(   r)   r   r*   r+   r-   r/   r0   r
   intr1   r2   r3   r4   boolr5   r   listr7   r   r>   propertyr   r?   r@   r<   r<   r<   r=   r%   $   sD   
 
r%   c                   @   s0   e Zd ZU dZdZee ed< 	 dZe	ed< dS )RowzInformation regarding a row.Nr+   Fend_section)
rB   rC   rD   rE   r+   r
   r   rF   rL   rH   r<   r<   r<   r=   rK   `   s   
 rK   c                   @   s$   e Zd ZU dZeed< 	 ded< dS )_CellzA single cell in a table.r+   r#   
renderableN)rB   rC   rD   rE   r   rF   r<   r<   r<   r=   rM   k   s   
 rM   c                7   @   sd  e Zd ZU dZee ed< ee ed< ddddej	ddddddddddd	dd
ddddddddde
eef dee dee dee dee deej dee dededededededededededeee  d ee d!ee d"ee d#ee d$ee d%d&d'd&d(ed)df6d*d+Zeddddd,de
eef dedededed)d fd-d.Zed)efd/d0Zejded)dfd1d0Zed)efd2d3Zed)efd4d5Zd6d7d8ed)efd9d:Zd6d7d;d<d)efd=d>Zed)eeeeef fd?d@Z e jded)d fdAd@Z 	B	BdiddddCdDddddddE
dFdGdHdGd ee d!ee dee dId&dJdKdee dee dLee dMee dNed)dfdOdPZ!dddQdRedG dee dSed)dfdTdUZ"	6	7	;	<	)	VdjdWdXZ#d6d7d;d<d)ee fdYdZZ$ed[ee d\ee dLed)ee fd]d^Z%d6d7d_ed`ed)ee& fdadbZ'd_ed)efdcddZ(d6d7d;d<d`ed)efdedfZ)d6d7d;d<d[ee d)dVfdgdhZ*dS )kTablea
  A console renderable to draw a table.

    Args:
        *headers (Union[Column, str]): Column headers, either as a string, or :class:`~rich.table.Column` instance.
        title (Union[str, Text], optional): The title of the table rendered at the top. Defaults to None.
        caption (Union[str, Text], optional): The table caption rendered below. Defaults to None.
        width (int, optional): The width in characters of the table, or ``None`` to automatically fit. Defaults to None.
        min_width (Optional[int], optional): The minimum width of the table, or ``None`` for no minimum. Defaults to None.
        box (box.Box, optional): One of the constants in box.py used to draw the edges (see :ref:`appendix_box`). Defaults to box.HEAVY_HEAD.
        safe_box (Optional[bool], optional): Disable box characters that don't display on windows legacy terminal with *raster* fonts. Defaults to True.
        padding (PaddingDimensions, optional): Padding for cells (top, right, bottom, left). Defaults to (0, 1).
        collapse_padding (bool, optional): Enable collapsing of padding around cells. Defaults to False.
        pad_edge (bool, optional): Enable padding of edge cells. Defaults to True.
        expand (bool, optional): Expand the table to fit the available space if ``True``, otherwise the table width will be auto-calculated. Defaults to False.
        show_header (bool, optional): Show a header row. Defaults to True.
        show_footer (bool, optional): Show a footer row. Defaults to False.
        show_edge (bool, optional): Draw a box around the outside of the table. Defaults to True.
        show_lines (bool, optional): Draw lines between every row. Defaults to False.
        leading (bool, optional): Number of blank lines between rows (precludes ``show_lines``). Defaults to 0.
        style (Union[str, Style], optional): Default style for the table. Defaults to "none".
        row_styles (List[Union, str], optional): Optional list of row styles, if more that one style is give then the styles will alternate. Defaults to None.
        header_style (Union[str, Style], optional): Style of the header. Defaults to "table.header".
        footer_style (Union[str, Style], optional): Style of the footer. Defaults to "table.footer".
        border_style (Union[str, Style], optional): Style of the border. Defaults to None.
        title_style (Union[str, Style], optional): Style of the title. Defaults to None.
        caption_style (Union[str, Style], optional): Style of the caption. Defaults to None.
        title_justify (str, optional): Justify method for title. Defaults to "center".
        caption_justify (str, optional): Justify method for caption. Defaults to "center".
        highlight (bool, optional): Highlight cell contents (if str). Defaults to False.
    columnsrowsN)r   r   FTr   noneztable.headerztable.footercenter)titlecaptionr0   r1   r   safe_boxpaddingcollapse_paddingpad_edgeexpandshow_headershow_footer	show_edge
show_linesleadingr+   
row_stylesr)   r*   border_styletitle_stylecaption_styletitle_justifycaption_justify	highlightheadersrT   rU   r0   r1   r   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r+   r`   r)   r*   ra   rb   rc   rd   r!   re   rf   r8   c                G   s   g | _ g | _|| _|| _|| _|| _|| _|| _t	|| _
|	| _|
| _|| _|| _|| _|| _|| _|| _|| _|p<d| _|pAd| _|| _|| _|| _|| _|| _|| _t|pYg | _| j j}|D ]}t|t rp| j!|d qbt"| j |_#|| qbd S )Nr&   )r'   )$rP   rQ   rT   rU   r0   r1   r   rV   r   unpack_paddingrY   _expandr[   r\   r]   r^   r_   rX   r+   r)   r*   ra   rb   rc   rd   re   rf   rI   r`   append
isinstancestr
add_columnlenr5   )r;   rT   rU   r0   r1   r   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r+   r`   r)   r*   ra   rb   rc   rd   re   rf   rg   append_columnr'   r<   r<   r=   __init__   sD   



zTable.__init__)rW   rX   rY   rZ   c                G   s   | |d||ddd||dS )a  Get a table with no lines, headers, or footer.

        Args:
            *headers (Union[Column, str]): Column headers, either as a string, or :class:`~rich.table.Column` instance.
            padding (PaddingDimensions, optional): Get padding around cells. Defaults to 0.
            collapse_padding (bool, optional): Enable collapsing of padding around cells. Defaults to True.
            pad_edge (bool, optional): Enable padding around edges of table. Defaults to False.
            expand (bool, optional): Expand the table to fit the available space if ``True``, otherwise the table width will be auto-calculated. Defaults to False.

        Returns:
            Table: A table instance.
        NF)r   rW   rX   r[   r\   r]   rY   rZ   r<   )clsrW   rX   rY   rZ   rg   r<   r<   r=   grid   s   z
Table.gridc                 C   s   | j p| jduS )z-Setting a non-None self.width implies expand.N)rj   r0   r:   r<   r<   r=   rZ      s   zTable.expandc                 C   s
   || _ dS )zSet expand.N)rj   )r;   rZ   r<   r<   r=   rZ      rA   c                 C   s4   d}| j r| jr|d7 }| j r|t| jd 7 }|S )z'Get extra width to add to cell content.r      r   )r   r]   ro   rP   )r;   r0   r<   r<   r=   _extra_width  s   zTable._extra_widthc                 C   s
   t | jS )zGet the current number of rows.)ro   rQ   r:   r<   r<   r=   	row_count  rA   zTable.row_countconsoler   indexc                 C   sR   t  }| jr||| j|t| j  7 }| j| j}|dur'|||7 }|S )zGet the current row style.N)r   nullr`   	get_stylero   rQ   r+   )r;   rw   rx   r+   	row_styler<   r<   r=   get_row_style  s   zTable.get_row_styleoptionsr    c                    s   j | jd ur| jdk rtddS | j}t| | | j  fdd| jD }tdd |D | }| jd u rOtdd |D | n| j}t||}|	| j
}|S )Nr   c                    s   g | ]}  |qS r<   )update_width.0column_measure_columnrw   r2   r}   r<   r=   
<listcomp>-  s    z*Table.__rich_measure__.<locals>.<listcomp>c                 s       | ]}|j V  qd S N)minimumr   measurementr<   r<   r=   	<genexpr>2      z)Table.__rich_measure__.<locals>.<genexpr>c                 s   r   r   maximumr   r<   r<   r=   r   5  r   )r2   r0   r   ru   sum_calculate_column_widthsr~   r   rP   clampr1   )r;   rw   r}   extra_widthmeasurementsminimum_widthmaximum_widthr   r<   r   r=   __rich_measure__  s0   



zTable.__rich_measure__c                 C   s   | j S )zGet cell padding.)ri   r:   r<   r<   r=   rW   =  s   zTable.paddingc                 C   s   t || _| S )zSet cell padding.)r   rh   ri   )r;   rW   r<   r<   r=   rW   B  s   r&   r,   r.   )
r)   r*   r+   r-   r/   r0   r1   r2   r3   r4   r'   r#   r(   r-   r/   r"   r2   r3   r4   c       
         C   sD   t t| j|||p
d|pd|pd||||	|
||d}| j| dS )a  Add a column to the table.

        Args:
            header (RenderableType, optional): Text or renderable for the header.
                Defaults to "".
            footer (RenderableType, optional): Text or renderable for the footer.
                Defaults to "".
            header_style (Union[str, Style], optional): Style for the header, or None for default. Defaults to None.
            footer_style (Union[str, Style], optional): Style for the footer, or None for default. Defaults to None.
            style (Union[str, Style], optional): Style for the column cells, or None for default. Defaults to None.
            justify (JustifyMethod, optional): Alignment for cells. Defaults to "left".
            width (int, optional): Desired width of column in characters, or None to fit to contents. Defaults to None.
            min_width (Optional[int], optional): Minimum width of column, or ``None`` for no minimum. Defaults to None.
            max_width (Optional[int], optional): Maximum width of column, or ``None`` for no maximum. Defaults to None.
            ratio (int, optional): Flexible ratio for the column (requires ``Table.expand`` or ``Table.width``). Defaults to None.
            no_wrap (bool, optional): Set to ``True`` to disable wrapping of this column.
        r&   )r5   r'   r(   r)   r*   r+   r-   r/   r0   r1   r2   r3   r4   N)r%   ro   rP   rk   )r;   r'   r(   r)   r*   r+   r-   r/   r0   r1   r2   r3   r4   r   r<   r<   r=   rn   H  s    "zTable.add_columnr+   rL   renderablesrL   c                G   s   dt ddddfdd}t|}| j}t|t|k r)g |dgt|t|  }t|D ]F\}}|t|krPt |d}	| jD ]	}
||	td	 q?| j|	 n|| }	|du r^||	d	 q-t|rh||	| q-t	
d
t|j d| jt||d dS )a  Add a row of renderables.

        Args:
            *renderables (None or renderable): Each cell in a row must be a renderable object (including str),
                or ``None`` for a blank cell.
            style (StyleType, optional): An optional style to apply to the entire row. Defaults to None.
            end_section (bool, optional): End a section and draw a line. Defaults to False.

        Raises:
            errors.NotRenderableError: If you add something that can't be rendered.
        r   rN   r#   r8   Nc                 S   s   | j | d S r   )r7   rk   )r   rN   r<   r<   r=   add_cell  s   zTable.add_row.<locals>.add_cell)r5   r&   zunable to render z1; a string or other renderable object is requiredr   )r%   rI   rP   ro   	enumeraterQ   r   rk   r   r   NotRenderableErrortyperB   rK   )r;   r+   rL   r   r   cell_renderablesrP   rx   rN   r   _r<   r<   r=   add_row{  s0   

zTable.add_rowr$   c              	   #   s    | j stdV  d S |j}| jd ur| j}| j}|  ||| }t|| }|j|| j	d d	ddt
dtdddd	f fd
d}| jrY|| jt| jd| jdE d H  |  |E d H  | jry|| jt| jd| jdE d H  d S d S )N
)r0   rf   heightrS   textr+   r-   r!   r8   r$   c                    s4   t | tr j| |ddn| } j|j|ddS )NF)r+   rf   r-   )r}   )rl   rm   
render_strrenderupdate)r   r+   r-   render_textrw   render_optionsr<   r=   render_annotation  s   z1Table.__rich_console__.<locals>.render_annotationztable.title)r+   r-   ztable.caption)rS   )rP   r   r2   r0   ru   r   r~   r   r   rf   r   r   rT   r   
pick_firstrb   rd   _renderrU   rc   re   )r;   rw   r}   r2   r   widthstable_widthr   r<   r   r=   __rich_console__  sN   

zTable.__rich_console__c                    s  j }j} fdd|D }dd |D }jj}jredd |D }t|redd t||D }	fdd|D }
|t|	 }t|||
}t	|}t
|D ]\}}|jrd|	| t| ||< qSt|}||kr|dd |D |}t|}||kr|| }t|dgt| ||}t|} fd	dt||D }d
d |D }||k rjsjdur|j| k r݈jdu r|ntj| |}t|| |}dd t||D }|S )zNCalculate the widths of each column, including padding, not including borders.c                    s   g | ]	}  |qS r<   )r   r   rw   r}   r;   r<   r=   r     s    z2Table._calculate_column_widths.<locals>.<listcomp>c                 S      g | ]}|j pd qS r   r   r   _ranger<   r<   r=   r         c                 S   s   g | ]
}|j r|jpd qS r   )r@   r3   )r   colr<   r<   r=   r     s    c                 S   s    g | ]\}}|j rd n|jqS r   )r@   r   )r   r   r   r<   r<   r=   r     s    c                    s&   g | ]}|j r|jpd  |j qS r   )r@   r0   r5   r   )get_padding_widthr<   r=   r     s    c                 S   s   g | ]}|j d u o|j qS r   )r0   r4   r   r<   r<   r=   r     s    r   c                    s$   g | ]\}}  ||qS r<   )r   r~   )r   r0   r   r   r<   r=   r         c                 S   r   r   r   r   r<   r<   r=   r     r   Nc                 S   s   g | ]\}}|| qS r<   r<   )r   _widthpadr<   r<   r=   r     s    )r2   rP   _get_padding_widthru   rZ   anyzipr   r   iterr   r@   next_collapse_widthsr   ro   r1   min)r;   rw   r}   r2   rP   width_rangesr   r   ratiosfixed_widthsflex_minimumflexible_widthflex_widthsiter_flex_widthsrx   r   r   excess_width
_max_width
pad_widthsr<   )rw   r   r}   r;   r=   r     sb   

zTable._calculate_column_widthsr   wrapablec           
         s   t |}|| }t|rd|rd|dkrdtdd t||D  t fddt||D } | } fddt||D }t|rB|sE	 |S t||gt| }	t|||	|}t |}|| }|rd|dks|S )aM  Reduce widths so that the total is under max_width.

        Args:
            widths (List[int]): List of widths.
            wrapable (List[bool]): List of booleans that indicate if a column may shrink.
            max_width (int): Maximum width to reduce to.

        Returns:
            List[int]: A new list of widths.
        r   c                 s   s    | ]	\}}|r|V  qd S r   r<   r   r0   
allow_wrapr<   r<   r=   r   ,  s    
z)Table._collapse_widths.<locals>.<genexpr>c                 3   s(    | ]\}}|r| kr|nd V  qdS )r   Nr<   r   
max_columnr<   r=   r   /  s
    
c                    s$   g | ]\}}| kr|rd ndqS )r   r   r<   r   r   r<   r=   r   4  r   z*Table._collapse_widths.<locals>.<listcomp>)r   r   maxr   r   ro   r   )
rr   r   r   r2   total_widthr   second_max_columncolumn_differencer   
max_reducer<   r   r=   r     s.   
zTable._collapse_widthscolumn_indexr   c              
   #   sR   | j | j| jt}|dk|t| jd ki  dtdtdtttttf f fdd}g }|j	}|j
}| jrS|| jpEd||j }	||	|jf ||jpXd}
|jD ]}||
|f q]| jr||| jpnd||j }|||jf |rt}t|D ]\}}\}}t||||||V  qd	S |D ]
\}}t||V  qd	S )
z3Get all the cells with padding and optional header.r   r   	first_rowlast_rowr8   c                    s     | |f}|r|S \}}}}r%std|| }|s%td|| }s7r+d}r/d}| r3d}|r7d}||||f}| | |f< |S )Nr   )getr   )r   r   cachedtoprightbottomr,   ri   _padding_cacherX   first_columnlast_columnrY   rW   r<   r=   get_paddingP  s*   z%Table._get_cells.<locals>.get_paddingr&   N)rX   rY   rW   r   ro   rP   rH   r   rG   rk   rz   r[   r)   r'   r+   r?   r\   r*   r(   r   r   rM   )r;   rw   r   r   any_paddingr   	raw_cells_appendrz   r)   
cell_stylecellr*   _Paddingfirstlastr+   rN   r<   r   r=   
_get_cellsA  sB   0
zTable._get_cellsc                 C   s2   | j \}}}}| jr|dkrtd|| }|| S )zGet extra width from padding.r   )rW   rX   r   )r;   r   r   	pad_rightpad_leftr<   r<   r=   r     s
   zTable._get_padding_widthc                 C   s  |j }|dk rtddS | |j}|jdur%t|j| |j| |S g }g }|j}|j}	tj}
| ||j|D ]}|
|||j	\}}|| |	| q:t|rUt
|nd|r\t
|n||}||jdu rkdn|j| |j du ryd}|S |j | }|S )z0Get the minimum and maximum width of the column.r   r   N)r2   r   r   r5   r0   with_maximumrk   r   r   rN   r   r   r1   )r;   rw   r}   r   r2   padding_width
min_widths
max_widths
append_min
append_maxget_render_widthr   _min_maxr   r<   r<   r=   r     sB   


zTable._measure_columnc           (   	   #   s:    jpd jpd }fddtjD }tt| }jr6jj|t	j
j
dnd }t }j}	j}
j}j}j}j}t |r |j| |j| |j|f |j| |j| |j|f |j| |j| |j|fg}|r |||V  |V  ng }j}j }tt|D ]b\}\}}}|o|
}|o|}|s|sj||
  nd }dg }|s|rt  n|||
r|d n|t|||	D ]0\}}}|j!||j"|j#|j$d d} ||j }j%|j&||d}t't(||)| qއ fdd	t||D |d d < |r|r:|r: |j*|d
|d|V  |V  ||r@dn|rEdnd \} }!}"|"j+, rS|"n	 |"j+j-|"j }#t.D ])}$|ri| V  t/|D ]\}%}&|&|$ E d H  |%s~|#V  qm|r|!V  |V  qant.D ]}$|D ]
}&|&|$ E d H  q|V  q|r|r|
r |j*|d|d|V  |V  |o|j0}'|r|s|s|'r|s|r|t(|d ks|
r|s|r |j*|d|d| |V  n |j*|d|d|V  |V  q|r|r |1||V  |V  d S d S d S )Nr&   c                 3   s"    | ]\}}  ||V  qd S r   )r   )r   r   r   )rw   r;   r<   r=   r     s
    
z Table._render.<locals>.<genexpr>)safer   )r0   r-   r4   r/   r   r+   c                    s&   g | ]\}} j || d qS )r   )	set_shape)r   r0   _cell)_Segment
max_heightr{   table_styler<   r=   r   
  s    z!Table._render.<locals>.<listcomp>foot)edger   rt   headmidrow)2rz   r+   ra   r   rP   rI   r   r   
substituter   rV   r   liner[   r\   r]   r^   r_   	head_left
head_righthead_vertical	foot_left
foot_rightfoot_verticalmid_left	mid_rightmid_verticalget_topr|   r   rQ   r   ry   r   r-   r4   r/   render_linesrN   r   ro   rk   get_rowr   stripbackground_styleranger   rL   
get_bottom)(r;   rw   r}   r   ra   _column_cells	row_cells_boxnew_linerP   r[   r\   r]   r^   r_   box_segmentsr|   rz   rx   r   r   row_cell
header_row
footer_rowr  r?   r0   r   r   r   r   linesr,   r   _dividerdividerline_no	last_cellrendered_cellrL   r<   )r   rw   r   r{   r;   r   r=   r     s  	









"


zTable._render)r&   r&   )rw   r   r}   r    r8   r$   )+rB   rC   rD   rE   r   r%   rF   rK   r   
HEAVY_HEADr   rm   r
   r   rG   BoxrH   r   r   r   rq   classmethodrs   rJ   rZ   setterru   rv   r|   r   r   r   rW   rn   r   r   r   r   rM   r   r   r   r   r<   r<   r<   r=   rO   t   s  
 
	


A
 	

!	

6
/
0
@&
A
(rO   __main__)r   )ReprHighlighter)rO   zStar Wars MovieszRich example tabler   )rT   rU   re   Releasedbright_cyancyanT)r)   r+   r4   Titlemagentar   z
Box Officegreen)r-   r+   zDec 20, 2019z Star Wars: The Rise of Skywalkerz$952,110,690zMay 25, 2018zSolo: A Star Wars Storyz$393,151,347zDec 15, 2017z!Star Wars Ep. V111: The Last Jediz$1,332,539,889zon blackr   zDec 16, 2016zRogue One: A Star Wars Storyz$1,332,439,889r   r8   Nc                 C   s"   t   t t|  t   d S r   )rw   printrulerf   )r   r<   r<   r=   r'   n  s   r'   zExample TablerS   r   zexpand=True2   zwidth=50FdimrR   zrow_styles=['dim', 'none']z%leading=1, row_styles=['dim', 'none']z+show_lines=True, row_styles=['dim', 'none'])Edataclassesr   r   r   typingr   r   r   r   r	   r
   r   r   r&   r   r   _loopr   r   _pickr   _ratior   r   jupyterr   measurer   rW   r   r   protocolr   segmentr   r+   r   r   r   r   r   rw   r   r    r!   r"   r#   r$   r%   rK   rM   rO   rB   rich.consolerich.highlighterr(  
rich.tabletablern   r   rm   r'   rf   r/  rZ   r0   r`   r_   r^   r<   r<   r<   r=   <module>   s    ( 
;
	     
]



