o
    Ni[                     @   s   d Z dZddlZddlmZ ddlmZ ddl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 dd
lmZ ddlmZ dd ZG dd deZG dd deZG dd deZG dd deZdS )z 
Directives for table elements.
reStructuredText    N)urlopen)URLError)nodesstatemachine)	FileInputStringInput)	Directive)
directives)
adapt_path)SystemMessagePropagationc                 C   s   t | dS )N)leftcenterright)r
   choice)argument r   Z/home/ubuntu/.local/lib/python3.10/site-packages/docutils/parsers/rst/directives/tables.pyalign   s   r   c                   @   sp   e Zd ZdZdZdZejeje	ej
edejdZdZdd Zdd	 Zd
d Zedd Zdd Zdd ZdS )Tablez#
    Generic table base class.
       T)autogrid)classnamer   widthwidthsc                 C   sf   | j r+| j d }| j|| j\}}tj|dg|R  }| j| j\|_|_	||fS d }g }||fS )Nr    )
	argumentsstateinline_textlinenor   titlestate_machineget_source_and_linesourceline)self
title_text
text_nodesmessagesr"   r   r   r   
make_title-   s   
zTable.make_titlec                 C   sH  t ||k r"| jjd|t || jf t| j| j| jd}t|t ||  kr.dkrNn n| jjdt | d| j dt| j| j| jd}t||D ]Q}t ||k rx| jj| dt | d| j d	t| j| j| jd}t|t ||  krdkrn qP| jjd
t || jf t| j| j| jd}t|qPd S )NzP%s header row(s) specified but only %s row(s) of data supplied ("%s" directive).r&   r   zInsufficient data supplied (z9 row(s)); no data remaining for table body, required by "z" directive.z# stub column(s) specified but only z columns(s) of data supplied ("z" directive).ziInsufficient data supplied (%s columns(s)); no data remaining for table body, required by "%s" directive.)	lenreportererrorr   r   literal_block
block_textr!   r   )r'   rowsheader_rowsstub_columnsr/   rowr   r   r   check_table_dimensions:   sP   zTable.check_table_dimensionsc                 C   s"   d| j v r| j d|d< d S d S )Nr   optionsget)r'   
table_noder   r   r   set_table_width\   s   
zTable.set_table_widthc                 C   s   | j ddS )Nr   r   r7   r'   r   r   r   r   `   s   zTable.widthsc                 C   s   t | jtr+t| j|kr&| jjd| j|f t| j	| j	| j
d}t|| j}|S |r6d| g| }|S | jjdt| j	| j	| j
d}t|)Nz="%s" widths do not match the number of columns in table (%s).r,   d   z#No table data detected in CSV file.)
isinstancer   listr-   r.   r/   r   r   r0   r1   r!   r   )r'   n_colsr/   
col_widthsr   r   r   get_column_widthsd   s&   zTable.get_column_widthsc                 C   sD   |D ]}|D ]}t ||k r|dddg fg|t |   qqd S )Nr   )r-   extend)r'   columnspartspartr5   r   r   r   "extend_short_rows_with_empty_cellsw   s    z(Table.extend_short_rows_with_empty_cellsN)__name__
__module____qualname____doc__optional_argumentsfinal_argument_whitespacer
   class_option	unchangedr    length_or_percentage_or_unitlessvalue_orpositive_int_listoption_spechas_contentr+   r6   r;   propertyr   rB   rG   r   r   r   r   r      s(    "
r   c                   @   s   e Zd ZdZdd ZdS )RSTTablez
    Class for the `"table" directive`__ for formal tables using rST syntax.

    __ https://docutils.sourceforge.io/docs/ref/rst/directives.html
    c              
   C   s  | j s| jjd| j t| j| j| jd}|gS |  \}}t	 }| j
| j | j| t|dks;t|d tjsQ| jjd| j t| j| j| jd}|gS |d }|d  | jdg 7  < | | d| jv rt| jd|d< t| jtr|d }z	| |d	 }W n ty }	 z|	jd gW  Y d }	~	S d }	~	ww d
d |jD }
t|
|D ]\}}||d< q| jdkr|d  dg7  < n| jr|d  dg7  < | | |r|d| |g| S )Nz:Content block expected for the "%s" directive; none found.r,   r   r   zOError parsing content block for the "%s" directive: exactly one table expected.classesr   r   colsc                 S   s   g | ]	}|j d kr|qS )colspec)tagname).0childr   r   r   
<listcomp>   s    
z RSTTable.run.<locals>.<listcomp>colwidthr   colwidths-autocolwidths-given)contentr.   warningr   r   r0   r1   r!   r+   Elementr   nested_parsecontent_offsetr-   r>   tabler/   r8   r9   r;   r   r?   rB   r   argschildrenzipadd_nameinsert)r'   rb   r"   r*   noder/   r:   tgrouprA   detailcolspecsrY   	col_widthr   r   r   run   sV   





