o
    M=&bV                     @   s   d Z dZddlZddlZddlZddlmZmZm	Z	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.
ZreStructuredText    N)ionodesstatemachineutils)SystemMessagePropagation)	Directive)
directivesc                 C   s   t | dS )N)leftcenterright)r   choice)Zargument r   Jusr/lib/python3.10/site-packages/docutils/parsers/rst/directives/tables.pyalign   s   r   c                   @   sx   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dd Zedd Zdd Zdd ZdS )Tablez#
    Generic table base class.
       T)autoZ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stateZinline_textlinenor   titlestate_machineZget_source_and_linesourceline)selfZ
title_textZ
text_nodesmessagesr   r   r   r   
make_title*   s   
zTable.make_titlec                 C   sX   | j | jd }g }d}d| jv r(| | jd d|  |\}}|| ||fS )Nr   r   header
)r   
get_sourcer   optionsparse_csv_data_into_rowssplitHeaderDialectextend)r   r   
table_headmax_header_colsrowsr   r   r   process_header_option7   s   

zTable.process_header_optionc                 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   r,   header_rowsstub_columnsr1   rowr   r   r   check_table_dimensionsB   sP   zTable.check_table_dimensionsc                 C   s"   d| j v r| j d|d< d S d S )Nr   r%   get)r   
table_noder   r   r   set_table_widthd   s   
zTable.set_table_widthc                 C   s   | j ddS )Nr   r   r8   r   r   r   r   r   h   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/   r0   r1   r   r   r2   r3   r   r   )r   Zn_colsr1   
col_widthsr   r   r   get_column_widthsl   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/   r)   )r   columnspartspartr6   r   r   r   "extend_short_rows_with_empty_cells   s    z(Table.extend_short_rows_with_empty_cellsN)__name__
__module____qualname____doc__Zoptional_argumentsZfinal_argument_whitespacer   class_option	unchangedr    length_or_percentage_or_unitlessvalue_orpositive_int_listoption_specZhas_contentr!   r-   r7   r;   propertyr   rA   rE   r   r   r   r   r      s*    "
r   c                   @   s   e Zd Zdd ZdS )RSTTablec              
   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)Ztagname).0childr   r   r   
<listcomp>   s    
z RSTTable.run.<locals>.<listcomp>colwidthr   colwidths-autocolwidths-given)contentr0   warningr   r   r2   r3   r   r!   Elementr   nested_parsecontent_offsetr/   r>   tabler1   r%   r9   r;   r   r?   rA   r   argschildrenzipadd_nameinsert)r   r\   r   r    noder1   r:   tgroupr@   detailZcolspecsrT   	col_widthr   r   r   run   sV   





zRSTTable.runN)rF   rG   rH   rj   r   r   r   r   rQ      s    rQ   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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-columnsr"   r   r   fileurlencodingr   r   r   delim	keepspacequoteescapec                   @   s6   e Zd ZdZdZdZdZdZdZdZ	e
jZdd ZdS )	zCSVTable.DocutilsDialectz&CSV dialect for `csv_table` directive.,"Tr#   c                 C   sZ   d|v r	|d | _ d|v rd| _d|v r|d | _d|v r%d| _|d | _tj|  d S )Nrr   rs   Frt   ru   )	delimiterskipinitialspace	quotechardoublequote
escapecharcsvDialect__init__)r   r%   r   r   r   r      s   


z!CSVTable.DocutilsDialect.__init__N)rF   rG   rH   rI   rx   rz   r{   ry   strictlineterminatorr}   QUOTE_MINIMALquotingr   r   r   r   r   DocutilsDialect   s    r   c                   @   s2   e Zd ZdZdZdZdZdZdZdZ	dZ
ejZdS )	zCSVTable.HeaderDialectz0CSV dialect to use for the "header" option data.rv   rw   \FTr#   N)rF   rG   rH   rI   rx   rz   r|   r{   ry   r   r   r}   r   r   r   r   r   r   r(      s    
r(   c                 C   s   d S )Nr   r<   r   r   r   check_requirements   s   zCSVTable.check_requirementsc              
   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 )Nro   rp   z9File and URL access deactivated; ignoring "%s" directive.r.   rm   r   rn   z)Error with CSV data in "%s" directive:
