o
    5tiLP                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZmZ d dlZd dlmZmZmZ ddlmZ ddlmZmZmZmZmZ d	d
lmZ d	dlmZ ddlmZm Z  e j!G dd de j"Z#G dd dee Z$G dd de$eZ%dS )    N)Sequence)chain)IOAnyOptionalUnioncast)ColumnDataPropertyDataPropertyLineBreakHandling   )EmptyTableDataError)CellColSeparatorStyleFilterFuncStyleStylerInterface
TextStyler   )
HEADER_ROW)AbstractTableWriter   )IndentationInterfaceTextWriterInterfacec                   @   s   e Zd ZdZdZdZdZdS )RowTypeopeningzheader separatormiddleclosingN)__name__
__module____qualname__OPENINGHEADER_SEPARATORMIDDLECLOSING r$   r$   Z/home/ubuntu/.local/lib/python3.10/site-packages/pytablewriter/writer/text/_text_writer.pyr      s
    r   c                
       s  e Zd ZdZdmddZdeddf fddZdefd	d
Ze	de
fddZejde
ddfddZdmddZdeddfddZdm fddZdmddZdeddf fddZdndeeef dededdfddZdedefdd Zd!eddfd"d#Zd$edefd%d&Zdeddf fd'd(Zdeddfd)d*Zdee fd+d,Zdee fd-d.Z dee fd/d0Z!dee fd1d2Z"d3ed4edee fd5d6Z#d7e$d8e%def fd9d:Z&d;e
d7e$d8e%d<e'def
 fd=d>Z(d?eddfd@dAZ)dod?eddfdCdDZ*dEeddfdFdGZ+dodEeddfdHdIZ,dJe-e. dKe-e. dLe'de'fdMdNZ/dOe
