o
    iY                     @  s   d Z ddlm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.
    )annotationsreStructuredTextN)urlopen)URLError)nodesstatemachine)	FileInputStringInput)	Directive)
directives)
adapt_path)SystemMessagePropagationc                 C  s   t | dS )N)leftcenterright)r   choice)argument r   l/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/docutils/parsers/rst/directives/tables.pyalign   s   r   c                   @  st   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dZedd Zdd Zd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_columnsr0   rowr   r   r   check_table_dimensions;   sP   zTable.check_table_dimensionsreturnNonec                 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   r:   r(   r   r   r   r   a   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/   r0   r   r   r1   r2   r"   r   )r(   n_colsr0   
col_widthsr   r   r   get_column_widthse   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partr6   r   r   r   "extend_short_rows_with_empty_cellsx   s    z(Table.extend_short_rows_with_empty_cellsNr8   r9   )__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,   r7   r>   propertyr   rE   rJ   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   r1   r2   r"   r,   Elementr    nested_parsecontent_offsetr.   rA   tabler0   r;   r<   r>   r   rB   rE   r   argschildrenzipadd_nameinsert)r(   rf   r#   r+   noder0   r=   tgrouprD   detailcolspecsr]   	col_widthr   r   r   run   sV   





zRSTTable.runN)rL   rM   rN   rO   ru   r   r   r   r   rZ      s    rZ   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	d
Zdd Zdd Z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d
 fdd	Z  ZS )zCSVTable.DocutilsDialectz&CSV dialect for `csv_table` directive.,"T
r8   r9   c                   sX   d|v r	|d | _ d|v rd| _d|v r|d | _d|v r%d| _|d | _t   d S )Nr~   r   Fr   r   )	delimiterskipinitialspace	quotechardoublequote
escapecharsuper__init__)r(   r;   	__class__r   r   r      s   


z!CSVTable.DocutilsDialect.__init__rK   )rL   rM   rN   rO   r   r   r   r   strictlineterminatorcsvQUOTE_MINIMALquotingr   __classcell__r   r   r   r   DocutilsDialect   s    r   c                      sD   e Zd ZdZdZdZdZdZdZdZ	dZ
ejZd 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   r8   r9   c                   s   t jdtdd t   d S )Nz6CSVTable.HeaderDialect will be removed in Docutils 1.0   
stacklevel)warningswarnDeprecationWarningr   r   r?   r   r   r   r      s   zCSVTable.HeaderDialect.__init__rK   )rL   rM   rN   rO   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   HeaderDialect   s    r   r8   r9   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   r   r   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   rz   r   )r$   
get_sourcer"   r;   parse_csv_data_into_rowssplitr   rF   )r(   r&   
table_headmax_header_colsr3   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 )Nr{   r|   z9File and URL access deactivated; ignoring "%s" directive.r-   rx   r   ry   z)Error with CSV data in "%s" directive:
%s)r   r[   r   r   )#r    documentsettingsfile_insertion_enabledr;   r/   rf   r   r   r1   r2   r"   r,   get_csv_datar   r   r   maxr<   r7   rF   rE   rJ   r   rk   r   Errorstrr0   build_tableri   r   r>   rn   ro   )r(   rf   r#   r+   csv_datar&   r   r   r3   max_colsr4   r5   
table_bodyrD   rr   messager0   rj   r=   r   r   r   ru     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.
        r}   r{   r|   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_pathr}   error_handlerz&Problems with "%s" directive path:
%s.Nz*Problems with "%s" directive URL "%s":
%s.)r&   r   r}   r   z3The "%s" directive requires content; none supplied.) r    r   r   r;   r<   input_encodinginput_encoding_error_handlerre   r/   r0   r   r   r1   r2   r"   r   r&   r   current_sourceroot_prefixr   read
splitlinesOSErrorsevererecord_dependenciesaddr   r   
ValueErrorr	   rf   )r(   r   r}   r   r0   r&   r   csv_filer   responsecsv_textr   r   r   r   A  s   
2





zCSVTable.get_csv_datac              	   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_readerr3   r   r6   row_datacell	cell_datar   r   r   r     s   

z!CSVTable.parse_csv_data_into_rowsNrK   )rL   rM   rN   r   nonnegative_intrS   rT   rU   rV   pathurir}   rR   r   $single_char_or_whitespace_or_unicodeflagsingle_char_or_unicoderW   r   Dialectr   r   staticmethodr   r   ru   r   r   r   r   r   r   rv      s6    #-Erv   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
    rw   )rx   ry   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   )rl   )r_   itemr   r   r   ra     s    z,ListTable.run.<locals>.<listcomp>.<listcomp>r   r   )r_   row_listr   r   r   ra     s    z!ListTable.run.<locals>.<listcomp>r   rx   ry   r   r[   r   )re   r/   r0   r   r   r1   r2   r"   r,   rg   r    rh   ri   check_list_contentr;   r<   r7   r   rk   build_table_from_listr>   rn   ro   )r(   r0   r#   r+   rp   	_num_colsrD   
table_datar4   r5   rr   r=   r   r   r   ru     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.   rA   r   bullet_listr/   r0   r   r1   r2   r"   r   rangerE   )r(   rp   r0   	list_nodenum_cols
item_indexr   rD   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   r[   rc   rd   )r\   rb   Tstubr   )r   rj   r   rq   r.   r]   
attributesr6   entryr   theadrF   tbody)r(   r   rD   r4   r5   rj   rq   rt   r]   r3   r6   row_noder   r   r   r   r   r   r   r     s>   




zListTable.build_table_from_listN)rL   rM   rN   rO   r   r   rT   rU   rV   rR   rS   r   rW   ru   r   r   r   r   r   r   r     s    	&r   )rO   
__future__r   __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   rZ   rv   r   r   r   r   r   <module>   s&   a0 h