Mercurial > cpython-lru_cache-weakref
annotate Lib/difflib.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | a9ee869cae40 |
| children |
| rev | line source |
|---|---|
|
59522
fcfe324663a6
convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents:
57417
diff
changeset
|
1 #! /usr/bin/env python3 |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4 Module difflib -- helpers for computing deltas between objects. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
5 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
6 Function get_close_matches(word, possibilities, n=3, cutoff=0.6): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
7 Use SequenceMatcher to return list of the best "good enough" matches. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
8 |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
9 Function context_diff(a, b): |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
10 For two lists of strings, return a delta in context diff format. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
11 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
12 Function ndiff(a, b): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
13 Return a delta: the difference between `a` and `b` (lists of strings). |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
14 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
15 Function restore(delta, which): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
16 Return one of the two sequences that generated an ndiff delta. |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
17 |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
18 Function unified_diff(a, b): |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
19 For two lists of strings, return a delta in unified diff format. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
20 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
21 Class SequenceMatcher: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
22 A flexible class for comparing pairs of sequences of any type. |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
23 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
24 Class Differ: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
25 For producing human-readable deltas from sequences of lines of text. |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
26 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
27 Class HtmlDiff: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
28 For producing HTML side by side comparison with change highlights. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
29 """ |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
30 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
31 __all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher', |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
32 'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff', |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
33 'unified_diff', 'HtmlDiff', 'Match'] |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
34 |
|
66666
6b03491ee171
Issue 10534 deprecate isbjunk and isbpopular methods.
Terry Reedy <tjreedy@udel.edu>
parents:
66653
diff
changeset
|
35 import warnings |
|
32339
408a40e65247
Apply heapq.nlargest() to find best matches.
Raymond Hettinger <python@rcn.com>
parents:
31515
diff
changeset
|
36 import heapq |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
37 from collections import namedtuple as _namedtuple |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
38 |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
39 Match = _namedtuple('Match', 'a b size') |
|
32339
408a40e65247
Apply heapq.nlargest() to find best matches.
Raymond Hettinger <python@rcn.com>
parents:
31515
diff
changeset
|
40 |
|
29719
fa4cf878c538
Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents:
29517
diff
changeset
|
41 def _calculate_ratio(matches, length): |
|
fa4cf878c538
Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents:
29517
diff
changeset
|
42 if length: |
|
fa4cf878c538
Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents:
29517
diff
changeset
|
43 return 2.0 * matches / length |
|
fa4cf878c538
Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents:
29517
diff
changeset
|
44 return 1.0 |
|
fa4cf878c538
Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents:
29517
diff
changeset
|
45 |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
46 class SequenceMatcher: |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
47 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
48 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
49 SequenceMatcher is a flexible class for comparing pairs of sequences of |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
50 any type, so long as the sequence elements are hashable. The basic |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
51 algorithm predates, and is a little fancier than, an algorithm |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
52 published in the late 1980's by Ratcliff and Obershelp under the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
53 hyperbolic name "gestalt pattern matching". The basic idea is to find |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
54 the longest contiguous matching subsequence that contains no "junk" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
55 elements (R-O doesn't address junk). The same idea is then applied |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
56 recursively to the pieces of the sequences to the left and to the right |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
57 of the matching subsequence. This does not yield minimal edit |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
58 sequences, but does tend to yield matches that "look right" to people. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
59 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
60 SequenceMatcher tries to compute a "human-friendly diff" between two |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
61 sequences. Unlike e.g. UNIX(tm) diff, the fundamental notion is the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
62 longest *contiguous* & junk-free matching subsequence. That's what |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
63 catches peoples' eyes. The Windows(tm) windiff has another interesting |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
64 notion, pairing up elements that appear uniquely in each sequence. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
65 That, and the method here, appear to yield more intuitive difference |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
66 reports than does diff. This method appears to be the least vulnerable |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
67 to synching up on blocks of "junk lines", though (like blank lines in |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
68 ordinary text files, or maybe "<P>" lines in HTML files). That may be |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
69 because this is the only method of the 3 that has a *concept* of |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
70 "junk" <wink>. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
71 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
72 Example, comparing two strings, and considering blanks to be "junk": |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
73 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
74 >>> s = SequenceMatcher(lambda x: x == " ", |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
75 ... "private Thread currentThread;", |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
76 ... "private volatile Thread currentThread;") |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
77 >>> |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
78 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
79 .ratio() returns a float in [0, 1], measuring the "similarity" of the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
80 sequences. As a rule of thumb, a .ratio() value over 0.6 means the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
81 sequences are close matches: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
82 |
|
40786
0ce14c4ca133
Revert doubly-converted doctests.
Guido van Rossum <guido@python.org>
parents:
40785
diff
changeset
|
83 >>> print(round(s.ratio(), 3)) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
84 0.866 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
85 >>> |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
86 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
87 If you're only interested in where the sequences match, |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
88 .get_matching_blocks() is handy: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
89 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
90 >>> for block in s.get_matching_blocks(): |
|
40786
0ce14c4ca133
Revert doubly-converted doctests.
Guido van Rossum <guido@python.org>
parents:
40785
diff
changeset
|
91 ... print("a[%d] and b[%d] match for %d elements" % block) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
92 a[0] and b[0] match for 8 elements |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
93 a[8] and b[17] match for 21 elements |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
94 a[29] and b[38] match for 0 elements |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
95 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
96 Note that the last tuple returned by .get_matching_blocks() is always a |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
97 dummy, (len(a), len(b), 0), and this is the only case in which the last |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
98 tuple element (number of elements matched) is 0. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
99 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
100 If you want to know how to change the first sequence into the second, |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
101 use .get_opcodes(): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
102 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
103 >>> for opcode in s.get_opcodes(): |
|
40786
0ce14c4ca133
Revert doubly-converted doctests.
Guido van Rossum <guido@python.org>
parents:
40785
diff
changeset
|
104 ... print("%6s a[%d:%d] b[%d:%d]" % opcode) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
105 equal a[0:8] b[0:8] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
106 insert a[8:8] b[8:17] |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
107 equal a[8:29] b[17:38] |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
108 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
109 See the Differ class for a fancy human-friendly file differencer, which |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
110 uses SequenceMatcher both to compare sequences of lines, and to compare |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
111 sequences of characters within similar (near-matching) lines. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
112 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
113 See also function get_close_matches() in this module, which shows how |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
114 simple code building on SequenceMatcher can be used to do useful work. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
115 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
116 Timing: Basic R-O is cubic time worst case and quadratic time expected |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
117 case. SequenceMatcher is quadratic time for the worst case and has |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
118 expected-case behavior dependent in a complicated way on how many |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
119 elements the sequences have in common; best case time is linear. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
120 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
121 Methods: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
122 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
123 __init__(isjunk=None, a='', b='') |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
124 Construct a SequenceMatcher. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
125 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
126 set_seqs(a, b) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
127 Set the two sequences to be compared. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
128 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
129 set_seq1(a) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
130 Set the first sequence to be compared. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
131 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
132 set_seq2(b) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
133 Set the second sequence to be compared. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
134 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
135 find_longest_match(alo, ahi, blo, bhi) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
136 Find longest matching block in a[alo:ahi] and b[blo:bhi]. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
137 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
138 get_matching_blocks() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
139 Return list of triples describing matching subsequences. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
140 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
141 get_opcodes() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
142 Return list of 5-tuples describing how to turn a into b. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
143 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
144 ratio() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
145 Return a measure of the sequences' similarity (float in [0,1]). |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
146 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
147 quick_ratio() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
148 Return an upper bound on .ratio() relatively quickly. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
149 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
150 real_quick_ratio() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
151 Return an upper bound on ratio() very quickly. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
152 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
153 |
|
66440
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
154 def __init__(self, isjunk=None, a='', b='', autojunk=True): |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
155 """Construct a SequenceMatcher. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
156 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
157 Optional arg isjunk is None (the default), or a one-argument |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
158 function that takes a sequence element and returns true iff the |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
159 element is junk. None is equivalent to passing "lambda x: 0", i.e. |
|
16695
e14c5f34fa05
Fix a few small typos in the docstrings.
Fred Drake <fdrake@acm.org>
parents:
16551
diff
changeset
|
160 no elements are considered to be junk. For example, pass |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
161 lambda x: x in " \\t" |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
162 if you're comparing lines as sequences of characters, and don't |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
163 want to synch up on blanks or hard tabs. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
164 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
165 Optional arg a is the first of two sequences to be compared. By |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
166 default, an empty string. The elements of a must be hashable. See |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
167 also .set_seqs() and .set_seq1(). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
168 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
169 Optional arg b is the second of two sequences to be compared. By |
|
16695
e14c5f34fa05
Fix a few small typos in the docstrings.
Fred Drake <fdrake@acm.org>
parents:
16551
diff
changeset
|
170 default, an empty string. The elements of b must be hashable. See |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
171 also .set_seqs() and .set_seq2(). |
|
66440
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
172 |
|
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
173 Optional arg autojunk should be set to False to disable the |
|
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
174 "automatic junk heuristic" that treats popular elements as junk |
|
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
175 (see module documentation for more information). |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
176 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
177 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
178 # Members: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
179 # a |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
180 # first sequence |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
181 # b |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
182 # second sequence; differences are computed as "what do |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
183 # we need to do to 'a' to change it into 'b'?" |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
184 # b2j |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
185 # for x in b, b2j[x] is a list of the indices (into b) |
|
66666
6b03491ee171
Issue 10534 deprecate isbjunk and isbpopular methods.
Terry Reedy <tjreedy@udel.edu>
parents:
66653
diff
changeset
|
186 # at which x appears; junk and popular elements do not appear |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
187 # fullbcount |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
188 # for x in b, fullbcount[x] == the number of times x |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
189 # appears in b; only materialized if really needed (used |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
190 # only for computing quick_ratio()) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
191 # matching_blocks |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
192 # a list of (i, j, k) triples, where a[i:i+k] == b[j:j+k]; |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
193 # ascending & non-overlapping in i and in j; terminated by |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
194 # a dummy (len(a), len(b), 0) sentinel |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
195 # opcodes |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
196 # a list of (tag, i1, i2, j1, j2) tuples, where tag is |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
197 # one of |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
198 # 'replace' a[i1:i2] should be replaced by b[j1:j2] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
199 # 'delete' a[i1:i2] should be deleted |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
200 # 'insert' b[j1:j2] should be inserted |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
201 # 'equal' a[i1:i2] == b[j1:j2] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
202 # isjunk |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
203 # a user-supplied function taking a sequence element and |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
204 # returning true iff the element is "junk" -- this has |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
205 # subtle but helpful effects on the algorithm, which I'll |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
206 # get around to writing up someday <0.9 wink>. |
|
73917
a3309634f38c
Fix comment in difflib.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
72485
diff
changeset
|
207 # DON'T USE! Only __chain_b uses this. Use "in self.bjunk". |
| 66653 | 208 # bjunk |
| 209 # the items in b for which isjunk is True. | |
| 210 # bpopular | |
| 211 # nonjunk items in b treated as junk by the heuristic (if used). | |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
212 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
213 self.isjunk = isjunk |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
214 self.a = self.b = None |
|
66440
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
215 self.autojunk = autojunk |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
216 self.set_seqs(a, b) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
217 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
218 def set_seqs(self, a, b): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
219 """Set the two sequences to be compared. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
220 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
221 >>> s = SequenceMatcher() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
222 >>> s.set_seqs("abcd", "bcde") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
223 >>> s.ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
224 0.75 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
225 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
226 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
227 self.set_seq1(a) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
228 self.set_seq2(b) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
229 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
230 def set_seq1(self, a): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
231 """Set the first sequence to be compared. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
232 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
233 The second sequence to be compared is not changed. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
234 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
235 >>> s = SequenceMatcher(None, "abcd", "bcde") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
236 >>> s.ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
237 0.75 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
238 >>> s.set_seq1("bcde") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
239 >>> s.ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
240 1.0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
241 >>> |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
242 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
243 SequenceMatcher computes and caches detailed information about the |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
244 second sequence, so if you want to compare one sequence S against |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
245 many sequences, use .set_seq2(S) once and call .set_seq1(x) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
246 repeatedly for each of the other sequences. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
247 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
248 See also set_seqs() and set_seq2(). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
249 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
250 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
251 if a is self.a: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
252 return |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
253 self.a = a |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
254 self.matching_blocks = self.opcodes = None |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
255 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
256 def set_seq2(self, b): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
257 """Set the second sequence to be compared. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
258 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
259 The first sequence to be compared is not changed. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
260 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
261 >>> s = SequenceMatcher(None, "abcd", "bcde") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
262 >>> s.ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
263 0.75 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
264 >>> s.set_seq2("abcd") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
265 >>> s.ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
266 1.0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
267 >>> |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
268 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
269 SequenceMatcher computes and caches detailed information about the |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
270 second sequence, so if you want to compare one sequence S against |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
271 many sequences, use .set_seq2(S) once and call .set_seq1(x) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
272 repeatedly for each of the other sequences. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
273 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
274 See also set_seqs() and set_seq1(). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
275 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
276 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
277 if b is self.b: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
278 return |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
279 self.b = b |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
280 self.matching_blocks = self.opcodes = None |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
281 self.fullbcount = None |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
282 self.__chain_b() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
283 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
284 # For each element x in b, set b2j[x] to a list of the indices in |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
285 # b where x appears; the indices are in increasing order; note that |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
286 # the number of times x appears in b is len(b2j[x]) ... |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
287 # when self.isjunk is defined, junk elements don't show up in this |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
288 # map at all, which stops the central find_longest_match method |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
289 # from starting any matching block at a junk element ... |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
290 # b2j also does not contain entries for "popular" elements, meaning |
|
66440
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
291 # elements that account for more than 1 + 1% of the total elements, and |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
292 # when the sequence is reasonably large (>= 200 elements); this can |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
293 # be viewed as an adaptive notion of semi-junk, and yields an enormous |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
294 # speedup when, e.g., comparing program files with hundreds of |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
295 # instances of "return NULL;" ... |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
296 # note that this is only called when b changes; so for cross-product |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
297 # kinds of matches, it's best to call set_seq2 once, then set_seq1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
298 # repeatedly |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
299 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
300 def __chain_b(self): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
301 # Because isjunk is a user-defined (not C) function, and we test |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
302 # for junk a LOT, it's important to minimize the number of calls. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
303 # Before the tricks described here, __chain_b was by far the most |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
304 # time-consuming routine in the whole module! If anyone sees |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
305 # Jim Roskind, thank him again for profile.py -- I never would |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
306 # have guessed that. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
307 # The first trick is to build b2j ignoring the possibility |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
308 # of junk. I.e., we don't call isjunk at all yet. Throwing |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
309 # out the junk later is much cheaper than building b2j "right" |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
310 # from the start. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
311 b = self.b |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
312 self.b2j = b2j = {} |
|
66440
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
313 |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
314 for i, elt in enumerate(b): |
|
66440
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
315 indices = b2j.setdefault(elt, []) |
|
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
316 indices.append(i) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
317 |
|
66440
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
318 # Purge junk elements |
| 66653 | 319 self.bjunk = junk = set() |
|
66440
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
320 isjunk = self.isjunk |
|
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
321 if isjunk: |
|
66931
9a2e56d1146d
Issue 10534, difflib: tweak doc; test new SequenceMatcher instance attributes; avoid unneeded lists of SM.b2j keys and items in .__chain_b. Do not backport.
Terry Reedy <tjreedy@udel.edu>
parents:
66666
diff
changeset
|
322 for elt in b2j.keys(): |
|
66440
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
323 if isjunk(elt): |
|
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
324 junk.add(elt) |
|
66931
9a2e56d1146d
Issue 10534, difflib: tweak doc; test new SequenceMatcher instance attributes; avoid unneeded lists of SM.b2j keys and items in .__chain_b. Do not backport.
Terry Reedy <tjreedy@udel.edu>
parents:
66666
diff
changeset
|
325 for elt in junk: # separate loop avoids separate list of keys |
|
9a2e56d1146d
Issue 10534, difflib: tweak doc; test new SequenceMatcher instance attributes; avoid unneeded lists of SM.b2j keys and items in .__chain_b. Do not backport.
Terry Reedy <tjreedy@udel.edu>
parents:
66666
diff
changeset
|
326 del b2j[elt] |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
327 |
|
66440
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
328 # Purge popular elements that are not junk |
| 66653 | 329 self.bpopular = popular = set() |
|
66440
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
330 n = len(b) |
|
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
331 if self.autojunk and n >= 200: |
|
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
332 ntest = n // 100 + 1 |
|
66931
9a2e56d1146d
Issue 10534, difflib: tweak doc; test new SequenceMatcher instance attributes; avoid unneeded lists of SM.b2j keys and items in .__chain_b. Do not backport.
Terry Reedy <tjreedy@udel.edu>
parents:
66666
diff
changeset
|
333 for elt, idxs in b2j.items(): |
|
66440
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
334 if len(idxs) > ntest: |
|
c54c411d3da7
Issue 2986: Add autojunk paramater to SequenceMatcher to turn off heuristic. Patch by Terry Reedy, Eli Bendersky, and Simon Cross
Terry Reedy <tjreedy@udel.edu>
parents:
60554
diff
changeset
|
335 popular.add(elt) |
|
66931
9a2e56d1146d
Issue 10534, difflib: tweak doc; test new SequenceMatcher instance attributes; avoid unneeded lists of SM.b2j keys and items in .__chain_b. Do not backport.
Terry Reedy <tjreedy@udel.edu>
parents:
66666
diff
changeset
|
336 for elt in popular: # ditto; as fast for 1% deletion |
|
9a2e56d1146d
Issue 10534, difflib: tweak doc; test new SequenceMatcher instance attributes; avoid unneeded lists of SM.b2j keys and items in .__chain_b. Do not backport.
Terry Reedy <tjreedy@udel.edu>
parents:
66666
diff
changeset
|
337 del b2j[elt] |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
338 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
339 def find_longest_match(self, alo, ahi, blo, bhi): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
340 """Find longest matching block in a[alo:ahi] and b[blo:bhi]. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
341 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
342 If isjunk is not defined: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
343 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
344 Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
345 alo <= i <= i+k <= ahi |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
346 blo <= j <= j+k <= bhi |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
347 and for all (i',j',k') meeting those conditions, |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
348 k >= k' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
349 i <= i' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
350 and if i == i', j <= j' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
351 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
352 In other words, of all maximal matching blocks, return one that |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
353 starts earliest in a, and of all those maximal matching blocks that |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
354 start earliest in a, return the one that starts earliest in b. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
355 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
356 >>> s = SequenceMatcher(None, " abcd", "abcd abcd") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
357 >>> s.find_longest_match(0, 5, 0, 9) |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
358 Match(a=0, b=4, size=5) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
359 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
360 If isjunk is defined, first the longest matching block is |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
361 determined as above, but with the additional restriction that no |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
362 junk element appears in the block. Then that block is extended as |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
363 far as possible by matching (only) junk elements on both sides. So |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
364 the resulting block never matches on junk except as identical junk |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
365 happens to be adjacent to an "interesting" match. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
366 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
367 Here's the same example as before, but considering blanks to be |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
368 junk. That prevents " abcd" from matching the " abcd" at the tail |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
369 end of the second sequence directly. Instead only the "abcd" can |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
370 match, and matches the leftmost "abcd" in the second sequence: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
371 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
372 >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
373 >>> s.find_longest_match(0, 5, 0, 9) |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
374 Match(a=1, b=0, size=4) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
375 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
376 If no blocks match, return (alo, blo, 0). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
377 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
378 >>> s = SequenceMatcher(None, "ab", "c") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
379 >>> s.find_longest_match(0, 2, 0, 1) |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
380 Match(a=0, b=0, size=0) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
381 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
382 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
383 # CAUTION: stripping common prefix or suffix would be incorrect. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
384 # E.g., |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
385 # ab |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
386 # acab |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
387 # Longest matching block is "ab", but if common prefix is |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
388 # stripped, it's "a" (tied with "b"). UNIX(tm) diff does so |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
389 # strip, so ends up claiming that ab is changed to acab by |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
390 # inserting "ca" in the middle. That's minimal but unintuitive: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
391 # "it's obvious" that someone inserted "ac" at the front. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
392 # Windiff ends up at the same place as diff, but by pairing up |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
393 # the unique 'b's and then matching the first two 'a's. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
394 |
|
66666
6b03491ee171
Issue 10534 deprecate isbjunk and isbpopular methods.
Terry Reedy <tjreedy@udel.edu>
parents:
66653
diff
changeset
|
395 a, b, b2j, isbjunk = self.a, self.b, self.b2j, self.bjunk.__contains__ |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
396 besti, bestj, bestsize = alo, blo, 0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
397 # find longest junk-free match |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
398 # during an iteration of the loop, j2len[j] = length of longest |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
399 # junk-free match ending with a[i-1] and b[j] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
400 j2len = {} |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
401 nothing = [] |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
402 for i in range(alo, ahi): |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
403 # look at all instances of a[i] in b; note that because |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
404 # b2j has no junk keys, the loop is skipped if a[i] is junk |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
405 j2lenget = j2len.get |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
406 newj2len = {} |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
407 for j in b2j.get(a[i], nothing): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
408 # a[i] matches b[j] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
409 if j < blo: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
410 continue |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
411 if j >= bhi: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
412 break |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
413 k = newj2len[j] = j2lenget(j-1, 0) + 1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
414 if k > bestsize: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
415 besti, bestj, bestsize = i-k+1, j-k+1, k |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
416 j2len = newj2len |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
417 |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
418 # Extend the best by non-junk elements on each end. In particular, |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
419 # "popular" non-junk elements aren't in b2j, which greatly speeds |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
420 # the inner loop above, but also means "the best" match so far |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
421 # doesn't contain any junk *or* popular non-junk elements. |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
422 while besti > alo and bestj > blo and \ |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
423 not isbjunk(b[bestj-1]) and \ |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
424 a[besti-1] == b[bestj-1]: |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
425 besti, bestj, bestsize = besti-1, bestj-1, bestsize+1 |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
426 while besti+bestsize < ahi and bestj+bestsize < bhi and \ |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
427 not isbjunk(b[bestj+bestsize]) and \ |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
428 a[besti+bestsize] == b[bestj+bestsize]: |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
429 bestsize += 1 |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
430 |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
431 # Now that we have a wholly interesting match (albeit possibly |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
432 # empty!), we may as well suck up the matching junk on each |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
433 # side of it too. Can't think of a good reason not to, and it |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
434 # saves post-processing the (possibly considerable) expense of |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
435 # figuring out what to do with it. In the case of an empty |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
436 # interesting match, this is clearly the right thing to do, |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
437 # because no other kind of match is possible in the regions. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
438 while besti > alo and bestj > blo and \ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
439 isbjunk(b[bestj-1]) and \ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
440 a[besti-1] == b[bestj-1]: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
441 besti, bestj, bestsize = besti-1, bestj-1, bestsize+1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
442 while besti+bestsize < ahi and bestj+bestsize < bhi and \ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
443 isbjunk(b[bestj+bestsize]) and \ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
444 a[besti+bestsize] == b[bestj+bestsize]: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
445 bestsize = bestsize + 1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
446 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
447 return Match(besti, bestj, bestsize) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
448 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
449 def get_matching_blocks(self): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
450 """Return list of triples describing matching subsequences. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
451 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
452 Each triple is of the form (i, j, n), and means that |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
453 a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
454 i and in j. New in Python 2.5, it's also guaranteed that if |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
455 (i, j, n) and (i', j', n') are adjacent triples in the list, and |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
456 the second is not the last triple in the list, then i+n != i' or |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
457 j+n != j'. IOW, adjacent triples never describe adjacent equal |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
458 blocks. |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
459 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
460 The last triple is a dummy, (len(a), len(b), 0), and is the only |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
461 triple with n==0. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
462 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
463 >>> s = SequenceMatcher(None, "abxcd", "abcd") |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
464 >>> list(s.get_matching_blocks()) |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
465 [Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)] |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
466 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
467 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
468 if self.matching_blocks is not None: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
469 return self.matching_blocks |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
470 la, lb = len(self.a), len(self.b) |
|
36429
16bfe9386068
Patch #1413711: Certain patterns of differences were making difflib
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
35276
diff
changeset
|
471 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
472 # This is most naturally expressed as a recursive algorithm, but |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
473 # at least one user bumped into extreme use cases that exceeded |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
474 # the recursion limit on their box. So, now we maintain a list |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
475 # ('queue`) of blocks we still need to look at, and append partial |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
476 # results to `matching_blocks` in a loop; the matches are sorted |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
477 # at the end. |
|
36429
16bfe9386068
Patch #1413711: Certain patterns of differences were making difflib
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
35276
diff
changeset
|
478 queue = [(0, la, 0, lb)] |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
479 matching_blocks = [] |
|
36429
16bfe9386068
Patch #1413711: Certain patterns of differences were making difflib
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
35276
diff
changeset
|
480 while queue: |
|
16bfe9386068
Patch #1413711: Certain patterns of differences were making difflib
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
35276
diff
changeset
|
481 alo, ahi, blo, bhi = queue.pop() |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
482 i, j, k = x = self.find_longest_match(alo, ahi, blo, bhi) |
|
36429
16bfe9386068
Patch #1413711: Certain patterns of differences were making difflib
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
35276
diff
changeset
|
483 # a[alo:i] vs b[blo:j] unknown |
|
16bfe9386068
Patch #1413711: Certain patterns of differences were making difflib
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
35276
diff
changeset
|
484 # a[i:i+k] same as b[j:j+k] |
|
16bfe9386068
Patch #1413711: Certain patterns of differences were making difflib
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
35276
diff
changeset
|
485 # a[i+k:ahi] vs b[j+k:bhi] unknown |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
486 if k: # if k is 0, there was no matching block |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
487 matching_blocks.append(x) |
|
36429
16bfe9386068
Patch #1413711: Certain patterns of differences were making difflib
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
35276
diff
changeset
|
488 if alo < i and blo < j: |
|
16bfe9386068
Patch #1413711: Certain patterns of differences were making difflib
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
35276
diff
changeset
|
489 queue.append((alo, i, blo, j)) |
|
16bfe9386068
Patch #1413711: Certain patterns of differences were making difflib
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
35276
diff
changeset
|
490 if i+k < ahi and j+k < bhi: |
|
16bfe9386068
Patch #1413711: Certain patterns of differences were making difflib
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
35276
diff
changeset
|
491 queue.append((i+k, ahi, j+k, bhi)) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
492 matching_blocks.sort() |
|
36429
16bfe9386068
Patch #1413711: Certain patterns of differences were making difflib
Gustavo Niemeyer <gustavo@niemeyer.net>
parents:
35276
diff
changeset
|
493 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
494 # It's possible that we have adjacent equal blocks in the |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
495 # matching_blocks list now. Starting with 2.5, this code was added |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
496 # to collapse them. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
497 i1 = j1 = k1 = 0 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
498 non_adjacent = [] |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
499 for i2, j2, k2 in matching_blocks: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
500 # Is this block adjacent to i1, j1, k1? |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
501 if i1 + k1 == i2 and j1 + k1 == j2: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
502 # Yes, so collapse them -- this just increases the length of |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
503 # the first block by the length of the second, and the first |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
504 # block so lengthened remains the block to compare against. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
505 k1 += k2 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
506 else: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
507 # Not adjacent. Remember the first block (k1==0 means it's |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
508 # the dummy we started with), and make the second block the |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
509 # new block to compare against. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
510 if k1: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
511 non_adjacent.append((i1, j1, k1)) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
512 i1, j1, k1 = i2, j2, k2 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
513 if k1: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
514 non_adjacent.append((i1, j1, k1)) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
515 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
516 non_adjacent.append( (la, lb, 0) ) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
517 self.matching_blocks = non_adjacent |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43020
diff
changeset
|
518 return map(Match._make, self.matching_blocks) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
519 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
520 def get_opcodes(self): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
521 """Return list of 5-tuples describing how to turn a into b. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
522 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
523 Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
524 has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
525 tuple preceding it, and likewise for j1 == the previous j2. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
526 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
527 The tags are strings, with these meanings: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
528 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
529 'replace': a[i1:i2] should be replaced by b[j1:j2] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
530 'delete': a[i1:i2] should be deleted. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
531 Note that j1==j2 in this case. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
532 'insert': b[j1:j2] should be inserted at a[i1:i1]. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
533 Note that i1==i2 in this case. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
534 'equal': a[i1:i2] == b[j1:j2] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
535 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
536 >>> a = "qabxcd" |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
537 >>> b = "abycdf" |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
538 >>> s = SequenceMatcher(None, a, b) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
539 >>> for tag, i1, i2, j1, j2 in s.get_opcodes(): |
|
40786
0ce14c4ca133
Revert doubly-converted doctests.
Guido van Rossum <guido@python.org>
parents:
40785
diff
changeset
|
540 ... print(("%7s a[%d:%d] (%s) b[%d:%d] (%s)" % |
|
0ce14c4ca133
Revert doubly-converted doctests.
Guido van Rossum <guido@python.org>
parents:
40785
diff
changeset
|
541 ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2]))) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
542 delete a[0:1] (q) b[0:0] () |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
543 equal a[1:3] (ab) b[0:2] (ab) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
544 replace a[3:4] (x) b[2:3] (y) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
545 equal a[4:6] (cd) b[3:5] (cd) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
546 insert a[6:6] () b[5:6] (f) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
547 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
548 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
549 if self.opcodes is not None: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
550 return self.opcodes |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
551 i = j = 0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
552 self.opcodes = answer = [] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
553 for ai, bj, size in self.get_matching_blocks(): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
554 # invariant: we've pumped out correct diffs to change |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
555 # a[:i] into b[:j], and the next matching block is |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
556 # a[ai:ai+size] == b[bj:bj+size]. So we need to pump |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
557 # out a diff to change a[i:ai] into b[j:bj], pump out |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
558 # the matching block, and move (i,j) beyond the match |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
559 tag = '' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
560 if i < ai and j < bj: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
561 tag = 'replace' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
562 elif i < ai: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
563 tag = 'delete' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
564 elif j < bj: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
565 tag = 'insert' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
566 if tag: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
567 answer.append( (tag, i, ai, j, bj) ) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
568 i, j = ai+size, bj+size |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
569 # the list of matching blocks is terminated by a |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
570 # sentinel with size 0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
571 if size: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
572 answer.append( ('equal', ai, i, bj, j) ) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
573 return answer |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
574 |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
575 def get_grouped_opcodes(self, n=3): |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
576 """ Isolate change clusters by eliminating ranges with no changes. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
577 |
|
85221
e07f104133d5
#18705: fix a number of typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73917
diff
changeset
|
578 Return a generator of groups with up to n lines of context. |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
579 Each group is in the same format as returned by get_opcodes(). |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
580 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
581 >>> from pprint import pprint |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42132
diff
changeset
|
582 >>> a = list(map(str, range(1,40))) |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
583 >>> b = a[:] |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
584 >>> b[8:8] = ['i'] # Make an insertion |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
585 >>> b[20] += 'x' # Make a replacement |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
586 >>> b[23:28] = [] # Make a deletion |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
587 >>> b[30] += 'y' # Make another replacement |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
588 >>> pprint(list(SequenceMatcher(None,a,b).get_grouped_opcodes())) |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
589 [[('equal', 5, 8, 5, 8), ('insert', 8, 8, 8, 9), ('equal', 8, 11, 9, 12)], |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
590 [('equal', 16, 19, 17, 20), |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
591 ('replace', 19, 20, 20, 21), |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
592 ('equal', 20, 22, 21, 23), |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
593 ('delete', 22, 27, 23, 23), |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
594 ('equal', 27, 30, 23, 26)], |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
595 [('equal', 31, 34, 27, 30), |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
596 ('replace', 34, 35, 30, 31), |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
597 ('equal', 35, 38, 31, 34)]] |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
598 """ |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
599 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
600 codes = self.get_opcodes() |
|
32612
5546c7ae24f7
SequenceMatcher(None, [], []).get_grouped_opcodes() now returns a generator
Brett Cannon <bcannon@gmail.com>
parents:
32357
diff
changeset
|
601 if not codes: |
|
5546c7ae24f7
SequenceMatcher(None, [], []).get_grouped_opcodes() now returns a generator
Brett Cannon <bcannon@gmail.com>
parents:
32357
diff
changeset
|
602 codes = [("equal", 0, 1, 0, 1)] |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
603 # Fixup leading and trailing groups if they show no changes. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
604 if codes[0][0] == 'equal': |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
605 tag, i1, i2, j1, j2 = codes[0] |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
606 codes[0] = tag, max(i1, i2-n), i2, max(j1, j2-n), j2 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
607 if codes[-1][0] == 'equal': |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
608 tag, i1, i2, j1, j2 = codes[-1] |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
609 codes[-1] = tag, i1, min(i2, i1+n), j1, min(j2, j1+n) |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
610 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
611 nn = n + n |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
612 group = [] |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
613 for tag, i1, i2, j1, j2 in codes: |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
614 # End the current group and start a new one whenever |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
615 # there is a large range with no changes. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
616 if tag == 'equal' and i2-i1 > nn: |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
617 group.append((tag, i1, min(i2, i1+n), j1, min(j2, j1+n))) |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
618 yield group |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
619 group = [] |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
620 i1, j1 = max(i1, i2-n), max(j1, j2-n) |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
621 group.append((tag, i1, i2, j1 ,j2)) |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
622 if group and not (len(group)==1 and group[0][0] == 'equal'): |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
623 yield group |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
624 |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
625 def ratio(self): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
626 """Return a measure of the sequences' similarity (float in [0,1]). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
627 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
628 Where T is the total number of elements in both sequences, and |
|
32867
8d25835f88ae
Repair typo in docstring.
Tim Peters <tim.peters@gmail.com>
parents:
32612
diff
changeset
|
629 M is the number of matches, this is 2.0*M / T. |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
630 Note that this is 1 if the sequences are identical, and 0 if |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
631 they have nothing in common. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
632 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
633 .ratio() is expensive to compute if you haven't already computed |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
634 .get_matching_blocks() or .get_opcodes(), in which case you may |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
635 want to try .quick_ratio() or .real_quick_ratio() first to get an |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
636 upper bound. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
637 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
638 >>> s = SequenceMatcher(None, "abcd", "bcde") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
639 >>> s.ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
640 0.75 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
641 >>> s.quick_ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
642 0.75 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
643 >>> s.real_quick_ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
644 1.0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
645 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
646 |
|
39627
e86c0b29b4a2
Kill reduce(). A coproduction of John Reese, Jacques Frechet, and Alex M.
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
647 matches = sum(triple[-1] for triple in self.get_matching_blocks()) |
|
29719
fa4cf878c538
Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents:
29517
diff
changeset
|
648 return _calculate_ratio(matches, len(self.a) + len(self.b)) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
649 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
650 def quick_ratio(self): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
651 """Return an upper bound on ratio() relatively quickly. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
652 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
653 This isn't defined beyond that it is an upper bound on .ratio(), and |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
654 is faster to compute. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
655 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
656 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
657 # viewing a and b as multisets, set matches to the cardinality |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
658 # of their intersection; this counts the number of matches |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
659 # without regard to order, so is clearly an upper bound |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
660 if self.fullbcount is None: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
661 self.fullbcount = fullbcount = {} |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
662 for elt in self.b: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
663 fullbcount[elt] = fullbcount.get(elt, 0) + 1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
664 fullbcount = self.fullbcount |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
665 # avail[x] is the number of times x appears in 'b' less the |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
666 # number of times we've seen it in 'a' so far ... kinda |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
667 avail = {} |
|
39575
63237fcefd99
Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
668 availhas, matches = avail.__contains__, 0 |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
669 for elt in self.a: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
670 if availhas(elt): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
671 numb = avail[elt] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
672 else: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
673 numb = fullbcount.get(elt, 0) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
674 avail[elt] = numb - 1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
675 if numb > 0: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
676 matches = matches + 1 |
|
29719
fa4cf878c538
Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents:
29517
diff
changeset
|
677 return _calculate_ratio(matches, len(self.a) + len(self.b)) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
678 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
679 def real_quick_ratio(self): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
680 """Return an upper bound on ratio() very quickly. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
681 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
682 This isn't defined beyond that it is an upper bound on .ratio(), and |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
683 is faster to compute than either .ratio() or .quick_ratio(). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
684 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
685 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
686 la, lb = len(self.a), len(self.b) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
687 # can't have more matches than the number of elements in the |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
688 # shorter sequence |
|
29719
fa4cf878c538
Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents:
29517
diff
changeset
|
689 return _calculate_ratio(min(la, lb), la + lb) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
690 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
691 def get_close_matches(word, possibilities, n=3, cutoff=0.6): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
692 """Use SequenceMatcher to return list of the best "good enough" matches. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
693 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
694 word is a sequence for which close matches are desired (typically a |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
695 string). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
696 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
697 possibilities is a list of sequences against which to match word |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
698 (typically a list of strings). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
699 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
700 Optional arg n (default 3) is the maximum number of close matches to |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
701 return. n must be > 0. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
702 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
703 Optional arg cutoff (default 0.6) is a float in [0, 1]. Possibilities |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
704 that don't score at least that similar to word are ignored. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
705 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
706 The best (no more than n) matches among the possibilities are returned |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
707 in a list, sorted by similarity score, most similar first. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
708 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
709 >>> get_close_matches("appel", ["ape", "apple", "peach", "puppy"]) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
710 ['apple', 'ape'] |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
711 >>> import keyword as _keyword |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
712 >>> get_close_matches("wheel", _keyword.kwlist) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
713 ['while'] |
|
42132
863ed7cd45dc
Merged revisions 56020-56124 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
714 >>> get_close_matches("Apple", _keyword.kwlist) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
715 [] |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
716 >>> get_close_matches("accept", _keyword.kwlist) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
717 ['except'] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
718 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
719 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
720 if not n > 0: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30698
diff
changeset
|
721 raise ValueError("n must be > 0: %r" % (n,)) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
722 if not 0.0 <= cutoff <= 1.0: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30698
diff
changeset
|
723 raise ValueError("cutoff must be in [0.0, 1.0]: %r" % (cutoff,)) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
724 result = [] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
725 s = SequenceMatcher() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
726 s.set_seq2(word) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
727 for x in possibilities: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
728 s.set_seq1(x) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
729 if s.real_quick_ratio() >= cutoff and \ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
730 s.quick_ratio() >= cutoff and \ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
731 s.ratio() >= cutoff: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
732 result.append((s.ratio(), x)) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
733 |
|
30698
3e3fd56ce595
Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents:
29891
diff
changeset
|
734 # Move the best scorers to head of list |
|
32357
a46bde9e0c74
Reverse argument order for nsmallest() and nlargest().
Raymond Hettinger <python@rcn.com>
parents:
32339
diff
changeset
|
735 result = heapq.nlargest(n, result) |
|
30698
3e3fd56ce595
Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents:
29891
diff
changeset
|
736 # Strip scores for the best n matches |
|
32339
408a40e65247
Apply heapq.nlargest() to find best matches.
Raymond Hettinger <python@rcn.com>
parents:
31515
diff
changeset
|
737 return [x for score, x in result] |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
738 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
739 def _count_leading(line, ch): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
740 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
741 Return number of `ch` characters at the start of `line`. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
742 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
743 Example: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
744 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
745 >>> _count_leading(' abc', ' ') |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
746 3 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
747 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
748 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
749 i, n = 0, len(line) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
750 while i < n and line[i] == ch: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
751 i += 1 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
752 return i |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
753 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
754 class Differ: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
755 r""" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
756 Differ is a class for comparing sequences of lines of text, and |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
757 producing human-readable differences or deltas. Differ uses |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
758 SequenceMatcher both to compare sequences of lines, and to compare |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
759 sequences of characters within similar (near-matching) lines. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
760 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
761 Each line of a Differ delta begins with a two-letter code: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
762 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
763 '- ' line unique to sequence 1 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
764 '+ ' line unique to sequence 2 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
765 ' ' line common to both sequences |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
766 '? ' line not present in either input sequence |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
767 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
768 Lines beginning with '? ' attempt to guide the eye to intraline |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
769 differences, and were not present in either input sequence. These lines |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
770 can be confusing if the sequences contain tab characters. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
771 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
772 Note that Differ makes no claim to produce a *minimal* diff. To the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
773 contrary, minimal diffs are often counter-intuitive, because they synch |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
774 up anywhere possible, sometimes accidental matches 100 pages apart. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
775 Restricting synch points to contiguous matches preserves some notion of |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
776 locality, at the occasional cost of producing a longer diff. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
777 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
778 Example: Comparing two texts. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
779 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
780 First we set up the texts, sequences of individual single-line strings |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
781 ending with newlines (such sequences can also be obtained from the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
782 `readlines()` method of file-like objects): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
783 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
784 >>> text1 = ''' 1. Beautiful is better than ugly. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
785 ... 2. Explicit is better than implicit. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
786 ... 3. Simple is better than complex. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
787 ... 4. Complex is better than complicated. |
|
72485
83f43b58c988
#13012: use splitlines(keepends=True/False) instead of splitlines(0/1).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
69281
diff
changeset
|
788 ... '''.splitlines(keepends=True) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
789 >>> len(text1) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
790 4 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
791 >>> text1[0][-1] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
792 '\n' |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
793 >>> text2 = ''' 1. Beautiful is better than ugly. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
794 ... 3. Simple is better than complex. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
795 ... 4. Complicated is better than complex. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
796 ... 5. Flat is better than nested. |
|
72485
83f43b58c988
#13012: use splitlines(keepends=True/False) instead of splitlines(0/1).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
69281
diff
changeset
|
797 ... '''.splitlines(keepends=True) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
798 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
799 Next we instantiate a Differ object: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
800 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
801 >>> d = Differ() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
802 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
803 Note that when instantiating a Differ object we may pass functions to |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
804 filter out line and character 'junk'. See Differ.__init__ for details. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
805 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
806 Finally, we compare the two: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
807 |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
808 >>> result = list(d.compare(text1, text2)) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
809 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
810 'result' is a list of strings, so let's pretty-print it: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
811 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
812 >>> from pprint import pprint as _pprint |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
813 >>> _pprint(result) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
814 [' 1. Beautiful is better than ugly.\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
815 '- 2. Explicit is better than implicit.\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
816 '- 3. Simple is better than complex.\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
817 '+ 3. Simple is better than complex.\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
818 '? ++\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
819 '- 4. Complex is better than complicated.\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
820 '? ^ ---- ^\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
821 '+ 4. Complicated is better than complex.\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
822 '? ++++ ^ ^\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
823 '+ 5. Flat is better than nested.\n'] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
824 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
825 As a single multi-line string it looks like this: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
826 |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
827 >>> print(''.join(result), end="") |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
828 1. Beautiful is better than ugly. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
829 - 2. Explicit is better than implicit. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
830 - 3. Simple is better than complex. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
831 + 3. Simple is better than complex. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
832 ? ++ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
833 - 4. Complex is better than complicated. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
834 ? ^ ---- ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
835 + 4. Complicated is better than complex. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
836 ? ++++ ^ ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
837 + 5. Flat is better than nested. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
838 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
839 Methods: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
840 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
841 __init__(linejunk=None, charjunk=None) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
842 Construct a text differencer, with optional filters. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
843 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
844 compare(a, b) |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
845 Compare two sequences of lines; generate the resulting delta. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
846 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
847 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
848 def __init__(self, linejunk=None, charjunk=None): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
849 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
850 Construct a text differencer, with optional filters. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
851 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
852 The two optional keyword parameters are for filter functions: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
853 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
854 - `linejunk`: A function that should accept a single string argument, |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
855 and return true iff the string is junk. The module-level function |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
856 `IS_LINE_JUNK` may be used to filter out lines without visible |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
857 characters, except for at most one splat ('#'). It is recommended |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
858 to leave linejunk None; as of Python 2.3, the underlying |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
859 SequenceMatcher class has grown an adaptive notion of "noise" lines |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
860 that's better than any static definition the author has ever been |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
861 able to craft. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
862 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
863 - `charjunk`: A function that should accept a string of length 1. The |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
864 module-level function `IS_CHARACTER_JUNK` may be used to filter out |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
865 whitespace characters (a blank or tab; **note**: bad idea to include |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
866 newline in this!). Use of IS_CHARACTER_JUNK is recommended. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
867 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
868 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
869 self.linejunk = linejunk |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
870 self.charjunk = charjunk |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
871 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
872 def compare(self, a, b): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
873 r""" |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
874 Compare two sequences of lines; generate the resulting delta. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
875 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
876 Each sequence must contain individual single-line strings ending with |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
877 newlines. Such sequences can be obtained from the `readlines()` method |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
878 of file-like objects. The delta generated also consists of newline- |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
879 terminated strings, ready to be printed as-is via the writeline() |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
880 method of a file-like object. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
881 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
882 Example: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
883 |
|
72485
83f43b58c988
#13012: use splitlines(keepends=True/False) instead of splitlines(0/1).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
69281
diff
changeset
|
884 >>> print(''.join(Differ().compare('one\ntwo\nthree\n'.splitlines(True), |
|
83f43b58c988
#13012: use splitlines(keepends=True/False) instead of splitlines(0/1).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
69281
diff
changeset
|
885 ... 'ore\ntree\nemu\n'.splitlines(True))), |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
886 ... end="") |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
887 - one |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
888 ? ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
889 + ore |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
890 ? ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
891 - two |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
892 - three |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
893 ? - |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
894 + tree |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
895 + emu |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
896 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
897 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
898 cruncher = SequenceMatcher(self.linejunk, a, b) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
899 for tag, alo, ahi, blo, bhi in cruncher.get_opcodes(): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
900 if tag == 'replace': |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
901 g = self._fancy_replace(a, alo, ahi, b, blo, bhi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
902 elif tag == 'delete': |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
903 g = self._dump('-', a, alo, ahi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
904 elif tag == 'insert': |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
905 g = self._dump('+', b, blo, bhi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
906 elif tag == 'equal': |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
907 g = self._dump(' ', a, alo, ahi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
908 else: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42142
diff
changeset
|
909 raise ValueError('unknown tag %r' % (tag,)) |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
910 |
| 79378 | 911 yield from g |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
912 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
913 def _dump(self, tag, x, lo, hi): |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
914 """Generate comparison results for a same-tagged range.""" |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
915 for i in range(lo, hi): |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
916 yield '%s %s' % (tag, x[i]) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
917 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
918 def _plain_replace(self, a, alo, ahi, b, blo, bhi): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
919 assert alo < ahi and blo < bhi |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
920 # dump the shorter block first -- reduces the burden on short-term |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
921 # memory if the blocks are of very different sizes |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
922 if bhi - blo < ahi - alo: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
923 first = self._dump('+', b, blo, bhi) |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
924 second = self._dump('-', a, alo, ahi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
925 else: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
926 first = self._dump('-', a, alo, ahi) |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
927 second = self._dump('+', b, blo, bhi) |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
928 |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
929 for g in first, second: |
| 79378 | 930 yield from g |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
931 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
932 def _fancy_replace(self, a, alo, ahi, b, blo, bhi): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
933 r""" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
934 When replacing one block of lines with another, search the blocks |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
935 for *similar* lines; the best-matching pair (if any) is used as a |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
936 synch point, and intraline difference marking is done on the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
937 similar pair. Lots of work, but often worth it. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
938 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
939 Example: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
940 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
941 >>> d = Differ() |
|
29891
9ca291865f9c
Fix faulty doctests. There is no results attribute.
Raymond Hettinger <python@rcn.com>
parents:
29719
diff
changeset
|
942 >>> results = d._fancy_replace(['abcDefghiJkl\n'], 0, 1, |
|
9ca291865f9c
Fix faulty doctests. There is no results attribute.
Raymond Hettinger <python@rcn.com>
parents:
29719
diff
changeset
|
943 ... ['abcdefGhijkl\n'], 0, 1) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
944 >>> print(''.join(results), end="") |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
945 - abcDefghiJkl |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
946 ? ^ ^ ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
947 + abcdefGhijkl |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
948 ? ^ ^ ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
949 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
950 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
951 # don't synch up unless the lines have a similarity score of at |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
952 # least cutoff; best_ratio tracks the best score seen so far |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
953 best_ratio, cutoff = 0.74, 0.75 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
954 cruncher = SequenceMatcher(self.charjunk) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
955 eqi, eqj = None, None # 1st indices of equal lines (if any) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
956 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
957 # search for the pair that matches best without being identical |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
958 # (identical lines must be junk lines, & we don't want to synch up |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
959 # on junk -- unless we have to) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
960 for j in range(blo, bhi): |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
961 bj = b[j] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
962 cruncher.set_seq2(bj) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
963 for i in range(alo, ahi): |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
964 ai = a[i] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
965 if ai == bj: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
966 if eqi is None: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
967 eqi, eqj = i, j |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
968 continue |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
969 cruncher.set_seq1(ai) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
970 # computing similarity is expensive, so use the quick |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
971 # upper bounds first -- have seen this speed up messy |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
972 # compares by a factor of 3. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
973 # note that ratio() is only expensive to compute the first |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
974 # time it's called on a sequence pair; the expensive part |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
975 # of the computation is cached by cruncher |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
976 if cruncher.real_quick_ratio() > best_ratio and \ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
977 cruncher.quick_ratio() > best_ratio and \ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
978 cruncher.ratio() > best_ratio: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
979 best_ratio, best_i, best_j = cruncher.ratio(), i, j |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
980 if best_ratio < cutoff: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
981 # no non-identical "pretty close" pair |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
982 if eqi is None: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
983 # no identical pair either -- treat it as a straight replace |
| 79378 | 984 yield from self._plain_replace(a, alo, ahi, b, blo, bhi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
985 return |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
986 # no close pair, but an identical pair -- synch up on that |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
987 best_i, best_j, best_ratio = eqi, eqj, 1.0 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
988 else: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
989 # there's a close pair, so forget the identical pair (if any) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
990 eqi = None |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
991 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
992 # a[best_i] very similar to b[best_j]; eqi is None iff they're not |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
993 # identical |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
994 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
995 # pump out diffs from before the synch point |
| 79378 | 996 yield from self._fancy_helper(a, alo, best_i, b, blo, best_j) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
997 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
998 # do intraline marking on the synch pair |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
999 aelt, belt = a[best_i], b[best_j] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1000 if eqi is None: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1001 # pump out a '-', '?', '+', '?' quad for the synched lines |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1002 atags = btags = "" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1003 cruncher.set_seqs(aelt, belt) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1004 for tag, ai1, ai2, bj1, bj2 in cruncher.get_opcodes(): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1005 la, lb = ai2 - ai1, bj2 - bj1 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1006 if tag == 'replace': |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1007 atags += '^' * la |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1008 btags += '^' * lb |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1009 elif tag == 'delete': |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1010 atags += '-' * la |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1011 elif tag == 'insert': |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1012 btags += '+' * lb |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1013 elif tag == 'equal': |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1014 atags += ' ' * la |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1015 btags += ' ' * lb |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1016 else: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42142
diff
changeset
|
1017 raise ValueError('unknown tag %r' % (tag,)) |
| 79378 | 1018 yield from self._qformat(aelt, belt, atags, btags) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1019 else: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1020 # the synch pair is identical |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
1021 yield ' ' + aelt |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1022 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1023 # pump out diffs from after the synch point |
| 79378 | 1024 yield from self._fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1025 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1026 def _fancy_helper(self, a, alo, ahi, b, blo, bhi): |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
1027 g = [] |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1028 if alo < ahi: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1029 if blo < bhi: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
1030 g = self._fancy_replace(a, alo, ahi, b, blo, bhi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1031 else: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
1032 g = self._dump('-', a, alo, ahi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1033 elif blo < bhi: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
1034 g = self._dump('+', b, blo, bhi) |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
1035 |
| 79378 | 1036 yield from g |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1037 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1038 def _qformat(self, aline, bline, atags, btags): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1039 r""" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1040 Format "?" output and deal with leading tabs. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1041 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1042 Example: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1043 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1044 >>> d = Differ() |
|
57417
3a2b149ae66e
Merged revisions 76469 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
51776
diff
changeset
|
1045 >>> results = d._qformat('\tabcDefghiJkl\n', '\tabcdefGhijkl\n', |
|
3a2b149ae66e
Merged revisions 76469 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
51776
diff
changeset
|
1046 ... ' ^ ^ ^ ', ' ^ ^ ^ ') |
|
40786
0ce14c4ca133
Revert doubly-converted doctests.
Guido van Rossum <guido@python.org>
parents:
40785
diff
changeset
|
1047 >>> for line in results: print(repr(line)) |
|
0ce14c4ca133
Revert doubly-converted doctests.
Guido van Rossum <guido@python.org>
parents:
40785
diff
changeset
|
1048 ... |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1049 '- \tabcDefghiJkl\n' |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1050 '? \t ^ ^ ^\n' |
|
57417
3a2b149ae66e
Merged revisions 76469 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
51776
diff
changeset
|
1051 '+ \tabcdefGhijkl\n' |
|
3a2b149ae66e
Merged revisions 76469 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
51776
diff
changeset
|
1052 '? \t ^ ^ ^\n' |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1053 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1054 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1055 # Can hurt, but will probably help most of the time. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1056 common = min(_count_leading(aline, "\t"), |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1057 _count_leading(bline, "\t")) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1058 common = min(common, _count_leading(atags[:common], " ")) |
|
57417
3a2b149ae66e
Merged revisions 76469 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
51776
diff
changeset
|
1059 common = min(common, _count_leading(btags[:common], " ")) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1060 atags = atags[common:].rstrip() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1061 btags = btags[common:].rstrip() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1062 |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
1063 yield "- " + aline |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1064 if atags: |
|
20240
5c4261e74478
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20038
diff
changeset
|
1065 yield "? %s%s\n" % ("\t" * common, atags) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1066 |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
1067 yield "+ " + bline |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1068 if btags: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
1069 yield "? %s%s\n" % ("\t" * common, btags) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1070 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1071 # With respect to junk, an earlier version of ndiff simply refused to |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1072 # *start* a match with a junk element. The result was cases like this: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1073 # before: private Thread currentThread; |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1074 # after: private volatile Thread currentThread; |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1075 # If you consider whitespace to be junk, the longest contiguous match |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1076 # not starting with junk is "e Thread currentThread". So ndiff reported |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1077 # that "e volatil" was inserted between the 't' and the 'e' in "private". |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1078 # While an accurate view, to people that's absurd. The current version |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1079 # looks for matching blocks that are entirely junk-free, then extends the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1080 # longest one of those as far as possible but only with matching junk. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1081 # So now "currentThread" is matched, then extended to suck up the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1082 # preceding blank; then "private" is matched, and extended to suck up the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1083 # following blank; then "Thread" is matched; and finally ndiff reports |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1084 # that "volatile " was inserted before "Thread". The only quibble |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1085 # remaining is that perhaps it was really the case that " volatile" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1086 # was inserted after "private". I can live with that <wink>. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1087 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1088 import re |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1089 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1090 def IS_LINE_JUNK(line, pat=re.compile(r"\s*#?\s*$").match): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1091 r""" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1092 Return 1 for ignorable line: iff `line` is blank or contains a single '#'. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1093 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1094 Examples: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1095 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1096 >>> IS_LINE_JUNK('\n') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1097 True |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1098 >>> IS_LINE_JUNK(' # \n') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1099 True |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1100 >>> IS_LINE_JUNK('hello\n') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1101 False |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1102 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1103 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1104 return pat(line) is not None |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1105 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1106 def IS_CHARACTER_JUNK(ch, ws=" \t"): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1107 r""" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1108 Return 1 for ignorable character: iff `ch` is a space or tab. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1109 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1110 Examples: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1111 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1112 >>> IS_CHARACTER_JUNK(' ') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1113 True |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1114 >>> IS_CHARACTER_JUNK('\t') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1115 True |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1116 >>> IS_CHARACTER_JUNK('\n') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1117 False |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1118 >>> IS_CHARACTER_JUNK('x') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1119 False |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1120 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1121 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1122 return ch in ws |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1123 |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1124 |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1125 ######################################################################## |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1126 ### Unified Diff |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1127 ######################################################################## |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1128 |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1129 def _format_range_unified(start, stop): |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1130 'Convert range to the "ed" format' |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1131 # Per the diff spec at http://www.unix.org/single_unix_specification/ |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1132 beginning = start + 1 # lines start numbering with one |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1133 length = stop - start |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1134 if length == 1: |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1135 return '{}'.format(beginning) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1136 if not length: |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1137 beginning -= 1 # empty ranges begin at line just before the range |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1138 return '{},{}'.format(beginning, length) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1139 |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1140 def unified_diff(a, b, fromfile='', tofile='', fromfiledate='', |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1141 tofiledate='', n=3, lineterm='\n'): |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1142 r""" |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1143 Compare two sequences of lines; generate the delta as a unified diff. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1144 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1145 Unified diffs are a compact way of showing line changes and a few |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1146 lines of context. The number of context lines is set by 'n' which |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1147 defaults to three. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1148 |
| 29517 | 1149 By default, the diff control lines (those with ---, +++, or @@) are |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1150 created with a trailing newline. This is helpful so that inputs |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1151 created from file.readlines() result in diffs that are suitable for |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1152 file.writelines() since both the inputs and outputs have trailing |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1153 newlines. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1154 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1155 For inputs that do not have trailing newlines, set the lineterm |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1156 argument to "" so that the output will be uniformly newline free. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1157 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1158 The unidiff format normally has a header for filenames and modification |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1159 times. Any or all of these may be specified using strings for |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1160 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1161 The modification times are normally expressed in the ISO 8601 format. |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1162 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1163 Example: |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1164 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1165 >>> for line in unified_diff('one two three four'.split(), |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1166 ... 'zero one tree four'.split(), 'Original', 'Current', |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1167 ... '2005-01-26 23:30:50', '2010-04-02 10:20:52', |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1168 ... lineterm=''): |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1169 ... print(line) # doctest: +NORMALIZE_WHITESPACE |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1170 --- Original 2005-01-26 23:30:50 |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1171 +++ Current 2010-04-02 10:20:52 |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1172 @@ -1,4 +1,4 @@ |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1173 +zero |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1174 one |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1175 -two |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1176 -three |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1177 +tree |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1178 four |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1179 """ |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1180 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1181 started = False |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1182 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n): |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1183 if not started: |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1184 started = True |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1185 fromdate = '\t{}'.format(fromfiledate) if fromfiledate else '' |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1186 todate = '\t{}'.format(tofiledate) if tofiledate else '' |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1187 yield '--- {}{}{}'.format(fromfile, fromdate, lineterm) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1188 yield '+++ {}{}{}'.format(tofile, todate, lineterm) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1189 |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1190 first, last = group[0], group[-1] |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1191 file1_range = _format_range_unified(first[1], last[2]) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1192 file2_range = _format_range_unified(first[3], last[4]) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1193 yield '@@ -{} +{} @@{}'.format(file1_range, file2_range, lineterm) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1194 |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1195 for tag, i1, i2, j1, j2 in group: |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1196 if tag == 'equal': |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1197 for line in a[i1:i2]: |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1198 yield ' ' + line |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1199 continue |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1200 if tag in {'replace', 'delete'}: |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1201 for line in a[i1:i2]: |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1202 yield '-' + line |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1203 if tag in {'replace', 'insert'}: |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1204 for line in b[j1:j2]: |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1205 yield '+' + line |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1206 |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1207 |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1208 ######################################################################## |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1209 ### Context Diff |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1210 ######################################################################## |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1211 |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1212 def _format_range_context(start, stop): |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1213 'Convert range to the "ed" format' |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1214 # Per the diff spec at http://www.unix.org/single_unix_specification/ |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1215 beginning = start + 1 # lines start numbering with one |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1216 length = stop - start |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1217 if not length: |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1218 beginning -= 1 # empty ranges begin at line just before the range |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1219 if length <= 1: |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1220 return '{}'.format(beginning) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1221 return '{},{}'.format(beginning, beginning + length - 1) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1222 |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1223 # See http://www.unix.org/single_unix_specification/ |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1224 def context_diff(a, b, fromfile='', tofile='', |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1225 fromfiledate='', tofiledate='', n=3, lineterm='\n'): |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1226 r""" |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1227 Compare two sequences of lines; generate the delta as a context diff. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1228 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1229 Context diffs are a compact way of showing line changes and a few |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1230 lines of context. The number of context lines is set by 'n' which |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1231 defaults to three. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1232 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1233 By default, the diff control lines (those with *** or ---) are |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1234 created with a trailing newline. This is helpful so that inputs |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1235 created from file.readlines() result in diffs that are suitable for |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1236 file.writelines() since both the inputs and outputs have trailing |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1237 newlines. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1238 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1239 For inputs that do not have trailing newlines, set the lineterm |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1240 argument to "" so that the output will be uniformly newline free. |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1241 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1242 The context diff format normally has a header for filenames and |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1243 modification times. Any or all of these may be specified using |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1244 strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1245 The modification times are normally expressed in the ISO 8601 format. |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1246 If not specified, the strings default to blanks. |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1247 |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1248 Example: |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1249 |
|
72485
83f43b58c988
#13012: use splitlines(keepends=True/False) instead of splitlines(0/1).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
69281
diff
changeset
|
1250 >>> print(''.join(context_diff('one\ntwo\nthree\nfour\n'.splitlines(True), |
|
83f43b58c988
#13012: use splitlines(keepends=True/False) instead of splitlines(0/1).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
69281
diff
changeset
|
1251 ... 'zero\none\ntree\nfour\n'.splitlines(True), 'Original', 'Current')), |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
1252 ... end="") |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1253 *** Original |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1254 --- Current |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1255 *************** |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1256 *** 1,4 **** |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1257 one |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1258 ! two |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1259 ! three |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1260 four |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1261 --- 1,4 ---- |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1262 + zero |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1263 one |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1264 ! tree |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1265 four |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1266 """ |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1267 |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1268 prefix = dict(insert='+ ', delete='- ', replace='! ', equal=' ') |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1269 started = False |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1270 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n): |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1271 if not started: |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1272 started = True |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1273 fromdate = '\t{}'.format(fromfiledate) if fromfiledate else '' |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1274 todate = '\t{}'.format(tofiledate) if tofiledate else '' |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1275 yield '*** {}{}{}'.format(fromfile, fromdate, lineterm) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1276 yield '--- {}{}{}'.format(tofile, todate, lineterm) |
|
29384
2d39ec0e55aa
For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents:
29381
diff
changeset
|
1277 |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1278 first, last = group[0], group[-1] |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1279 yield '***************' + lineterm |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1280 |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1281 file1_range = _format_range_context(first[1], last[2]) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1282 yield '*** {} ****{}'.format(file1_range, lineterm) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1283 |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1284 if any(tag in {'replace', 'delete'} for tag, _, _, _, _ in group): |
|
29384
2d39ec0e55aa
For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents:
29381
diff
changeset
|
1285 for tag, i1, i2, _, _ in group: |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1286 if tag != 'insert': |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1287 for line in a[i1:i2]: |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1288 yield prefix[tag] + line |
|
29384
2d39ec0e55aa
For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents:
29381
diff
changeset
|
1289 |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1290 file2_range = _format_range_context(first[3], last[4]) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1291 yield '--- {} ----{}'.format(file2_range, lineterm) |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1292 |
|
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1293 if any(tag in {'replace', 'insert'} for tag, _, _, _, _ in group): |
|
29384
2d39ec0e55aa
For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents:
29381
diff
changeset
|
1294 for tag, _, _, j1, j2 in group: |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1295 if tag != 'delete': |
|
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1296 for line in b[j1:j2]: |
|
69277
707078ca0a77
Issue 11747: Fix output format for context diffs.
Raymond Hettinger <python@rcn.com>
parents:
68585
diff
changeset
|
1297 yield prefix[tag] + line |
|
29381
15f1dc5b7ddc
Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents:
25953
diff
changeset
|
1298 |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
1299 def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK): |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1300 r""" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1301 Compare `a` and `b` (lists of strings); return a `Differ`-style delta. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1302 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1303 Optional keyword parameters `linejunk` and `charjunk` are for filter |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1304 functions (or None): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1305 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1306 - linejunk: A function that should accept a single string argument, and |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
1307 return true iff the string is junk. The default is None, and is |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
1308 recommended; as of Python 2.3, an adaptive notion of "noise" lines is |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
1309 used that does a good job on its own. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1310 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1311 - charjunk: A function that should accept a string of length 1. The |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1312 default is module-level function IS_CHARACTER_JUNK, which filters out |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1313 whitespace characters (a blank or tab; note: bad idea to include newline |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1314 in this!). |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1315 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1316 Tools/scripts/ndiff.py is a command-line front-end to this function. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1317 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1318 Example: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1319 |
|
72485
83f43b58c988
#13012: use splitlines(keepends=True/False) instead of splitlines(0/1).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
69281
diff
changeset
|
1320 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True), |
|
83f43b58c988
#13012: use splitlines(keepends=True/False) instead of splitlines(0/1).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
69281
diff
changeset
|
1321 ... 'ore\ntree\nemu\n'.splitlines(keepends=True)) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
1322 >>> print(''.join(diff), end="") |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1323 - one |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1324 ? ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1325 + ore |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1326 ? ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1327 - two |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1328 - three |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1329 ? - |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1330 + tree |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1331 + emu |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1332 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1333 return Differ(linejunk, charjunk).compare(a, b) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1334 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1335 def _mdiff(fromlines, tolines, context=None, linejunk=None, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1336 charjunk=IS_CHARACTER_JUNK): |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39627
diff
changeset
|
1337 r"""Returns generator yielding marked up from/to side by side differences. |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1338 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1339 Arguments: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1340 fromlines -- list of text lines to compared to tolines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1341 tolines -- list of text lines to be compared to fromlines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1342 context -- number of context lines to display on each side of difference, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1343 if None, all from/to text lines will be generated. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1344 linejunk -- passed on to ndiff (see ndiff documentation) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1345 charjunk -- passed on to ndiff (see ndiff documentation) |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1346 |
|
85221
e07f104133d5
#18705: fix a number of typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73917
diff
changeset
|
1347 This function returns an iterator which returns a tuple: |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1348 (from line tuple, to line tuple, boolean flag) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1349 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1350 from/to line tuple -- (line num, line text) |
|
51776
d06a3ebf306f
Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
44643
diff
changeset
|
1351 line num -- integer or None (to indicate a context separation) |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1352 line text -- original line text with following markers inserted: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1353 '\0+' -- marks start of added text |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1354 '\0-' -- marks start of deleted text |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1355 '\0^' -- marks start of changed text |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1356 '\1' -- marks end of added/deleted/changed text |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1357 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1358 boolean flag -- None indicates context separation, True indicates |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1359 either "from" or "to" line contains a change, otherwise False. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1360 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1361 This function/iterator was originally developed to generate side by side |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1362 file difference for making HTML pages (see HtmlDiff class for example |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1363 usage). |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1364 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1365 Note, this function utilizes the ndiff function to generate the side by |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1366 side difference markup. Optional ndiff arguments may be passed to this |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1367 function and they in turn will be passed to ndiff. |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1368 """ |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1369 import re |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1370 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1371 # regular expression for finding intraline change indices |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1372 change_re = re.compile('(\++|\-+|\^+)') |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1373 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1374 # create the difference iterator to generate the differences |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1375 diff_lines_iterator = ndiff(fromlines,tolines,linejunk,charjunk) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1376 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1377 def _make_line(lines, format_key, side, num_lines=[0,0]): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1378 """Returns line of text with user's change markup and line formatting. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1379 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1380 lines -- list of lines from the ndiff generator to produce a line of |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1381 text from. When producing the line of text to return, the |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1382 lines used are removed from this list. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1383 format_key -- '+' return first line in list with "add" markup around |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1384 the entire line. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1385 '-' return first line in list with "delete" markup around |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1386 the entire line. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1387 '?' return first line in list with add/delete/change |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1388 intraline markup (indices obtained from second line) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1389 None return first line in list with no markup |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1390 side -- indice into the num_lines list (0=from,1=to) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1391 num_lines -- from/to current line number. This is NOT intended to be a |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1392 passed parameter. It is present as a keyword argument to |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1393 maintain memory of the current line numbers between calls |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1394 of this function. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1395 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1396 Note, this function is purposefully not defined at the module scope so |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1397 that data it needs from its parent function (within whose context it |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1398 is defined) does not need to be of module scope. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1399 """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1400 num_lines[side] += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1401 # Handle case where no user markup is to be added, just return line of |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1402 # text with user's line format to allow for usage of the line number. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1403 if format_key is None: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1404 return (num_lines[side],lines.pop(0)[2:]) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1405 # Handle case of intraline changes |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1406 if format_key == '?': |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1407 text, markers = lines.pop(0), lines.pop(0) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1408 # find intraline changes (store change type and indices in tuples) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1409 sub_info = [] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1410 def record_sub_info(match_object,sub_info=sub_info): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1411 sub_info.append([match_object.group(1)[0],match_object.span()]) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1412 return match_object.group(1) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1413 change_re.sub(record_sub_info,markers) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1414 # process each tuple inserting our special marks that won't be |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1415 # noticed by an xml/html escaper. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1416 for key,(begin,end) in sub_info[::-1]: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1417 text = text[0:begin]+'\0'+key+text[begin:end]+'\1'+text[end:] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1418 text = text[2:] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1419 # Handle case of add/delete entire line |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1420 else: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1421 text = lines.pop(0)[2:] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1422 # if line of text is just a newline, insert a space so there is |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1423 # something for the user to highlight and see. |
|
33981
ec29955b8543
SF bug 1054821: difflib HtmlDiff() extra space on inserted 1 character lines
Tim Peters <tim.peters@gmail.com>
parents:
33448
diff
changeset
|
1424 if not text: |
|
ec29955b8543
SF bug 1054821: difflib HtmlDiff() extra space on inserted 1 character lines
Tim Peters <tim.peters@gmail.com>
parents:
33448
diff
changeset
|
1425 text = ' ' |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1426 # insert marks that won't be noticed by an xml/html escaper. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1427 text = '\0' + format_key + text + '\1' |
|
35276
4adbd331d335
Fix all wrong instances of "it's".
Georg Brandl <georg@python.org>
parents:
33981
diff
changeset
|
1428 # Return line of text, first allow user's line formatter to do its |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1429 # thing (such as adding the line number) then replace the special |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1430 # marks with what the user's change markup. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1431 return (num_lines[side],text) |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1432 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1433 def _line_iterator(): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1434 """Yields from/to lines of text with a change indication. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1435 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1436 This function is an iterator. It itself pulls lines from a |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1437 differencing iterator, processes them and yields them. When it can |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1438 it yields both a "from" and a "to" line, otherwise it will yield one |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1439 or the other. In addition to yielding the lines of from/to text, a |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1440 boolean flag is yielded to indicate if the text line(s) have |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1441 differences in them. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1442 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1443 Note, this function is purposefully not defined at the module scope so |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1444 that data it needs from its parent function (within whose context it |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1445 is defined) does not need to be of module scope. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1446 """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1447 lines = [] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1448 num_blanks_pending, num_blanks_to_yield = 0, 0 |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1449 while True: |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1450 # Load up next 4 lines so we can look ahead, create strings which |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1451 # are a concatenation of the first character of each of the 4 lines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1452 # so we can do some very readable comparisons. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1453 while len(lines) < 4: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1454 try: |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40809
diff
changeset
|
1455 lines.append(next(diff_lines_iterator)) |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1456 except StopIteration: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1457 lines.append('X') |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1458 s = ''.join([line[0] for line in lines]) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1459 if s.startswith('X'): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1460 # When no more lines, pump out any remaining blank lines so the |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1461 # corresponding add/delete lines get a matching blank line so |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1462 # all line pairs get yielded at the next level. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1463 num_blanks_to_yield = num_blanks_pending |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1464 elif s.startswith('-?+?'): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1465 # simple intraline change |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1466 yield _make_line(lines,'?',0), _make_line(lines,'?',1), True |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1467 continue |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1468 elif s.startswith('--++'): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1469 # in delete block, add block coming: we do NOT want to get |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1470 # caught up on blank lines yet, just process the delete line |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1471 num_blanks_pending -= 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1472 yield _make_line(lines,'-',0), None, True |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1473 continue |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
1474 elif s.startswith(('--?+', '--+', '- ')): |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1475 # in delete block and see a intraline change or unchanged line |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1476 # coming: yield the delete line and then blanks |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1477 from_line,to_line = _make_line(lines,'-',0), None |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1478 num_blanks_to_yield,num_blanks_pending = num_blanks_pending-1,0 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1479 elif s.startswith('-+?'): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1480 # intraline change |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1481 yield _make_line(lines,None,0), _make_line(lines,'?',1), True |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1482 continue |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1483 elif s.startswith('-?+'): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1484 # intraline change |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1485 yield _make_line(lines,'?',0), _make_line(lines,None,1), True |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1486 continue |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1487 elif s.startswith('-'): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1488 # delete FROM line |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1489 num_blanks_pending -= 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1490 yield _make_line(lines,'-',0), None, True |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1491 continue |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1492 elif s.startswith('+--'): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1493 # in add block, delete block coming: we do NOT want to get |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1494 # caught up on blank lines yet, just process the add line |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1495 num_blanks_pending += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1496 yield None, _make_line(lines,'+',1), True |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1497 continue |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36429
diff
changeset
|
1498 elif s.startswith(('+ ', '+-')): |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1499 # will be leaving an add block: yield blanks then add line |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1500 from_line, to_line = None, _make_line(lines,'+',1) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1501 num_blanks_to_yield,num_blanks_pending = num_blanks_pending+1,0 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1502 elif s.startswith('+'): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1503 # inside an add block, yield the add line |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1504 num_blanks_pending += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1505 yield None, _make_line(lines,'+',1), True |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1506 continue |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1507 elif s.startswith(' '): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1508 # unchanged text, yield it to both sides |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1509 yield _make_line(lines[:],None,0),_make_line(lines,None,1),False |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1510 continue |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1511 # Catch up on the blank lines so when we yield the next from/to |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1512 # pair, they are lined up. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1513 while(num_blanks_to_yield < 0): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1514 num_blanks_to_yield += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1515 yield None,('','\n'),True |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1516 while(num_blanks_to_yield > 0): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1517 num_blanks_to_yield -= 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1518 yield ('','\n'),None,True |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1519 if s.startswith('X'): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1520 raise StopIteration |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1521 else: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1522 yield from_line,to_line,True |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1523 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1524 def _line_pair_iterator(): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1525 """Yields from/to lines of text with a change indication. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1526 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1527 This function is an iterator. It itself pulls lines from the line |
|
35276
4adbd331d335
Fix all wrong instances of "it's".
Georg Brandl <georg@python.org>
parents:
33981
diff
changeset
|
1528 iterator. Its difference from that iterator is that this function |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1529 always yields a pair of from/to text lines (with the change |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1530 indication). If necessary it will collect single from/to lines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1531 until it has a matching pair from/to pair to yield. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1532 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1533 Note, this function is purposefully not defined at the module scope so |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1534 that data it needs from its parent function (within whose context it |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1535 is defined) does not need to be of module scope. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1536 """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1537 line_iterator = _line_iterator() |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1538 fromlines,tolines=[],[] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1539 while True: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1540 # Collecting lines of text until we have a from/to pair |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1541 while (len(fromlines)==0 or len(tolines)==0): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40809
diff
changeset
|
1542 from_line, to_line, found_diff = next(line_iterator) |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1543 if from_line is not None: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1544 fromlines.append((from_line,found_diff)) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1545 if to_line is not None: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1546 tolines.append((to_line,found_diff)) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1547 # Once we have a pair, remove them from the collection and yield it |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1548 from_line, fromDiff = fromlines.pop(0) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1549 to_line, to_diff = tolines.pop(0) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1550 yield (from_line,to_line,fromDiff or to_diff) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1551 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1552 # Handle case where user does not want context differencing, just yield |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1553 # them up without doing anything else with them. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1554 line_pair_iterator = _line_pair_iterator() |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1555 if context is None: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1556 while True: |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40809
diff
changeset
|
1557 yield next(line_pair_iterator) |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1558 # Handle case where user wants context differencing. We must do some |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1559 # storage of lines until we know for sure that they are to be yielded. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1560 else: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1561 context += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1562 lines_to_write = 0 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1563 while True: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1564 # Store lines up until we find a difference, note use of a |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1565 # circular queue because we only need to keep around what |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1566 # we need for context. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1567 index, contextLines = 0, [None]*(context) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1568 found_diff = False |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1569 while(found_diff is False): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40809
diff
changeset
|
1570 from_line, to_line, found_diff = next(line_pair_iterator) |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1571 i = index % context |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1572 contextLines[i] = (from_line, to_line, found_diff) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1573 index += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1574 # Yield lines that we have collected so far, but first yield |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1575 # the user's separator. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1576 if index > context: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1577 yield None, None, None |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1578 lines_to_write = context |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1579 else: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1580 lines_to_write = index |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1581 index = 0 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1582 while(lines_to_write): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1583 i = index % context |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1584 index += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1585 yield contextLines[i] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1586 lines_to_write -= 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1587 # Now yield the context lines after the change |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1588 lines_to_write = context-1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1589 while(lines_to_write): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40809
diff
changeset
|
1590 from_line, to_line, found_diff = next(line_pair_iterator) |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1591 # If another change within the context, extend the context |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1592 if found_diff: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1593 lines_to_write = context-1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1594 else: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1595 lines_to_write -= 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1596 yield from_line, to_line, found_diff |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1597 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1598 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1599 _file_template = """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1600 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1601 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1602 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1603 <html> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1604 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1605 <head> |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1606 <meta http-equiv="Content-Type" |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1607 content="text/html; charset=ISO-8859-1" /> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1608 <title></title> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1609 <style type="text/css">%(styles)s |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1610 </style> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1611 </head> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1612 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1613 <body> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1614 %(table)s%(legend)s |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1615 </body> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1616 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1617 </html>""" |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1618 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1619 _styles = """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1620 table.diff {font-family:Courier; border:medium;} |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1621 .diff_header {background-color:#e0e0e0} |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1622 td.diff_header {text-align:right} |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1623 .diff_next {background-color:#c0c0c0} |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1624 .diff_add {background-color:#aaffaa} |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1625 .diff_chg {background-color:#ffff77} |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1626 .diff_sub {background-color:#ffaaaa}""" |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1627 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1628 _table_template = """ |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1629 <table class="diff" id="difflib_chg_%(prefix)s_top" |
|
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1630 cellspacing="0" cellpadding="0" rules="groups" > |
|
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1631 <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1632 <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1633 %(header_row)s |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1634 <tbody> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1635 %(data_rows)s </tbody> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1636 </table>""" |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1637 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1638 _legend = """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1639 <table class="diff" summary="Legends"> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1640 <tr> <th colspan="2"> Legends </th> </tr> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1641 <tr> <td> <table border="" summary="Colors"> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1642 <tr><th> Colors </th> </tr> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1643 <tr><td class="diff_add"> Added </td></tr> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1644 <tr><td class="diff_chg">Changed</td> </tr> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1645 <tr><td class="diff_sub">Deleted</td> </tr> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1646 </table></td> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1647 <td> <table border="" summary="Links"> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1648 <tr><th colspan="2"> Links </th> </tr> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1649 <tr><td>(f)irst change</td> </tr> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1650 <tr><td>(n)ext change</td> </tr> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1651 <tr><td>(t)op</td> </tr> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1652 </table></td> </tr> |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1653 </table>""" |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1654 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1655 class HtmlDiff(object): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1656 """For producing HTML side by side comparison with change highlights. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1657 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1658 This class can be used to create an HTML table (or a complete HTML file |
|
33448
5570fae1f01c
Typo fix: 'comparisions' is not a word
Andrew M. Kuchling <amk@amk.ca>
parents:
33326
diff
changeset
|
1659 containing the table) showing a side by side, line by line comparison |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1660 of text with inter-line and intra-line change highlights. The table can |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1661 be generated in either full or contextual difference mode. |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1662 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1663 The following methods are provided for HTML generation: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1664 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1665 make_table -- generates HTML for a single side by side table |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1666 make_file -- generates complete HTML file with a single side by side table |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1667 |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1668 See tools/scripts/diff.py for an example usage of this class. |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1669 """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1670 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1671 _file_template = _file_template |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1672 _styles = _styles |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1673 _table_template = _table_template |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1674 _legend = _legend |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1675 _default_prefix = 0 |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1676 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1677 def __init__(self,tabsize=8,wrapcolumn=None,linejunk=None, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1678 charjunk=IS_CHARACTER_JUNK): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1679 """HtmlDiff instance initializer |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1680 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1681 Arguments: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1682 tabsize -- tab stop spacing, defaults to 8. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1683 wrapcolumn -- column number where lines are broken and wrapped, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1684 defaults to None where lines are not wrapped. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1685 linejunk,charjunk -- keyword arguments passed into ndiff() (used to by |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1686 HtmlDiff() to generate the side by side HTML differences). See |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1687 ndiff() documentation for argument default values and descriptions. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1688 """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1689 self._tabsize = tabsize |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1690 self._wrapcolumn = wrapcolumn |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1691 self._linejunk = linejunk |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1692 self._charjunk = charjunk |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1693 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1694 def make_file(self,fromlines,tolines,fromdesc='',todesc='',context=False, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1695 numlines=5): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1696 """Returns HTML file of side by side comparison with change highlights |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1697 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1698 Arguments: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1699 fromlines -- list of "from" lines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1700 tolines -- list of "to" lines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1701 fromdesc -- "from" file column header string |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1702 todesc -- "to" file column header string |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1703 context -- set to True for contextual differences (defaults to False |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1704 which shows full differences). |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1705 numlines -- number of context lines. When context is set True, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1706 controls number of lines displayed before and after the change. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1707 When context is False, controls the number of lines to place |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1708 the "next" link anchors before the next change (so click of |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1709 "next" link jumps to just before the change). |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1710 """ |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1711 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1712 return self._file_template % dict( |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1713 styles = self._styles, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1714 legend = self._legend, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1715 table = self.make_table(fromlines,tolines,fromdesc,todesc, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1716 context=context,numlines=numlines)) |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1717 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1718 def _tab_newline_replace(self,fromlines,tolines): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1719 """Returns from/to line lists with tabs expanded and newlines removed. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1720 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1721 Instead of tab characters being replaced by the number of spaces |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1722 needed to fill in to the next tab stop, this function will fill |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1723 the space with tab characters. This is done so that the difference |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1724 algorithms can identify changes in a file when tabs are replaced by |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1725 spaces and vice versa. At the end of the HTML generation, the tab |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1726 characters will be replaced with a nonbreakable space. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1727 """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1728 def expand_tabs(line): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1729 # hide real spaces |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1730 line = line.replace(' ','\0') |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1731 # expand tabs into spaces |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1732 line = line.expandtabs(self._tabsize) |
|
68585
bf94b6a73fc8
#11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57417
diff
changeset
|
1733 # replace spaces from expanded tabs back into tab characters |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1734 # (we'll replace them with markup after we do differencing) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1735 line = line.replace(' ','\t') |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1736 return line.replace('\0',' ').rstrip('\n') |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1737 fromlines = [expand_tabs(line) for line in fromlines] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1738 tolines = [expand_tabs(line) for line in tolines] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1739 return fromlines,tolines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1740 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1741 def _split_line(self,data_list,line_num,text): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1742 """Builds list of text lines by splitting text lines at wrap point |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1743 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1744 This function will determine if the input text line needs to be |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1745 wrapped (split) into separate lines. If so, the first wrap point |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1746 will be determined and the first line appended to the output |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1747 text line list. This function is used recursively to handle |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1748 the second part of the split line to further split it. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1749 """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1750 # if blank line or context separator, just add it to the output list |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1751 if not line_num: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1752 data_list.append((line_num,text)) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1753 return |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1754 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1755 # if line text doesn't need wrapping, just add it to the output list |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1756 size = len(text) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1757 max = self._wrapcolumn |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1758 if (size <= max) or ((size -(text.count('\0')*3)) <= max): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1759 data_list.append((line_num,text)) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1760 return |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1761 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1762 # scan text looking for the wrap point, keeping track if the wrap |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1763 # point is inside markers |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1764 i = 0 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1765 n = 0 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1766 mark = '' |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1767 while n < max and i < size: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1768 if text[i] == '\0': |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1769 i += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1770 mark = text[i] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1771 i += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1772 elif text[i] == '\1': |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1773 i += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1774 mark = '' |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1775 else: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1776 i += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1777 n += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1778 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1779 # wrap point is inside text, break it up into separate lines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1780 line1 = text[:i] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1781 line2 = text[i:] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1782 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1783 # if wrap point is inside markers, place end marker at end of first |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1784 # line and start marker at beginning of second line because each |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1785 # line will have its own table tag markup around it. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1786 if mark: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1787 line1 = line1 + '\1' |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1788 line2 = '\0' + mark + line2 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1789 |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1790 # tack on first line onto the output list |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1791 data_list.append((line_num,line1)) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1792 |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1793 # use this routine again to wrap the remaining text |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1794 self._split_line(data_list,'>',line2) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1795 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1796 def _line_wrapper(self,diffs): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1797 """Returns iterator that splits (wraps) mdiff text lines""" |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1798 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1799 # pull from/to data and flags from mdiff iterator |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1800 for fromdata,todata,flag in diffs: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1801 # check for context separators and pass them through |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1802 if flag is None: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1803 yield fromdata,todata,flag |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1804 continue |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1805 (fromline,fromtext),(toline,totext) = fromdata,todata |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1806 # for each from/to line split it at the wrap column to form |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1807 # list of text lines. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1808 fromlist,tolist = [],[] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1809 self._split_line(fromlist,fromline,fromtext) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1810 self._split_line(tolist,toline,totext) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1811 # yield from/to line in pairs inserting blank lines as |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1812 # necessary when one side has more wrapped lines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1813 while fromlist or tolist: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1814 if fromlist: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1815 fromdata = fromlist.pop(0) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1816 else: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1817 fromdata = ('',' ') |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1818 if tolist: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1819 todata = tolist.pop(0) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1820 else: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1821 todata = ('',' ') |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1822 yield fromdata,todata,flag |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1823 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1824 def _collect_lines(self,diffs): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1825 """Collects mdiff output into separate lists |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1826 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1827 Before storing the mdiff from/to data into a list, it is converted |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1828 into a single line of text with HTML markup. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1829 """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1830 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1831 fromlist,tolist,flaglist = [],[],[] |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1832 # pull from/to data and flags from mdiff style iterator |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1833 for fromdata,todata,flag in diffs: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1834 try: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1835 # store HTML markup of the lines into the lists |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1836 fromlist.append(self._format_line(0,flag,*fromdata)) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1837 tolist.append(self._format_line(1,flag,*todata)) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1838 except TypeError: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1839 # exceptions occur for lines where context separators go |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1840 fromlist.append(None) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1841 tolist.append(None) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1842 flaglist.append(flag) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1843 return fromlist,tolist,flaglist |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1844 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1845 def _format_line(self,side,flag,linenum,text): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1846 """Returns HTML markup of "from" / "to" text lines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1847 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1848 side -- 0 or 1 indicating "from" or "to" text |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1849 flag -- indicates if difference on line |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1850 linenum -- line number (used for line number column) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1851 text -- line text to be marked up |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1852 """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1853 try: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1854 linenum = '%d' % linenum |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1855 id = ' id="%s%s"' % (self._prefix[side],linenum) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1856 except TypeError: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1857 # handle blank lines where linenum is '>' or '' |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1858 id = '' |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1859 # replace those things that would get confused with HTML symbols |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1860 text=text.replace("&","&").replace(">",">").replace("<","<") |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1861 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1862 # make space non-breakable so they don't get compressed or line wrapped |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1863 text = text.replace(' ',' ').rstrip() |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1864 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1865 return '<td class="diff_header"%s>%s</td><td nowrap="nowrap">%s</td>' \ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1866 % (id,linenum,text) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1867 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1868 def _make_prefix(self): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1869 """Create unique anchor prefixes""" |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1870 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1871 # Generate a unique anchor prefix so multiple tables |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1872 # can exist on the same HTML page without conflicts. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1873 fromprefix = "from%d_" % HtmlDiff._default_prefix |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1874 toprefix = "to%d_" % HtmlDiff._default_prefix |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1875 HtmlDiff._default_prefix += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1876 # store prefixes so line format method has access |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1877 self._prefix = [fromprefix,toprefix] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1878 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1879 def _convert_flags(self,fromlist,tolist,flaglist,context,numlines): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1880 """Makes list of "next" links""" |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1881 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1882 # all anchor names will be generated using the unique "to" prefix |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1883 toprefix = self._prefix[1] |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1884 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1885 # process change flags, generating middle column of next anchors/links |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1886 next_id = ['']*len(flaglist) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1887 next_href = ['']*len(flaglist) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1888 num_chg, in_change = 0, False |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1889 last = 0 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1890 for i,flag in enumerate(flaglist): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1891 if flag: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1892 if not in_change: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1893 in_change = True |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1894 last = i |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1895 # at the beginning of a change, drop an anchor a few lines |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1896 # (the context lines) before the change for the previous |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1897 # link |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1898 i = max([0,i-numlines]) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1899 next_id[i] = ' id="difflib_chg_%s_%d"' % (toprefix,num_chg) |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1900 # at the beginning of a change, drop a link to the next |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1901 # change |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1902 num_chg += 1 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1903 next_href[last] = '<a href="#difflib_chg_%s_%d">n</a>' % ( |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1904 toprefix,num_chg) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1905 else: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1906 in_change = False |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1907 # check for cases where there is no content to avoid exceptions |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1908 if not flaglist: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1909 flaglist = [False] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1910 next_id = [''] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1911 next_href = [''] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1912 last = 0 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1913 if context: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1914 fromlist = ['<td></td><td> No Differences Found </td>'] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1915 tolist = fromlist |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1916 else: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1917 fromlist = tolist = ['<td></td><td> Empty File </td>'] |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1918 # if not a change on first line, drop a link |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1919 if not flaglist[0]: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1920 next_href[0] = '<a href="#difflib_chg_%s_0">f</a>' % toprefix |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1921 # redo the last link to link to the top |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1922 next_href[last] = '<a href="#difflib_chg_%s_top">t</a>' % (toprefix) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1923 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1924 return fromlist,tolist,flaglist,next_href,next_id |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1925 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1926 def make_table(self,fromlines,tolines,fromdesc='',todesc='',context=False, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1927 numlines=5): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1928 """Returns HTML table of side by side comparison with change highlights |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1929 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1930 Arguments: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1931 fromlines -- list of "from" lines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1932 tolines -- list of "to" lines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1933 fromdesc -- "from" file column header string |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1934 todesc -- "to" file column header string |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1935 context -- set to True for contextual differences (defaults to False |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1936 which shows full differences). |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1937 numlines -- number of context lines. When context is set True, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1938 controls number of lines displayed before and after the change. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1939 When context is False, controls the number of lines to place |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1940 the "next" link anchors before the next change (so click of |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1941 "next" link jumps to just before the change). |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1942 """ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1943 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1944 # make unique anchor prefixes so that multiple tables may exist |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1945 # on the same page without conflict. |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1946 self._make_prefix() |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1947 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1948 # change tabs to spaces before it gets more difficult after we insert |
|
85221
e07f104133d5
#18705: fix a number of typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
73917
diff
changeset
|
1949 # markup |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1950 fromlines,tolines = self._tab_newline_replace(fromlines,tolines) |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1951 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1952 # create diffs iterator which generates side by side from/to data |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1953 if context: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1954 context_lines = numlines |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1955 else: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1956 context_lines = None |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1957 diffs = _mdiff(fromlines,tolines,context_lines,linejunk=self._linejunk, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1958 charjunk=self._charjunk) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1959 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1960 # set up iterator to wrap lines that exceed desired width |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1961 if self._wrapcolumn: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1962 diffs = self._line_wrapper(diffs) |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
1963 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1964 # collect up from/to lines and flags into lists (also format the lines) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1965 fromlist,tolist,flaglist = self._collect_lines(diffs) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1966 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1967 # process change flags, generating middle column of next anchors/links |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1968 fromlist,tolist,flaglist,next_href,next_id = self._convert_flags( |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1969 fromlist,tolist,flaglist,context,numlines) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1970 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
1971 s = [] |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1972 fmt = ' <tr><td class="diff_next"%s>%s</td>%s' + \ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1973 '<td class="diff_next">%s</td>%s</tr>\n' |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1974 for i in range(len(flaglist)): |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1975 if flaglist[i] is None: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1976 # mdiff yields None on separator lines skip the bogus ones |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1977 # generated for the first line |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1978 if i > 0: |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
1979 s.append(' </tbody> \n <tbody>\n') |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1980 else: |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
1981 s.append( fmt % (next_id[i],next_href[i],fromlist[i], |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1982 next_href[i],tolist[i])) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1983 if fromdesc or todesc: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1984 header_row = '<thead><tr>%s%s%s%s</tr></thead>' % ( |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1985 '<th class="diff_next"><br /></th>', |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1986 '<th colspan="2" class="diff_header">%s</th>' % fromdesc, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1987 '<th class="diff_next"><br /></th>', |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1988 '<th colspan="2" class="diff_header">%s</th>' % todesc) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1989 else: |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1990 header_row = '' |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1991 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1992 table = self._table_template % dict( |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
1993 data_rows=''.join(s), |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1994 header_row=header_row, |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1995 prefix=self._prefix[1]) |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1996 |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1997 return table.replace('\0+','<span class="diff_add">'). \ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1998 replace('\0-','<span class="diff_sub">'). \ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
1999 replace('\0^','<span class="diff_chg">'). \ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
2000 replace('\1','</span>'). \ |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
2001 replace('\t',' ') |
|
33326
787fff51bf2a
Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents:
33324
diff
changeset
|
2002 |
|
33319
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
2003 del re |
|
6d3bdd19beeb
Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents:
32867
diff
changeset
|
2004 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2005 def restore(delta, which): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2006 r""" |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
2007 Generate one of the two sequences that generated a delta. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2008 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2009 Given a `delta` produced by `Differ.compare()` or `ndiff()`, extract |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2010 lines originating from file 1 or 2 (parameter `which`), stripping off line |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2011 prefixes. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2012 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2013 Examples: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2014 |
|
72485
83f43b58c988
#13012: use splitlines(keepends=True/False) instead of splitlines(0/1).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
69281
diff
changeset
|
2015 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True), |
|
83f43b58c988
#13012: use splitlines(keepends=True/False) instead of splitlines(0/1).
Ezio Melotti <ezio.melotti@gmail.com>
parents:
69281
diff
changeset
|
2016 ... 'ore\ntree\nemu\n'.splitlines(keepends=True)) |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
2017 >>> diff = list(diff) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
2018 >>> print(''.join(restore(diff, 1)), end="") |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2019 one |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2020 two |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2021 three |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40582
diff
changeset
|
2022 >>> print(''.join(restore(diff, 2)), end="") |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2023 ore |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2024 tree |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2025 emu |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2026 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2027 try: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2028 tag = {1: "- ", 2: "+ "}[int(which)] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2029 except KeyError: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42142
diff
changeset
|
2030 raise ValueError('unknown delta choice (must be 1 or 2): %r' |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2031 % which) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2032 prefixes = (" ", tag) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2033 for line in delta: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2034 if line[:2] in prefixes: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
2035 yield line[2:] |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
2036 |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2037 def _test(): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2038 import doctest, difflib |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2039 return doctest.testmod(difflib) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2040 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2041 if __name__ == "__main__": |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2042 _test() |
