o
    hG                  	   @   s  d Z dZz!dZddlZdd ZejddddedddZdddi fdd	ZW n ey0   dZY nw G d
d dZ	e	 Z	dZ
dZdZdZeeZdZg dZdd ZddefddZer]eZdd ZdZdd ZddefddZdd Zd)ddZd)ddZed kredd! ddlZddlZdd"lmZ ddlZe Z dZ!ej"dd D ];Z#ej$%e#se&d# qe'e#Z(e() Z*W d   n1 sw   Y  e&d$e#d%ee*  ee*dd! e!d7 Z!qe!re&d&e e  d'd( dS dS dS )*at  Very simple and fast XML parser, used for intra-paragraph text.

Devised by Aaron Watters in the bad old days before Python had fast
parsers available.  Constructs the lightest possible in-memory
representation; parses most files we have seen in pure python very
quickly.

The output structure is the same as the one produced by pyRXP,
our validating C-based parser, which was written later.  It will
use pyRXP if available.

This is used to parse intra-paragraph markup.

Example parse::

    <this type="xml">text <b>in</b> xml</this>

    ( "this",
      {"type": "xml"},
      [ "text ",
        ("b", None, ["in"], None),
        " xml"
        ]
       None )

    { 0: "this"
      "type": "xml"
      1: ["text ",
          {0: "b", 1:["in"]},
          " xml"]
    }

Ie, xml tag translates to a tuple:
 (name, dictofattributes, contentlist, miscellaneousinfo)

where miscellaneousinfo can be anything, (but defaults to None)
(with the intention of adding, eg, line number information)

special cases: name of "" means "top level, no containing tag".
Top level parse always looks like this::

    ("", list, None, None)

 contained text of None means <simple_tag/>

In order to support stuff like::

    <this></this><one></one>

AT THE MOMENT &amp; ETCETERA ARE IGNORED. THEY MUST BE PROCESSED
IN A POST-PROCESSING STEP.

PROLOGUES ARE NOT UNDERSTOOD.  OTHER STUFF IS PROBABLY MISSING.
    Nc                 C   s   t |  d S )N)print)s r   Y/var/www/html/optinet_system/venv/lib/python3.10/site-packages/reportlab/lib/rparsexml.pywarnCB>   s   r      zstring input)ErrorOnValidityErrorsNoNoDTDWarningExpandCharacterEntitiesExpandGeneralEntitiesr   srcName
ReturnUTF8c                 C   s.   |t _t j| fi |}|r|pdd |gd fS )N )pyRXP_parsereoCBparse)xmlTextoneOutermostTagr   entityReplacer	parseOptspr   r   r   parsexmlI   s   r   c                   @   s   e Zd Zedd ZdS )smartDecodec                    s,   ddl m   fdd}t|tj_|| S )Nr   detectc                    s$   t | tr| S  | }| |d S )Nencoding)
isinstancestrdecode)r   cddr   r   r   __call__T   s   z&smartDecode.__call__.<locals>.__call__)charset_normalizerr   staticmethodr   	__class__r    )r   r    r   r   r   r    Q   s   zsmartDecode.__call__N)__name__
__module____qualname__r"   r    r   r   r   r   r   P   s    r   r   	<![CDATA[z]]>))z&lt;<)z&gt;>)z&amp;&c                 C   s>   g }| D ]}d|v rt D ]
\}}|||}q|| q|S )Nr*   )replacelistreplaceappend)contentListresulteoldnewr   r   r   unEscapeContentListd   s   r3   c                 C   s0   t rtdt| |d\}}|r|d d S |S )z.official interface: discard unused cursor infoz)pyRXP not found, fallback parser disabled)r      r   )RequirePyRXPImportError	parsexml0)xmltextr   r   r   r/   cursorr   r   r   parsexmlSimplem   s   r:   c                 C   s   t | d }t|S )Nr)openreadr   )filenamerawr   r   r   	parseFilez   s   r@   c           	      C   s   d}d}|du rM|  d|}|dk r|S |d }d}|D ]&}t|}| |||  |krBd}|  d|}|dk r>td| |d }q|du rId}|du s|S )zBskip any prologue found after cursor, return index of rest of text)z!DOCTYPEz?xmlz!--Nr(   r   r   r)   zcan't close prologue %r)findlen
ValueError)	textr9   prologue_elementsdoneopenbracketpastfoundr0   ler   r   r   skip_prologue   s*   rK   c           1      C   s  t | } t}d } }}|dur|  } |}| d|}	| |	d |	d  }
d}|	dk rU|durG| |d g}|r=||}||||ft| fS tdt| ||d   g }|durft }}t| |}n|	dk rvtdt| dd	  |
d
kr| |	|	d  dkr|	d }| t|}|dk rtdt| ||d   t	}| || g}|tt }d}n|
dkr| |	|	d  dkr| d|	d }||	k rtdt| ||d   |d }| | dkrtdt| ||d   d|d fS | d|	}|dk }|d }|	d }| || }d|vr/|d dkr%|dd }d}| }|}|}nGd|v rvd}|sEt|d 
dd rGd}|du rv| d|}|d }|dk }| || }|sot|d 
dd rqd}|du sL|rtdt| |	|	d   |}| |d  dkr|d }|dd }d}| }|
d}|d }|
 }|d }|}|d }|d d |d< i  }}d}t|}||k rv|| }|d }| }|d dkrtdt| d|dd vr||krtdt| || } |d }d|| f }d|dd vs| }|
 }!|!d }"|dt|"  }#|# }#z