zRSTTable.runN)rH   rI   rJ   rK   rq   r   r   r   r   rV   ~   s    rV   c                   @   s   e Zd Zejejejejedejej	ej
ejejejeejejejejdZG dd dejZG dd dejZedd Zd	d
 Zdd Zdd Zedd Zedd Zdd ZdS )CSVTabler   )header-rowsstub-columnsheaderr   r   fileurlencodingr   r   r   delim	keepspacequoteescapec                       s>   e Zd ZdZdZdZdZdZdZdZ	e
jZ fddZ  ZS )zCSVTable.DocutilsDialectz&CSV dialect for `csv_table` directive.,"T
c                    sX   d|v r	|d | _ d|v rd| _d|v r|d | _d|v r%d| _|d | _t   d S )Nrz   r{   Fr|   r}   )	delimiterskipinitialspace	quotechardoublequote
escapecharsuper__init__)r'   r8   	__class__r   r   r      s   


z!CSVTable.DocutilsDialect.__init__)rH   rI   rJ   rK   r   r   r   r   strictlineterminatorcsvQUOTE_MINIMALquotingr   __classcell__r   r   r   r   DocutilsDialect   s    r   c                       sB   e Zd ZdZdZdZdZdZdZdZ	dZ
ejZ fdd	Z  ZS )
zCSVTable.HeaderDialectzw
        CSV dialect used for the "header" option data.

        Deprecated. Will be removed in Docutils 0.22.
        r~   r   \FTr   c                    s   t jdtdd t   d S )Nz8CSVTable.HeaderDialect will be removed in Docutils 0.22.   
stacklevel)warningswarnPendingDeprecationWarningr   r   r<   r   r   r   r      s   zCSVTable.HeaderDialect.__init__)rH   rI   rJ   rK   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   HeaderDialect   s    r   c                   C   s   t jdtdd d S )NzaCSVTable.check_requirements() is not required with Python 3 and will be removed in Docutils 0.22.r   r   r   r   DeprecationWarningr   r   r   r   check_requirements   s   
zCSVTable.check_requirementsc                 C   s\   | j | jd }g }d}d| jv r*| | jd d| | j|\}}|| ||fS )Nr   r   rv   r   )r#   
get_sourcer!   r8   parse_csv_data_into_rowssplitr   rC   )r'   r%   
table_headmax_header_colsr2   r   r   r   process_header_option  s   


zCSVTable.process_header_optionc              
   C   s  z~| j jjjs(d| jv sd| jv r(| jjd| j t	| j
| j
| jd}|gW S |  \}}|  \}}|  \}}| || | j|\}}	t|	|}	| jdd}
| jdd}| ||
| ||d |
  ||
d  }| |	}| |	||f W nF ty } z|jd gW  Y d }~S d }~w tjy } z"t|}| jjd| j|f t	| j
| j
| jd}|gW  Y d }~S d }~ww |||f}| j j|| j|| jd	}|d
  | jdg 7  < d| jv r| jd|d< |  | | !| |r|"d| |g| S )Nrw   rx   z9File and URL access deactivated; ignoring "%s" directive.r,   rt   r   ru   z)Error with CSV data in "%s" directive:
%s)r   rW   r   r   )#r   documentsettingsfile_insertion_enabledr8   r.   rb   r   r   r0   r1   r!   r+   get_csv_datar   r   r   maxr9   r6   rC   rB   rG   r   rg   r   Errorstrr/   build_tablere   r   r;   rj   rk   )r'   rb   r"   r*   csv_datar%   r   r   r2   max_colsr3   r4   
table_bodyrA   rn   messager/   rf   r:   r   r   r   rq     sj   








zCSVTable.runc              
   C   sR  | j jj}| jd|j}|j}| jrAd| jv sd| jv r4| jj	d| j
 t| j| j| jd}t|| jd}| j}||fS d| jv rd| jv rb| jj	d| j
 t| j| j| jd}t|t| jd | j jj|j}zt|||d}|  }W n% ty } z| jjd	| j
|f t| j| j| jd}t|d
}~ww |j| ||fS d| jv r| jd }zt|}	|	 }
W d
   n1 sw   Y  W n, tttfy } z| jjd| j
| jd |f t| j| j| jd}t|d
}~ww t|
|||d}|  }||fS | jjd| j
 t| j| j| jd}t|)zr
        Get CSV data from the directive content, from an external
        file, or from a URL reference.
        ry   rw   rx   zF"%s" directive may not both specify an external file and have content.r,   r   zXThe "file" and "url" options may not be simultaneously specified for the "%s" directive.)source_pathry   error_handlerz&Problems with "%s" directive path:
%s.Nz*Problems with "%s" directive URL "%s":
%s.)r%   r   ry   r   z3The "%s" directive requires content; none supplied.) r   r   r   r8   r9   input_encodinginput_encoding_error_handlerra   r.   r/   r   r   r0   r1   r!   r   r%   r   current_sourceroot_prefixr   read
splitlinesOSErrorsevererecord_dependenciesaddr   r   
ValueErrorr   rb   )r'   r   ry   r   r/   r%   r   csv_filer   responsecsv_textr   r   r   r   @  s   
2