dPe-e$ dQe-e$ dRedef
dSdTZ0dOe
dUe1e ddfdVdWZ2dmdXdYZ3dOe
dUe1e dZe1e% ddfd[d\Z4e5j6fdUe1e d]e5ddfd^d_Z7dmd`daZ8dmdbdcZ9dmdddeZ:dmdfdgZ;dhedefdidjZ<dm fdkdlZ=  Z>S )pTextTableWritera  
    A base class for table writer with text formats.

    .. figure:: ss/table_char.png
       :scale: 60%
       :alt: table_char

       Character attributes that compose a table

    .. py:attribute:: column_delimiter
        :type: str

        A column delimiter of a table.

    .. py:attribute:: char_left_side_row
        :type: str

        A character of a left side of a row.

    .. py:attribute:: char_right_side_row
        :type: str

        A character of a right side of a row.

    .. py:attribute:: char_cross_point
        :type: str

        A character of the crossing point of column delimiter and row
        delimiter.

    .. py:attribute:: char_opening_row
        :type: str

        A character of the first line of a table.

    .. py:attribute:: char_header_row_separator
        :type: str

        A character of a separator line of the header and
        the body of the table.

    .. py:attribute:: char_value_row_separator
        :type: str

        A character of a row separator line of the table.

    .. py:attribute:: char_closing_row
        :type: str

        A character of the last line of a table.

    .. py:attribute:: is_write_header_separator_row
        :type: bool

        Write a header separator line of the table if the value is |True|.

    .. py:attribute:: is_write_value_separator_row
        :type: bool

        Write row separator line(s) of the table if the value is |True|.

    .. py:attribute:: is_write_opening_row
        :type: bool

        Write an opening line of the table if the value is |True|.

    .. py:attribute:: is_write_closing_row
        :type: bool

        Write a closing line of the table if the value is |True|.

    .. py:attribute:: is_write_null_line_after_table
        :type: bool

        Write a blank line of after writing a table if the value is |True|.

    .. py:attribute:: margin
        :type: int

        Margin size for each cells

    returnNc                 C   sH   |  d| _|  | j| _|  | j| _|  | j| _|  | j| _	d S )N )
$_TextTableWriter__make_margin_format*_TextTableWriter__value_cell_margin_formatchar_opening_row)_TextTableWriter__opening_row_cell_formatchar_header_row_separator!_header_row_separator_cell_formatchar_value_row_separator1_TextTableWriter__value_row_separator_cell_formatchar_closing_row)_TextTableWriter__closing_row_cell_formatselfr$   r$   r%   __update_templaten   s   z!TextTableWriter.__update_templatekwargsc                    s   t  jd	i | tj| _| d d| _d| _d| _d| _	d| _
d| _d| _d| _|dd| _tj| jj_|dd| _|   g | _d|v rR| |d  d S d S )
N |-marginr   is_write_null_line_after_tableFthemer$   )super__init__sysstdoutstream
_set_charscolumn_delimiterr+   char_opening_row_cross_pointr-   char_header_row_cross_pointr/   r1   char_closing_row_cross_pointget_marginr   REPLACE_dp_extractorpreprocessorline_break_handlingr;   _init_cross_point_maps_col_separator_style_filters	set_themer4   r6   	__class__r$   r%   r>   y   s&   
zTextTableWriter.__init__c                 C   s   |   S N)dumpsr3   r$   r$   r%   __repr__      zTextTableWriter.__repr__c                 C   s   | j S rS   rH   r3   r$   r$   r%   r:      s   zTextTableWriter.marginvaluec                 C   s0   |dk rt d| j|krd S || _|   d S )Nr   z$margin value must be zero or greater)
ValueErrorrH   _clear_preprocess)r4   rX   r$   r$   r%   r:      s   
c                 C   sv   t j| jt j| jt j| jt j| ji| _	t j| j
t j| jt j| jt j| ji| _t j| jt j| jt j| jt j| ji| _d S rS   )r   r    rD   r!   rE   r"   char_cross_pointr#   rF   "_TextTableWriter__cross_point_mapschar_top_left_cross_point char_header_row_left_cross_pointchar_left_cross_pointchar_bottom_left_cross_point'_TextTableWriter__left_cross_point_mapschar_top_right_cross_point!char_header_row_right_cross_pointchar_right_cross_pointchar_bottom_right_cross_point(_TextTableWriter__right_cross_point_mapsr3   r$   r$   r%   rM      s   
z&TextTableWriter._init_cross_point_mapsstyle_filterc                 C   s   | j d| |   dS )a  Add a style filter function for columns to the writer.

        Args:
            style_filter:
                A function that called for each cell in the table to apply a style
                to table cells.
                The function will be required to implement the following Protocol:

                .. code-block:: python

                    class ColSeparatorStyleFilterFunc(Protocol):
                        def __call__(
                            self, left_cell: Optional[Cell], right_cell: Optional[Cell], **kwargs: Any
                        ) -> Optional[Style]:
                            ...

                If more than one style filter function is added to the writer,
                it will be called from the last one added.
                These style functions should return |None| when not needed to apply styles.
                If all of the style functions returned |None|,
                :py:attr:`~.default_style` will be applied.

                You can pass keyword arguments to style filter functions via
                :py:attr:`~.style_filter_kwargs`. In default, the attribute includes:

                    - ``writer``: the writer instance that the caller of a ``style_filter function``
        r   N)rN   insertrZ   )r4   rg   r$   r$   r%   add_col_separator_style_filter   s   z.TextTableWriter.add_col_separator_style_filterc                    s&   t    | js
dS g | _|   dS )z Remove all of the style filters.N)r=   clear_themerN   rZ   r3   rQ   r$   r%   rj      s
   
zTextTableWriter.clear_themec                 C   s   |    dS )z4
        Write a null line to the |stream|.
        N)_write_liner3   r$   r$   r%   write_null_line   s   zTextTableWriter.write_null_linec                    s>   zt  jdi | W n ty    w | jr|   dS dS )zo
        |write_table|.

        .. note::
            - |None| values are written as an empty string.
        Nr$   )r=   write_tabler   r;   rl   rP   rQ   r$   r%   rm      s   zTextTableWriter.write_tableToutputclose_after_writec                 K   s   z|j  || _W n ty   t|ddd| _Y nw | j}d| _z| jdi | W |r5| j  tj| _|| _dS |rE| j  tj| _|| _w )a  Write data to the output with tabular format.

        During the executing this method,
        :py:attr:`~pytablewriter.writer._table_writer.AbstractTableWriter.enable_ansi_escape`
        attribute will be temporarily set to |False|.

        Args:
            output:
                The value must either an output stream or a path to an output file.

            close_after_write:
                Close the output after write.
                Defaults to |True|.
        wzutf-8)encodingFNr$   )	writerA   AttributeErroropenenable_ansi_escaperm   closer?   r@   )r4   rn   ro   r6   stashr$   r$   r%   dump  s$   



zTextTableWriter.dumpc                 K   s@   | j }zt | _ | jdi | | j  }W || _ |S || _ w )a  Get rendered tabular text from the table data.

        Only available for text format table writers.

        Args:
            **kwargs:
                Optional arguments that the writer takes.

        Returns:
            str: Rendered tabular text.
        Nr$   )rA   ioStringIOrm   getvalue)r4   r6   
old_streamtabular_textr$   r$   r%   rT   #  s   
zTextTableWriter.dumpscc                 C   sx   || _ || _|| _|| _|| _|| _|| _|| _|| _|| _	|| _
|| _|| _|| _|| _|| _|| _|| _|   d S rS   )char_left_side_rowchar_right_side_rowr[   r_   rd   r]   rb   r`   re   r+   rD   r-   rE   r^   rc   r/   r1   rF   rM   )r4   r~   r$   r$   r%   rB   ;  s&   zTextTableWriter._set_charswriterc                 C   s   t |S rS   )r   )r4   r   r$   r$   r%   _create_stylerV  rV   zTextTableWriter._create_stylerc                    s    t    | jr|   d S d S rS   )r=   _write_table_iterr;   rl   rP   rQ   r$   r%   r   Y  s   
z!TextTableWriter._write_table_iterc              	   K   s   |    |   z
|   |   W n	 ty   Y nw d}tt| j| jD ])\}\}}z|r3d}n| j	r:| 
  | |ttt || W q' tyP   Y q'w |   d S )NTF)_preprocess_write_opening_row_write_header,_TextTableWriter__write_header_row_separatorrY   	enumeratezip_table_value_matrix_table_value_dp_matrixis_write_value_separator_row_write_value_row_separator_write_value_rowr   liststr	TypeError_write_closing_row)r4   r6   is_first_value_rowrowvaluesvalue_dp_listr$   r$   r%   _write_table^  s,   zTextTableWriter._write_tablec                 C      |  | j| jS rS   ))_TextTableWriter__get_row_separator_itemsr,   r+   r3   r$   r$   r%   _get_opening_row_itemsy     z&TextTableWriter._get_opening_row_itemsc                 C   r   rS   )r   r.   r-   r3   r$   r$   r%   _get_header_row_separator_items|     z/TextTableWriter._get_header_row_separator_itemsc                 C   r   rS   )r   r0   r/   r3   r$   r$   r%   _get_value_row_separator_items  r   z.TextTableWriter._get_value_row_separator_itemsc                 C   r   rS   )r   r2   r1   r3   r$   r$   r%   _get_closing_row_items  r   z&TextTableWriter._get_closing_row_itemsmargin_formatseparator_charc                    s    fddj D S )Nc                    s    g | ]}  | qS r$   )format_get_padding_len).0col_dpr   r4   r   r$   r%   
