a
    ia~                     @   s  d dl mZmZmZ d dlmZ d dlmZmZ 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mZmZ dd	lmZ dd
l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dZejred dkreddksJ e
 edd e!d d Z"n
e
 eZ"h dZ#e
 dZ$i Z%G dd de&Z'dd Z(G dd de&Z)G dd de)Z*G dd  d e+Z,G d!d" d"e&Z-G d#d$ d$e&Z.d%d& Z/dS )'    )absolute_importdivisionunicode_literals)	text_type)http_clienturllibN)BytesIOStringIO)webencodings   )EOFspaceCharactersasciiLettersasciiUppercase)_ReparseException)_utilsc                 C   s   g | ]}| d qS asciiencode.0item r   c/home/ec2-user/npost_license/.venv/lib/python3.9/site-packages/pip/_vendor/html5lib/_inputstream.py
<listcomp>       r   c                 C   s   g | ]}| d qS r   r   r   r   r   r   r      r   c                 C   s   g | ]}| d qS r   r   r   r   r   r   r      r      >   <u   [---﷐-﷯￾￿🿾🿿𯿾𯿿𿿾𿿿񏿾񏿿񟿾񟿿񯿾񯿿񿿾񿿿򏿾򏿿򟿾򟿿򯿾򯿿򿿾򿿿󏿾󏿿󟿾󟿿󯿾󯿿󿿾󿿿􏿾􏿿]]z"\uD800-\uDFFF">    i
 i i i i i i i i i i i i i	 i i i	 i i i i i i i i i i i
 i i i i z[	- -/:-@\[-`{-~]c                   @   sH   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S )BufferedStreamzBuffering for streams that do not have buffering of their own

    The buffer is implemented as a list of chunks on the assumption that
    joining many strings will be slow since it is O(n**2)
    c                 C   s   || _ g | _ddg| _d S )Nr   r   )streambufferposition)selfr"   r   r   r   __init__:   s    zBufferedStream.__init__c                 C   s<   d}| j d | jd  D ]}|t|7 }q|| jd 7 }|S Nr   r   )r#   r$   len)r%   poschunkr   r   r   tell?   s
    zBufferedStream.tellc                 C   sT   ||   ksJ |}d}t| j| |k rF|t| j| 8 }|d7 }q||g| _d S r'   )_bufferedBytesr(   r#   r$   )r%   r)   offsetir   r   r   seekF   s    
zBufferedStream.seekc                 C   sT   | j s| |S | jd t| j krF| jd t| j d krF| |S | |S d S )Nr   r   r   )r#   _readStreamr$   r(   _readFromBufferr%   bytesr   r   r   readO   s    

zBufferedStream.readc                 C   s   t dd | jD S )Nc                 S   s   g | ]}t |qS r   )r(   r   r   r   r   r   Y   r   z1BufferedStream._bufferedBytes.<locals>.<listcomp>)sumr#   r%   r   r   r   r,   X   s    zBufferedStream._bufferedBytesc                 C   s<   | j |}| j| | jd  d7  < t|| jd< |S r'   )r"   r4   r#   appendr$   r(   )r%   r3   datar   r   r   r0   [   s
    zBufferedStream._readStreamc                 C   s   |}g }| j d }| j d }|t| jk r|dkr|dks>J | j| }|t|| krl|}||| g| _ n"t|| }|t|g| _ |d7 }|||||   ||8 }d}q|r|| | d|S )Nr   r   r   )r$   r(   r#   r7   r0   join)r%   r3   remainingBytesrvbufferIndexbufferOffsetbufferedDatabytesToReadr   r   r   r1   b   s&    


zBufferedStream._readFromBufferN)__name__
__module____qualname____doc__r&   r+   r/   r4   r,   r0   r1   r   r   r   r   r!   3   s   		r!   c                 K   s   t | tjs(t | tjjr.t | jtjr.d}n&t| drJt | dt	}n
t | t	}|rdd |D }|rvt
d| t| fi |S t| fi |S d S )NFr4   r   c                 S   s   g | ]}| d r|qS )	_encoding)endswith)r   xr   r   r   r      r   z#HTMLInputStream.<locals>.<listcomp>z3Cannot set an encoding with a unicode input, set %r)
isinstancer   HTTPResponser   responseaddbasefphasattrr4   r   	TypeErrorHTMLUnicodeInputStreamHTMLBinaryInputStream)sourcekwargs	isUnicode	encodingsr   r   r   HTMLInputStream}   s    

rT   c                   @   sp   e Zd 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dZdd Zdd ZdddZdd ZdS )rN   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.

    i (  c                 C   sZ   t jsd| _ntddkr$| j| _n| j| _dg| _tddf| _| 	|| _
|   dS )  Initialises the HTMLInputStream.

        HTMLInputStream(source, [encoding]) -> Normalized stream from source
        for use by html5lib.

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

        The optional encoding parameter must be a string that indicates
        the encoding.  If specified, that encoding will be used,
        regardless of any BOM or later declaration (such as in a meta
        element)

        Nu   􏿿r   r   utf-8certain)r   supports_lone_surrogatesreportCharacterErrorsr(   characterErrorsUCS4characterErrorsUCS2newLineslookupEncodingcharEncoding
openStream
dataStreamreset)r%   rP   r   r   r   r&      s    
zHTMLUnicodeInputStream.__init__c                 C   s.   d| _ d| _d| _g | _d| _d| _d | _d S )N r   )r*   	chunkSizechunkOffseterrorsprevNumLinesprevNumCols_bufferedCharacterr6   r   r   r   rb      s    zHTMLUnicodeInputStream.resetc                 C   s   t |dr|}nt|}|S zvProduces a file object from source.

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

        r4   )rL   r	   r%   rP   r"   r   r   r   r`      s    
z!HTMLUnicodeInputStream.openStreamc                 C   sT   | j }|dd|}| j| }|dd|}|dkr@| j| }n||d  }||fS )N
r   r   r   )r*   countrg   rfindrh   )r%   r-   r*   nLinespositionLinelastLinePospositionColumnr   r   r   	_position   s    
z HTMLUnicodeInputStream._positionc                 C   s   |  | j\}}|d |fS )z:Returns (line, col) of the current position in the stream.r   )rs   re   )r%   linecolr   r   r   r$      s    zHTMLUnicodeInputStream.positionc                 C   s6   | j | jkr|  stS | j }| j| }|d | _ |S )zo Read one character from the stream or queue if available. Return
            EOF when EOF is reached.
        r   )re   rd   	readChunkr   r*   )r%   re   charr   r   r   rw      s    