zCSVTable.get_csv_datac                 C      t jdtdd | S )NzgCSVTable.decode_from_csv() is not required with Python 3 and will be removed in Docutils 0.21 or later.r   r   r   sr   r   r   decode_from_csv     zCSVTable.decode_from_csvc                 C   r   )NzgCSVTable.encode_from_csv() is not required with Python 3 and will be removed in Docutils 0.21 or later.r   r   r   r   r   r   r   encode_for_csv  r   zCSVTable.encode_for_csvc              	   C   s|   t jdd |D |d}g }d}|D ]'}g }|D ]}	dddtj|	 |df}
||
 q|| t|t|}q||fS )Nc                 s   s    | ]}|d  V  qdS )r   Nr   )r[   r&   r   r   r   	<genexpr>  s    z4CSVTable.parse_csv_data_into_rows.<locals>.<genexpr>)dialectr   )r%   )r   readerr   
StringListr   appendr   r-   )r'   r   r   r%   
csv_readerr2   r   r5   row_datacell	cell_datar   r   r   r     s   

z!CSVTable.parse_csv_data_into_rowsN)rH   rI   rJ   r
   nonnegative_intrO   rP   rQ   rR   pathuriry   rN   r   $single_char_or_whitespace_or_unicodeflagsingle_char_or_unicoderS   r   Dialectr   r   staticmethodr   r   rq   r   r   r   r   r   r   r   r   rr      s>    #
-E

rr   c                   @   sP   e Zd ZdZejejejedejej	ej
edZdd Zdd Zdd	 Zd
S )	ListTablez
    Implement tables whose data is encoded as a uniform two-level bullet list.
    For further ideas, see
    https://docutils.sourceforge.io/docs/dev/rst/alternatives.html#list-driven-tables
    rs   )rt   ru   r   r   r   r   r   c              
   C   sL  | j s| jjd| j t| j| j| jd}|gS |  \}}t	 }| j
| j | j| z'| |\}}dd |d D }| jdd}| jdd}	| |||	 W n tyl }
 z|
jd gW  Y d }
~
S d }
~
ww | ||||	}d| jv r| jd|d< |d	  | jd
g 7  < | | | | |r|d| |g| S )Nz.The "%s" directive is empty; content required.r,   c                 S   s   g | ]}d d |d D qS )c                 S   s   g | ]}|j qS r   )rh   )r[   itemr   r   r   r]     s    z,ListTable.run.<locals>.<listcomp>.<listcomp>r   r   )r[   row_listr   r   r   r]     s    z!ListTable.run.<locals>.<listcomp>r   rt   ru   r   rW   r   )ra   r.   r/   r   r   r0   r1   r!   r+   rc   r   rd   re   check_list_contentr8   r9   r6   r   rg   build_table_from_listr;   rj   rk   )r'   r/   r"   r*   rl   num_colsrA   
table_datar3   r4   rn   r:   r   r   r   rq     sD   



zListTable.runc                 C   s8  t |dkst|d tjs%| jjd| j t| j| j| j	d}t
||d }d}tt |D ]a}|| }t |dksEt|d tjs`| jjd| j|d f t| j| j| j	d}t
||rt |d |kr| jjd| j|d t |d |f t| j| j| j	d}t
|q1t |d }q1| |}||fS )Nr   r   zUError parsing content block for the "%s" directive: exactly one bullet list expected.r,   zError parsing content block for the "%s" directive: two-level bullet list expected, but row %s does not contain a second-level bullet list.zError parsing content block for the "%s" directive: uniform two-level bullet list expected, but row %s does not contain the same number of items as row 1 (%s vs %s).)r-   r>   r   bullet_listr.   r/   r   r0   r1   r!   r   rangerB   )r'   rl   r/   	list_noder   
item_indexr   rA   r   r   r   r     sH   
zListTable.check_list_contentc                 C   s&  t  }| jdkr|d  dg7  < n| jr|d  dg7  < t jt|d}||7 }|D ]}t  }|d ur<||jd< |rGd|jd< |d8 }||7 }q-g }	|D ]}
t  }|
D ]}t  }||7 }||7 }qX|		| qP|rt 
 }||	d |  ||7 }t  }||	|d   ||7 }|S )	Nr   rW   r_   r`   )rX   r^   r   stub)r   rf   r   rm   r-   rY   
attributesr5   entryr   theadrC   tbody)r'   r   rA   r3   r4   rf   rm   rp   rY   r2   r5   row_noder   r   r   r   r   r   r   r     s>   




zListTable.build_table_from_listN)rH   rI   rJ   rK   r
   r   rP   rQ   rR   rN   rO   r   rS   rq   r   r   r   r   r   r   r     s    	&r   )rK   __docformat__r   urllib.requestr   urllib.errorr   r   docutilsr   r   docutils.ior   r   docutils.parsers.rstr	   r
   $docutils.parsers.rst.directives.miscr   docutils.utilsr   r   r   rV   rr   r   r   r   r   r   <module>   s$   a0 x