o
    gDY                     @   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   d/home/ubuntu/cloudmapper/venv/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)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rdkrn 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%   n_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< 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.r6   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!   r8   warningr   r   r:   r;   r   r)   Elementr   nested_parsecontent_offsetr7   rF   tabler9   r-   rA   rC   r   rG   rJ   r   argschildrenzipadd_nameinsert)r%   ri   r    r(   noder9   rB   tgrouprI   detailcolspecsr`   	col_widthr   r   r   run   sb   





zRSTTable.runN)rO   rP   rQ   rx   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   )
ry   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 rd	|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 )Nr}   r~   z9File and URL access deactivated; ignoring "%s" directive.r6   r{   r   r|      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   ri   r   r   r:   r;   r   r   r)   get_csv_datar5   r.   r   maxrA   r?   r1   rJ   rN   r   rn   r   Errorstrsysversion_infor9   build_tablerl   r   rC   rq   rr   )r%   ri   r    r(   csv_datar#   r2   r3   r4   max_colsr<   r=   
table_bodyrI   ru   messager9   rm   rB   r   r   r   rx      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 rd| 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rd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   r}   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_handlerrh   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StringInputri   )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_csvp     
zCSVTable.decode_from_csvc                 C   r   r   )encoder   r   r   r   r   r  r   zCSVTable.encode_for_csvc                 C      | S r   r   r   r   r   r   r   u  r   c                 C   r   r   r   r   r   r   r   r   w  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   )rb   r$   rD   r   r   rd   ~  s    z5CSVTable.parse_csv_data_into_rows.<locals>.<listcomp>)dialectr   )r#   )	r   readerr   r   
StringListr   appendr   r7   )r%   r   r   r#   
csv_readerr4   r   r>   row_datacell	cell_text	cell_datar   rD   r   r.   |  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   rx   r   r   r   r   r   staticmethodr.   r   r   r   r   ry      s@    0
O
ry   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
    rz   )r{   r|   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   )ro   )rb   itemr   r   r   rd     s    z,ListTable.run.<locals>.<listcomp>.<listcomp>r   r   )rb   row_listr   r   r   rd     s    z!ListTable.run.<locals>.<listcomp>r   r{   r|   r   r^   r   )rh   r!   r8   r9   r   r   r:   r;   r   r)   rj   r   rk   rl   check_list_contentr-   rA   r?   r   rn   build_table_from_listrC   rq   rr   )r%   r9   r    r(   rs   num_colsrI   
table_datar<   r=   ru   rB   r   r   r   rx     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||rt |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   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).)r7   rF   r   bullet_listr!   r8   r9   r   r:   r;   r   r   rangerJ   )r%   rs   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|rt 
 }||	d |  ||7 }t  }||	|d   ||7 }|S )	Nr   r^   rf   rg   )r_   re   r   stub)r   rm   r   rt   r7   r`   
attributesr>   entryr   theadr1   tbody)r%   r   rI   r<   r=   rm   rt   rw   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   rx   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]   ry   r   r   r   r   r   <module>   s    i+ b