|#d }$|#d }%W n   td t|#||!f |$|%  krVdksen |$|%  krcd!krkn n|#dd }#|#||< |"}||k s|dur}|}|durY| d|}&|&|k r|tkrd}| |d }'t| }|'r||' ntd"|t| ||d  f | |&d  dkr4| d|&}(|(|&k rtd#t| |&|&d   | |&d |( })|)
 }*|*d }+||+kr| d| },t|,
d$}-| d| },t|,
d$}.td%|.|-t|t|+t| ||d&  f | ||& }'|'r-||' |(d }d}n | ||& }'|'rB||' t| |&d|d'\}/}|/rT||/ |dus|rc|rc||}||||f}0|0|fS )(zsimple recursive descent xml parser...
       return (dictionary, endcharacter)
       special case: comment returns (None, endcharacter)Nr(   r      r   zno tags at non-toplevel %s   z.non top level entry should be at start tag: %s
   z![	   r'   zunclosed CDATA %sz!-   z<!--z--zunterminated comment %sr4   r)   z*invalid comment: contains double dashes %s=/".zunclosed start tag %sz fz-attribute value must start with double quoteszunclosed value %s=%szattvalue,attentry,attlist='z&no close bracket for %s found after %szunclosed close tag %s
z8at lines %s...%s close tag name doesn't match %s...%s %sd   )
startingattoplevelr   )r   NONAMEstriprA   rB   rC   reprrK   CDATAENDMARKERCDATAMARKERsplitr-   r7   )1r8   rZ   r[   r   
NameStringContentListAttDict
ExtraStuffr9   firstbracketafterbracket2char
docontentsLname
startcdataendcdataendcommentdashes
endcommentclosebracketnoclosestartsearchpastfirstbracket
tagcontentstoptaglisttaglist0taglist0listattributenameDtaglistindexlasttaglistindexattentrynextattentryattlistnextattnameattvaluefirstlastnextopenbracket	remaindernextclosebracketclosetagcontentsclosetaglist	closenameprefix
endlinenumlinenum	parsetreetr   r   r   r7      s.  	






6





 

"



:r7   c                 C   s   t | ttfr	| S | \}}}}|si }g }| D ]}|| }|d|t|f  qd|}|s8|r8td|dur`tt	t
|}	d|	}
|sL|
S |
d}dd| }
d|||
|f S d	||f S )
z!pretty printer mainly for testingrV    zname missing with attributes???NrX   z   z
   z<%s %s>
%s
</%s>z<%s %s/>)r   r   byteskeysr-   r^   joinrC   listmappprettyprintra   )	parsedxmlrj   attdicttextlistextrar~   kv
attributestextlistpprint
textpprintnllistr   r   r   r     s(   


r   c                 C   sr   ddl m } ddlm} | }t| dd}td| |  |d@ r%|| |d@ r7td t|}t| d S d S )	Nr   time)pprintr   )r   DONErP   z============== reformatting)r   r   r:   r   r   )r   dumpr   r   nowry   r   r   r   r   	testparse  s   r   c                 C   s   t d| d d S )Na-  <this type="xml">text &lt;&gt;<b>in</b> <funnytag foo="bar"/> xml</this>
                 <!-- comment -->
                 <![CDATA[
                 <this type="xml">text <b>in</b> xml</this> ]]>
                 <tag with="<brackets in values>">just testing brackets feature</tag>
                 r   )r   r   r   r   r   test  s   
r   __main__r   r   z!!!!! no file at {f!r}zparsing z |t|=z	timed at z.2fz secs.)r   )+__doc__r5   simpleparsepyRXPUr   Parserr   r   r6   r   r\   NAMEKEYCONTENTSKEYr`   rB   LENCDATAMARKERr_   r+   r3   r:   r@   verboserK   r7   r   r   r   r$   sysosr   	reportlabr   seenargvfpathisfiler   r<   _fr=   r   r   r   r   r   <module>   s|    7	
	

 i






