o
    î?À]Y  ã                   @   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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)ÚioÚnodesÚstatemachineÚutils)Ú
SafeString)ÚSystemMessagePropagation)Ú	Directive)Ú
directivesc                 C   s   t  | d¡S )N)ÚleftÚcenterÚright)r
   Úchoice)Úargument© r   úH/usr/lib/python3/dist-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   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   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_option8   s   
þ
zTable.process_header_optionc                 C   sB  t |ƒ|k r#| jjjd|t |ƒ| jf t | j| j¡| jd�}t	|ƒ‚t |ƒ|  kr/dkrMn n| jjjdt |ƒ| jf t | j| j¡| jd�}t	|ƒ‚|D ]O}t |ƒ|k rt| jjjd|t |ƒ| jf t | j| j¡| jd�}t	|ƒ‚t |ƒ|  kr€dkržn qO| jjjdt |ƒ| jf t | j| j¡| jd�}t	|ƒ‚qOd S )NzP%s header row(s) specified but only %s row(s) of data supplied ("%s" directive).©r$   r   zeInsufficient data supplied (%s row(s)); no data remaining for table body, required by "%s" directive.zU%s stub column(s) specified but only %s columns(s) of data supplied ("%s" directive).ziInsufficient data supplied (%s columns(s)); no data remaining for table body, required by "%s" directive.)
Úlenr!   ÚreporterÚerrorr   r   Úliteral_blockÚ
block_textr   r   )r%   r4   Úheader_rowsÚstub_columnsr9   Úrowr   r   r   Úcheck_table_dimensionsC   s^   þÿüþÿüþÿüþÿüúø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_widthb   s   
ÿzTable.set_table_widthc                 C   s   | j  dd¡S )Nr   r   r@   ©r%   r   r   r   r   f   s   zTable.widthsc                 C   s˜   t | jtƒr,t| jƒ|kr'| jjjd| j|f t 	| j
| j
¡| jd�}t|ƒ‚| j}|S |r7d| g| }|S | jjjdt 	| j
| j
¡| jd�}t|ƒ‚)Nz="%s" widths do not match the number of columns in table (%s).r6   éd   z#No table data detected in CSV file.)Ú
isinstancer   Úlistr7   r!   r8   r9   r   r   r:   r;   r   r   )r%   Úmax_colsr9   Ú
col_widthsr   r   r   Úget_column_widthsj   s0   ÿÿýùüÿþzTable.get_column_widthsc                 C   sD   |D ]}|D ]}t |ƒ|k r| dddg fg|t |ƒ  ¡ qqd S )Nr   )r7   r1   )r%   ÚcolumnsÚpartsÚpartr>   r   r   r   Ú"extend_short_rows_with_empty_cells|   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)   r5   r?   rC   Úpropertyr   rJ   rN   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jd| j t | j| j¡| jd�}|gS |  	¡ \}}t 
¡ }| j | j | j|¡ t|ƒdks<t|d tjƒsS| jjjd| j t | j| j¡| jd�}|gS |d }|d  | j dg ¡7  < |  |¡ d| jv rv| j d¡|d< |d }t| jtƒr—d	d
„ |jD ƒ}t|| jƒ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.r6   r   r   zOError parsing content block for the "%s" directive: exactly one table expected.Úclassesr   r   c                 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!   r8   Úwarningr   r   r:   r;   r   r)   ÚElementr   Únested_parseÚcontent_offsetr7   rF   Útabler9   r-   rA   rC   r   rG   ÚchildrenÚzipÚadd_nameÚinsert)r%   rh   r    r(   Únoder9   rB   ÚtgroupÚcolspecsr_   Ú	col_widthr   r   r   Úrun…   sV   ÿÿýÿÿý





zRSTTable.runN)rO   rP   rQ   ru   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dd„ Zd	d
„ Zdd„ Zejdk rTdd„ Zdd„ Zndd„ Zdd„ ZeeƒZeeƒ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   sl   d|v rt  |d ¡| _d|v rd| _d|v rt  |d ¡| _d|v r.d| _t  |d ¡| _tj 	| ¡ d S )Nr}   r~   Fr   r€   )
rv   Úencode_for_csvÚ	delimiterÚskipinitialspaceÚ	quotecharÚdoublequoteÚ
escapecharÚcsvÚDialectÚ__init__)r%   r-   r   r   r   r‹   Ð   s   z!CSVTable.DocutilsDialect.__init__N)rO   rP   rQ   rR   r„   r†   r‡   r…   Ú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.r�   r‚   ú\FTr+   N)rO   rP   rQ   rR   r„   r†   rˆ   r‡   r…   rŒ   r�   r‰   rŽ   r�   r   r   r   r   r0   Ý   s    
r0   c                 C   s   d S ©Nr   rD   r   r   r   Úcheck_requirementsê   ó   zCSVTable.check_requirementsc              
   C   s8  zƒ| j jjjs)d| jv sd| jv r)| jjjd| j t	 