zHTMLUnicodeInputStream.charNc                 C   s   |d u r| j }| | j\| _| _d| _d| _d| _| j|}| j	rX| j	| }d | _	n|s`dS t
|dkrt|d }|dksd|  krdkrn n|d | _	|d d }| jr| | |d	d
}|dd
}|| _t
|| _dS )Nrc   r   Fr   r         i  z
rl   T)_defaultChunkSizers   rd   rg   rh   r*   re   ra   r4   ri   r(   ordrZ   replace)r%   rd   r8   lastvr   r   r   rv      s0    
 


z HTMLUnicodeInputStream.readChunkc                 C   s(   t tt|D ]}| jd qd S )Ninvalid-codepoint)ranger(   invalid_unicode_refindallrf   r7   )r%   r8   _r   r   r   r[     s    z*HTMLUnicodeInputStream.characterErrorsUCS4c                 C   s   d}t |D ]}|rqt| }| }t|||d  rrt|||d  }|tv rl| j	
d d}q|dkr|dkr|t|d kr| j	
d qd}| j	
d qd S )NF   r   Try   i  r   )r   finditerr|   groupstartr   isSurrogatePairsurrogatePairToCodepointnon_bmp_invalid_codepointsrf   r7   r(   )r%   r8   skipmatch	codepointr)   char_valr   r   r   r\   #  s"    z*HTMLUnicodeInputStream.characterErrorsUCS2Fc           
      C   s  zt ||f }W nf tyv   |D ]}t|dk s"J q"ddd |D }|sXd| }td|  }t ||f< Y n0 g }|| j| j}|du r| j| j	krqn0|
 }|| j	kr|| j| j|  || _q|| j| jd  |  s|qq|d|}	|	S )z Returns a string of characters from the stream up to but not
        including any character in 'characters' or EOF. 'characters' must be
        a container that supports the 'in' method and iteration over its
        characters.
           rc   c                 S   s   g | ]}d t | qS )z\x%02x)r|   )r   cr   r   r   r   H  r   z5HTMLUnicodeInputStream.charsUntil.<locals>.<listcomp>z^%sz[%s]+N)charsUntilRegExKeyErrorr|   r9   recompiler   r*   re   rd   endr7   rv   )
r%   
charactersoppositecharsr   regexr;   mr   rr   r   r   
charsUntil:  s0     

z!HTMLUnicodeInputStream.charsUntilc                 C   sT   |t urP| jdkr.|| j | _|  jd7  _n"|  jd8  _| j| j |ksPJ d S r'   )r   re   r*   rd   )r%   rw   r   r   r   ungeti  s    
zHTMLUnicodeInputStream.unget)N)F)r@   rA   rB   rC   r{   r&   rb   r`   rs   r$   rw   rv   r[   r\   r   r   r   r   r   r   rN      s    
&
/rN   c                   @   sL   e Zd ZdZdddZdd Zd	d
 ZdddZdd Zdd Z	dd Z
dS )rO   rU   Nwindows-1252Tc                 C   sn   |  || _t| | j d| _d| _|| _|| _|| _|| _	|| _
| || _| jd dusbJ |   dS )rV   i   d   r   N)r`   	rawStreamrN   r&   numBytesMetanumBytesChardetoverride_encodingtransport_encodingsame_origin_parent_encodinglikely_encodingdefault_encodingdetermineEncodingr_   rb   )r%   rP   r   r   r   r   r   
useChardetr   r   r   r&     s    zHTMLBinaryInputStream.__init__c                 C   s&   | j d j| jd| _t|  d S )Nr   r}   )r_   
codec_infostreamreaderr   ra   rN   rb   r6   r   r   r   rb     s    zHTMLBinaryInputStream.resetc                 C   sJ   t |dr|}nt|}z||  W n tyD   t|}Y n0 |S rj   )rL   r   r/   r+   	Exceptionr!   rk   r   r   r   r`     s    
z HTMLBinaryInputStream.openStreamc                 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rX|S |  df}|d d urt|S t| jdf}|d d ur|d jds|S t| jdf}|d d ur|S |rnzddl	m
} W n ty   Y n0 g }| }|js:| j| j}t|tsJ |s$q:|| || q|  t|jd }| jd |d urn|dfS t| jdf}|d d ur|S tddfS )NrX   r   	tentativezutf-16)UniversalDetectorencodingr   )	detectBOMr^   r   r   detectEncodingMetar   name
startswithr   %pip._vendor.chardet.universaldetectorr   ImportErrordoner   r4   r   rG   r3   r7   feedcloseresultr/   r   )r%   chardetr_   r   buffersdetectorr#   r   r   r   r   r     sR    