%s)r   rR   r   r   )$r   documentsettingsZfile_insertion_enabledr%   r0   r\   r   r   r2   r3   r   r   r!   get_csv_datar-   r&   r   maxr9   r7   r)   rA   rE   r   ra   r}   Errorstrr1   Zbuild_tabler_   r   r;   rd   re   )r   r\   r   r    csv_datar   r*   r+   r,   max_colsr4   r5   Z
table_bodyr@   rh   messager1   r`   r:   r   r   r   rj      sl   








zCSVTable.runc              
   C   s  | j d| jjjj}| jjjj}| jrBd| j v sd| j v r5| jj	d| j
 t| j| j| jd}t|| jd}| j}||fS d| j v rd| j v rc| jj	d| j
 t| j| j| jd}t|tjtj| jjj}tjtj|| j d }td|}ztj|||d	}|  }W n% ty } z| jjd
| j
|f t| j| j| jd}t|d}~ww | jjjj !| ||fS d| j v r+ddl"m#}	 ddl$m%}
 | j d }z|	| }W n- |
tt&fy } z| jjd| j
| j d |f t| j| j| jd}t|d}~ww tj'|||| jjjj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.
        rq   ro   rp   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.N)source_pathrq   error_handlerz&Problems with "%s" directive path:
%s.)urlopen)URLErrorz*Problems with "%s" directive URL "%s":
%s.)r   r   rq   r   z3The "%s" directive requires content; none supplied.))r%   r9   r   r   r   Zinput_encodingZinput_encoding_error_handlerr[   r0   r1   r   r   r2   r3   r   r   r   ospathdirnameabspathZcurrent_sourcenormpathjoinr   Zrelative_pathr   Z	FileInputread
splitlinesOSErrorsevereZrecord_dependenciesaddZurllib.requestr   Zurllib.errorr   
ValueErrorZStringInputr\   )r   rq   r   r1   r   r   
source_dirZcsv_filer   r   r   Zcsv_textr   r   r   r     s   ;


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.   
stacklevelwarningswarnDeprecationWarningsr   r   r   decode_from_csvl     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_csvt  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   g | ]}|d  qS )r#   r   )rU   r   r   r   r   rW   }  s    z5CSVTable.parse_csv_data_into_rows.<locals>.<listcomp>)dialectr   )r   )r}   readerr   Z
StringListr   appendr   r/   )r   r   r   r   Z
csv_readerr,   r   r6   Zrow_datacellZ	cell_datar   r   r   r&   |  s   

z!CSVTable.parse_csv_data_into_rowsN)rF   rG   rH   r   nonnegative_intrK   rL   rM   rN   r   Zurirq   rJ   r   Z$single_char_or_whitespace_or_unicodeflagZsingle_char_or_unicoderO   r}   r~   r   r(   r   rj   r   staticmethodr   r   r&   r   r   r   r   rk      s:    .N

rk   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
    rl   )rm   rn   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   )rb   )rU   itemr   r   r   rW     s    z,ListTable.run.<locals>.<listcomp>.<listcomp>r   r   )rU   Zrow_listr   r   r   rW     s    z!ListTable.run.<locals>.<listcomp>r   rm   rn   r   rR   r   )r[   r0   r1   r   r   r2   r3   r   r!   r]   r   r^   r_   check_list_contentr%   r9   r7   r   ra   build_table_from_listr;   rd   re   )r   r1   r   r    rf   num_colsr@   
table_datar4   r5   rh   r:   r   r   r   rj     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   Zbullet_listr0   r1   r   r2   r3   r   r   rangerA   )r   rf   r1   Z	list_noder   
item_indexr   r@   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   rR   rY   rZ   )rS   rX   r   Zstub)r   r`   r   rg   r/   rT   Z
attributesr6   entryr   theadr)   tbody)r   r   r@   r4   r5   r`   rg   ri   rT   r,   r6   Zrow_noder   r   r   r   r   r   r   r     s>   




zListTable.build_table_from_listN)rF   rG   rH   rI   r   r   rL   rM   rN   rJ   rK   r   rO   rj   r   r   r   r   r   r   r     s    	&r   )rI   Z__docformat__r}   os.pathr   r   docutilsr   r   r   r   Zdocutils.utilsr   Zdocutils.parsers.rstr   r   r   r   rQ   rk   r   r   r   r   r   <module>   s   l+ \