o ?]Y@sdZdZddlZddlZddlZddlmZmZm Z m Z ddl m Z ddl mZddlmZddlmZd d ZGd d d eZGd ddeZGdddeZGdddeZdS)z Directives for table elements. reStructuredTextN)ionodes statemachineutils) SafeString)SystemMessagePropagation) Directive) directivescCs t|dS)N)leftcenterright)r choice)argumentrH/usr/lib/python3/dist-packages/docutils/parsers/rst/directives/tables.pyaligns rc@sxeZdZdZdZdZejeje ej e dej dZ dZddZdd Zd d Zd d ZeddZddZddZdS)Tablez# Generic table base class. T)autogrid)classnamerwidthwidthscCsf|jr+|jd}|j||j\}}tj|dg|R}|j|j\|_|_ ||fSd}g}||fS)Nr) argumentsstate inline_textlinenortitle state_machineget_source_and_linesourceline)self title_text text_nodesmessagesr rrr make_title+s  zTable.make_titlecCsX|j|jd}g}d}d|jvr(||jdd||\}}||||fS)Nrrheader )r! get_sourceroptionsparse_csv_data_into_rowssplit HeaderDialectextend)r%r# table_headmax_header_colsrowsrrrprocess_header_option8s  zTable.process_header_optioncCsBt||kr#|jjjd|t||jft|j|j|jd}t |t||kr/dkrMnn|jjjdt||jft|j|j|jd}t ||D]O}t||krt|jjjd|t||jft|j|j|jd}t |t||krdkrnqO|jjjdt||jft|j|j|jd}t |qOdS)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.) lenr!reportererrorrr literal_block block_textrr)r%r4 header_rows stub_columnsr9rowrrrcheck_table_dimensionsCs^    zTable.check_table_dimensionscCs"d|jvr|jd|d<dSdS)Nrr-get)r% table_noderrrset_table_widthbs zTable.set_table_widthcCs|jddS)Nrrr@r%rrrrfsz Table.widthscCst|jtr,t|j|kr'|jjjd|j|ft |j |j |j d}t ||j}|S|r7d|g|}|S|jjjdt |j |j |j d}t |)Nz="%s" widths do not match the number of columns in table (%s).r6dz#No table data detected in CSV file.) isinstancerlistr7r!r8r9rrr:r;rr)r%max_colsr9 col_widthsrrrget_column_widthsjs0 zTable.get_column_widthscCsD|D]}|D]}t||kr|dddgfg|t|qqdS)Nr)r7r1)r%columnspartspartr>rrr"extend_short_rows_with_empty_cells|s  z(Table.extend_short_rows_with_empty_cellsN)__name__ __module__ __qualname____doc__optional_argumentsfinal_argument_whitespacer class_option unchangedr length_or_percentage_or_unitlessvalue_orpositive_int_list option_spec has_contentr)r5r?rCpropertyrrJrNrrrrrs*    rc@seZdZddZdS)RSTTablec Cs|js|jjjd|jt|j|j|jd}|gS| \}}t }|j |j|j |t|dkss z RSTTable.run..colwidthrcolwidths-autocolwidths-given)contentr!r8warningrrr:r;rr)Elementr nested_parsecontent_offsetr7rFtabler9r-rArCrrGchildrenzipadd_nameinsert) r%rhr r(noder9rBtgroupcolspecsr_ col_widthrrrrunsV         z RSTTable.runN)rOrPrQrurrrrr]s r]c@seZdZejejejejedejej ej ej ej eje ejejejejdZGdddejZGdddejZddZd d Zd d Zejd krTddZddZnddZddZeeZeeZddZdS)CSVTabler) header-rows stub-columnsr*rrfileurlencodingrrrdelim keepspacequoteescapec@s6eZdZdZdZdZdZdZdZdZ e j Z ddZ dS) zCSVTable.DocutilsDialectz&CSV dialect for `csv_table` directive.,"Tr+cCsld|vr t|d|_d|vrd|_d|vrt|d|_d|vr.d|_t|d|_tj |dS)Nr}r~Frr) rvencode_for_csv delimiterskipinitialspace quotechar doublequote escapecharcsvDialect__init__)r%r-rrrrsz!CSVTable.DocutilsDialect.__init__N)rOrPrQrRrrrrstrictlineterminatorr QUOTE_MINIMALquotingrrrrrDocutilsDialects rc@s2eZdZdZdZdZdZdZdZdZ dZ e j Z dS) zCSVTable.HeaderDialectz0CSV dialect to use for the "header" option data.rr\FTr+N)rOrPrQrRrrrrrrrrrrrrrrr0s r0cCsdSNrrDrrrcheck_requirementszCSVTable.check_requirementsc Cs8z|jjjjs)d|jvsd|jvr)|jjjd|jt |j |j |j d}|gWS| |\}}|\}}|\}}||||j|\}} t| |} |jdd} |jdd} ||| | ||d| || d} || } || || fWnTty}z |jdgWYd}~Sd}~wtjy}z0t|}tjdkrd |vr|d 7}|jjj d |j|ft |j |j |j d}|gWYd}~Sd}~ww| || f}|jj!||j"| |j#d }|d |jdg7<d|jvr|jd|d<|$||%||r|&d||g|S)Nrzr{z9File and URL access deactivated; ignoring "%s" directive.r6rxrryrz1-character stringz1 with Python 2.x this must be an ASCII character.z)Error with CSV data in "%s" directive: %s)rr^rr)'rdocumentsettingsfile_insertion_enabledr-r!r8rhrrr:r;rrr) get_csv_datar5r.rmaxrAr?r1rJrNrargsrErrorstrsys version_infor9 build_tablerkrrCrorp)r%rhr r(csv_datar#r2r3r4rHr<r= table_bodyrIdetailmessager9rlrBrrrrus|               z CSVTable.runc Cs|jd|jjjj}|jjjj}|jrCd|jvsd|jvr6|jj j d|j t |j|j|jd}t||jd}|j}||fSd|jvrd|jvre|jj j d|j t |j|j|jd}t|tjtj|jjj}tjtj||jd}td|}z|jjjj|tj|||d }| }W||fSt!y}z|jj j"d |j t#|ft |j|j|jd}t|d}~wwd|jvrAt$j%d krdd l&m'} dd l(m)} nddl*m'} m)} |jd}z| |} Wn1| t!t+t,fy)}z |jj j"d|j |jdt#|ft |j|j|jd}t|d}~wwtj-| |||jjjjd}| }||fS|jj j.d|j t |j|j|jd}t|)zr Get CSV data from the directive content, from an external file, or from a URL reference. r|rzr{zF"%s" directive may not both specify an external file and have content.r6rzXThe "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)rrz*Problems with "%s" directive URL "%s": %s.)r#rr|rz3The "%s" directive requires content; none supplied.)/r-rArrrinput_encodinginput_encoding_error_handlerrgr!r8r9rrr:r;rrr#ospathdirnameabspathcurrent_sourcenormpathjoinr relative_pathrecord_dependenciesaddr FileInputread splitlinesIOErrorsevererrrurllib.requestr urllib.errorrurllib2OSError ValueError StringInputrh) r%r|rr9r#r source_dircsv_filerrrcsv_textrrrrs   <  (      zCSVTable.get_csv_datarcC |dSNzutf-8)decodesrrrdecode_from_csvn zCSVTable.decode_from_csvcCrr)encoderrrrrprzCSVTable.encode_for_csvcC|SrrrrrrrsrcCrrrrrrrrurc stjfdd|D|d}g}d}|D],}g}|D]} | } dddtj| |df} || q||t|t|}q||fS)Ncsg|] }|dqS)r+)r)rar$rDrrrc|sz5CSVTable.parse_csv_data_into_rows..)dialectr)r#) rreaderrr StringListrappendrr7) r%rrr# csv_readerr4rHr>row_datacell cell_text cell_datarrDrr.zs$    z!CSVTable.parse_csv_data_into_rowsN)rOrPrQr nonnegative_intrVrWrXrYrurir|rUr$single_char_or_whitespace_or_unicodeflagsingle_char_or_unicoderZrrrr0rrurrrrr staticmethodr.rrrrrvs@ 0 O  rvc@sPeZdZdZejejejedejej ej e dZ ddZ ddZdd 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 rw)rxryrrrrrc CsN|js|jjjd|jt|j|j|jd}|gS| \}}t }|j |j|j |z'||\}}dd|dD}|jdd}|jdd} |||| Wntym} z | jdgWYd} ~ Sd} ~ ww||||| } d|jvr|jd| d<| d |jd g7<|| || |r| d|| g|S) Nz.The "%s" directive is empty; content required.r6cSsg|] }dd|dDqS)cSsg|]}|jqSr)rm)raitemrrrrcsz,ListTable.run...rr)rarow_listrrrrcsz!ListTable.run..rrxryrr^r)rgr!r8r9rrr:r;rr)rirrjrkcheck_list_contentr-rAr?rrbuild_table_from_listrCrorp) r%r9r r(rqnum_colsrI table_datar<r=rrBrrrrusB      z ListTable.runcCs>t|dkst|dtjs&|jjjd|jt|j |j |j d}t ||d}d}t t|D]c}||}t|dksFt|dtjsb|jjjd|j|dft|j |j |j d}t ||rt|d|kr|jjjd|j|dt|d|ft|j |j |j d}t |q2t|d}q2| |}||fS)NrrzUError parsing content block for the "%s" directive: exactly one bullet list expected.r6zError 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).)r7rFr bullet_listr!r8r9rr:r;rrrangerJ)r%rqr9 list_noder item_indexrrIrrrrsL  zListTable.check_list_contentcCs&t}|jdkr|ddg7<n |jr|ddg7<tjt|d}||7}|D]}t}|dur<||jd<|rGd|jd<|d8}||7}q-g} |D]} t} | D]} t} | | 7} | | 7} qX| | qP|rt }| | d|||7}t }| | |d||7}|S) Nrr^rerf)colsrdrstub) rrlrrrr7r_ attributesr>entryrtheadr1tbody)r%rrIr<r=rlrrrtr_r4r>row_noderrrrrrrrs>      zListTable.build_table_from_listN)rOrPrQrRr rrWrXrYrUrVrrZrurrrrrrrs  %r)rR __docformat__ros.pathrrdocutilsrrrrdocutils.utils.error_reportingrdocutils.utilsrdocutils.parsers.rstr r rrr]rvrrrrrs     h*b