z'HTMLBinaryInputStream.determineEncodingc                 C   s   | j d dksJ t|}|d u r&d S |jdv rFtd}|d usJ nT|| j d krf| j d df| _ n4| jd |df| _ |   td| j d |f d S )Nr   rX   utf-16beutf-16lerW   r   zEncoding changed from %s to %s)r_   r^   r   r   r/   rb   r   )r%   newEncodingr   r   r   changeEncoding  s    

z$HTMLBinaryInputStream.changeEncodingc              
   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}|s~|
|}d}|s~|
|dd	 }d	}|r| j| t|S | jd
 dS dS )zAttempts 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 NonerW   r   r   zutf-32lezutf-32be   N   r   r   )codecsBOM_UTF8BOM_UTF16_LEBOM_UTF16_BEBOM_UTF32_LEBOM_UTF32_BEr   r4   rG   r3   getr/   r^   )r%   bomDictstringr   r/   r   r   r   r     s&    
zHTMLBinaryInputStream.detectBOMc                 C   sV   | j | j}t|tsJ t|}| j d | }|durR|jdv rRt	d}|S )z9Report the encoding declared by the meta element
        r   Nr   rW   )
r   r4   r   rG   r3   EncodingParserr/   getEncodingr   r^   )r%   r#   parserr   r   r   r   r   3  s    z(HTMLBinaryInputStream.detectEncodingMeta)NNNNr   T)T)r@   rA   rB   rC   r&   rb   r`   r   r   r   r   r   r   r   r   rO   z  s      
*
>"rO   c                   @   s   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eee
Zdd ZeeZefddZdd Zdd Zdd ZdS )EncodingByteszString-like object with an associated position and various extra methods
    If the position is ever greater than the string length then an exception is
    raisedc                 C   s   t |tsJ t| | S N)rG   r3   __new__lowerr%   valuer   r   r   r   F  s    zEncodingBytes.__new__c                 C   s
   d| _ d S )Nr   )rs   r   r   r   r   r&   J  s    zEncodingBytes.__init__c                 C   s   | S r   r   r6   r   r   r   __iter__N  s    zEncodingBytes.__iter__c                 C   s>   | j d  }| _ |t| kr"tn|dk r.t| ||d  S )Nr   r   rs   r(   StopIterationrM   r%   pr   r   r   __next__Q  s    zEncodingBytes.__next__c                 C   s   |   S r   )r   r6   r   r   r   nextY  s    zEncodingBytes.nextc                 C   sB   | j }|t| krtn|dk r$t|d  | _ }| ||d  S r'   r   r   r   r   r   previous]  s    zEncodingBytes.previousc                 C   s   | j t| krt|| _ d S r   rs   r(   r   )r%   r$   r   r   r   setPositionf  s    zEncodingBytes.setPositionc                 C   s*   | j t| krt| j dkr"| j S d S d S )Nr   r   r6   r   r   r   getPositionk  s
    
