U
    ZSh]                     @   s   d Z dZddlZddlm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	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)Path)ionodesstatemachineutils)SystemMessagePropagation)	Directive)
directives)urlopen)URLErrorc                 C   s   t | dS )N)leftcenterright)r
   choice)argument r   Y/root/rtd-docs/venv/lib/python3.8/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)autogrid)classnamer   widthwidthsc                 C   s^   | j rN| j d }| j|| j\}}tj|df| }| j| j\|_|_	nd }g }||fS )Nr    )
	argumentsstateZinline_textlinenor   titlestate_machineZget_source_and_linesourceline)self
title_textZ
text_nodesmessagesr!   r   r   r   
make_title,   s    
zTable.make_titlec              	   C   sz   | j | jd }g }d}d| jkrrt  td |  }W 5 Q R X | | jd 	d||\}}|
| ||fS )Nr   r   headerignore
)r"   
get_sourcer    optionswarningscatch_warningssimplefilterHeaderDialectparse_csv_data_into_rowssplitextend)r%   r#   
table_headmax_header_colsZheader_dialectrowsr   r   r   process_header_option9   s    


 
zTable.process_header_optionc                 C   sJ  t ||k rD| jjd|t || jf t| j| j| jd}t|t ||  kr\dkrn n<| jjdt | d| j dt| j| j| jd}t||D ]}t ||k r| jj| dt | d| j d	t| j| j| jd}t|t ||  kr
dkrn q| jjd
t || jf t| j| j| jd}t|qd 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%   r7   header_rowsstub_columnsr<   rowr   r   r   check_table_dimensionsL   s@    zTable.check_table_dimensionsc                 C   s   d| j kr| j d|d< d S )Nr   r-   get)r%   
table_noder   r   r   set_table_widthn   s    
zTable.set_table_widthc                 C   s   | j ddS )Nr   r   rC   r%   r   r   r   r   r   s    zTable.widthsc                 C   s   t | jtrTt| j|krL| jjd| j|f t| j	| j	| j
d}t|| j}n<|rhd| g| }n(| jjdt| j	| j	| j
d}t||S )Nz="%s" widths do not match the number of columns in table (%s).r9   d   z#No table data detected in CSV file.)
isinstancer   listr:   r;   r<   r   r   r=   r>   r    r   )r%   Zn_colsr<   
col_widthsr   r   r   get_column_widthsv   s$    zTable.get_column_widthsc                 C   sD   |D ]:}|D ]0}t ||k r|dddg fg|t |   qqd S )Nr   )r:   r4   )r%   columnspartspartrA   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(   r8   rB   rF   propertyr   rL   rP   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 s2| jjd| j t| j| j| jd}|gS |  \}}t	 }| j
| j | j| t|dksvt|d tjs| jjd| j t| j| j| jd}|gS |d }|d  | jdg 7  < | | d| jkr| jd|d< t| jtrt|d }z| |d	 }W n4 tk
rD }	 z|	jd g W Y S d }	~	X Y nX 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.r9   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   r=   r>   r    r(   Elementr   nested_parsecontent_offsetr:   rI   tabler<   r-   rD   rF   r   rJ   rL   r   argschildrenzipadd_nameinsert)r%   rh   r!   r'   noder<   rE   tgrouprK   detailZcolspecsr_   	col_widthr   r   r   run   sR    

"
zRSTTable.runN)rQ   rR   rS   rT   rv   r   r   r   r   r\      s   r\   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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                       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.,"Tr+   c                    sX   d|kr|d | _ d|kr d| _d|kr2|d | _d|krJd| _|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__)rQ   rR   rS   rT   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)r.   warnPendingDeprecationWarningr   r   rG   r   r   r   r     s
     zCSVTable.HeaderDialect.__init__)rQ   rR   rS   rT   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r1      s   r1   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  z| j jjjsPd| jks"d| jkrP| jjd| j t	| j
| j
| jd}|gW S |  \}}|  \}}|  \}}| || | j|\}}	t|	|}	| jdd}
| jdd}| ||
| ||d |
  ||
d  }| |	}| |	||f W n tk
r0 } z|jd g W Y S d }~X Y nb tjk
r } z@t|}| jjd| j|f t	| j
| j
| jd}|g W Y S d }~X Y nX |||f}| j j|| j|| jd	}|d
  | jdg 7  < d| jkr| jd|d< |  | | !| |r|"d| |g| S )Nr{   r|   z9File and URL access deactivated; ignoring "%s" directive.r9   ry   r   rz   z)Error with CSV data in "%s" directive:
%s)r   r]   r   r   )#r   documentsettingsZfile_insertion_enabledr-   r;   rh   r   r   r=   r>   r    r(   get_csv_datar8   r2   r   maxrD   rB   r4   rL   rP   r   rm   r   Errorstrr<   Zbuild_tablerk   r   rF   rp   rq   )r%   rh   r!   r'   csv_datar#   r5   r6   r7   max_colsr?   r@   Z
table_bodyrK   rt   messager<   rl   rE   r   r   r   rv     sn     
 

 
 

zCSVTable.runc              
   C   st  | j d| jjjj}| jjjj}| jrd| j ks<d| j krj| jj	d| j
 t| j| j| jd}t|| jd}| j}nd| j krzd| j kr| jj	d| j
 t| j| j| jd}t|| j d }t| jjjj}td|| }z tj|||d	}|  }W nV tk
