3 \I @s4dddddddddd d d g Zd d lmZd dlmZed dZddZGdddZ d7ddZ ddZ GdddZ d dl Z e jdjfddZd8ddZddZd9d"dZd#d$Zd:d%dZd&d'Zd;d*d Zdefd+dZddefd,d-Zd.Zd/Zd0Zd1ZGd2d d eZ[ d3dZ d4d5Z!e"d6kr0e!dS)<get_close_matchesndiffrestoreSequenceMatcherDifferIS_CHARACTER_JUNK IS_LINE_JUNK context_diff unified_diff diff_bytesHtmlDiffMatch)nlargest) namedtupleza b sizecCs|rd||SdS)Ng@g?)matcheslengthrr/usr/lib64/python3.6/difflib.py_calculate_ratio&s rc@speZdZdddZddZdd Zd d Zd d ZddZddZ ddZ dddZ ddZ ddZ ddZdS)rNTcCs(||_d|_|_||_|j||dS)N)isjunkabautojunkset_seqs)selfrrrrrrr__init__s; zSequenceMatcher.__init__cCs|j||j|dS)N)set_seq1set_seq2)rrrrrrrs zSequenceMatcher.set_seqscCs$||jkrdS||_d|_|_dS)N)rmatching_blocksopcodes)rrrrrrs zSequenceMatcher.set_seq1cCs2||jkrdS||_d|_|_d|_|jdS)N)rrr fullbcount_SequenceMatcher__chain_b)rrrrrrs   zSequenceMatcher.set_seq2c Cs|j}i|_}x*t|D]\}}|j|g}|j|qWt|_}|j}|rx"|jD]}||r\|j |q\Wx|D] }||=q|Wt|_ }t |} |j r| dkr| dd} x*|j D]\}} t | | kr|j |qWx|D] }||=qWdS)Nd)rb2j enumerate setdefaultappendsetbjunkrkeysaddZbpopularlenritems) rrr&ieltindicesZjunkrZpopularnZntestZidxsrrrZ __chain_b)s,          zSequenceMatcher.__chain_bcCsN|j|j|j|jjf\}}}}||d} } } i} g} xt||D]}| j}i}xn|j||| D]Z}||krpqb||krzP||ddd}||<|| krb||d||d|} } } qbW|} qBWxb| |kr*| |kr*||| d r*|| d|| dkr*| d| d| d} } } qWxX| | |kr| | |kr||| |  r|| | || | kr| d7} q.Wxb| |kr| |kr||| dr|| d|| dkr| d| d| d} } } qWxV| | |kr@| | |kr@||| | r@|| | || | kr@| d} qWt| | | S)Nr r%)rrr&r+ __contains__rangegetr )raloahiblobhirrr&ZisbjunkZbestiZbestjZbestsizeZj2lenZnothingr0Zj2lengetZnewj2lenjkrrrfind_longest_matchPsB8$  "z"SequenceMatcher.find_longest_matchcCs|jdk r|jSt|jt|j}}d|d|fg}g}x|r|j\}}}}|j||||\} } } } | r:|j| || kr|| kr|j|| || f| | |kr:| | |kr:|j| | || | |fq:W|jd} }}g}x^|D]V\}}}| ||kr|||kr||7}q|r2|j| ||f|||} }}qW|r\|j| ||f|j||dftt t j ||_|jS)Nr ) rr.rrpopr=r)sortlistmapr _make)rlalbZqueuerr7r8r9r:r0r;r<xi1j1Zk1Z non_adjacenti2j2Zk2rrrget_matching_blockss8    z#SequenceMatcher.get_matching_blockscCs|jdk r|jSd}}g|_}x|jD]\}}}d}||krP||krPd}n||kr^d}n ||krjd}|r|j|||||f||||}}|r,|jd||||fq,W|S)Nr rreplacedeleteinsertequal)r rJr))rr0r;Zansweraibjsizetagrrr get_opcodess$  zSequenceMatcher.get_opcodesc csr|j}|sdg}|dddkrZ|d\}}}}}|t||||t||||f|d<|dddkr|d\}}}}}||t||||t|||f|d<||}g} x|D]\}}}}}|dko|||kr(| j||t||||t|||f| Vg} t|||t|||}}| j|||||fqW| rnt| dkob| dddk rn| VdS)NrNr r%)rNr r%r r%rUrU)rSmaxminr)r.) rr3ZcodesrRrFrHrGrIZnngrouprrrget_grouped_opcodes<s(&&((z#SequenceMatcher.get_grouped_opcodescCs0tdd|jD}t|t|jt|jS)Ncss|]}|dVqdS)r%NrUr).0Ztriplerrr sz(SequenceMatcher.ratio..)sumrJrr.rr)rrrrrrationszSequenceMatcher.ratiocCs|jdkr8i|_}x"|jD]}|j|dd||<qW|j}i}|jd}}xH|jD]>}||rl||}n |j|d}|d||<|dkrV|d}qVWt|t|jt|jS)Nr r%)r!rr6r4rrr.)rr!r1ZavailZavailhasrZnumbrrr quick_ratios         zSequenceMatcher.quick_ratiocCs*t|jt|j}}tt||||S)N)r.rrrrW)rrCrDrrrreal_quick_ratiosz SequenceMatcher.real_quick_ratio)NrrT)rT)__name__ __module__ __qualname__rrrrr"r=rJrSrYr]r^r_rrrrr+sl @ ,'nG7 2rT333333?cCs|dkstd|fd|ko(dkns 0: %rgg?z cutoff must be in [0.0, 1.0]: %rcSsg|] \}}|qSrr)rZZscorerErrr sz%get_close_matches..) ValueErrorrrrr_r^r]r) _nlargest)ZwordZ possibilitiesr3cutoffresultsrErrrrs       cCs4dt|}}x ||kr.|||kr.|d7}qW|S)Nr r%)r.)linechr0r3rrr_count_leadings  rlc@sFeZdZdddZddZddZdd Zd d Zd d ZddZ dS)rNcCs||_||_dS)N)linejunkcharjunk)rrmrnrrrrMszDiffer.__init__c cst|j||}x|jD]\}}}}}|dkrD|j||||||} n\|dkr^|jd|||} nB|dkrx|jd|||} n(|dkr|jd|||} ntd|f| EdHqWdS) NrKrL-rM+rN zunknown tag %r)rrmrS_fancy_replace_dumpre) rrrcruncherrRr7r8r9r:grrrcomparedszDiffer.compareccs*x$t||D]}d|||fVq WdS)Nz%s %s)r5)rrRrElohir0rrrrssz Differ._dumpc csr||||kr2|jd|||}|jd|||}n |jd|||}|jd|||}x||fD]} | EdHq\WdS)Nrpro)rs) rrr7r8rr9r:firstsecondrurrr_plain_replaceszDiffer._plain_replaceccsHd\}}t|j} d\} } xt||D]} || } | j| xxt||D]j}||}|| krp| dkrH|| } } qH| j|| j|krH| j|krH| j|krH| j|| }}}qHWq&W||kr| dkr|j||||||EdHdS| | d}}}nd} |j ||||||EdH||||}}| dkrd}}| j ||x| j D]\}}}}}||||}}|dkr|d|7}|d|7}nb|dkr|d|7}nJ|d kr|d |7}n2|d kr|d |7}|d |7}nt d |fqTW|j ||||EdHn d|V|j ||d|||d|EdHdS)NGz??g?rrK^rLrorMrprNrqzunknown tag %rz r%)r|r})NN)rrnr5rrr_r^r]r{ _fancy_helperrrSre_qformat)rrr7r8rr9r:Z best_ratiorgrtZeqiZeqjr;rPr0rOZbest_iZbest_jZaeltZbeltatagsbtagsrRZai1Zai2Zbj1Zbj2rCrDrrrrrsX                 zDiffer._fancy_replaceccsbg}||kr<||kr*|j||||||}qT|jd|||}n||krT|jd|||}|EdHdS)Nrorp)rrrs)rrr7r8rr9r:rurrrrszDiffer._fancy_helperccstt|dt|d}t|t|d|d}t|t|d|d}||dj}||dj}d|V|rdd||fVd|V|rdd||fVdS)N rqz- z? %s%s z+ )rWrlrstrip)rZalineZblinerrcommonrrrr s    zDiffer._qformat)NN) r`rarbrrvrsr{rrrrrrrrrs^ )^ Nz \s*(?:#\s*)?$cCs ||dk S)Nr)rjZpatrrrr>s cCs||kS)Nr)rkZwsrrrrNscCs:|d}||}|dkr"dj|S|s.|d8}dj||S)Nr%z{}z{},{})format)startstop beginningrrrr_format_range_unifiedes rr ccsht|||||||d}xHtd||j|D]0} |sd}|rJdj|nd} |r\dj|nd} dj|| |Vdj|| |V| d| d} } t| d| d }t| d | d }d j|||Vx| D]\}}}}}|d krx|||D]}d|VqWq|dkr2x |||D]}d|VqW|dkrx |||D]}d|VqHWqWq.WdS)NFTz {}rz --- {}{}{}z +++ {}{}{}r r%rTz@@ -{} +{} @@{}rNrqrKrLrorMrprU>rLrK>rMrK) _check_typesrrYrr)rrfromfiletofile fromfiledate tofiledater3linetermstartedrXfromdatetodaterylast file1_range file2_rangerRrFrHrGrIrjrrrr ps0)  cCsB|d}||}|s|d8}|dkr.dj|Sdj|||dS)Nr%z{}z{},{})r)rrrrrrr_format_range_contexts rccst|||||||tddddd}d} xztd||j|D]b} | sd} |rZdj|nd } |rldj|nd } d j|| |Vd j|| |V| d | d} }d|Vt| d |d}dj||Vtdd| Dr&xD| D]<\}}}}}|dkrx$|||D]}|||Vq WqWt| d|d}dj||Vtdd| Dr>xH| D]@\}}}}}|dkr^x$|||D]}|||VqWq^Wq>WdS)Nz+ z- z! z )rMrLrKrNFTz {}rz *** {}{}{}z --- {}{}{}r r%z***************rz *** {} ****{}css |]\}}}}}|dkVqdS)rKrLN>rLrKr)rZrR_rrrr[szcontext_diff..rMrTrz --- {} ----{}css |]\}}}}}|dkVqdS)rKrMN>rMrKr)rZrRrrrrr[ srLrU)rdictrrYrrany)rrrrrrr3rprefixrrXrrryrrrRrFrHrrjrrGrIrrrrs4,  cGs|r2t|dt r2tdt|dj|df|rdt|dt rdtdt|dj|dfx$|D]}t|tsjtd|fqjWdS)Nr z)lines to compare must be str, not %s (%r)z"all arguments must be str, not: %r) isinstancestr TypeErrortyper`)rrargsargrrrrs  r c csdd} tt| |}tt| |}| |}| |}| |}| |}| |}|||||||||} x| D]} | jddVqhWdS)NcSsPy |jddStk rJ}z"dt|j|f}t||WYdd}~XnXdS)Nasciisurrogateescapez(all arguments must be bytes, not %s (%r))decodeAttributeErrorrr`r)rierrmsgrrrr-s  zdiff_bytes..decoderr)r@rAencode) Zdfuncrrrrrrr3rrlinesrjrrrr "s  cCst||j||S)N)rrv)rrrmrnrrrr@s#c #sddl}|jdt||||ddgffdd fddfdd}|}|dkrj|EdHn.|d 7}d}xddg|} } d } xR| d kryt|\} } } Wntk rdSX| |}| | | f| |<| d 7} qW| |krd V|}n| }d} x.|r.| |}| d 7} | |V|d 8}qW|d }yDx>|rxt|\} } } | r`|d }n|d 8}| | | fVq._make_line..record_sub_inforq)r>subreversed) rZ format_keysideZ num_linestextZmarkersrrkeyZbeginend) change_rerr _make_lines   4z_mdiff.._make_linec3sg}d\}}xlx t|dkr0|jtdqWdjdd|D}|jdrX|}n|jdr|dd|dd d fVqn|jd r|d 8}|d ddd fVqnl|jdrވ|d dd}}|d d}}n>|jdr |dd|dd d fVqn|jdr8|dd|dd d fVqn|jd rd|d 8}|d ddd fVqn|jdr|d 7}d|dd d fVqn|jdrd|dd }}|d d}}n^|jdr|d 7}d|dd d fVqn2|jdr|dddd|dd dfVqx|dkr:|d 7}dVqWx|dkrZ|d 8}dVq>W|jdrldS||d fVqWdS) Nr rXrcSsg|] }|dqS)r r)rZrjrrrrdsz2_mdiff.._line_iterator..z-?+?rr%Tz--++ro--?+--+- z-+?z-?+z+--rp+ +-rqFr)r r )rrr)rrrr)NrTrr)rNT)r.r)nextjoin startswith)rZnum_blanks_pendingZnum_blanks_to_yieldri from_lineto_line)rdiff_lines_iteratorrr_line_iteratorsf           $     z_mdiff.._line_iteratorc 3s}gg}}xxpt|dks,t|dkryt|\}}}Wntk rRdSX|dk rj|j||f|dk r|j||fqW|jd\}}|jd\}}|||p|fVqWdS)Nr )r.r StopIterationr)r>)Z line_iterator fromlinestolinesrr found_diffZfromDiffZto_diff)rrr_line_pair_iterators  z#_mdiff.._line_pair_iteratorr%F)NNN)recompilerrr)rrcontextrmrnrrZline_pair_iteratorZlines_to_writeindexZ contextLinesrrrr0r)rrrrr_mdiffesT" 8X !     ran %(table)s%(legend)s aH table.diff {font-family:Courier; border:medium;} .diff_header {background-color:#e0e0e0} td.diff_header {text-align:right} .diff_next {background-color:#c0c0c0} .diff_add {background-color:#aaffaa} .diff_chg {background-color:#ffff77} .diff_sub {background-color:#ffaaaa}aZ %(header_row)s %(data_rows)s
a
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op
c@seZdZeZeZeZeZdZdddefddZ dd d d d Z d dZ ddZ ddZ ddZddZddZddZdddZdS)r r NcCs||_||_||_||_dS)N)_tabsize _wrapcolumn _linejunk _charjunk)rtabsizeZ wrapcolumnrmrnrrrrs zHtmlDiff.__init__rFzutf-8)charsetc Cs:|jt|j|j|j||||||d|dj|dj|S)N)rnumlines)ZstylesZlegendtablerxmlcharrefreplace)_file_templater_styles_legend make_tablerr)rrrfromdesctodescrrrrrr make_files  zHtmlDiff.make_filecs8fddfdd|D}fdd|D}||fS)Ncs6|jdd}|jj}|jdd}|jddjdS)Nrqrrr)rK expandtabsrr)rj)rrr expand_tabss   z2HtmlDiff._tab_newline_replace..expand_tabscsg|] }|qSrr)rZrj)rrrrdsz1HtmlDiff._tab_newline_replace..csg|] }|qSrr)rZrj)rrrrdsr)rrrr)rrr_tab_newline_replaces zHtmlDiff._tab_newline_replacec Cs|s|j||fdSt|}|j}||ksB||jdd|krT|j||fdSd}d}d}xd||kr||kr||dkr|d7}||}|d7}qb||dkr|d7}d}qb|d7}|d7}qbW|d|} ||d} |r| d} d|| } |j|| f|j|d| dS)NrrTr rr%r>)r)r.rcount _split_line) rZ data_listZline_numrrQrVr0r3ZmarkZline1Zline2rrrrs8        zHtmlDiff._split_linec csx|D]\}}}|dkr&|||fVq||\}}\}}gg} } |j| |||j| ||x@| sh| r| rx| jd}nd}| r| jd}nd}|||fVq`WqWdS)Nr rrq)rrq)rrq)rr>) rdiffsfromdatatodataflagZfromlineZfromtextZtolineZtotextfromlisttolistrrr _line_wrapper;s      zHtmlDiff._line_wrapperc Csggg}}}xz|D]r\}}}y4|j|jd|f||j|jd|f|Wn(tk r||jd|jdYnX|j|qW|||fS)Nr r%)r) _format_liner)rrrrflaglistrrrrrr_collect_linesWs zHtmlDiff._collect_linesc Csryd|}d|j||f}Wntk r6d}YnX|jddjddjdd }|jd d j}d |||fS) Nz%dz id="%s%s"r&z&rz>%s%s)_prefixrrKr)rrrZlinenumridrrrrls zHtmlDiff._format_linecCs0dtj}dtj}tjd7_||g|_dS)Nzfrom%d_zto%d_r%)r _default_prefixr)rZ fromprefixtoprefixrrr _make_prefixs  zHtmlDiff._make_prefixcCs|jd}dgt|}dgt|}d \} } d} xbt|D]V\} } | r| sd} | } td| |g} d|| f|| <| d7} d|| f|| <qnz2 No Differences Found z( Empty File z!fz#t)r F)rr.r'rV)rrrrrrrnext_id next_hrefZnum_chgZ in_changerr0rrrr_convert_flagss:    zHtmlDiff._convert_flagsc CsV|j|j||\}}|r"|}nd}t||||j|jd}|jrL|j|}|j|\} } } |j| | | ||\} } } } } g}d}x`t t | D]P}| |dkr|dkr|j dq|j || || || || || |fqW|s|rddd|dd|f}nd }|j t d j|||jd d }|jd d jddjddjddjddS)N)rmrnz1 %s%sz%%s%s r z) z %s%s%s%sz!
z+%srr%)Z data_rows header_rowrz+zz-zz^zrzrz zV %s%s%s%s )rrrrrrrrrr5r.r)_table_templaterrrrK)rrrrrrrZ context_linesrrrrrrriZfmtr0rrrrrrsJ       zHtmlDiff.make_table)rrFr)rrFr)r`rarbrrrrrrrrrrrrrrrrrrrrr s$ 7 /c cspydddt|}Wn tk r6td|YnXd|f}x*|D]"}|dd|krF|ddVqFWdS)Nz- z+ )r%rz)unknown delta choice (must be 1 or 2): %rz r)intKeyErrorre)ZdeltaZwhichrRprefixesrjrrrr s cCsddl}ddl}|j|S)Nr )doctestdifflibZtestmod)rr rrr_test,sr __main__)rTrc)r)rrrrrTr)rrrrrTr)rrrrrTr)#__all__heapqrrf collectionsrZ _namedtupler rrrrlrrrmatchrrrr rrrr rrrrrrobjectr rr r`rrrrs\     0O   H  K % !  a