U ÀÒ.e(ã!@süddlmZmZmZddlmZmZddlmZm Z ddl Z ddl Z ddl m Z ddlmZmZmZmZddlmZdd lmZdd lmZzdd lmZWnek r´eZYnXed d „eDƒƒZedd „eDƒƒZedd „eDƒƒZeeddgƒBZdZej �r(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-ƒZ.GdAdB„dBe(ƒZ/GdCdD„dDe(ƒZ0dEdF„Z1dS)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%rrrÚ_bufferedBytes^szBufferedStream._bufferedBytescCs<|j |¡}|j |¡|jdd7<t|ƒ|jd<|Sr')r"r2r#Úappendr$r()r%r1Údatarrrr/as   zBufferedStream._readStreamcCsÄ|}g}|jd}|jd}|t|jƒkr¦|dkr¦|j|}|t|ƒ|kr`|}|||g|_n"t|ƒ|}|t|ƒg|_|d7}| ||||…¡||8}d}q|rº| | |¡¡d |¡S)Nrr ó)r$r(r#r6r/Újoin)r%r1ZremainingBytesÚrvZ bufferIndexZ bufferOffsetZ bufferedDataZ bytesToReadrrrr0hs$    zBufferedStream._readFromBufferN) Ú__name__Ú __module__Ú __qualname__Ú__doc__r&r+r.r2r5r/r0rrrrr!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)NFr2rcSsg|]}| d¡r|‘qS)Z _encoding)Úendswith)rÚxrrrr‘s z#HTMLInputStream..z3Cannot set an encoding with a unicode input, set %r) Ú isinstancerZ HTTPResponserZresponseZaddbaseÚfpÚhasattrr2rÚ TypeErrorÚHTMLUnicodeInputStreamÚHTMLBinaryInputStream)ÚsourceÚkwargsZ isUnicodeZ encodingsrrrÚHTMLInputStreamƒs  þ ý    rIc@speZdZdZdZdd„Zdd„Zdd„Zd d „Zd d „Z d d„Z ddd„Z dd„Z dd„Z ddd„Zdd„ZdS)rEú×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%rGrrrr&¤s   zHTMLUnicodeInputStream.__init__cCs.d|_d|_d|_g|_d|_d|_d|_dS)NÚr)r*Ú chunkSizeÚ chunkOffsetÚerrorsÚ prevNumLinesÚ prevNumColsÚ_bufferedCharacterr4rrrrVÄszHTMLUnicodeInputStream.resetcCst|dƒr|}nt|ƒ}|S©zvProduces a file object from source. source can be either a file object, local filename or a string. r2)rCr©r%rGr"rrrrTÒ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 )rcrY)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 )rYrXÚ readChunkr r*)r%rYÚcharrrrrgð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) NrWrFr ré éØiÿÛz r`ú T)Ú_defaultChunkSizercrXr[r\r*rYrUr2r]r(ÚordrOÚreplace)r%rXr7Zlastvrrrrfÿs0           z HTMLUnicodeInputStream.readChunkcCs(ttt |¡ƒƒD]}|j d¡qdS)Núinvalid-codepoint)Úranger(Úinvalid_unicode_reÚfindallrZr6)r%r7Ú_rrrrP%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érnTriiÿßr ) rpÚfinditerrlÚgroupÚstartrZisSurrogatePairZsurrogatePairToCodepointÚnon_bmp_invalid_codepointsrZr6r()r%r7ÚskipÚmatchZ codepointr)Zchar_valrrrrQ)s"  ÿz*HTMLUnicodeInputStream.characterErrorsUCS2Fc Csìzt||f}WnNtk r^d dd„|Dƒ¡}|s@d|}t d|¡}t||f<YnXg}| |j|j¡}|dkrŒ|j|jkr¼qÞn0|  ¡}||jkr¼|  |j|j|…¡||_qÞ|  |j|jd…¡|  ¡sdqÞqdd |¡}|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. rWcSsg|]}dt|ƒ‘qS)z\x%02x)rl)rÚcrrrrNsz5HTMLUnicodeInputStream.charsUntil..z^%sz[%s]+N) ÚcharsUntilRegExÚKeyErrorr9ÚreÚcompileryr*rYrXÚendr6rf) r%Z charactersZoppositeÚcharsZregexr:ÚmrÚrrrrÚ charsUntil@s,    z!HTMLUnicodeInputStream.charsUntilcCs@|dk r<|jdkr.||j|_|jd7_n|jd8_dSr')rYr*rX)r%rgrrrÚungetos   zHTMLUnicodeInputStream.unget)N)F)r;r<r=r>rkr&rVrTrcr$rgrfrPrQrƒr„rrrrrEšs   & /rEc@sLeZdZdZddd„Zdd„Zd d „Zdd d „Zd d„Zdd„Z dd„Z dS)rFrJNú windows-1252TcCs\| |¡|_t ||j¡d|_d|_||_||_||_||_ ||_ |  |¡|_ |  ¡dS)rKiédN)rTÚ rawStreamrEr&Ú numBytesMetaÚnumBytesChardetÚoverride_encodingÚtransport_encodingÚsame_origin_parent_encodingÚlikely_encodingÚdefault_encodingÚdetermineEncodingrSrV)r%rGrŠr‹rŒr�rŽZ useChardetrrrr&ˆs  zHTMLBinaryInputStream.__init__cCs&|jdj |jd¡|_t |¡dS)Nrrm)rSZ codec_infoÚ streamreaderr‡rUrErVr4rrrrV²szHTMLBinaryInputStream.resetcCsDt|dƒr|}nt|ƒ}z| | ¡¡Wnt|ƒ}YnX|Sr^)rCrr.r+r!r_rrrrT¶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|�r`zddl m }Wnt k rêYnvXg}|ƒ}|j �s,|j  |j¡}|�s�q,| |¡| |¡qö| ¡t|jdƒ}|j  d¡|dk �r`|dfSt|jƒdf}|ddk �r€|StdƒdfS)NrMrZ tentativezutf-16)ÚUniversalDetectorÚencodingr…)Ú detectBOMrRrŠr‹ÚdetectEncodingMetarŒÚnameÚ startswithr�Z%pip._vendor.chardet.universaldetectorr‘Ú ImportErrorZdoner‡r2r‰r6ZfeedÚcloseÚresultr.rŽ)r%ZchardetrSr‘ZbuffersZdetectorr#r’rrrr�ÉsP           z'HTMLBinaryInputStream.determineEncodingcCs€t|ƒ}|dkrdS|jdkr(tdƒ}nT||jdkrH|jddf|_n4|j d¡|df|_| ¡td|jd|fƒ‚dS)N©úutf-16beúutf-16lerLrrMzEncoding changed from %s to %s)rRr•rSr‡r.rVr)r%Z newEncodingrrrÚchangeEncodings   z$HTMLBinaryInputStream.changeEncodingc Csœtjdtjdtjdtjdtjdi}|j d¡}| |dd…¡}d}|sp| |¡}d}|sp| |dd …¡}d }|rˆ|j  |¡t |ƒS|j  d ¡dSdS) z©Attempts 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 NonerLrœr›zutf-32lezutf-32beéNérsr) ÚcodecsÚBOM_UTF8Ú BOM_UTF16_LEÚ BOM_UTF16_BEÚ BOM_UTF32_LEÚ BOM_UTF32_BEr‡r2Úgetr.rR)r%ZbomDictÚstringr’r.rrrr“s2ý    zHTMLBinaryInputStream.detectBOMcCsH|j |j¡}t|ƒ}|j d¡| ¡}|dk rD|jdkrDtdƒ}|S)z9Report the encoding declared by the meta element rNršrL)r‡r2rˆÚEncodingParserr.Ú getEncodingr•rR)r%r#Úparserr’rrrr”9s z(HTMLBinaryInputStream.detectEncodingMeta)NNNNr…T)T) r;r<r=r>r&rVrTr�r�r“r”rrrrrF€sþ * >"rFc@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 || ¡¡S©N)r1Ú__new__Úlower©r%Úvaluerrrr­LszEncodingBytes.__new__cCs d|_dS)Nr)rcr¯rrrr&PszEncodingBytes.__init__cCs|Sr¬rr4rrrÚ__iter__TszEncodingBytes.__iter__cCs>|jd}|_|t|ƒkr"t‚n |dkr.t‚|||d…S)Nr r©rcr(Ú StopIterationrD©r%ÚprrrÚ__next__Ws  zEncodingBytes.__next__cCs| ¡Sr¬)r¶r4rrrÚnext_szEncodingBytes.nextcCsB|j}|t|ƒkrt‚n |dkr$t‚|d|_}|||d…Sr'r²r´rrrÚpreviouscs zEncodingBytes.previouscCs|jt|ƒkrt‚||_dSr¬©rcr(r³)r%r$rrrÚ setPositionlszEncodingBytes.setPositioncCs*|jt|ƒkrt‚|jdkr"|jSdSdS)Nrr¹r4rrrÚ getPositionqs  zEncodingBytes.getPositioncCs||j|jd…S©Nr )r$r4rrrÚgetCurrentByte{szEncodingBytes.getCurrentBytecCsH|j}|t|ƒkr>|||d…}||kr4||_|S|d7}q||_dS)zSkip past a list of charactersr N©r$r(rc©r%r€rµrzrrrrx€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%r1rµr7r: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$Úfindrcr(r³)r%r1Z newPositionrrrÚjumpTo¢s zEncodingBytes.jumpToN)r;r<r=r>r­r&r±r¶r·r¸rºr»Úpropertyr$r½Ú currentByteÚspaceCharactersBytesrxrÀ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«r7r’©r%r7rrrr&³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©r7rÃr4rrrrÈÏ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) r7rÅrÆÚ getAttributer’rRÚContentAttrParserr«Úparse)r%Z hasPragmaZpendingEncodingÚattrZtentativeEncodingÚcodecZ contentParserrrrrÉÓs8      zEncodingParser.handleMetacCs | d¡S)NF)ÚhandlePossibleTagr4rrrrÌ÷sz%EncodingParser.handlePossibleStartTagcCst|jƒ| d¡S)NT)r·r7rÖr4rrrrÊús z#EncodingParser.handlePossibleEndTagcCsb|j}|jtkr(|r$| ¡| ¡dS| t¡}|dkrD| ¡n| ¡}|dk r^| ¡}qLdS)NTr)r7rÅÚasciiLettersBytesr¸rËrÀÚspacesAngleBracketsrÑ)r%ZendTagr7rzrÔrrrrÖþs    z EncodingParser.handlePossibleTagcCs |j d¡S)NrrÏr4rrrrËszEncodingParser.handleOthercCsÜ|j}| ttdgƒB¡}|dkr&dSg}g}|dkr>|r>q nX|tkrR| ¡}q nD|dkrhd |¡dfS|tkr€| | ¡¡n|dkrŒdS| |¡t|ƒ}q.|dkr¾|  ¡d |¡dfSt|ƒ| ¡}|dk�r2|}t|ƒ}||k�r t|ƒd |¡d |¡fS|tk�r$| | ¡¡qÜ| |¡qÜnJ|dk�rJd |¡dfS|tk�rd| | ¡¡n|dk�rrdS| |¡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ó/)rNNó=)rÙrr8)ó'ó"r) r7rxrÆÚ frozensetr9ÚasciiUppercaseBytesr6r®r·r¸rØ)r%r7rzZattrNameZ attrValueZ quoteCharrrrrÑs`             zEncodingParser.getAttributeN) r;r<r=r>r&r©rÈrÉrÌrÊrÖrËrÑrrrrr¨°s$r¨c@seZdZdd„Zdd„ZdS)rÒcCs ||_dSr¬)r7rÇrrrr&fszContentAttrParser.__init__cCs�zü|j d¡|jjd7_|j ¡|jjdkss†     ÿþ  ú JgIh6'