rd } z6| jjd
| j
|f t| j| j| jd}t|W 5 d}~X Y nX | jjjj| nd| j kr>| j d }z t|}	|	 }
W 5 Q R X W nd ttt fk
r } z>| jjd| j
| j d |f t| j| j| jd}t|W 5 d}~X Y nX tj!|
||| jjjjd}|  }n.| jj"d| j
 t| j| j| jd}t|||fS )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.r9   r   zXThe "file" and "url" options may not be simultaneously specified for the "%s" directive.N)source_pathr}   error_handlerz&Problems with "%s" directive path:
%s.z*Problems with "%s" directive URL "%s":
%s.)r#   r   r}   r   z3The "%s" directive requires content; none supplied.)#r-   rD   r   r   r   Zinput_encodingZinput_encoding_error_handlerrg   r;   r<   r   r   r=   r>   r    r   r#   r   Zcurrent_sourceparentr   relative_pathr   Z	FileInputread
splitlinesOSErrorsevereZrecord_dependenciesaddr   r   
ValueErrorZStringInputrh   )r%   r}   r   r<   r#   r   Z_baseZcsv_filer   responseZcsv_textr   r   r   r   F  s     





  
zCSVTable.get_csv_datac                 C   s   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  s
     zCSVTable.decode_from_csvc                 C   s   t jdtdd | S )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  s
     zCSVTable.encode_for_csvc              	   C   s|   t jdd |D |d}g }d}|D ]N}g }|D ](}	dddtj|	 |df}
||
 q0|| t|t|}q$||fS )Nc                 s   s   | ]}|d  V  qdS )r+   Nr   )ra   r$   r   r   r   	<genexpr>  s     z4CSVTable.parse_csv_data_into_rows.<locals>.<genexpr>)dialectr   )r#   )r   readerr   Z
StringListr   appendr   r:   )r%   r   r   r#   Z
csv_readerr7   r   rA   Zrow_datacellZ	cell_datar   r   r   r2     s     
 
z!CSVTable.parse_csv_data_into_rowsN)rQ   rR   rS   r
   nonnegative_intrV   rW   rX   rY   pathurir}   rU   r   Z$single_char_or_whitespace_or_unicodeflagZsingle_char_or_unicoderZ   r   Dialectr   r1   staticmethodr   rv   r   r   r   r2   r   r   r   r   rw      s:   #
-G

rw   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
    rx   )ry   rz   r   r   r   r   r   c              
   C   sR  | j s2| jjd| j t| j| j| jd}|gS |  \}}t	 }| j
| j | j| zN| |\}}dd |d D }| jdd}| jdd}	| |||	 W n2 tk
r }
 z|
jd g W Y S d }
~
X Y nX | ||||	}d| jkr| jd|d< |d	  | jd
g 7  < | | | | |rH|d| |g| S )Nz.The "%s" directive is empty; content required.r9   c                 S   s   g | ]}d d |d D qS )c                 S   s   g | ]
}|j qS r   )rn   )ra   itemr   r   r   rc     s     z,ListTable.run.<locals>.<listcomp>.<listcomp>r   r   )ra   Zrow_listr   r   r   rc     s   z!ListTable.run.<locals>.<listcomp>r   ry   rz   r   r]   r   )rg   r;   r<   r   r   r=   r>   r    r(   ri   r   rj   rk   check_list_contentr-   rD   rB   r   rm   build_table_from_listrF   rp   rq   )r%   r<   r!   r'   rr   num_colsrK   
table_datar?   r@   rt   rE   r   r   r   rv     sB    " 

zListTable.runc                 C   s>  t |dkst|d tjsJ| jjd| j t| j| j| j	d}t
||d }d}tt |D ]}|| }t |dkst|d tjs| jjd| j|d f t| j| j| j	d}t
|qb|rt |d |kr*| jjd| j|d t |d |f t| j| j| j	d}t
|qbt |d }qb| |}||fS )Nr   r   zUError parsing content block for the "%s" directive: exactly one bullet list expected.r9   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:   rI   r   Zbullet_listr;   r<   r   r=   r>   r    r   rangerL   )r%   rr   r<   Z	list_noder   Z
item_indexr   rK   r   r   r   r     sF    


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 k	rx||jd< |rd|jd< |d8 }||7 }qZg }	|D ]8}
t  }|
D ]}t  }||7 }||7 }q|		| q|rt 
 }||	d |  ||7 }t  }||	|d   ||7 }|S )	Nr   r]   re   rf   )r^   rd   r   Zstub)r   rl   r   rs   r:   r_   
attributesrA   entryr   theadr4   tbody)r%   r   rK   r?   r@   rl   rs   ru   r_   r7   rA   Zrow_noder   r   r   r   r   r   r   r     s>    




zListTable.build_table_from_listN)rQ   rR   rS   rT   r
   r   rW   rX   rY   rU   rV   r   rZ   rv   r   r   r   r   r   r   r     s   	&r   )rT   __docformat__r   pathlibr   r.   Zdocutilsr   r   r   r   Zdocutils.utilsr   Zdocutils.parsers.rstr	   r
   urllib.requestr   urllib.errorr   r   r   r\   rw   r   r   r   r   r   <module>   s    t0 n