3 E\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)Zargumentr/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/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).)rrzeInsufficient 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_columnsr1rowrrrcheck_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).)rdz#No table data detected in CSV file.) typerlistr/rr0r1rrr2r3rr)r max_colsr1 col_widthsrrrget_column_widthseszTable.get_column_widthscCsLxF|D]>}x8|D]0}t||kr|jdddgfg|t|qWqWdS)Nr)r/r*)r columnspartspartr6rrr"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.r7propertyrr>rBrrrrrs    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.)rrrzOError parsing content block for the "%s" directive: exactly one table expected.classesrrcSsg|]}|jdkr|qS)colspec)tagname).0childrrr sz RSTTable.run..colwidthrzcolwidths-autozcolwidths-given)contentrr0warningrrr2r3rr"Elementr nested_parsecontent_offsetr/ isinstancetabler1r&r8r:rr;childrenzipadd_nameinsert) r rVrr!noder1 table_nodetgroupZcolspecsrO col_widthrrrruns@       z RSTTable.runN)rCrDrErdrrrrrM~srMc@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)NrirjFrkrl) reencode_for_csv delimiterskipinitialspace quotechar doublequote escapecharcsvDialect__init__)r r&rrrrwsz!CSVTable.DocutilsDialect.__init__N)rCrDrErFrprrrsrqstrictlineterminatorru QUOTE_MINIMALquotingrwrrrrDocutilsDialectsr|c@s2eZdZdZdZdZdZdZdZdZ dZ e j Z dS) zCSVTable.HeaderDialectz0CSV dialect to use for the "header" option data.rmrn\FTr$N)rCrDrErFrprrrtrsrqrxryrurzr{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)Nrfrgz9File and URL access deactivated; ignoring "%s" directive.)rz 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)rrNrr)r)&rdocumentsettingsZfile_insertion_enabledr&rr0rVrrr2r3rr~r" get_csv_datar.r'r|maxr8r7r*r>rBrargsruErrorstrsys version_infor1Z build_tablerYrr^r_)r rVrr!csv_datarr+r,r-r<r4r5Z table_bodyr=detailmessager1r[rarrrrdsZ               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. rhrfrgzF"%s" directive may not both specify an external file and have content.)rrzXThe "file" and "url" options may not be simultaneously specified for the "%s" directive.N) source_pathrh error_handlerz&Problems with "%s" directive path: %s.z*Problems with "%s" directive URL "%s": %s.)rrrhrz3The "%s" directive requires content; none supplied.).r&r8rrrZinput_encodingZinput_encoding_error_handlerrUrr0r1rrr2r3rrrospathdirnameabspathZcurrent_sourcenormpathjoinrZ relative_pathZrecord_dependenciesaddrZ FileInputread splitlinesIOErrorsevererurllib.request urllib.error urllib.parserequesturlopenURLErrorOSError ValueErrorZ StringInputrV) r rhrr1rr source_dirZcsv_filerurllibZcsv_textrrrrsx             zCSVTable.get_csv_datarcCs |jdS)Nzutf-8)decode)srrrdecode_from_csvaszCSVTable.decode_from_csvcCs |jdS)Nzutf-8)encode)rrrrrocszCSVTable.encode_for_csvcCs|S)Nr)rrrrrfscCs|S)Nr)rrrrrohsc 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$)ro)rQr)r rrrSosz5CSVTable.parse_csv_data_into_rows..)dialectr)r) rureaderrrZ StringListrappendrr/) r rrrZ csv_readerr-r<r6Zrow_datacellZ cell_textZ cell_datar)r rr'ms     z!CSVTable.parse_csv_data_into_rowsN)r)r)rCrDrEr nonnegative_intrHrIrJrurirhrGrZ$single_char_or_whitespace_or_unicodeflagZsingle_char_or_unicoderKrurvr|r)r~rdrrrrro staticmethodr'rrrrres8  /J  rec@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.)rcSsg|]}dd|dDqS)cSsg|] }|jqSr)r\)rQitemrrrrSsz,ListTable.run...rr)rQZrow_listrrrrSsz!ListTable.run..rz header-rowsz stub-columnsrrNr)rUrr0r1rrr2r3rr"rWrrXrYcheck_list_contentr&r8r7rrbuild_table_from_listr^r_) r r1rr!r`num_colsr= table_datar4r5rrarrrrds6      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.)rzError 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/rZrZ bullet_listrr0r1rr2r3rrranger>)r r`r1Z 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) NrrNzcolwidths-autozcolwidths-given)colsrTrZstub) rr[rrbr/rO attributesr6entryrtheadr*tbody)r rr=r4r5r[rbrcrOr-r6Zrow_noderrrrrrrrs>        zListTable.build_table_from_listN)r)rCrDrErFr rrIrJrGrHrrKrdrrrrrrrs'r)rFZ __docformat__ros.pathrruZdocutilsrrrrZdocutils.utils.error_reportingrZdocutils.utilsrZdocutils.parsers.rstrr rrrMrerrrrrs    c)[