o
    »Ùøi~a  ã                   @   s  d dl Z d dlZd dlmZmZ d dlmZ d dlmZm	Z	 d dl
Z
ddlmZmZmZ edd„ eD ƒƒZed	d„ eD ƒƒZed
d„ e	D ƒƒZeeddgƒB Ze d¡Zh d£Ze d¡Zi Zdd„ ZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ dƒZG dd„ dƒZdd„ Z dS )é    N)ÚBytesIOÚStringIO)ÚPath)Úascii_lettersÚascii_uppercaseé   )ÚEOFÚReparseErrorÚspace_charactersc                 c   ó   � | ]}|  ¡ V  qd S ©N©Úencode©Ú.0Úitem© r   úb/home/develop/edictos_py/central_edictos/env/lib/python3.10/site-packages/tinyhtml5/inputstream.pyÚ	<genexpr>   ó   € r   c                 c   r   r   r   r   r   r   r   r      r   c                 c   r   r   r   r   r   r   r   r      r   ó   >ó   <u¡   [---ÂŸï·�-ï·¯ï¿¾ï¿¿ðŸ¿¾ðŸ¿¿ð¯¿¾ð¯¿¿ð¿¿¾ð¿¿¿ñ�¿¾ñ�¿¿ñŸ¿¾ñŸ¿¿ñ¯¿¾ñ¯¿¿ñ¿¿¾ñ¿¿¿ò�¿¾ò�¿¿òŸ¿¾òŸ¿¿ò¯¿¾ò¯¿¿ò¿¿¾ò¿¿¿ó�¿¾ó�¿¿óŸ¿¾óŸ¿¿ó¯¿¾ó¯¿¿ó¿¿¾ó¿¿¿ô�¿¾ô�¿¿í €-í¿¿]>    éþÿ éþÿ éþÿ éþÿ éþÿ éþÿ éþÿ éþÿ éþÿ	 éþÿ
 éþÿ éþÿ éþÿ éþÿ éþÿ éþÿ éÿÿ éÿÿ éÿÿ éÿÿ éÿÿ éÿÿ éÿÿ éÿÿ éÿÿ	 éÿÿ
 éÿÿ éÿÿ éÿÿ éÿÿ éÿÿ éÿÿ z[	- -/:-@\[-`{-~]c                 K   s–   t | tƒrt| ƒdk rt| ƒ ¡ rtt| ƒ ¡ fi |¤ŽS t | tƒr,t|  ¡ fi |¤ŽS t t| dƒr7|  d¡n| tƒrCt| fi |¤ŽS t	| fi |¤ŽS )NéÈ   Úreadr   )
Ú
isinstanceÚstrÚlenr   Úis_fileÚHTMLUnicodeInputStreamÚ	read_textÚhasattrr9   ÚHTMLBinaryInputStream)ÚsourceÚkwargsr   r   r   ÚHTMLInputStream&   s   "
rD   c                   @   sZ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
ddd„Zdd„ ZdS )r>   z×Provides a Unicode stream of characters to the HTMLTokenizer.

    This class takes care of character encoding and removing or replacing
    incorrect byte-sequences and also provides column and line tracking.

    c                 K   s.   dg| _ tdƒdf| _|  |¡| _|  ¡  dS )z¸Initialise the HTMLInputStream.

        Create a normalized stream from source for use by tinyhtml5.

        source can be either a file-object, local filename or a string.

        r   úutf-8ÚcertainN)Ú	new_linesÚlookup_encodingÚencodingÚopen_streamÚstreamÚreset)ÚselfrB   rC   r   r   r   Ú__init__9   s   	zHTMLUnicodeInputStream.__init__c                 C   s.   d| _ d| _d| _g | _d| _d| _d | _d S )NÚ r   )ÚchunkÚ
chunk_sizeÚchunk_offsetÚerrorsÚprevious_number_linesÚprevious_number_columnsÚ_buffered_character©rM   r   r   r   rL   I   s   
zHTMLUnicodeInputStream.resetc                 C   s   t |dƒr|S t|ƒS )zuProduce a file object from source.

        source can be either a file object, local filename or a string.

        r9   )r@   r   ©rM   rB   r   r   r   rJ   W   s   z"HTMLUnicodeInputStream.open_streamc                 C   sZ   | j }| dd|¡}| j| }| dd|¡}|dkr#| j| }||fS ||d  }||fS )NÚ
r   éÿÿÿÿr   )rP   ÚcountrT   ÚrfindrU   )rM   ÚoffsetrP   Únumber_linesÚposition_lineÚlast_line_positionÚposition_columnr   r   r   Ú	_position_   s   

ÿz HTMLUnicodeInputStream._positionc                 C   s   |   | j¡\}}|d |fS )z9Return (line, col) of the current position in the stream.r   )rb   rR   )rM   ÚlineÚcolumnr   r   r   Úpositionj   s   zHTMLUnicodeInputStream.positionc                 C   s6   | j | jkr|  ¡ stS | j }| j| }|d | _ |S )zlRead one character from the stream or queue if available.

        Return EOF when EOF is reached.

        r   )rR   rQ   Ú
read_chunkr   rP   )rM   rR   Ú	characterr   r   r   rg   o   s   

z HTMLUnicodeInputStream.characterc                 C   sð   |   | j¡\| _| _d| _d| _d| _| j d¡}| jr%| j| }d | _n|s)dS t	|ƒdkrPt
|d ƒ}|dksEd|  krCd	krPn n|d | _|d d… }tt	t |¡ƒƒD ]}| j d
¡ qY| dd¡}| dd¡}|| _t	|ƒ| _dS )NrO   r   i (  Fr   rZ   é   i Ø  iÿÛ  zinvalid-codepointz
rY   úT)rb   rQ   rT   rU   rP   rR   rK   r9   rV   r<   ÚordÚrangeÚinvalid_unicode_reÚfindallrS   ÚappendÚreplace)rM   ÚdataÚlastÚ_r   r   r   rf   €   s0   ÿ
 

z!HTMLUnicodeInputStream.read_chunkFc                 C   sò   zt ||f }W n+ ty3   d dd„ |D ƒ¡}|s d|› �}t d|› d�¡}| }t ||f< Y nw g }	 | | j| j¡}|du rK| j| jkrJn*n| 	¡ }|| jkrc| 
| j| j|… ¡ || _n| 
| j| jd… ¡ |  ¡ ssnq7d |¡S )	a   Return a string of characters from the stream.

        String goes up to but does not include any character in 'characters' or
        EOF. 'characters' must be a container that supports the 'in' method and
        iteration over its characters.

        rO   c                 S   s   g | ]
}d t |ƒd›�‘qS )z\xÚ02x)rj   )r   rg   r   r   r   Ú
<listcomp>²   s    z6HTMLUnicodeInputStream.chars_until.<locals>.<listcomp>ú^ú[z]+TN)Úcharacters_until_regexÚKeyErrorÚjoinÚreÚcompileÚmatchrP   rR   rQ   Úendrn   rf   )rM   Ú
charactersÚoppositeÚregexÚresultr|   r}   r   r   r   Úchars_until¥   s4   

ûÿ
ë
z"HTMLUnicodeInputStream.chars_untilc                 C   sZ   |t ur)| jdkr|| j | _|  jd7  _d S |  jd8  _| j| j |ks+J ‚d S d S )Nr   r   )r   rR   rP   rQ   )rM   Úcharr   r   r   ÚungetÓ   s   
ö
zHTMLUnicodeInputStream.ungetN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rN   rL   rJ   rb   re   rg   rf   r‚   r„   r   r   r   r   r>   1   s    
%.r>   c                       sX   e Zd ZdZ			ddd„Z‡ fdd„Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
‡  ZS )rA   zÕProvide a binary stream of characters to the HTMLTokenizer.

    This class takes care of character encoding and removing or replacing
    incorrect byte-sequences and also provides column and line tracking.

    Núwindows-1252c                 K   sX   |   |¡| _d| _|| _|| _|| _|| _|| _|  ¡ | _	| j	d d us&J ‚|  
¡  d S )Ni   r   )rJ   Ú
raw_streamÚnumber_bytes_metaÚoverride_encodingÚtransport_encodingÚsame_origin_parent_encodingÚlikely_encodingÚdefault_encodingÚdetermine_encodingrI   rL   )rM   rB   rŒ   r�   rŽ   r�   r�   rC   r   r   r   rN   ë   s   
zHTMLBinaryInputStream.__init__c                    s*   | j d jj}|| jdƒ| _tƒ  ¡  d S )Nr   ro   )rI   Ú
codec_infoÚstreamreaderrŠ   rK   ÚsuperrL   )rM   r“   ©Ú	__class__r   r   rL     s   zHTMLBinaryInputStream.resetc                 C   s0   t |dƒrt |dƒr| ¡ r|S | ¡ }t|ƒS )Nr9   Úseekable)r@   r—   r9   r   rX   r   r   r   rJ   	  s
   
z!HTMLBinaryInputStream.open_streamc                 C   sê   |   ¡ df}|d d ur|S t| jƒdf}|d d ur|S t| jƒdf}|d d ur,|S |  ¡ df}|d d ur:|S t| jƒdf}|d d urQ|d j d¡sQ|S t| jƒdf}|d d ur`|S t| j	ƒdf}|d d uro|S tdƒdfS )NrF   r   Ú	tentativezutf-16r‰   )
Ú
detect_bomrH   rŒ   r�   Údetect_encoding_metarŽ   ÚnameÚ
startswithr�   r�   )rM   rI   r   r   r   r‘     s,   z(HTMLBinaryInputStream.determine_encodingc                 C   s¢   | j d dks	J ‚t|ƒ }d u rd S |jdv r$tdƒ}|d us"J ‚d S || j d kr5| j d df| _ d S | j d¡ |df| _ |  ¡  td| j d › d|› �ƒ‚)Nr   rF   ©úutf-16beúutf-16lerE   r   zEncoding changed from z to )rI   rH   r›   rŠ   ÚseekrL   r	   )rM   Únew_encodingr   r   r   Úchange_encoding8  s   