<listcomp>  s    z=TextTableWriter.__get_row_separator_items.<locals>.<listcomp>)_column_dp_list)r4   r   r   r$   r   r%   __get_row_separator_items  s   z)TextTableWriter.__get_row_separator_itemsr   value_dpc                    s   | j t ||S rS   )r*   r   r=   _to_header_item)r4   r   r   rQ   r$   r%   r        zTextTableWriter._to_header_itemrow_idxstylec                    s   | j t ||||S rS   )r*   r   r=   _apply_style_to_row_item)r4   r   r   r   r   rQ   r$   r%   r     s   z(TextTableWriter._apply_style_to_row_itemunicode_textc                 C   s   | j | d S rS   )rA   rr   r4   r   r$   r$   r%   _write_raw_string  r   z!TextTableWriter._write_raw_stringr7   c                 C   s   |  |d  d S )N
r   r   r$   r$   r%   _write_raw_line  s   zTextTableWriter._write_raw_linetextc                 C      |  | d S rS   r   r4   r   r$   r$   r%   _write     zTextTableWriter._writec                 C   r   rS   )r   r   r$   r$   r%   rk     r   zTextTableWriter._write_line	left_cell
right_celldefault_stylec                 C   s0   | j D ]}|||fi | j}|r|  S q|S rS   )rN   style_filter_kwargs)r4   r   r   r   rg   r   r$   r$   r%   _fetch_col_separator_style  s   
z*TextTableWriter._fetch_col_separator_styler   left_col_dpright_col_dpcol_delimiterc                 C   sf   d }|rt ||j|| |jd}d }|r"t ||j|| |jd}| j||| jd}| jj||dS )N)r   colrX   r   )r   r   r   )r   )r   column_index_get_col_styler   r   _stylerapply_terminal_style)r4   r   r   r   r   r   r   r   r$   r$   r%   __to_column_delimiter  s,   