zEncodingBytes.getPositionc                 C   s   | | j | j d  S Nr   )r$   r6   r   r   r   getCurrentByteu  s    zEncodingBytes.getCurrentBytec                 C   sH   | j }|t| k r>| ||d  }||vr4|| _|S |d7 }q|| _dS )zSkip past a list of charactersr   Nr$   r(   rs   r%   r   r   r   r   r   r   r   z  s    
zEncodingBytes.skipc                 C   sH   | j }|t| k r>| ||d  }||v r4|| _|S |d7 }q|| _d S r   r   r   r   r   r   	skipUntil  s    
zEncodingBytes.skipUntilc                 C   s(   |  || j}|r$|  jt|7  _|S )zLook 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   r$   r(   )r%   r3   r;   r   r   r   
matchBytes  s    zEncodingBytes.matchBytesc                 C   s<   z |  || jt| d | _W n ty6   tY n0 dS )zLook for the next sequence of bytes matching a given sequence. If
        a match is found advance the position to the last byte of the matchr   T)indexr$   r(   rs   
ValueErrorr   r2   r   r   r   jumpTo  s
     
zEncodingBytes.jumpToN)r@   rA   rB   rC   r   r&   r   r   r   r   r   r   propertyr$   r   currentBytespaceCharactersBytesr   r   r   r   r   r   r   r   r   B  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 elementsc                 C   s   t || _d| _dS )z3string - the data to work on for encoding detectionN)r   r8   r   r%   r8   r   r   r   r&     s    
zEncodingParser.__init__c              
   C   s   d| j vrd S d| jfd| jfd| jfd| jfd| jfd| jff}| j D ]}d}z| j d W n tyx   Y  qY n0 |D ]B\}}| j |r~z| }W  qW q~ ty   d}Y  qY q~0 q~|sH qqH| j	S )	Ns   <metas   <!--s   </s   <!s   <?r   TF)
