5.1.1.3.2. RawTextDocumentDiff

This module provides an API to compute and store the difference between two text documents.

The difference between two documents is computed in therms of line difference, thus documents are split to a set of contiguous lines called chunks. Chunks are implemented as document views.

There is three types of differences, some lines was removed, some lines was inserted and some lines was replaced by something else. A difference is located in the document using a number of context lines, these lines are the same in both documents. These context lines are useful to make patch.

The differences are grouped as ordered set of contiguous line differences (any combination of removed, inserted and replaced chunk) and are delimited by two equal chunks to define the context.

Thus we have five types of chunks: removed, inserted, replaced, equal for the context lines and equal_block for the equal contents that are out of the context lines. This separation for equal contents permits to keep at this level the structure computed by the difference algorithm.

For replaced contents, we can apply a similar logic and compute a difference in therms of flat slices instead of line slices.

To sumarize, a document difference is an ordered list of group differences. Each group is made of any combination of removed, inserted and replaced chunk type and delimited by two equal chunks. And replaced chunks are made of any combination of removed, inserted, replaced and equal sub-chunks. Moreover we can complete the structure with equal_block chunks.

class CodeReview.Diff.RawTextDocumentDiff.TwoWayChunk(chunk1, chunk2)[source]

Bases: object

This class implements a two way chunk.

Public attributes:

chunk1
view for document1
chunk2
view for document2

The parameters chunk1 and chunk2 are the corresponding document views for the chunk.

class CodeReview.Diff.RawTextDocumentDiff.TwoWayChunkDelete(chunk1, chunk2)[source]

Bases: CodeReview.Diff.RawTextDocumentDiff.TwoWayChunk

This class implements a two way delete flat chunk.

The parameters chunk1 and chunk2 are the corresponding document views for the chunk.

chunk_type = delete
class CodeReview.Diff.RawTextDocumentDiff.TwoWayChunkEqual(chunk1, chunk2)[source]

Bases: CodeReview.Diff.RawTextDocumentDiff.TwoWayChunk

This class implements a two way equal flat chunk.

The parameters chunk1 and chunk2 are the corresponding document views for the chunk.

chunk_type = equal
class CodeReview.Diff.RawTextDocumentDiff.TwoWayChunkInsert(chunk1, chunk2)[source]

Bases: CodeReview.Diff.RawTextDocumentDiff.TwoWayChunk

This class implements a two way insert flat chunk.

The parameters chunk1 and chunk2 are the corresponding document views for the chunk.

chunk_type = insert
class CodeReview.Diff.RawTextDocumentDiff.TwoWayChunkReplace(chunk1, chunk2)[source]

Bases: CodeReview.Diff.RawTextDocumentDiff.TwoWayChunk

This class implements a two way replace flat chunk.

The parameters chunk1 and chunk2 are the corresponding document views for the chunk.

chunk_type = replace
class CodeReview.Diff.RawTextDocumentDiff.TwoWayFileDiff(document1, document2)[source]

Bases: object

This class stores the difference between two files.

The parameters document1 and document2 are two DiffViewer.RawTextDocument documents.

The class implements the iter and getitem protocol to access the groups.

Public attributes:

document1

document2

append(group)[source]

Append a group.

pretty_print()[source]

Pretty-print the file differences.

print_unidiff()[source]

Pretty-print the file differences using unidiff format.

class CodeReview.Diff.RawTextDocumentDiff.TwoWayFileDiffFactory[source]

Bases: object

This class implements a factory to compute file differences.

_process_group(file_diff, opcodes)[source]

Process a group of contiguous line changes and append the group to the file diff.

_process_replace_chunk(chunk1, chunk2)[source]

Process a replace chunk type and return the sub-chunks.

The text is encoded in UTF-32 before to be passed to the Patience algorithm in order to have fixed character boundaries.

process(document1, document2, number_of_lines_of_context=3)[source]

Compute the difference between two RawTextDocument documents using the Patience algorithm and return a TwoWayFileDiff instance. The parameter number_of_lines_of_context provides the number of lines of context for the diff algorithm.

class CodeReview.Diff.RawTextDocumentDiff.TwoWayGroup[source]

Bases: object

This class implements a group of contiguous line changes between two files.

The class implements the iter and getitem protocol to access the groups.

Public attributes:

slice1
Line slice for document1
slice2
Line slice for document2
append(chunk)[source]

Append a chunk.

class CodeReview.Diff.RawTextDocumentDiff.TwoWayLineChunkDelete(chunk1, chunk2)[source]

Bases: CodeReview.Diff.RawTextDocumentDiff.TwoWayChunk

This class implements a two way delete line chunk.

The parameters chunk1 and chunk2 are the corresponding document views for the chunk.

chunk_type = delete
class CodeReview.Diff.RawTextDocumentDiff.TwoWayLineChunkEqual(chunk1, chunk2)[source]

Bases: CodeReview.Diff.RawTextDocumentDiff.TwoWayChunk

This class implements a two way equal line chunk.

The parameters chunk1 and chunk2 are the corresponding document views for the chunk.

chunk_type = equal
class CodeReview.Diff.RawTextDocumentDiff.TwoWayLineChunkInsert(chunk1, chunk2)[source]

Bases: CodeReview.Diff.RawTextDocumentDiff.TwoWayChunk

This class implements a two way insert line chunk.

The parameters chunk1 and chunk2 are the corresponding document views for the chunk.

chunk_type = insert
class CodeReview.Diff.RawTextDocumentDiff.TwoWayLineChunkReplace(chunk1, chunk2, chunks)[source]

Bases: CodeReview.Diff.RawTextDocumentDiff.TwoWayChunk

This class implements the specific case of replace line chunk type.

The class implements the iter and getitem protocol to access the sub-chunks.

chunk_type = replace
CodeReview.Diff.RawTextDocumentDiff.chunk_type

Defines the type of chunks

alias of TwoWayChunkTypes