ifQdZdZddlZddlZddlmZddlmZGddeZGdd Z Gd d e Z Gd d e Z dZ dS)a This module defines table parser classes,which parse plaintext-graphic tables and produce a well-formed data structure suitable for building a CALS table. :Classes: - `GridTableParser`: Parse fully-formed tables represented with a grid. - `SimpleTableParser`: Parse simple tables, delimited by top & bottom borders. :Exception class: `TableMarkupError` :Function: `update_dict_of_lists()`: Merge two dictionaries containing list values. reStructuredTextN) DataErrorstrip_combining_charsceZdZdZdZdS)TableMarkupErrorz Raise if there is any problem with table markup. The keyword argument `offset` denotes the offset of the problem from the table's start line. c`|dd|_tj|g|RdS)Noffsetr)popr r__init__)selfargskwargss /builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib/python3.11/site-packages/docutils/parsers/rst/tableparser.pyr zTableMarkupError.__init__&s6jj1-- 4'$''''''N)__name__ __module__ __qualname____doc__r rrrrs-(((((rrc*eZdZdZdZ dZ dZdZdS) TableParserzR Abstract superclass for the common parts of the syntax-specific parsers. Nc|||||S)an Analyze the text `block` and return a table data structure. Given a plaintext-graphic table in `block` (list of lines of text; no whitespace padding), parse the table, construct and return the data necessary to construct a CALS table or equivalent. Raise `TableMarkupError` if there is any problem with the markup. )setupfind_head_body_sep parse_tablestructure_from_cellsr blocks rparsezTableParser.parse7sL 5 !!! ((***rctt|jD]x}|j|}|j|rO|jr#t d|jdzd|dzd|||_|dd|j|<y|jdks |jt|jdz krt d |d S) z>Look for a head/body row separator line; store the line index.z/Multiple head/body row separators (table lines z and z); only one allowed.r =-rzKThe head/body row separator may not be the first or last line of the table.N)rangelenr head_body_separator_patmatch head_body_seprreplace)r ilines rrzTableParser.find_head_body_sepFss4:'' ; ;A:a=D+11$77 ;%;** -a///16>?AAAA *+D&$(LLc$:$:DJqM   " "d&8S__?@>A'B'B"$J*+--- -'B'Br)rrrrr)double_width_pad_charr!rrrrrr+sP#D"= + + +-----rrcneZdZdZejdZdZdZdZ dZ dZ dZ d Z d Zd Zd Zd S)GridTableParserai Parse a grid table using `parse()`. Here's an example of a grid table:: +------------------------+------------+----------+----------+ | Header row, column 1 | Header 2 | Header 3 | Header 4 | +========================+============+==========+==========+ | body row 1, column 1 | column 2 | column 3 | column 4 | +------------------------+------------+----------+----------+ | body row 2 | Cells may span columns. | +------------------------+------------+---------------------+ | body row 3 | Cells may | - Table cells | +------------------------+ span rows. | - contain | | body row 4 | | - body elements. | +------------------------+------------+---------------------+ Intersections use '+', row separators use '-' (except for one optional head/body row separator, which uses '='), and column separators use '|'. Passing the above table to the `parse()` method will result in the following data structure:: ([24, 12, 10, 10], [[(0, 0, 1, ['Header row, column 1']), (0, 0, 1, ['Header 2']), (0, 0, 1, ['Header 3']), (0, 0, 1, ['Header 4'])]], [[(0, 0, 3, ['body row 1, column 1']), (0, 0, 3, ['column 2']), (0, 0, 3, ['column 3']), (0, 0, 3, ['column 4'])], [(0, 0, 5, ['body row 2']), (0, 2, 5, ['Cells may span columns.']), None, None], [(0, 0, 7, ['body row 3']), (1, 0, 7, ['Cells may', 'span rows.', '']), (1, 1, 7, ['- Table cells', '- contain', '- body elements.']), None], [(0, 0, 9, ['body row 4']), None, None, None]]) The first item is a list containing column widths (colspecs). The second item is a list of head rows, and the third is a list of body rows. Each row contains a list of cells. Each cell is either None (for a cell unused because of another cell's span), or a tuple. A cell tuple contains four items: the number of extra rows used by the cell in a vertical span (morerows); the number of extra columns used by the cell in a horizontal span (morecols); the line offset of the first line of the cell contents; and the cell contents, a list of lines of text. z\+=[=+]+=\+ *$c>|dd|_|jt|dz |_t|ddz |_d|_dgt|dz|_g|_ddgi|_ddgi|_ dS)Nr#r) r disconnectr(bottomrightr+donecellsrowsepscolsepsrs rrzGridTableParser.setups111X  %jj1n q]]Q& !D3uQx==(  A3x A3x rcdg}|rE|d\}}||jks||jks||j|krC|||}|s\|\}}}}t |j|t |j|||||||j |dz|dz||} | | |j d|j||||| f|||f||fg||E|st'ddS)a  Start with a queue of upper-left corners, containing the upper-left corner of the table itself. Trace out one rectangular cell, remember it, and add its upper-right and lower-left corners to the queue of potential upper-left corners of further cells. Process the queue in top-to-bottom order, keeping track of how much of each text column has been seen. We'll end up knowing all the row and column boundaries, cell positions and their dimensions. )rrrr#z"Malformed table; parse incomplete.N)r r5r6r7 scan_cellupdate_dict_of_listsr9r: mark_doner get_2D_blockr4r,r/r8appendextendsortcheck_parse_completer) r cornerstopleftresultr5r6r9r: cellblocks rrzGridTableParser.parse_tables(  AICt{""4:%%$)D/))^^C..F .4 +FE7G w 7 7 7 w 7 7 7 NN3fe 4 4 4 //a06??I  " " "   d8" = = = J  sD&%C D D D NNS%L64.9 : : : LLNNN' (((** I"#GHH H I Ircz|dz }|dz }t||D]}|j||ksJ||j|< dS)z@For keeping track of how much of each text column has been seen.r#N)r'r7)r rFrGr5r6beforeaftercols rr?zGridTableParser.mark_dones[q u%% # #C9S>V++++"DIcNN # #rcp|jdz }t|jD]}|j||krdSdS)z2Each text column should have been completely seen.r#FT)r5r'r6r7)r lastrMs rrDz$GridTableParser.check_parse_completesH{Q$$  Cy~%%uu&trc`|j||dksJ|||S)z:Starting at the top-left corner, start tracing out a cell.+)r scan_right)r rFrGs rr=zGridTableParser.scan_cells2z#t$++++sD)))rci}|j|}t|dz|jdzD][}||dkr>|g||<||||}|r|\}}} t || ||||fcSL||dkrdS\dS)zr Look for the top-right corner of the cell, and make note of all column boundaries ('+'). r#rQr&N)r r'r6 scan_downr>) r rFrGr:r.r-rHr5r9 newcolsepss rrRzGridTableParser.scan_rights z#taxa00  AAw#~~!U T155728/FGZ(*===!1gw66667aCtt trc"i}t|dz|jdzD]p}|j||dkr=|g||<|||||}|r|\}}t |||||fcSV|j||dkrdSqdS)zj Look for the bottom-right corner of the cell, making note of all row boundaries. r#rQ|N)r'r5r scan_leftr>) r rFrGr6r9r-rH newrowsepsr:s rrTzGridTableParser.scan_downs sQw a00  Az!}U#s**#W T1e<</*0'J(*===gw..../Au%,,tt-trci}|j|}t|dz |dD]$}||dkr|g||<||dkrdS%||dkrdS|||||}||} | |fSdS)z Noting column boundaries, look for the bottom-left corner of the cell. It must line up with the starting point. r#r3rQr&N)r r'scan_up) r rFrGr5r6r:r.r-rHr9s rrXzGridTableParser.scan_lefts z&!uqy$++  AAw#~~$X aCtt :  4c477  GG# #trci}t|dz |dD]:}|j||dkr|g||< |j||dkrdS;|S)zT Noting row boundaries, see if we can return to the starting point. r#r3rQrWN)r'r )r rFrGr5r6r9r-s rr[zGridTableParser.scan_upsrvz3++  Az!}T"c))"V At$++tt,rct|j}i}tt |D] }||||<t|ji}tt D] }|||<fdtdt D}dtt dz Dfdtt |dz D}t |dz t dz z}|jD]y\}} } } } ||} || }|| |Jd| dzd|dzd|| | z dz }|| |z dz }||dz|dzzz}|||dz| f|| |<z|d ks Jd |jr"||j}|d|}||d}ng}|}|||fS) zh From the data collected by `scan_cell()`, convert to the final data structure. c>g|]}||dz z dz S)r#r).0r-r:s r z8GridTableParser.structure_from_cells..(s?555QZ'!a%.014555rr#cg|]}dSNr)r_r-s rr`z8GridTableParser.structure_from_cells..+s8881$888rc$g|] }dd Srbr)r_r-onerows rr`z8GridTableParser.structure_from_cells..,s!;;;aqqq ;;;rNz Cell (row z , column z) already used.rzUnused cells remaining.)sortedr9keysr'r(r:r8r+)r r9rowindexr-colindexcolspecsrows remainingrFrGr5r6r rownumcolnummorerowsmorecols numheadrowsheadrowsbodyrowsr:rds @@rrz$GridTableParser.structure_from_cellssw **,,--s7||$$ % %A#$HWQZ **,,--s7||$$ % %A#$HWQZ 5555"1c'll3355598c'llQ&6 7 7888;;;;5W)9#:#:;;;\\A%#g,,*:; /3z H H +Cvuec]Fd^F<'///aZZZ!-0// '&014H/!3H (Q,8a<8 8I$,ha#GDL A~~~8~~~   "4#56KL[L)HKLL)HHHH8++rN)rrrrrecompiler)rrr?rDr=rRrTrXr[rrrrr1r1Zs22h)bj):;;    "I"I"IH###*** &$(   ',',',',',rr1c~eZdZdZejdZejdZdZdZ dZ dZ d d Z d Z d ZdS) SimpleTableParseraQ Parse a simple table using `parse()`. Here's an example of a simple table:: ===== ===== col 1 col 2 ===== ===== 1 Second column of row 1. 2 Second column of row 2. Second line of paragraph. 3 - Second column of row 3. - Second item in bullet list (row 3, column 2). 4 is a span ------------ 5 ===== ===== Top and bottom borders use '=', column span underlines use '-', column separation is indicated with spaces. Passing the above table to the `parse()` method will result in the following data structure, whose interpretation is the same as for `GridTableParser`:: ([5, 25], [[(0, 0, 1, ['col 1']), (0, 0, 1, ['col 2'])]], [[(0, 0, 3, ['1']), (0, 0, 3, ['Second column of row 1.'])], [(0, 0, 4, ['2']), (0, 0, 4, ['Second column of row 2.', 'Second line of paragraph.'])], [(0, 0, 6, ['3']), (0, 0, 6, ['- Second column of row 3.', '', '- Second item in bullet', ' list (row 3, column 2).'])], [(0, 1, 10, ['4 is a span'])], [(0, 0, 12, ['5']), (0, 0, 12, [''])]]) z=[ =]*$z-[ -]*$c|dd|_|j|jddd|jd<|jddd|jd<d|_g|_d|_g|_dgt|dz|_ddgi|_ ddgi|_ dS)Nrr%r&r3) r r4r,r+columns border_endtabler(r7r9r:rs rrzSimpleTableParser.setupws111X   1 --c377 1 B//S99 2!  D3uQx==( A3x A3x rc||jdd|_|jdd|_|jd\}}d}d}d}|t |jkr|j|}|j|r@||j|||||f|dz}d}nS||| r0|r)||kr#||j||||}d}n|s|dz}|dz }|t |jkdSdS)z First determine the column boundaries from the top border, then process rows. Each row may consist of multiple lines; accumulate lines until a row is complete. Call `self.parse_row` to finish the job. rr3r#N) parse_columnsr rxryr(span_patr* parse_rowrstripstrip)r firststartfirstendr start text_foundr.s rrzSimpleTableParser.parse_tablesd))$*Q-;; ,r*1-#|A H s4:&&:f%D}""4(( #tz%,7 $ v6888 ! j)*0022 #D&E//NN4:eFl#;UCCC  #  aKF!s4:&&&&&&rcg}d} |d|}|d|}|dkrn-|dkrt|}|||f`|jrR|dd|jkrt d|dzz||dd|jddf|d<|S) zU Given a column span underline, return a list of (begin, end) pairs. rTr& r3r#z(Column span incomplete in table line %s.r$)findr(rArxryr)r r.r colsendbegins rr|zSimpleTableParser.parse_columnss &IIc3''E))C''CqyyQww$ii KK % % % & < :Bx{do--&(25;AX(?.46666R T\"%5a%89DH rcbd}g}|D]\}}d} ||j|dksJ||j|dkr!|dz }|dz }||j|dk!n/#ttf$rtd|dzz|dzwxYw|d||gg|dz }|S)Nrr#z/Column span alignment problem in table line %s.r$)rxAssertionError IndexErrorrrA)r colspecr r-r8rrros rinit_rowzSimpleTableParser.init_rows !  JE3H 8 Q 22222T\!_Q///FAMHT\!_Q///#J/ 8 8 8&(;>DQh(H.4Qh8888 8 LL!Xvr2 3 3 3 FAA s AA!!,B Nc|s|sdS|r |j|}n|jdd}|||||||}t t |D]l}||\}}|d|t ||}|||j d|||d<m|j |dS)af Given the text `lines` of a row, parse it and append to `self.table`. The row is parsed according to the current column spec (either `spanline` if provided or `self.columns`). For each column, extract text from each line, and check for text in column margins. Finally, adjust for insignificant whitespace. Nrr<) r|rx check_columnsrr'r(r@r4r,r/rzrA) r linesrspanlinerxrowr-rrIs rr~zSimpleTableParser.parse_rows    F  &(d((3GGl111oG 5%111mmGU++s7||$$ " "A JE3**1eSZZEEI  " " "   d8" = = =!CF1II #rc|tjdft|dz }d|D}t t|dz D]}||\}}||dzd}d} |D]} ||kr| |drf| |d} |t| z} |jd\} }|t|| f||<| |kr | | f|jd<n9| ||rtd|| zdzz|| z| dz } Ɍ| dS) z Check for text in column margins and text overflow in the last column. Raise TableMarkupError if anything but whitespace is in column margins. Adjust the end value for the last column if there is text overflow. Nrc,g|]}t|Srr)r_r.s rr`z3SimpleTableParser.check_columns..s!???&t,,???rr#rr3z'Text in column margin in table line %s.r$) rAsysmaxsizer(r'rrrxmaxrr )r r first_linerxlastcolr-rr nextstartr r.textnew_end main_startmain_ends rrzSimpleTableParser.check_columnss  T*+++g,,"?????s7||a'((  A JE3! QIF  <. s ???JE3C%K???rrr)rxr+r'r(rz)r rifirst_body_rowr-s rrz&SimpleTableParser.structure_from_cellss??$,???   3tz??++  :a=#A&);;;%&NE<$*_n_5 >??+- -rrb)rrrrrsrtr)r}rrr|rr~rrrrrrvrvEs++Z)bj33rz)$$H    @.$4@ - - - - -rrvc|D].\}}||g|/dS)z Extend the list values of `master` with those from `newdata`. Both parameters must be dictionaries containing list values. N)items setdefaultrB)masternewdatakeyvaluess rr>r>sP }}22 V#r""))&111122r) r __docformat__rsrdocutilsrdocutils.utilsrrrr1rvr>rrrrs   #  000000 ( ( ( ( (y ( ( (,-,-,-,-,-,-,-,-^h,h,h,h,h,kh,h,h,VL-L-L-L-L- L-L-L-^22222r