ÿz%HTMLBinaryInputStream.change_encodingc              
   C   s„   t jdt jdt jdt jdt jdi}| j d¡}t|t	ƒsJ ‚dD ]}| 
|d|… ¡ }r9| j |¡ t|ƒ  S q | j d	¡ dS )
z´Attempt to detect at BOM at the start of the stream.

        If an encoding can be determined from the BOM return the name of the
        encoding otherwise return None.

        rE   rŸ   rž   zutf-32lezutf-32beé   )é   r£   é   Nr   )ÚcodecsÚBOM_UTF8ÚBOM_UTF16_LEÚBOM_UTF16_BEÚBOM_UTF32_LEÚBOM_UTF32_BErŠ   r9   r:   ÚbytesÚgetr    rH   )rM   ÚbomsÚstringr    rI   r   r   r   r™   H  s   û	ýz HTMLBinaryInputStream.detect_bomc                 C   sV   | j  | j¡}t|tƒsJ ‚t|ƒ}| j  d¡ | ¡ }|dur)|jdv r)t	dƒ}|S )z1Report the encoding declared by the meta element.r   Nr�   rE   )
rŠ   r9   r‹   r:   r¬   ÚEncodingParserr    Úget_encodingr›   rH   )rM   ÚbufferÚparserrI   r   r   r   rš   e  s   z*HTMLBinaryInputStream.detect_encoding_meta)NNNNr‰   )r…   r†   r‡   rˆ   rN   rL   rJ   r‘   r¢   r™   rš   Ú__classcell__r   r   r•   r   rA   ã   s    
þ(rA   c                   @   sz   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	e
e	eƒZe
dd„ ƒZefdd„Zdd„ Zdd„ Zdd„ ZdS )ÚEncodingBytesz«Bytes-like object with an associated position and various extra methods.

    If the position is ever greater than the string length then an exception is
    raised.

    c                 C   s   t |tƒsJ ‚t | | ¡ ¡S r   )r:   r¬   Ú__new__Úlower)ÚclsÚvaluer   r   r   r¶   {  s   zEncodingBytes.__new__c                 C   s
   d| _ d S )NrZ   ©rb   )rM   r¹   r   r   r   rN     s   