| j| j¡| jd�}|gW S |  ¡  |  ¡ \}}|  ¡ \}}|  ¡ \}}|  ||  | j¡|¡\}}	t|	|ƒ}	| j dd¡}
| j dd¡}|  ||
|¡ | |d |
… ¡ ||
d … }|  |	¡}|  |	||f¡ W nT ty› } z|jd gW  Y d }~S d }~w tjy× } z0t|ƒ}tjdk r´d	|v r´|d
7 }| j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 )Nrz   r{   z9File and URL access deactivated; ignoring "%s" directive.r6   rx   r   ry   ©é   r   z1-character stringz1
with Python 2.x this must be an ASCII character.z)Error with CSV data in "%s" directive:
%s)r   r^   r   r   )'r   ÚdocumentÚsettingsÚfile_insertion_enabledr-   r!   r8   rh   r   r   r:   r;   r   r“   r)   Úget_csv_datar5   r.   r�   ÚmaxrA   r?   r1   rJ   rN   r   Úargsr‰   ÚErrorÚstrÚsysÚversion_infor9   Úbuild_tablerk   r   rC   ro   rp   )r%   rh   r    r(   Úcsv_datar#   r2   r3   r4   rH   r<   r=   Ú
table_bodyrI   ÚdetailÚmessager9   rl   rB   r   r   r   ru   í   s|   

ÿÿýÿ

ÿ€ÿÿý€ø
	ÿ


zCSVTable.runc                 C   s²  | j  d| jjjj¡}| jjjj}| jrCd| j v sd| j v r6| jj	j
d| j t | j| j¡| jd�}t|ƒ‚| j d¡}| j}||fS d| j v rËd| j v re| j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| jjjj |¡ tj|||d	�}| ¡   ¡ }W ||fS  t!yÊ } z| jj	j"d
| jt#|ƒf t | j| j¡| jd�}t|ƒ‚d}~ww d| j v �rAt$j%dkrãddl&m'}	 ddl(m)}
 nddl*m'}	m)}
 | j d }z|	|ƒ ¡ }W n1 |
t!t+t,f�y) } z | jj	j"d| j| j d t#|ƒf t | j| j¡| jd�}t|ƒ‚d}~ww tj-|||| jjjjd�}| ¡   ¡ }||fS | j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|   rz   r{   zF"%s" directive may not both specify an external file and have content.r6   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.r•   )Úurlopen)ÚURLError)r¨   r©   z*Problems with "%s" directive URL "%s":
%s.)r#   r¦   r|   r§   z3The "%s" directive requires content; none supplied.)/r-   rA   r   r—   r˜   Úinput_encodingÚinput_encoding_error_handlerrg   r!   r8   r9   r   r   r:   r;   r   r   r#   ÚosÚpathÚdirnameÚabspathÚcurrent_sourceÚnormpathÚjoinr   Úrelative_pathÚrecord_dependenciesÚaddr   Ú	FileInputÚreadÚ
splitlinesÚIOErrorÚseverer   rŸ   r    Úurllib.requestr¨   Úurllib.errorr©   Úurllib2ÚOSErrorÚ
ValueErrorÚStringInputrh   )r%   r|   r§   r9   r#   r¢   Ú
source_dirÚcsv_filerº   r¨   r©   Úcsv_textr   r   r   rš     s¶   ÿÿÿý<
Å
ÿüÿÿþ(Ùÿü€ú

ÿü€úýûÿÿýzCSVTable.get_csv_datar•   c                 C   ó
   |   d¡S ©Nzutf-8)Údecode©Úsr   r   r   Údecode_from_csvn  ó   