z%TextTableWriter.__to_column_delimiterr   c                    s   t |rd S  d jd jg fddttjd D   jd d jg }dgt|t|  }||d d d< t||dd d< 	d
t| d S )Nr   c              	      s.   g | ]}  j| j|d   jqS )r   )%_TextTableWriter__to_column_delimiterr   rC   )r   col_idxr   r4   r$   r%   r     s    z.TextTableWriter._write_row.<locals>.<listcomp>r   r7   r   )typepyis_empty_sequencer   r   r   rangelenr   r   rk   joinr   from_iterable)r4   r   r   col_delimiters	row_itemsr$   r   r%   
_write_row  s4   
zTextTableWriter._write_rowc                 C   s0   | j sd S t| jrtd| t| j d S )Nzheader is empty)is_write_headerr   r   _table_headersrY   r   r   r3   r$   r$   r%   r     s
   zTextTableWriter._write_headerr   c                 C   s   |  || d S rS   )r   )r4   r   r   r   r$   r$   r%   r     s   z TextTableWriter._write_value_rowrow_typec                 C   s   t |rd S | j| }| j| }| j| }|r|n|}|r |n|}t | jr*d}t | jr2d}| ||	| |  d S Nr7   )
r   r   r\   ra   rf   is_null_stringr   r   rk   r   )r4   r   r   cross_pointleft_cross_pointright_cross_pointr$   r$   r%   __write_separator_row  s   



z%TextTableWriter.__write_separator_rowc                 C   "   | j sd S | j|  tjd d S N)r   )is_write_opening_row%_TextTableWriter__write_separator_rowr   r   r    r3   r$   r$   r%   r        z"TextTableWriter._write_opening_rowc                 C   s0   t | j | j grd S | j|  tjd d S r   )anyr   is_write_header_separator_rowr   r   r   r!   r3   r$   r$   r%   __write_header_row_separator  s
   