zEncodingBytes.__init__c                 C   s0   | j d  }| _ |t| ƒkrt‚| ||d … S ©Nr   ©rb   r<   ÚStopIteration©rM   re   r   r   r   Ú__next__‚  s   zEncodingBytes.__next__c                 C   s    | j d  | _ }| ||d … S r»   rº   r¾   r   r   r   Úpreviousˆ  s   zEncodingBytes.previousc                 C   s"   | j t| ƒkr	t‚td|ƒ| _ d S ©Nr   )rb   r<   r½   Úmaxr¾   r   r   r   Úset_positionŒ  s   zEncodingBytes.set_positionc                 C   s&   | j t| ƒkr	t‚| j dkr| j S d S rÁ   r¼   rW   r   r   r   Úget_position‘  s
   
ÿzEncodingBytes.get_positionc                 C   s   | | j | j d … S r»   )re   rW   r   r   r   Úcurrent_byte™  s   zEncodingBytes.current_bytec                 C   sR   | j }|t| ƒk r$| ||d … }||vr|| _|S |d7 }|t| ƒk s	|| _dS )zSkip past a list of characters.r   N©re   r<   rb   ©rM   r~   re   rg   r   r   r   Úskip�  s   ûzEncodingBytes.skipc                 C   sR   | j }|t| ƒk r$| ||d … }||v r|| _|S |d7 }|t| ƒk s	|| _d S r»   rÆ   rÇ   r   r   r   Ú
skip_until©  s   ûzEncodingBytes.skip_untilc                 C   s(   |   || j¡ }r|  jt|ƒ7  _|S )zßLook for a sequence of bytes at the start of a string.

        If the bytes are found return True and advance the position to the byte
        after the match. Otherwise return False and leave the position alone.

        )rœ   re   r<   )rM   r¬   r�   r   r   r   Úmatch_bytes´  s   zEncodingBytes.match_bytesc                 C   s6   z|   || j¡t|ƒ d | _W dS  ty   t‚w )z™Look for the next sequence of bytes matching a given sequence.

        If a match is found advance the position to the last byte of the match.

        r   T)Úindexre   r<   rb   Ú
