3 Bu_a~ @sddlmZmZmZddlmZddlmZmZddl Z ddl Z ddl m Z m Z ddlmZddlmZmZmZmZdd lmZdd lmZed d eDZed d eDZedd eDZeeddgBZdZejredEdkrejddkst e j!eddFe"ddZ#n e j!eZ#ddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3h Z$e j!d4Z%iZ&Gd5d6d6e'Z(d7d8Z)Gd9d:d:e'Z*Gd;d<dd>e,Z-Gd?d@d@e'Z.GdAdBdBe'Z/dCdDZ0dS)G)absolute_importdivisionunicode_literals) text_type) http_clienturllibN)BytesIOStringIO) webencodings)EOFspaceCharacters asciiLettersasciiUppercase)_ReparseException)_utilscCsg|]}|jdqS)ascii)encode).0itemr/builddir/build/BUILDROOT/alt-python36-pip-20.2.4-1.el7.x86_64/opt/alt/python36/lib/python3.6/site-packages/pip/_vendor/html5lib/_inputstream.py srcCsg|]}|jdqS)r)r)rrrrrrscCsg|]}|jdqS)r)r)rrrrrrs>.)sumr)r!rrrr'XszBufferedStream._bufferedBytescCs<|jj|}|jj||jdd7<t||jd<|S)Nrr )rr/rappendr r#)r!r.datarrrr,[s   zBufferedStream._readStreamcCs|}g}|jd}|jd}x|t|jkr|dkr|dks@t|j|}|t||krn|}|||g|_n"t||}|t|g|_|d7}|j||||||8}d}qW|r|j|j|dj|S)Nrr )r r#rr(r1r,join)r!r.remainingBytesrv bufferIndex bufferOffset bufferedData bytesToReadrrrr-bs&     zBufferedStream._readFromBufferN) __name__ __module__ __qualname____doc__r"r&r+r/r'r,r-rrrrr3s  rcKst|tjs(t|tjjr.t|jtjr.d}n&t|drJt|jdt }n t|t }|rdd|D}|rvt d|t |f|St |f|SdS)NFr/rcSsg|]}|jdr|qS) _encoding)endswith)rxrrrrsz#HTMLInputStream..z3Cannot set an encoding with a unicode input, set %r) isinstancer HTTPResponserresponseaddbasefphasattrr/r TypeErrorHTMLUnicodeInputStreamHTMLBinaryInputStream)sourcekwargs isUnicode encodingsrrrHTMLInputStream}s     rOc@speZdZdZdZddZddZddZd d Zd d Z d dZ dddZ ddZ ddZ dddZddZdS)rIzProvides 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|_ntddkr$|j|_n|j|_dg|_tddf|_|j ||_ |j dS)aInitialises 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 rzutf-8certain) rsupports_lone_surrogatesreportCharacterErrorsr#characterErrorsUCS4characterErrorsUCS2newLineslookupEncoding charEncoding openStream dataStreamreset)r!rKrrrr"s   zHTMLUnicodeInputStream.__init__cCs.d|_d|_d|_g|_d|_d|_d|_dS)Nr)r% chunkSize chunkOffseterrors prevNumLines prevNumCols_bufferedCharacter)r!rrrrZszHTMLUnicodeInputStream.resetcCst|dr|}nt|}|S)zvProduces a file object from source. source can be either a file object, local filename or a string. r/)rGr )r!rKrrrrrXs z!HTMLUnicodeInputStream.openStreamcCsT|j}|jdd|}|j|}|jdd|}|dkr@|j|}n ||d}||fS)N rr r)r%countr_rfindr`)r!r)r%nLines positionLine lastLinePospositionColumnrrr _positions   z HTMLUnicodeInputStream._positioncCs|j|j\}}|d|fS)z:Returns (line, col) of the current position in the stream.r )rir])r!linecolrrrr szHTMLUnicodeInputStream.positioncCs6|j|jkr|jstS|j}|j|}|d|_|S)zo Read one character from the stream or queue if available. Return EOF when EOF is reached. r )r]r\ readChunkr r%)r!r]charrrrrms   zHTMLUnicodeInputStream.charNcCs|dkr|j}|j|j\|_|_d|_d|_d|_|jj|}|j rX|j |}d|_ n|s`dSt |dkrt |d }|dksd|kodknr|d |_ |dd}|j r|j ||j dd }|j d d }||_t ||_d S)Nr[rFr iiz rb Trrr)_defaultChunkSizerir\r_r`r%r]rYr/rar#ordrRreplace)r!r\r2lastvrrrrls0           z HTMLUnicodeInputStream.readChunkcCs,x&tttj|D]}|jjdqWdS)Nzinvalid-codepoint)ranger#invalid_unicode_refindallr^r1)r!r2_rrrrSsz*HTMLUnicodeInputStream.characterErrorsUCS4cCsd}xtj|D]}|rqt|j}|j}tj|||drttj|||d}|tkrn|j j dd}q|dkr|dkr|t |dkr|j j dqd}|j j dqWdS)NFzinvalid-codepointTiir ) rufinditerrqgroupstartrisSurrogatePairsurrogatePairToCodepointnon_bmp_invalid_codepointsr^r1r#)r!r2skipmatch codepointr$char_valrrrrT#s   z*HTMLUnicodeInputStream.characterErrorsUCS2Fc Csyt||f}Wnltk r|x|D]}t|dks&tq&Wdjdd|D}|s^d|}tjd|}t||f<YnXg}x||j|j|j }|dkr|j |j krPn0|j }||j kr|j |j|j |||_ P|j |j|j d|j sPqWdj|} | 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. r[cSsg|]}dt|qS)z\x%02x)rq)rcrrrrHsz5HTMLUnicodeInputStream.charsUntil..z^%sz[%s]+N)charsUntilRegExKeyErrorrqr(r4recompilerr%r]r\endr1rl) r! charactersoppositecharsrregexr6mrrrrr charsUntil:s2     z!HTMLUnicodeInputStream.charsUntilcCsT|tk rP|jdkr.||j|_|jd7_n"|jd8_|j|j|ksPtdS)Nrr )r r]r%r\r()r!rmrrrungetis   zHTMLUnicodeInputStream.unget)N)F)r;r<r=r>rpr"rZrXrir rmrlrSrTrrrrrrrIs   & /rIc@sLeZdZdZdddZddZd d Zdd d Zd dZddZ ddZ dS)rJzProvides 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. N windows-1252TcCsn|j||_tj||jd|_d|_||_||_||_||_ ||_ |j ||_ |j ddk sbt |jdS)aInitialises 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) idrN)rX rawStreamrIr" numBytesMetanumBytesChardetoverride_encodingtransport_encodingsame_origin_parent_encodinglikely_encodingdefault_encodingdetermineEncodingrWr(rZ)r!rKrrrrr useChardetrrrr"s  zHTMLBinaryInputStream.__init__cCs&|jdjj|jd|_tj|dS)Nrrr)rW codec_info streamreaderrrYrIrZ)r!rrrrZszHTMLBinaryInputStream.resetc CsLt|dr|}nt|}y|j|jWntk rFt|}YnX|S)zvProduces a file object from source. source can be either a file object, local filename or a string. r/)rGrr+r& Exceptionr)r!rKrrrrrXs z HTMLBinaryInputStream.openStreamc Cs|jdf}|ddk r|St|jdf}|ddk r:|St|jdf}|ddk rX|S|jdf}|ddk rt|St|jdf}|ddk r|djjd r|St|jdf}|ddk r|S|rtyddl m }Wnt k rYnXg}|}xF|j s>|j j|j}t|ts t|s(P|j||j|qW|jt|jd}|j jd|dk rt|dfSt|jdf}|ddk r|StddfS)NrPr tentativezutf-16)UniversalDetectorencodingz windows-1252) detectBOMrVrrdetectEncodingMetarname startswithr%pip._vendor.chardet.universaldetectorr ImportErrordonerr/rrBr.r(r1feedcloseresultr+r)r!chardetrWrbuffersdetectorrrrrrrsR           z'HTMLBinaryInputStream.determineEncodingcCs|jddkstt|}|dkr&dS|jdkrFtd}|dk stnT||jdkrf|jddf|_n4|jjd|df|_|jtd|jd|fdS) Nr rPutf-16beutf-16lezutf-8rzEncoding changed from %s to %s)rr)rWr(rVrrr+rZr)r! newEncodingrrrchangeEncodings   z$HTMLBinaryInputStream.changeEncodingc Cstjdtjdtjdtjdtjdi}|jjd}t|t sr"rZrXrrrrrrrrrJzs ( >"rJc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ e e e Z ddZe eZefddZddZddZddZdS) 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 raisedcCst|tsttj||jS)N)rBr.r(__new__lower)r!valuerrrrFszEncodingBytes.__new__cCs d|_dS)Nr r)ri)r!rrrrr"JszEncodingBytes.__init__cCs|S)Nr)r!rrr__iter__NszEncodingBytes.__iter__cCs>|jd}|_|t|kr"tn |dkr.t|||dS)Nr r)rir# StopIterationrH)r!prrr__next__Qs  zEncodingBytes.__next__cCs|jS)N)r)r!rrrnextYszEncodingBytes.nextcCsB|j}|t|krtn |dkr$t|d|_}|||dS)Nrr )rir#rrH)r!rrrrprevious]s zEncodingBytes.previouscCs|jt|krt||_dS)N)rir#r)r!r rrr setPositionfszEncodingBytes.setPositioncCs*|jt|krt|jdkr"|jSdSdS)Nr)rir#r)r!rrr getPositionks  zEncodingBytes.getPositioncCs||j|jdS)Nr )r )r!rrrgetCurrentByteuszEncodingBytes.getCurrentBytecCsL|j}x:|t|kr@|||d}||kr6||_|S|d7}qW||_dS)zSkip past a list of charactersr N)r r#ri)r!rrrrrrrzs zEncodingBytes.skipcCsL|j}x:|t|kr@|||d}||kr6||_|S|d7}qW||_dS)Nr )r r#ri)r!rrrrrr skipUntils zEncodingBytes.skipUntilcCs(|j||j}|r$|jt|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)rr r#)r!r.r6rrr matchBytesszEncodingBytes.matchBytesc Cs>y |j||jt|d|_Wntk r8tYnXdS)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#ri ValueErrorr)r!r.rrrjumpTos   zEncodingBytes.jumpToN)r;r<r=r>rr"rrrrrrpropertyr r currentBytespaceCharactersBytesrrrrrrrrrBs      rc@sXeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS)rz?Mini parser for detecting character encoding from meta elementscCst||_d|_dS)z3string - the data to work on for encoding detectionN)rr2r)r!r2rrrr"s zEncodingParser.__init__cCsd|jkrdSd|jfd|jfd|jfd|jfd|jfd|jff}x|jD]|}d}y|jjdWntk rxPYnXxD|D]<\}}|jj|ry |}PWqtk rd}PYqXqW|sJPqJW|j S) Ns)r2r)r!rrrrszEncodingParser.handleCommentcCs|jjtkrdSd}d}x|j}|dkr.dS|ddkr^|ddk}|r|dk r||_dSq|ddkr|d}t|}|dk r||_dSq|ddkrtt|d}|j}|dk rt|}|dk r|r||_dS|}qWdS) NTFrs http-equivr s content-typescharsetscontent) r2rr getAttributerrVContentAttrParserrparse)r! hasPragmapendingEncodingattrtentativeEncodingcodec contentParserrrrrs:      zEncodingParser.handleMetacCs |jdS)NF)handlePossibleTag)r!rrrrsz%EncodingParser.handlePossibleStartTagcCst|j|jdS)NT)rr2r)r!rrrrs z#EncodingParser.handlePossibleEndTagcCsf|j}|jtkr(|r$|j|jdS|jt}|dkrD|jn|j}x|dk r`|j}qNWdS)NTr)r2rasciiLettersBytesrrrspacesAngleBracketsr)r!endTagr2rrrrrrs     z EncodingParser.handlePossibleTagcCs |jjdS)Nr)r2r)r!rrrrszEncodingParser.handleOthercCs|j}|jttdgB}|dks2t|dks2t|d kr>dSg}g}xt|dkrX|rXPnX|tkrl|j}PnD|d krdj|dfS|tkr|j|j n|dkrdS|j|t |}qHW|dkr|j dj|dfSt ||j}|d krT|}xt |}||kr(t |dj|dj|fS|tkrB|j|j n |j|qWnJ|dkrldj|dfS|tkr|j|j n|dkrdS|j|x^t |}|t krdj|dj|fS|tkr|j|j n|dkrdS|j|qWdS) z_Return a name,value pair for the next attribute in the stream, if one is found, or None/Nr r=r3'")rN)rr)rr) r2rr frozensetr#r(r4asciiUppercaseBytesr1rrrr)r!r2rattrName attrValue quoteCharrrrrsh             zEncodingParser.getAttributeN) r;r<r=r>r"rrrrrrrrrrrrrs$rc@seZdZddZddZdS)rcCst|tst||_dS)N)rBr.r(r2)r!r2rrrr"aszContentAttrParser.__init__cCsy|jjd|jjd7_|jj|jjdks8dS|jjd7_|jj|jjdkr|jj}|jjd7_|jj}|jj|r|j||jjSdSnF|jj}y|jjt|j||jjStk r|j|dSXWntk rdSXdS)Nscharsetr rrr)rr)r2rr rrrrr)r! quoteMark oldPositionrrrres.       zContentAttrParser.parseN)r;r<r=r"rrrrrr`srcCs`t|tr.y|jd}Wntk r,dSX|dk rXy tj|Stk rTdSXndSdS)z{Return the python codec name corresponding to an encoding or None if the string doesn't correspond to a valid encoding.rN)rBr.decodeUnicodeDecodeErrorr lookupAttributeError)rrrrrVs  rVrr)1 __future__rrrZpip._vendor.sixrpip._vendor.six.movesrrrriorr pip._vendorr constantsr r rrrr[rrrrrrinvalid_unicode_no_surrogaterQrcr(revalrur~ascii_punctuation_rerobjectrrOrIrJr.rrrrVrrrrsP             JgIb='