z,TextTableWriter.__write_header_row_separatorc                 C   s   |  |   dS )z
        Write row separator of the table which matched to the table type
        regardless of the value of the
        :py:attr:`.is_write_value_separator_row`.
        N)r   r   r3   r$   r$   r%   r   %  s   z*TextTableWriter._write_value_row_separatorc                 C   r   r   )is_write_closing_rowr   r   r   r#   r3   r$   r$   r%   r   .  r   z"TextTableWriter._write_closing_rowmargin_charc                 C   s   || j  }|d | S )Nz{:s}rW   )r4   r   
margin_strr$   r$   r%   __make_margin_format4  s   
z$TextTableWriter.__make_margin_formatc                    s   t    |   |   d S rS   )r=   _preprocess_table_property!_TextTableWriter__update_templaterM   r3   rQ   r$   r%   r   9  s   
z*TextTableWriter._preprocess_table_propertyr'   N)Tr7   )?r   r   r   __doc__r   r   r>   r   rU   propertyintr:   setterrM   r   ri   rj   rl   rm   r   r   boolrx   rT   rB   r   r   r   r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   rk   r   r   r   r   r   r   r   r   r   r"   r   r   r   r   r   r)   r   __classcell__r$   r$   rQ   r%   r&      s    
S 

 
$"


!
&	





	r&   c                       s   e Zd ZdZdeddf fddZdeddfdd	Zdd
dZdddZ	deddf fddZ
defddZdeddfddZddeddfddZ  ZS )IndentationTextTableWriterzA base class for table writer with indentation text formats.

    Args:
        indent_level (int): Indentation level. Defaults to ``0``.

    .. py:attribute:: indent_string

        Indentation string for each level.
    r6   r'   Nc                    s6   t  jdi | | |dd |dd| _d S )Nindent_levelr   indent_stringr7   r$   )r=   r>   set_indent_levelrG   r   rP   rQ   r$   r%   r>   K  s   z#IndentationTextTableWriter.__init__r   c                 C   s
   || _ dS )ziSet the indentation level.

        Args:
            indent_level (int): New indentation level.
        N_indent_level)r4   r   r$   r$   r%   r   Q  s   
z+IndentationTextTableWriter.set_indent_levelc                 C   s   |  j d7  _ dS )z Increment the indentation level.r   Nr   r3   r$   r$   r%   inc_indent_levelZ     z+IndentationTextTableWriter.inc_indent_levelc                 C   s   |  j d8  _ dS )z Decrement the indentation level.r   Nr   r3   r$   r$   r%   dec_indent_level_  r   z+IndentationTextTableWriter.dec_indent_levelc                    sr   | dd}|dur| jjd|  | t| zt jdi | W dS  ty8   | jjd Y dS w )aV  
        |write_table|.

        Args:
            indent (Optional[int]):
                Indent level of an output.
                Interpretation of indent level value differ format to format.
                Some writer classes may ignore this value.

        .. note::
            - |None| values are written as an empty string.
        indentNzindent: zno tabular data foundr$   )	pop_loggerloggerdebugr   r   r=   rm   r   )r4   r6   r   rQ   r$   r%   rm   d  s   z&IndentationTextTableWriter.write_tablec                 C   s   | j | j S rS   )r   r   r3   r$   r$   r%   _get_indent_string~  s   z-IndentationTextTableWriter._get_indent_stringr   c                 C   s   |  |  |  d S rS   )r   r  r   r$   r$   r%   r     r   z!IndentationTextTableWriter._writer7   c                 C   s.   t |r| |  |  d S | d d S r   )r   is_not_null_stringr   r  r   r$   r$   r%   rk     s   
z&IndentationTextTableWriter._write_liner   r   )r   r   r   r   r   r>   r   r   r   r   rm   r   r  r   rk   r   r$   r$   rQ   r%   r   @  s    

	
r   )&enumry   r?   collections.abcr   	itertoolsr   typingr   r   r   r   r   r   datapropertyr	   r
   r   errorr   r   r   r   r   r   r   _commonr   _table_writerr   
_interfacer   r   uniqueEnumr   r&   r   r$   r$   r$   r%   <module>   s*        *