ValueErrorr½   )rM   r¬   r   r   r   Újump_to¿  s   þÿzEncodingBytes.jump_toN)r…   r†   r‡   rˆ   r¶   rN   r¿   rÀ   rÃ   rÄ   Úpropertyre   rÅ   Úspace_characters_bytesrÈ   rÉ   rÊ   rÍ   r   r   r   r   rµ   s  s    

rµ   c                   @   sX   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )r°   z@Mini parser for detecting character encoding from meta elements.c                 C   s   t |ƒ| _d | _d S r   )rµ   rp   rI   ©rM   rp   r   r   r   rN   Ï  s   

zEncodingParser.__init__c              
   C   s¼   d| j vrd S | j| j| j| j| j| jdœ}| j D ]A}d}z| j  d¡ W n ty1   Y  | j
S w | ¡ D ]\}}| j  	|¡rSz|ƒ }W  n tyR   d}Y  nw q6|sZ | j
S q| j
S )Nó   <meta)s   <!--rÑ   s   </s   <!s   <?r   Tr   F)rp   Úhandle_commentÚhandle_metaÚhandle_possible_end_tagÚhandle_otherÚhandle_possible_start_tagrÍ   r½   ÚitemsrÊ   rI   )rM   Úmethod_dispatchrr   Úkeep_parsingÚkeyÚmethodr   r   r   r±   Ó  s@   
ú
óþüýzEncodingParser.get_encodingc                 C   ó   | j  d¡S )zSkip over comments.s   -->©rp   rÍ   rW   r   r   r   rÒ   ò  s   zEncodingParser.handle_commentc                 C   sÞ   | j jtvrdS d}d }	 |  ¡  }d u rdS |d dkr/|d dk}|r.|d ur.|| _dS n?|d dkrG|d }t|ƒ}|d urF|| _dS n'|d dkrntt|d ƒƒ}| ¡  }d urnt|ƒ}|d urn|rl|| _dS |}q)	NTFr   s
   http-equivr   s   content-typeó   charsets   content)	rp   rÅ   rÏ   Úget_attributerI   rH   ÚContentAttributeParserrµ   Úparse)rM   Ú