zCSVTable.decode_from_csvc                 C   rÄ   rÅ   )ÚencoderÇ   r   r   r   rƒ   p  rÊ   zCSVTable.encode_for_csvc                 C   ó   | S r’   r   rÇ   r   r   r   rÉ   s  r”   c                 C   rÌ   r’   r   rÇ   r   r   r   rƒ   u  r”   c              	      sŠ   t j‡ fdd„|D ƒ|d�}g }d}|D ],}g }|D ]}	ˆ  |	¡}
dddtj|
 ¡ |d�f}| |¡ q| |¡ t|t|ƒƒ}q||fS )Nc                    s   g | ]	}ˆ   |d  ¡‘qS )r+   )rƒ   )ra   r$   rD   r   r   rc   |  s    ÿz5CSVTable.parse_csv_data_into_rows.<locals>.<listcomp>)Údialectr   )r#   )	r‰   ÚreaderrÉ   r   Ú
StringListr¸   Úappendr›   r7   )r%   r¢   rÍ   r#   Ú
csv_readerr4   rH   r>   Úrow_dataÚcellÚ	cell_textÚ	cell_datar   rD   r   r.   z  s$   ÿþ

ÿ
z!CSVTable.parse_csv_data_into_rowsN)rO   rP   rQ   r
   Únonnegative_intrV   rW   rX   rY   r­   Úurir|   rU   r   Ú$single_char_or_whitespace_or_unicodeÚflagÚsingle_char_or_unicoderZ   r‰   rŠ   r�   r0   r“   ru   rš   rŸ   r    rÉ   rƒ   Ústaticmethodr.   r   r   r   r   rv   ­   s@    ÿí0
O
rv   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
    http://docutils.sf.net/docs/dev/rst/alternatives.html#list-driven-tables
    rw   )rx   ry   r   r   r   r   r   c              
   C   sN  | j s| j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ym }
 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.r6   c                 S   s   g | ]}d d„ |d D ƒ‘qS )c                 S   s   g | ]}|j ‘qS r   )rm   )ra   Úitemr   r   r   rc   «  s    z,ListTable.run.<locals>.<listcomp>.<listcomp>r   r   )ra   Úrow_listr   r   r   rc   «  s    ÿz!ListTable.run.<locals>.<listcomp>r   rx   ry   r   r^   r   )rg   r!   r8   r9   r   r   r:   r;   r   r)   ri   r   rj   rk   Úcheck_list_contentr-   rA   r?   r   rœ   Úbuild_table_from_listrC   ro   rp   )r%   r9   r    r(   rq   Únum_colsrI   Ú
table_datar<   r=   r¤   rB   r   r   r   ru   Ÿ  sB   ýÿ€ÿÿ



zListTable.runc                 C   s>  t |ƒdkst|d tjƒs&| jjjd| j t | j	| j	¡| j
d�}t|ƒ‚|d }d}tt |ƒƒD ]c}|| }t |ƒdksFt|d tjƒsb| jjjd| j|d f t | j	| j	¡| j
d�}t|ƒ‚|r�t |d ƒ|krŽ| jjjd| j|d t |d ƒ|f t | j	| j	¡| j
d�}t|ƒ‚q2t |d ƒ}q2|  |¡}||fS )Nr   r   zUError parsing content block for the "%s" directive: exactly one bullet list expected.r6   z‹Error parsing content block for the "%s" directive: two-level bullet list expected, but row %s does not contain a second-level bullet list.z¥Error 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).)r7   rF   r   Úbullet_listr!   r8   r9   r   r:   r;   r   r   ÚrangerJ   )r%   rq   r9   Ú	list_noderá   Ú
item_indexrÝ   rI   r   r   r   rß   ½  sL   ÿüýÿûüù÷
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|r€t  
¡ }| |	d |… ¡ ||7 }t  ¡ }| |	|d … ¡ ||7 }|S )	Nr   r^   re   rf   )Úcolsrd   r   Ústub)r   rl   r   rr   r7   r_   Ú
attributesr>   ÚentryrÐ   Útheadr1   Útbody)r%   râ   rI   r<   r=   rl   rr   rt   r_   r4   r>   Úrow_noderÓ   rê   rë   rì   r   r   r   rà   â  s>   




zListTable.build_table_from_listN)rO   rP   rQ   rR   r
   rÖ   rW   rX   rY   rU   rV   r   rZ   ru   rß   rà   r   r   r   r   rÜ   Ž  s    ÿù	%rÜ   )rR   Ú__docformat__rŸ   Úos.pathr¬   r‰   Údocutilsr   r   r   r   Údocutils.utils.error_reportingr   Údocutils.utilsr   Údocutils.parsers.rstr	   r
   r   r   r]   rv   rÜ   r   r   r   r   Ú<module>   s    h* b