diff — Helpers for computing deltas#

Diff module.

class diff.Hunk(a, b, grouped_opcode)[source]#

Bases: object

One change hunk between a and b.

Note

parts of this code are taken from by difflib.get_grouped_opcodes().

Parameters:
  • a (Union[str, Sequence[str]]) – sequence of lines

  • b (Union[str, Sequence[str]]) – sequence of lines

  • grouped_opcode (Sequence[Tuple[str, int, int, int, int]]) – list of 5-tuples describing how to turn a into b. It has the same format as returned by difflib.get_opcodes().

APPR = 1#
NOT_APPR = -1#
PENDING = 0#
apply()[source]#

Turn a into b for this hunk.

Return type:

Sequence[str]

color_line(line, line_ref=None)[source]#

Color line characters.

If line_ref is None, the whole line is colored. If line_ref[i] is not blank, line[i] is colored. Color depends if line starts with +/-.

line_ref: string.

Parameters:
  • line (str) –

  • line_ref (Optional[str]) –

Return type:

str

create_diff()[source]#

Generator of diff text for this hunk, without formatting.

Check each line ends with line feed to prevent behaviour like Python issue 2142

Return type:

Iterable[str]

format_diff()[source]#

Color diff lines.

Return type:

Iterable[str]

get_header()[source]#

Provide header of unified diff.

Return type:

str

static get_header_text(a_rng, b_rng, affix='@@')[source]#

Provide header for any ranges.

Parameters:
  • a_rng (Tuple[int, int]) –

  • b_rng (Tuple[int, int]) –

  • affix (str) –

Return type:

str

class diff.PatchManager(text_a, text_b, context=0, by_letter=False, replace_invisible=False)[source]#

Bases: object

Apply patches to text_a to obtain a new text.

If all hunks are approved, text_b will be obtained.

Parameters:
  • text_a (str) – base text

  • text_b (str) – target text

  • context (int) – number of lines which are context

  • by_letter (bool) – if text_a and text_b are single lines, comparison can be done letter by letter.

  • replace_invisible (bool) – Replace invisible characters like U+200e with the charnumber in brackets (e.g. <200e>).

apply()[source]#

Apply changes. If there are undecided changes, ask to review.

Return type:

List[str]

get_blocks()[source]#

Return list with blocks of indexes.

Format of each block:

[-1, (i1, i2), (-1, -1)] -> block a[i1:i2] does not change from
    a to b then is there is no corresponding hunk.
[hunk index, (i1, i2), (j1, j2)] -> block a[i1:i2] becomes b[j1:j2]
Return type:

List[Tuple[int, Tuple[int, int], Tuple[int, int]]]

print_hunks()[source]#

Print the headers and diff texts of all hunks to the output.

Return type:

None

review_hunks()[source]#

Review hunks.

Return type:

None

diff.cherry_pick(oldtext, newtext, n=0, by_letter=False)[source]#

Propose a list of changes for approval.

Text with approved changes will be returned. n: int, line of context as defined in difflib.get_grouped_opcodes(). by_letter: if text_a and text_b are single lines, comparison can be done

Parameters:
  • oldtext (str) –

  • newtext (str) –

  • n (int) –

  • by_letter (bool) –

Return type:

str

diff.html_comparator(compare_string)[source]#

List of added and deleted contexts from ‘action=compare’ html string.

This function is useful when combineds with site.py’s “compare” method. Site.compare() returns HTML that is useful for displaying on a page. Here we use BeautifulSoup to get the un-HTML-ify the context of changes. Finally we present the added and deleted contexts. :param compare_string: HTML string from MediaWiki API :return: deleted and added list of contexts

Parameters:

compare_string (str) –

Return type:

Dict[str, List[str]]