has_pragmaÚpending_encodingÚ	attributeÚtentative_encodingÚcodecÚcontent_parserr   r   r   rÓ   ö  s<   €þèzEncodingParser.handle_metac                 C   s   | j dd�S )NF©Úend_tag)Úhandle_possible_tagrW   r   r   r   rÖ     ó   z(EncodingParser.handle_possible_start_tagc                 C   s   t | jƒ | jdd�S )NTrè   )Únextrp   rê   rW   r   r   r   rÔ     s   
z&EncodingParser.handle_possible_end_tagc                 C   s\   | j }|jtvr|r| ¡  |  ¡  dS | t¡}|dkr#| ¡  dS 	 |  ¡ d u r-	 dS q$)NTr   )rp   rÅ   Úascii_letters_bytesrÀ   rÕ   rÉ   Úspaces_angle_bracketsrß   )rM   ré   rp   rg   r   r   r   rê     s   

ýýz"EncodingParser.handle_possible_tagc                 C   rÜ   )Nr   rÝ   rW   r   r   r   rÕ   5  rë   zEncodingParser.handle_otherc                 C   sÜ  | j }| ttdgƒB ¡}|du st|ƒdksJ ‚|dv rdS g }g }	 |dkr+|r+n0|tv r4| ¡ }n'|dv r?d |¡dfS |tv rK| | ¡ ¡ n|du rQdS | |¡ t	|ƒ}q$|dkrj| 
¡  d |¡dfS t	|ƒ | ¡ }| }d	v r¡	 t	|ƒ}||kr�t	|ƒ d |¡d |¡fS |tv r›| | ¡ ¡ n| |¡ qy|d
kr¬d |¡dfS |tv r¸| | ¡ ¡ n|du r¾dS | |¡ 	 t	|ƒ}|tv rÖd |¡d |¡fS |tv râ| | ¡ ¡ n|du rèdS | |¡ qÄ)z{Return a (name, value) pair for the next attribute in the stream.

        If no attribute is found, return None.

        ó   /Nr   )r   NTó   =)rï   r   ó    )ó   'ó   "r   )rp   rÈ   rÏ   Ú	frozensetr<   ry   Úascii_uppercase_bytesrn   r·   rì   rÀ   rî   )rM   rp   rg   Úattribute_nameÚattribute_valueÚquoter   r   r   rß   8  sl   
ð
ô

÷zEncodingParser.get_attributeN)r…   r†   r‡   rˆ   rN   r±   rÒ   rÓ   rÖ   rÔ   rê   rÕ   rß   r   r   r   r   r°   Ì  s    !r°   c                   @   s   e Zd Zdd„ Zdd„ ZdS )rà   c                 C   s   t |tƒsJ ‚|| _d S r   )r:   r¬   rp   rÐ   r   r   r   rN   ˆ  s   
zContentAttributeParser.__init__c                 C   s  zy| j  d¡ | j  jd7  _| j  ¡  | j jdksW d S | j  jd7  _| j  ¡  | j jdv rS| j j}| j  jd7  _| j j}| j  |¡rP| j || j j… W S W d S | j j}z| j  t¡ | j || j j… W W S  tyy   | j |d …  Y W S w  tyƒ   Y d S w )NrÞ   r   rð   )ró   rò   )rp   rÍ   re   rÈ   rÅ   rÉ   rÏ   r½   )rM   rø   Úold_positionr   r   r   rá   Œ  s2   

þÿzContentAttributeParser.parseN)r…   r†   r‡   rN   rá   r   r   r   r   rà   ‡  s    rà   c                 C   s\   t | tƒrz|  d¡} W n
 ty   Y dS w | dur,zt | ¡W S  ty+   Y dS w dS )z‡Return the Python codec name corresponding to an encoding.

    Return None if the string doesn't correspond to a valid encoding.

    ÚasciiN)r:   r¬   ÚdecodeÚUnicodeDecodeErrorÚwebencodingsÚlookupÚAttributeError)rI   r   r   r   rH   ­  s   
ÿÿýrH   )!r¦   rz   Úior   r   Úpathlibr   r¯   r   r   rý   Ú	constantsr   r	   r
   rô   rÏ   rí   rõ   rî   r{   rl   Únon_bmp_invalid_codepointsÚascii_punctuation_rerw   rD   r>   rA   r¬   rµ   r°   rà   rH   r   r   r   r   Ú<module>   s:    ÿÿ 3 Y <&