o GMZ-@@s&dZddlmZddlZddlZddlmZmZmZm Z m Z m Z m Z m Z mZmZmZmZmZddlmZejddkZerOddlmZejZdd Zd d ZnddlmZeZeZd d ZeZeZeGd dde Z!eGddde"Z#eGddde"Z$Gddde"Z%eGddde"Z&dS)z>Classes used by the unified diff parser to keep the diff data.)unicode_literalsN) DEFAULT_ENCODINGLINE_TYPE_ADDEDLINE_TYPE_CONTEXTLINE_TYPE_EMPTYLINE_TYPE_REMOVEDLINE_TYPE_NO_NEWLINELINE_VALUE_NO_NEWLINERE_HUNK_BODY_LINERE_HUNK_EMPTY_BODY_LINERE_HUNK_HEADERRE_SOURCE_FILENAMERE_TARGET_FILENAMERE_NO_NEWLINE_MARKER)UnidiffParseError)StringIOcCs |tSN)encoderxr//usr/lib/python3/dist-packages/unidiff/patch.py6s rcCs|j|_dd|_|S)NcSs|tSr) __unicode__rrrrrrr:sz&implements_to_string..)__str__r)clsrrrimplements_to_string8s rcCs|Srrrrrrr@scs`eZdZdZ dfdd ZddZddZd d Zed d Z ed dZ eddZ Z S)Linez A diff line.Ncs0tt|||_||_||_||_||_dSr)superr__init__source_line_notarget_line_no diff_line_no line_typevalue)selfr%r$r!r"r# __class__rrr I  z Line.__init__cCstd|j|jfS)Nz )make_strr$r%r&rrr__repr__Rz Line.__repr__cCsd|j|jfS)Nz%s%s)r$r%r+rrrrUsz Line.__str__cCs<|j|jko|j|jko|j|jko|j|jko|j|jkSr)r!r"r#r$r%)r&otherrrr__eq__Xs     z Line.__eq__cC |jtkSr)r$rr+rrris_added_ z Line.is_addedcCr0r)r$rr+rrr is_removedcr2zLine.is_removedcCr0r)r$rr+rrr is_contextgr2zLine.is_context)NNN) __name__ __module__ __qualname____doc__r r,rr/propertyr1r3r4 __classcell__rrr'rrEs   rc@s eZdZdZddZddZdS) PatchInfozLines with extended patch info. Format of this info is not documented and it very much depends on patch producer. cCsd|d}t|S)Nzr)stripr*r&r%rrrr,uszPatchInfo.__repr__cCddd|DS)Ncs|]}t|VqdSrunicode.0linerrr zz$PatchInfo.__str__..joinr+rrrryr-zPatchInfo.__str__N)r5r6r7r8r,rrrrrr;ls r;csVeZdZdZ  dddZddZdd Zfd d Zd d ZddZ ddZ Z S)Hunkz&Each of the modified blocks of a file.rr?cCsb|durd}|dur d}d|_d|_g|_t||_t||_g|_t||_t||_||_ dS)Nr) addedremovedsourceint source_start source_lengthtarget target_start target_lengthsection_header)r& src_startsrc_len tgt_starttgt_lenrUrrrr s     z Hunk.__init__cCs$d|j|j|j|j|jf}t|S)Nz)rPrQrSrTrUr*r=rrrr,sz Hunk.__repr__cCsFd|j|j|j|j|jrd|jndf}ddd|D}||S)Nz@@ -%d,%d +%d,%d @@%s  r?csr@rrArCrrrrFrGzHunk.__str__..)rPrQrSrTrUrI)r&headcontentrrrrsz Hunk.__str__cstt||t|}|jr|jd7_|j|dS|jr0|jd7_|j |dS|j rA|j||j |dSdS)z?Append the line to hunk, and keep track of source/target lines.rKN) rrJappendstrr1rLrRr3rMrNr4)r&rEsr'rrr]s z Hunk.appendcCs t|j|jkot|j|jkS)z2Check hunk header data matches entered lines info.)lenrNrQrRrTr+rrris_validsz Hunk.is_validcCdd|DS)z(Hunk lines from source file (generator).cs |] }|js |jr|VqdSr)r4r3rDlrrrrFz$Hunk.source_lines..rr+rrr source_lineszHunk.source_linescCrb)z(Hunk lines from target file (generator).csrcr)r4r1rdrrrrFrfz$Hunk.target_lines..rr+rrr target_linesrhzHunk.target_lines)rrrrr?) r5r6r7r8r r,rr]rargrir:rrr'rrJ}s  rJcseZdZdZ  dfdd ZddZdd Zd d Zd d ZddZ e ddZ e ddZ e ddZ e ddZe ddZe ddZZS) PatchedFilez*Patch updated file, it is a list of Hunks.Nr?cs0tt|||_||_||_||_||_dSr)rrjr patch_info source_filesource_timestamp target_filetarget_timestamp)r&rkrNrRrmror'rrr r)zPatchedFile.__init__cCstdt|jS)Nz)r*pathr+rrrr,szPatchedFile.__repr__cCsx|jdurdnt|j}d|j|jrd|jndf}d|j|jr&d|jndf}ddd|D}||||S)Nr?z --- %s%s  z +++ %s%s csr@rrArDhunkrrrrFrGz&PatchedFile.__str__..)rkr^rlrmrnrorI)r&inforNrRhunksrrrrszPatchedFile.__str__cCsxt|}|}t|}|j}|j}||j} ||j} |D]\} } |dur,| |} t | } | s8t | } | s@t d| | d}|t krKt}| d}t||d}|tkrb||_|d7}n&|tkrn||_|d7}n|tkr||_|d7}||_|d7}n|tkrnd}|| ks|| krt d|r| |_|||| kr|| krnq|| ks|| krt d||dS) zParse hunk details.NzHunk diff line expected: %sr$r%r$rKzHunk is longer than expectedzHunk is shorter than expected)r matchgroupsrJrPrSrQrTdecoder r rgrouprrrrr"rr!rr#r])r&headerdiffencoding header_info hunk_inforsr!r"expected_source_endexpected_target_endr#rE valid_liner$r% original_linerrr _parse_hunks`               zPatchedFile._parse_hunkcCs2|stdt|d}|ttdtddS)NzUnexpected marker: rv)rr r]rrr& last_hunkrrr#_add_no_newline_marker_to_last_hunksz/PatchedFile._add_no_newline_marker_to_last_hunkcCs*|std|d}|tdtddS)Nz%Unexpected trailing newline characterrrrv)rr]rrrrrr_append_trailing_empty_line!sz'PatchedFile._append_trailing_empty_linecCs|jdr|jdr|jdd}|S|jdr)|jdkr)|jdd}|S|jdr=|jdkr=|jdd}|S|j}|S)z)Return the file path abstracted from VCS.za/zb/rNz /dev/null)rl startswithrn)r&filepathrrrrp's    zPatchedFile.pathcCtdd|DS)z"Return the file total added lines.cSg|]}|jqSrrLrrrrr :z%PatchedFile.added..sumr+rrrrL7zPatchedFile.addedcCr)z$Return the file total removed lines.cSrrrMrrrrrr?rz'PatchedFile.removed..rr+rrrrM<rzPatchedFile.removedcC(t|dko|djdko|djdkS)z(Return True if this patch adds the file.rKr)r`rPrQr+rrr is_added_fileA zPatchedFile.is_added_filecCr)z+Return True if this patch removes the file.rKr)r`rSrTr+rrris_removed_fileGrzPatchedFile.is_removed_filecCs|jp|j S)z,Return True if this patch modifies the file.)rrr+rrris_modified_fileMzPatchedFile.is_modified_file)Nr?r?NN)r5r6r7r8r r,rrrrr9rprLrMrrrr:rrr'rrjs,  @     rjcseZdZdZdfdd ZfddZddZd d Zee dfd d Z e dddZ edddZ eddZeddZeddZeddZeddZZS)PatchSetzA list of PatchedFiles.Ncs>tt|t|tr|||}t|}|j||ddS)N)r})rrr isinstance basestring_convert_stringiter_parse)r&fr}datar'rrr Ws   zPatchSet.__init__cstdtt|S)Nz)r*rrr,r+r'rrr,cszPatchSet.__repr__cCr>)Nr?csr@rrA)rD patched_filerrrrFgrGz#PatchSet.__str__..rHr+rrrrfr-zPatchSet.__str__cCsHd}d}t|d}|D]\}}|dur||}t|}|r,|d}|d} d}q t|} | rW|dur=td|| d} | d} t||| | | }||d}q t |} | rp|durhtd|| |||q t |}|r|durtd|| q |dkr|dur| q |durd}t}||q dS)NrKfilename timestampzTarget without source: %szUnexpected hunk found: %szUnexpected marker: %sr) enumerateryr rwrzrrrjr]r rrrrr;)r&r|r} current_filerkunused_diff_line_norEis_source_filenamerlrmis_target_filenamernrois_hunk_header is_no_newlinerrrrisZ                zPatchSet._parsecCs>t|d||d }||}Wd|S1swY|S)z1Return a PatchSet instance given a diff filename.rr}errorsN) open_file)rrr}rrinstancerrr from_filenames   zPatchSet.from_filenamestrictcCs|dur t|||d}t|S)Nr)rBr)rr}rrrrrszPatchSet._convert_stringcCs|||||S)z/Return a PatchSet instance given a diff string.)r)rrr}rrrr from_stringrzPatchSet.from_stringcCrb)z#Return patch added files as a list.cSg|]}|jr|qSr)rrDrrrrrz(PatchSet.added_files..rr+rrr added_filesrzPatchSet.added_filescCrb)z%Return patch removed files as a list.cSrr)rrrrrrrz*PatchSet.removed_files..rr+rrr removed_filesrzPatchSet.removed_filescCrb)z&Return patch modified files as a list.cSrr)rrrrrrrz+PatchSet.modified_files..rr+rrrmodified_filesrzPatchSet.modified_filescCr)z#Return the patch total added lines.cSrrrrrrrrrz"PatchSet.added..rr+rrrrLrzPatchSet.addedcCr)z%Return the patch total removed lines.cSrrrrrrrrrz$PatchSet.removed..rr+rrrrMrzPatchSet.removedr)Nr)r5r6r7r8r r,rr classmethodrr staticmethodrrr9rrrrLrMr:rrr'rrSs, <      r)'r8 __future__rcodecssysunidiff.constantsrrrrrrr r r r r rrunidiff.errorsr version_infoPY2ropenrr*rior^rBrobjectrlistr;rJrjrrrrrs8 <    &@