r8   handleComment
handleMetahandlePossibleEndTaghandleOtherhandlePossibleStartTagr   r   r   r   )r%   methodDispatchr   keepParsingkeymethodr   r   r   r     s4    


zEncodingParser.getEncodingc                 C   s   | j dS )zSkip over commentss   -->r8   r   r6   r   r   r   r     s    zEncodingParser.handleCommentc                 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 q|d dkr|d }t|}|d ur|| _dS q|d dkrtt|d }| }|d urt|}|d ur|r|| _dS |}qd S )	NTFr   s
   http-equivr   s   content-type   charsets   content)	r8   r   r   getAttributer   r^   ContentAttrParserr   parse)r%   	hasPragmapendingEncodingattrtentativeEncodingcodeccontentParserr   r   r   r     s8    zEncodingParser.handleMetac                 C   s
   |  dS )NF)handlePossibleTagr6   r   r   r   r     s    z%EncodingParser.handlePossibleStartTagc                 C   s   t | j | dS )NT)r   r8   r  r6   r   r   r   r     s    
z#EncodingParser.handlePossibleEndTagc                 C   sb   | j }|jtvr(|r$|  |   dS |t}|dkrD|  n|  }|d ur^|  }qLdS )NTr   )r8   r   asciiLettersBytesr   r   r   spacesAngleBracketsr   )r%   endTagr8   r   r   r   r   r   r    s    



z EncodingParser.handlePossibleTagc                 C   s   | j dS )Nr   r   r6   r   r   r   r     s    zEncodingParser.handleOtherc                 C   s  | j }|ttdgB }|du s2t|dks2J |dv r>dS g }g }|dkrV|rVqnX|tv rj| }qnD|dv rd|dfS |tv r||  n|du rdS || t	|}qF|dkr|
  d|dfS t	| | }|dv rJ|}t	|}||kr"t	| d|d|fS |tv r<||  q|| qnJ|d	krbd|dfS |tv r|||  n|du rdS || t	|}|tv rd|d|fS |tv r||  n|du rdS || qdS )
z_Return a name,value pair for the next attribute in the stream,
        if one is found, or None   /Nr   )r   N   =)r  r   r   )   '   "r   )r8   r   r   	frozensetr(   r9   asciiUppercaseBytesr7   r   r   r   r  )r%   r8   r   attrName	attrValue	quoteCharr   r   r   r     sb    











zEncodingParser.getAttributeN)r@   rA   rB   rC   r&   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   )rG   r3   r8   r   r   r   r   r&   a  s    zContentAttrParser.__init__c                 C   s   z| j d | j  jd7  _| j   | j jdks<W d S | j  jd7  _| j   | j jdv r| j j}| j  jd7  _| j j}| j |r| j || j j W S W d S nP| j j}z"| j t | j || j j W W S  t y   | j |d   Y W S 0 W n ty   Y d S 0 d S )Nr   r   r  )r	  r  )r8   r   r$   r   r   r   r   r   )r%   	quoteMarkoldPositionr   r   r   r   e  s.    

zContentAttrParser.parseN)r@   rA   rB   r&   r   r   r   r   r   r   `  s   r   c                 C   sb   t | tr.z| d} W n ty,   Y dS 0 | durZzt| W S  tyV   Y dS 0 ndS dS )z{Return the python codec name corresponding to an encoding or None if the
    string doesn't correspond to a valid encoding.r   N)rG   r3   decodeUnicodeDecodeErrorr
   lookupAttributeError)r   r   r   r   r^     s    

r^   )0
__future__r   r   r   Zpip._vendor.sixr   pip._vendor.six.movesr   r   r   r   ior   r	   pip._vendorr
   	constantsr   r   r   r   r   rc   r   r
  r   r  r  r  invalid_unicode_no_surrogaterY   rm   r   evalr   r   ascii_punctuation_rer   objectr!   rT   rN   rO   r3   r   r   r   r^   r   r   r   r   <module>   sH   

J g Ib ='