3 y\XW@sdZdZddlZddlZddlZddlmZmZm Z m Z ddl m Z ddl mZddlmZddlmZd d ZGd d d eZGd ddeZGdddeZGdddeZdS)z Directives for table elements. ZreStructuredTextN)ionodes statemachineutils) SafeString)SystemMessagePropagation) Directive) directivescCs tj|dS)Nleftcenterright)r r r )r choice)argumentrL/tmp/pip-install-19gh560x/docutils/docutils/parsers/rst/directives/tables.pyalignsrc@sleZdZdZdZdZejeje ej dej dZ dZ ddZd d Zd d Zed dZddZddZdS)Tablez# Generic table base class. Tautogrid)classnamerwidthscCs^|jrN|jd}|jj||j\}}tj|df|}|jj|j\|_|_ nd}g}||fS)Nr) argumentsstateZ inline_textlinenortitle state_machineZget_source_and_linesourceline)selfZ title_textZ text_nodesmessagesrrrr make_title*s  zTable.make_titlecCsX|jj|jd}g}d}d|jkrP|j|jdjd|j|\}}|j|||fS)Nrrheader )r get_sourceroptionsparse_csv_data_into_rowssplit HeaderDialectextend)r!r table_headmax_header_colsrowsrrrprocess_header_option7s   zTable.process_header_optioncCsHt||krF|jjjd|t||jftj|j|j|jd}t |t||ko\dknr|jjjdt||jftj|j|j|jd}t |x|D]}t||kr|jjjd|t||jftj|j|j|jd}t |t||kodknr|jjjdt||jftj|j|j|jd}t |qWdS)NzP%s header row(s) specified but only %s row(s) of data supplied ("%s" directive).)r rzeInsufficient 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.) lenrreportererrorrr literal_block block_textrr)r!r. header_rows stub_columnsr2rowrrrcheck_table_dimensionsBs2   zTable.check_table_dimensionscCs|jjddS)Nrr)r'get)r!rrrrasz Table.widthscCst|jtkrXt|j|krP|jjjd|j|ftj |j |j |j d}t ||j}n>|rld|g|}n*|jjjdtj |j |j |j d}t ||S)Nz="%s" widths do not match the number of columns in table (%s).)r dz#No table data detected in CSV file.) typerlistr0rr1r2rrr3r4rr)r!max_colsr2 col_widthsrrrget_column_widthseszTable.get_column_widthscCsLxF|D]>}x8|D]0}t||kr|jdddgfg|t|qWqWdS)Nr)r0r+)r!columnspartspartr7rrr"extend_short_rows_with_empty_cellsws   z(Table.extend_short_rows_with_empty_cellsN)rr)__name__ __module__ __qualname____doc__Zoptional_argumentsZfinal_argument_whitespacer class_option unchangedrvalue_orpositive_int_list option_specZ has_contentr#r/r8propertyrr?rCrrrrrs    rc@seZdZddZdS)RSTTablec Cs|js4|jjjd|jtj|j|j|jd}|gS|j \}}tj }|j j |j|j |t|dkszt|dtj r|jjjd|jtj|j|j|jd}|gS|d}|d|jjdg7<d|jkr|jjd|d<|d}t|jtkr0d d |jD}x"t||jD]\} } | | d <qW|jd krP|dd g7<n|jrj|ddg7<|j||r|jd||g|S)Nz:Content block expected for the "%s" directive; none found.)r rrzOError parsing content block for the "%s" directive: exactly one table expected.classesrrcSsg|]}|jdkr|qS)colspec)tagname).0childrrr sz RSTTable.run..colwidthrzcolwidths-autozcolwidths-given)contentrr1warningrrr3r4rr#Elementr nested_parsecontent_offsetr0 isinstancetabler2r'r9r;rr<childrenzipadd_nameinsert) r!rWrr"noder2 table_nodetgroupZcolspecsrP col_widthrrrruns@       z RSTTable.runN)rDrErFrerrrrrN~srNc@seZdZejejejejdejejej ej ej eje ej ejejejdZGdddejZGdddejZddZd d Zd d ZejdkrddZddZnddZddZeeZeeZddZdS)CSVTabler)z header-rowsz stub-columnsr$rfileurlencodingrrrdelim keepspacequoteescapec@s6eZdZdZdZdZdZdZdZdZ e j Z ddZ dS) zCSVTable.DocutilsDialectz&CSV dialect for `csv_table` directive.,"Tr%cCsld|krtj|d|_d|kr&d|_d|kr>tj|d|_d|kr\d|_tj|d|_tjj |dS)NrjrkFrlrm) rfencode_for_csv delimiterskipinitialspace quotechar doublequote escapecharcsvDialect__init__)r!r'rrrrxsz!CSVTable.DocutilsDialect.__init__N)rDrErFrGrqrsrtrrstrictlineterminatorrv QUOTE_MINIMALquotingrxrrrrDocutilsDialectsr}c@s2eZdZdZdZdZdZdZdZdZ dZ e j Z dS) zCSVTable.HeaderDialectz0CSV dialect to use for the "header" option data.rnro\FTr%N)rDrErFrGrqrsrurtrrryrzrvr{r|rrrrr*sr*cCsdS)Nr)r!rrrcheck_requirementsszCSVTable.check_requirementscCs(y|jjjj rTd|jks&d|jkrT|jjjd|jt j |j |j |j d}|gS|j |j\}}|j\}}|j\}}|j||j|j|\}} t| |} |jjdd} |jjdd} |j|| | |j|d| || d} |j| } |j| || fWntk r2}z |jdgSd}~Xnxtjk r}zXt|}tjdkrld |krl|d 7}|jjj d |j|ft j |j |j |j d}|gSd}~XnX| || f}|jj!||j"| |j#d }|d |jjdg7<d|jkr|jjd|d<|j$||r|j%d||g|S)Nrgrhz9File and URL access deactivated; ignoring "%s" directive.)r z header-rowsrz stub-columnsz1-character stringz1 with Python 2.x this must be an ASCII character.z)Error with CSV data in "%s" directive: %s)rrOrr)r)&rdocumentsettingsZfile_insertion_enabledr'rr1rWrrr3r4rrr# get_csv_datar/r(r}maxr9r8r+r?rCrargsrvErrorstrsys version_infor2Z build_tablerZrr_r`)r!rWrr"csv_datarr,r-r.r=r5r6Z table_bodyr>detailmessager2r\rbrrrresZ               z CSVTable.runc Cs|jjd|jjjj}|jjjj}|jrd|jks|jj j"d |j t#|ft j |j|j|jd}t|WYdd}~XnXn d|jkrzddl$} ddl%} ddl&} |jd}y| j'j(|j} Wnp| j j)t!t*t+fk rP}zF|jj j"d |j |jdt#|ft j |j|j|jd}t|WYdd}~XnXtj,| |||jjjjd }|jj }n0|jj j-d |j t j |j|j|jd}t|||fS)zr Get CSV data from the directive content, from an external file, or from a URL reference. rirgrhzF"%s" directive may not both specify an external file and have content.)r rzXThe "file" and "url" options may not be simultaneously specified for the "%s" directive.N) source_pathri error_handlerz&Problems with "%s" directive path: %s.z*Problems with "%s" directive URL "%s": %s.)rrrirz3The "%s" directive requires content; none supplied.).r'r9rrrZinput_encodingZinput_encoding_error_handlerrVrr1r2rrr3r4rrrospathdirnameabspathZcurrent_sourcenormpathjoinrZ relative_pathZrecord_dependenciesaddrZ FileInputread splitlinesIOErrorsevererurllib.request urllib.error urllib.parserequesturlopenURLErrorOSError ValueErrorZ StringInputrW) r!rirr2rr source_dirZcsv_filerurllibZcsv_textrrrrsx             zCSVTable.get_csv_datarcCs |jdS)Nzutf-8)decode)srrrdecode_from_csvaszCSVTable.decode_from_csvcCs |jdS)Nzutf-8)encode)rrrrrpcszCSVTable.encode_for_csvcCs|S)Nr)rrrrrfscCs|S)Nr)rrrrrphsc stjfdd|D|d}g}d}xd|D]\}g}x:|D]2} j| } dddtj| j|df} |j| q8W|j|t|t|}q*W||fS)Ncsg|]}j|dqS)r%)rp)rRr )r!rrrTosz5CSVTable.parse_csv_data_into_rows..)dialectr)r) rvreaderrrZ StringListrappendrr0) r!rrrZ csv_readerr.r=r7Zrow_datacellZ cell_textZ cell_datar)r!rr(ms     z!CSVTable.parse_csv_data_into_rowsN)r)r)rDrErFr nonnegative_intrIrJrKruririrHrZ$single_char_or_whitespace_or_unicodeflagZsingle_char_or_unicoderLrvrwr}r*rrerrrrrp staticmethodr(rrrrrfs8  /J  rfc@sLeZdZdZejejejd ejejej e dZ ddZ ddZ dd Zd 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 r)z header-rowsz stub-columnsrrrrc Cs@|js4|jjjd|jtj|j|j|jd}|gS|j \}}tj }|j j |j|j |yN|j|\}}dd|dD}|jjdd}|jjdd} |j||| Wn(tk r} z | jdgSd} ~ XnX|j|||| } d|jkr|jjd| d<| d |jjd g7<|j| |r6| jd|| g|S) Nz.The "%s" directive is empty; content required.)r cSsg|]}dd|dDqS)cSsg|] }|jqSr)r])rRitemrrrrTsz,ListTable.run...rr)rRZrow_listrrrrTsz!ListTable.run..rz header-rowsz stub-columnsrrOr)rVrr1r2rrr3r4rr#rXrrYrZcheck_list_contentr'r9r8rrbuild_table_from_listr_r`) r!r2rr"ranum_colsr> table_datar5r6rrbrrrres6      z ListTable.runcCsHt|dkst|dtj rN|jjjd|jtj|j |j |j d}t ||d}xt t|D]}||}t|dkst|dtj r|jjjd|j|dftj|j |j |j d}t |qd|r&t|d|kr2|jjjd|j|dt|d|ftj|j |j |j d}t |qdt|d}qdW|j |}||fS)NrrzUError 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).)r0r[rZ bullet_listrr1r2rr3r4rrranger?)r!rar2Z list_nodeZ item_indexrrr>rrrrs6     zListTable.check_list_contentcCs4tj}|jdkr&|ddg7<n|jr>|ddg7<tjt|d}||7}xD|D]<}tj}|dk rz||jd<|rd|jd<|d8}||7}q\Wg} xD|D]<} tj} x$| D]} tj} | | 7} | | 7} qW| j | qW|rtj }|j | d|||7}tj }|j | |d||7}|S) NrrOzcolwidths-autozcolwidths-given)colsrUrZstub) rr\rrcr0rP attributesr7entryrtheadr+tbody)r!rr>r5r6r\rcrdrPr.r7Zrow_noderrrrrrrrs>        zListTable.build_table_from_listN)r)rDrErFrGr rrJrKrHrIrrLrerrrrrrrs'r)rGZ __docformat__ros.pathrrvZdocutilsrrrrZdocutils.utils.error_reportingrZdocutils.utilsrZdocutils.parsers.rstrr rrrNrfrrrrrs    c)[