U ��.e(�!@sddlmZmZmZddlmZmZddlmZm Z ddl Z ddl Z ddl m Z ddlmZmZmZmZddlmZdd lmZdd lmZzdd lmZWnek r�eZYnXed d �eD��Zedd �eD��Zedd �eD��Zeeddg�BZdZej �rJeddk�r&e�!d�dk�s*t"�e �#edd�e$d�d�Z%n e �#e�Z%e&ddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5g �Z'e �#d6�Z(iZ)Gd7d8�d8e*�Z+d9d:�Z,Gd;d<�d�d>e-�Z.Gd?d@�d@e/�Z0GdAdB�dBe*�Z1GdCdD�dDe*�Z2dEdF�Z3dS)G�)�absolute_import�division�unicode_literals)� text_type� binary_type)� http_client�urllibN)� webencodings�)�EOF�spaceCharacters� asciiLetters�asciiUppercase)�_ReparseException)�_utils)�StringIO)�BytesIOcCsg|]}|�d��qS��ascii��encode��.0�item�r�E/usr/lib/python3.8/site-packages/pip/_vendor/html5lib/_inputstream.py� srcCsg|]}|�d��qSrrrrrrrscCsg|]}|�d��qSrrrrrrrs�>�.)�sumr#�r%rrrr,^szBufferedStream._bufferedBytescCs<|j�|�}|j�|�|jdd7<t|�|jd<|Sr')r"r4r#�appendr$r()r%r3�datarrrr1as   zBufferedStream._readStreamcCs�|}g}|jd}|jd}|t|j�kr�|dkr�|dks>t�|j|}|t|�|krl|}|||g|_n"t|�|}|t|�g|_|d7}|�||||��||8}d}q|r�|�|�|��d�|�S)Nrr �)r$r(r#r-r7r1�join)r%r3ZremainingBytes�rvZ bufferIndexZ bufferOffsetZ bufferedDataZ bytesToReadrrrr2hs&     zBufferedStream._readFromBufferN) �__name__� __module__� __qualname__�__doc__r&r+r0r4r,r1r2rrrrr!9s  r!cKs�t|tj�s(t|tjj�r.t|jtj�r.d}n&t|d�rJt|�d�t �}n t|t �}|r�dd�|D�}|rvt d|��t |f|�St |f|�SdS)NFr4rcSsg|]}|�d�r|�qS)Z _encoding)�endswith)r�xrrrr�s z#HTMLInputStream..z3Cannot set an encoding with a unicode input, set %r) � isinstancerZ HTTPResponserZresponseZaddbase�fp�hasattrr4r� TypeError�HTMLUnicodeInputStream�HTMLBinaryInputStream)�source�kwargsZ isUnicodeZ encodingsrrr�HTMLInputStream�s  � �    rJc@speZdZdZdZdd�Zdd�Zdd�Zd d �Zd d �Z d d�Z ddd�Z dd�Z dd�Z ddd�Zdd�ZdS)rF��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(cCsZtjsd|_ntd�dkr$|j|_n|j|_dg|_td�df|_|� |�|_ |� �dS)�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�characterErrorsUCS2ZnewLines�lookupEncoding� charEncoding� openStream� dataStream�reset)r%rHrrrr&�s   zHTMLUnicodeInputStream.__init__cCs.d|_d|_d|_g|_d|_d|_d|_dS)N�r)r*� chunkSize� chunkOffset�errors� prevNumLines� prevNumCols�_bufferedCharacterr6rrrrW�szHTMLUnicodeInputStream.resetcCst|d�r|}nt|�}|S�zvProduces a file object from source. source can be either a file object, local filename or a string. r4)rDr�r%rHr"rrrrU�s z!HTMLUnicodeInputStream.openStreamcCsT|j}|�dd|�}|j|}|�dd|�}|dkr@|j|}n ||d}||fS)N� rrr )r*�countr\�rfindr])r%r.r*ZnLinesZ positionLineZ lastLinePosZpositionColumnrrr� _position�s   z HTMLUnicodeInputStream._positioncCs|�|j�\}}|d|fS)z:Returns (line, col) of the current position in the stream.r )rdrZ)r%�line�colrrrr$�szHTMLUnicodeInputStream.positioncCs6|j|jkr|��stS|j}|j|}|d|_|S)zo Read one character from the stream or queue if available. Return EOF when EOF is reached. r )rZrY� readChunkr r*)r%rZ�charrrrrh�s   zHTMLUnicodeInputStream.charNcCs�|dkr|j}|�|j�\|_|_d|_d|_d|_|j�|�}|j rX|j |}d|_ n|s`dSt |�dkr�t |d�}|dks�d|kr�dkr�nn|d|_ |dd�}|j r�|� |�|� d d �}|� d d �}||_t |�|_d S) NrXrFr r� ��i��z ra� T)�_defaultChunkSizerdrYr\r]r*rZrVr4r^r(�ordrP�replace)r%rYr8Zlastvrrrrg�s0           z HTMLUnicodeInputStream.readChunkcCs(ttt�|���D]}|j�d�qdS)N�invalid-codepoint)�ranger(�invalid_unicode_re�findallr[r7)r%r8�_rrrrQ%sz*HTMLUnicodeInputStream.characterErrorsUCS4cCs�d}t�|�D]�}|rqt|���}|��}t�|||d��rrt�|||d��}|tkrl|j � d�d}q|dkr�|dkr�|t |�dkr�|j � d�qd}|j � d�qdS)NF�roTrji��r ) rq�finditerrm�group�startrZisSurrogatePairZsurrogatePairToCodepoint�non_bmp_invalid_codepointsr[r7r()r%r8�skip�matchZ codepointr)Zchar_valrrrrR)s"  �z*HTMLUnicodeInputStream.characterErrorsUCS2Fc Cszt||f}Wnhtk rx|D]}t|�dks$t�q$d�dd�|D��}|sZd|}t�d|�}t||f<YnXg}|�|j|j �}|dkr�|j |j kr�q�n0|� �}||j kr�|� |j|j |��||_ q�|� |j|j d��|� �s~q�q~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. �rXcSsg|]}dt|��qS)z\x%02x)rm)r�crrrrNsz5HTMLUnicodeInputStream.charsUntil..z^%sz[%s]+N)�charsUntilRegEx�KeyErrorrmr-r:�re�compilerzr*rZrY�endr7rg) r%Z charactersZopposite�charsr|Zregexr;�mr��rrrr� charsUntil@s0    z!HTMLUnicodeInputStream.charsUntilcCsT|dk rP|jdkr.||j|_|jd7_n"|jd8_|j|j|ksPt�dSr')rZr*rYr-)r%rhrrr�ungetos   zHTMLUnicodeInputStream.unget)N)F)r<r=r>r?rlr&rWrUrdr$rhrgrQrRr�r�rrrrrF�s   & /rFc@sLeZdZdZddd�Zdd�Zd d �Zdd d �Zd d�Zdd�Z dd�Z dS)rGrKN� windows-1252TcCsn|�|�|_t�||j�d|_d|_||_||_||_||_ ||_ |� |�|_ |j ddk sbt �|��dS)rLi�drN)rU� rawStreamrFr&� numBytesMeta�numBytesChardet�override_encoding�transport_encoding�same_origin_parent_encoding�likely_encoding�default_encoding�determineEncodingrTr-rW)r%rHr�r�r�r�r�Z useChardetrrrr&�s  zHTMLBinaryInputStream.__init__cCs&|jdj�|jd�|_t�|�dS)Nrrn)rTZ codec_info� streamreaderr�rVrFrWr6rrrrW�szHTMLBinaryInputStream.resetcCsDt|d�r|}nt|�}z|�|���Wnt|�}YnX|Sr_)rDrr0r+r!r`rrrrU�s z HTMLBinaryInputStream.openStreamcCs�|��df}|ddk r|St|j�df}|ddk r:|St|j�df}|ddk rX|S|��df}|ddk rt|St|j�df}|ddk r�|dj�d�s�|St|j�df}|ddk r�|S|�rpzddl m }Wnt k r�Yn�Xg}|�}|j �s<|j �|j�}t|t��st�|�s&�q<|�|�|�|�q�|��t|jd�}|j �d�|dk �rp|dfSt|j�df}|ddk �r�|Std�dfS)NrNrZ tentativezutf-16)�UniversalDetector�encodingr�)� detectBOMrSr�r��detectEncodingMetar��name� startswithr�Z%pip._vendor.chardet.universaldetectorr�� ImportErrorZdoner�r4r�rBr3r-r7Zfeed�close�resultr0r�)r%ZchardetrTr�ZbuffersZdetectorr#r�rrrr��sR           z'HTMLBinaryInputStream.determineEncodingcCs�|jddkst�t|�}|dkr&dS|jdkrFtd�}|dk s�t�nT||jdkrf|jddf|_n4|j�d�|df|_|��td|jd|f��dS)Nr rN��utf-16be�utf-16lerMrzEncoding changed from %s to %s)rTr-rSr�r�r0rWr)r%Z newEncodingrrr�changeEncodings   z$HTMLBinaryInputStream.changeEncodingc Cs�tjdtjdtjdtjdtjdi}|j�d�}t|t �sr?r&rWrUr�r�r�r�rrrrrG�s� * >"rGc@s�eZdZdZdd�Zdd�Zdd�Zdd �Zd d �Zd d �Z dd�Z dd�Z e e e �Z dd�Ze e�Zefdd�Zdd�Zdd�Zdd�ZdS)� EncodingBytesz�String-like object with an associated position and various extra methods If the position is ever greater than the string length then an exception is raisedcCst|t�st�t�||���S�N)rBr3r-�__new__�lower�r%�valuerrrr�LszEncodingBytes.__new__cCs d|_dS)Nr)rdr�rrrr&PszEncodingBytes.__init__cCs|Sr�rr6rrr�__iter__TszEncodingBytes.__iter__cCs>|jd}|_|t|�kr"t�n |dkr.t�|||d�S)Nr r�rdr(� StopIterationrE�r%�prrr�__next__Ws  zEncodingBytes.__next__cCs|��Sr�)r�r6rrr�next_szEncodingBytes.nextcCsB|j}|t|�krt�n |dkr$t�|d|_}|||d�Sr'r�r�rrr�previouscs zEncodingBytes.previouscCs|jt|�krt�||_dSr��rdr(r�)r%r$rrr� setPositionlszEncodingBytes.setPositioncCs*|jt|�krt�|jdkr"|jSdSdS)Nrr�r6rrr� getPositionqs  zEncodingBytes.getPositioncCs||j|jd�S�Nr )r$r6rrr�getCurrentByte{szEncodingBytes.getCurrentBytecCsH|j}|t|�kr>|||d�}||kr4||_|S|d7}q||_dS)zSkip past a list of charactersr N�r$r(rd�r%r�r�r|rrrry�s  zEncodingBytes.skipcCsH|j}|t|�kr>|||d�}||kr4||_|S|d7}q||_dSr�r�r�rrr� skipUntil�s  zEncodingBytes.skipUntilcCs>|j}|||t|��}|�|�}|r:|jt|�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$r(r�)r%r3r�r8r;rrr� matchBytes�s  zEncodingBytes.matchBytescCsR||jd��|�}|dkrJ|jdkr,d|_|j|t|�d7_dSt�dS)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 matchNrrr T)r$�findrdr(r�)r%r3Z newPositionrrr�jumpTo�s zEncodingBytes.jumpToN)r<r=r>r?r�r&r�r�r�r�r�r��propertyr$r�� currentByte�spaceCharactersBytesryr�r�r�rrrrr�Hs      r�c@sXeZdZdZdd�Zdd�Zdd�Zdd �Zd d �Zd d �Z dd�Z dd�Z dd�Z dS)r�z?Mini parser for detecting character encoding from meta elementscCst|�|_d|_dS)z3string - the data to work on for encoding detectionN)r�r8r��r%r8rrrr&�s zEncodingParser.__init__c Cs�d|jfd|jfd|jfd|jfd|jfd|jff}|jD]Z}d}|D]D\}}|j�|�rFz|�}Wq�WqFtk r�d}Yq�YqFXqF|s:q�q:|jS) Ns�r8r�r6rrrr��szEncodingParser.handleCommentcCs�|jjtkrdSd}d}|��}|dkr,dS|ddkr\|ddk}|r�|dk r�||_dSq|ddkr�|d}t|�}|dk r�||_dSq|ddkrtt|d��}|��}|dk rt|�}|dk r|r�||_dS|}qdS) NTFrs http-equivr s content-type�charsetscontent) r8r�r�� getAttributer�rS�ContentAttrParserr��parse)r%Z hasPragmaZpendingEncoding�attrZtentativeEncoding�codecZ contentParserrrrr��s8      zEncodingParser.handleMetacCs |�d�S)NF)�handlePossibleTagr6rrrr��sz%EncodingParser.handlePossibleStartTagcCst|j�|�d�S)NT)r�r8r�r6rrrr��s z#EncodingParser.handlePossibleEndTagcCsb|j}|jtkr(|r$|��|��dS|�t�}|dkrD|��n|��}|dk r^|��}qLdS)NTr)r8r��asciiLettersBytesr�r�r��spacesAngleBracketsr�)r%ZendTagr8r|r�rrrr��s    z EncodingParser.handlePossibleTagcCs |j�d�S)Nrr�r6rrrr�szEncodingParser.handleOthercCs�|j}|�ttdg�B�}|dks2t|�dks2t�|dkr>dSg}g}|dkrV|rVq�nX|tkrj|��}q�nD|dkr�d�|�dfS|tkr�|�|� ��n|dkr�dS|�|�t |�}qF|dkr�|� �d�|�dfSt |�|��}|dk�rJ|}t |�}||k�r"t |�d�|�d�|�fS|tk�r<|�|� ��q�|�|�q�nJ|d k�rbd�|�dfS|tk�r||�|� ��n|dk�r�dS|�|�t |�}|t k�r�d�|�d�|�fS|tk�r�|�|� ��n|dk�r�dS|�|��q�dS) z_Return a name,value pair for the next attribute in the stream, if one is found, or None�/Nr )rN�=)r�rr9)�'�"r) r8ryr�� frozensetr(r-r:�asciiUppercaseBytesr7r�r�r�r�)r%r8r|ZattrNameZ attrValueZ quoteCharrrrr�sb             zEncodingParser.getAttributeN) r<r=r>r?r&r�r�r�r�r�r�r�r�rrrrr��s$r�c@seZdZdd�Zdd�ZdS)r�cCst|t�st�||_dSr�)rBr3r-r8r�rrrr&fszContentAttrParser.__init__cCs�z�|j�d�|jjd7_|j��|jjdksr&r�rrrrr�esr�cCsft|t�r0z|�d�}Wntk r.YdSX|dk r^z t�|�WStk rZYdSXndSdS)z{Return the python codec name corresponding to an encoding or None if the string doesn't correspond to a valid encoding.rN)rBr�decode�UnicodeDecodeErrorr �lookup�AttributeError)r�rrrrS�s   rS)4Z __future__rrrZpip._vendor.sixrrZpip._vendor.six.movesrrr�rZ pip._vendorr Z constantsr r r rrrXr�iorrr�r�r�r�r�r�Zinvalid_unicode_no_surrogaterOrbr-r��evalrq�setrxZascii_punctuation_rer}�objectr!rJrFrGr3r�r�r�rSrrrr�s�     "��  � JgIh6'