blob: 5d756436a67e194b47e8867db3ecbac408ab10fa [file] [log] [blame]
Tim Peters9ae21482001-02-10 08:00:53 +00001"""
2Module difflib -- helpers for computing deltas between objects.
3
4Function get_close_matches(word, possibilities, n=3, cutoff=0.6):
Tim Peters9ae21482001-02-10 08:00:53 +00005 Use SequenceMatcher to return list of the best "good enough" matches.
6
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00007Function context_diff(a, b):
8 For two lists of strings, return a delta in context diff format.
9
Tim Peters5e824c32001-08-12 22:25:01 +000010Function ndiff(a, b):
11 Return a delta: the difference between `a` and `b` (lists of strings).
Tim Peters9ae21482001-02-10 08:00:53 +000012
Tim Peters5e824c32001-08-12 22:25:01 +000013Function restore(delta, which):
14 Return one of the two sequences that generated an ndiff delta.
Tim Peters9ae21482001-02-10 08:00:53 +000015
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +000016Function unified_diff(a, b):
17 For two lists of strings, return a delta in unified diff format.
18
Tim Peters5e824c32001-08-12 22:25:01 +000019Class SequenceMatcher:
20 A flexible class for comparing pairs of sequences of any type.
Tim Peters9ae21482001-02-10 08:00:53 +000021
Tim Peters5e824c32001-08-12 22:25:01 +000022Class Differ:
23 For producing human-readable deltas from sequences of lines of text.
Martin v. Löwise064b412004-08-29 16:34:40 +000024
25Class HtmlDiff:
26 For producing HTML side by side comparison with change highlights.
Tim Peters9ae21482001-02-10 08:00:53 +000027"""
28
Tim Peters5e824c32001-08-12 22:25:01 +000029__all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher',
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +000030 'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff',
Greg Ward4d9d2562015-04-20 20:21:21 -040031 'unified_diff', 'diff_bytes', 'HtmlDiff', 'Match']
Tim Peters5e824c32001-08-12 22:25:01 +000032
Raymond Hettingerae39fbd2014-08-03 22:40:59 -070033from heapq import nlargest as _nlargest
Christian Heimes25bb7832008-01-11 16:17:00 +000034from collections import namedtuple as _namedtuple
35
36Match = _namedtuple('Match', 'a b size')
Raymond Hettingerbb6b7342004-06-13 09:57:33 +000037
Neal Norwitze7dfe212003-07-01 14:59:46 +000038def _calculate_ratio(matches, length):
39 if length:
40 return 2.0 * matches / length
41 return 1.0
42
Tim Peters9ae21482001-02-10 08:00:53 +000043class SequenceMatcher:
Tim Peters5e824c32001-08-12 22:25:01 +000044
45 """
46 SequenceMatcher is a flexible class for comparing pairs of sequences of
47 any type, so long as the sequence elements are hashable. The basic
48 algorithm predates, and is a little fancier than, an algorithm
49 published in the late 1980's by Ratcliff and Obershelp under the
50 hyperbolic name "gestalt pattern matching". The basic idea is to find
51 the longest contiguous matching subsequence that contains no "junk"
52 elements (R-O doesn't address junk). The same idea is then applied
53 recursively to the pieces of the sequences to the left and to the right
54 of the matching subsequence. This does not yield minimal edit
55 sequences, but does tend to yield matches that "look right" to people.
56
57 SequenceMatcher tries to compute a "human-friendly diff" between two
58 sequences. Unlike e.g. UNIX(tm) diff, the fundamental notion is the
59 longest *contiguous* & junk-free matching subsequence. That's what
60 catches peoples' eyes. The Windows(tm) windiff has another interesting
61 notion, pairing up elements that appear uniquely in each sequence.
62 That, and the method here, appear to yield more intuitive difference
63 reports than does diff. This method appears to be the least vulnerable
64 to synching up on blocks of "junk lines", though (like blank lines in
65 ordinary text files, or maybe "<P>" lines in HTML files). That may be
66 because this is the only method of the 3 that has a *concept* of
67 "junk" <wink>.
68
69 Example, comparing two strings, and considering blanks to be "junk":
70
71 >>> s = SequenceMatcher(lambda x: x == " ",
72 ... "private Thread currentThread;",
73 ... "private volatile Thread currentThread;")
74 >>>
75
76 .ratio() returns a float in [0, 1], measuring the "similarity" of the
77 sequences. As a rule of thumb, a .ratio() value over 0.6 means the
78 sequences are close matches:
79
Guido van Rossumfff80df2007-02-09 20:33:44 +000080 >>> print(round(s.ratio(), 3))
Tim Peters5e824c32001-08-12 22:25:01 +000081 0.866
82 >>>
83
84 If you're only interested in where the sequences match,
85 .get_matching_blocks() is handy:
86
87 >>> for block in s.get_matching_blocks():
Guido van Rossumfff80df2007-02-09 20:33:44 +000088 ... print("a[%d] and b[%d] match for %d elements" % block)
Tim Peters5e824c32001-08-12 22:25:01 +000089 a[0] and b[0] match for 8 elements
Thomas Wouters0e3f5912006-08-11 14:57:12 +000090 a[8] and b[17] match for 21 elements
Tim Peters5e824c32001-08-12 22:25:01 +000091 a[29] and b[38] match for 0 elements
92
93 Note that the last tuple returned by .get_matching_blocks() is always a
94 dummy, (len(a), len(b), 0), and this is the only case in which the last
95 tuple element (number of elements matched) is 0.
96
97 If you want to know how to change the first sequence into the second,
98 use .get_opcodes():
99
100 >>> for opcode in s.get_opcodes():
Guido van Rossumfff80df2007-02-09 20:33:44 +0000101 ... print("%6s a[%d:%d] b[%d:%d]" % opcode)
Tim Peters5e824c32001-08-12 22:25:01 +0000102 equal a[0:8] b[0:8]
103 insert a[8:8] b[8:17]
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000104 equal a[8:29] b[17:38]
Tim Peters5e824c32001-08-12 22:25:01 +0000105
106 See the Differ class for a fancy human-friendly file differencer, which
107 uses SequenceMatcher both to compare sequences of lines, and to compare
108 sequences of characters within similar (near-matching) lines.
109
110 See also function get_close_matches() in this module, which shows how
111 simple code building on SequenceMatcher can be used to do useful work.
112
113 Timing: Basic R-O is cubic time worst case and quadratic time expected
114 case. SequenceMatcher is quadratic time for the worst case and has
115 expected-case behavior dependent in a complicated way on how many
116 elements the sequences have in common; best case time is linear.
117
118 Methods:
119
120 __init__(isjunk=None, a='', b='')
121 Construct a SequenceMatcher.
122
123 set_seqs(a, b)
124 Set the two sequences to be compared.
125
126 set_seq1(a)
127 Set the first sequence to be compared.
128
129 set_seq2(b)
130 Set the second sequence to be compared.
131
132 find_longest_match(alo, ahi, blo, bhi)
133 Find longest matching block in a[alo:ahi] and b[blo:bhi].
134
135 get_matching_blocks()
136 Return list of triples describing matching subsequences.
137
138 get_opcodes()
139 Return list of 5-tuples describing how to turn a into b.
140
141 ratio()
142 Return a measure of the sequences' similarity (float in [0,1]).
143
144 quick_ratio()
145 Return an upper bound on .ratio() relatively quickly.
146
147 real_quick_ratio()
148 Return an upper bound on ratio() very quickly.
149 """
150
Terry Reedy99f96372010-11-25 06:12:34 +0000151 def __init__(self, isjunk=None, a='', b='', autojunk=True):
Tim Peters9ae21482001-02-10 08:00:53 +0000152 """Construct a SequenceMatcher.
153
154 Optional arg isjunk is None (the default), or a one-argument
155 function that takes a sequence element and returns true iff the
Tim Peters5e824c32001-08-12 22:25:01 +0000156 element is junk. None is equivalent to passing "lambda x: 0", i.e.
Fred Drakef1da6282001-02-19 19:30:05 +0000157 no elements are considered to be junk. For example, pass
Tim Peters9ae21482001-02-10 08:00:53 +0000158 lambda x: x in " \\t"
159 if you're comparing lines as sequences of characters, and don't
160 want to synch up on blanks or hard tabs.
161
162 Optional arg a is the first of two sequences to be compared. By
163 default, an empty string. The elements of a must be hashable. See
164 also .set_seqs() and .set_seq1().
165
166 Optional arg b is the second of two sequences to be compared. By
Fred Drakef1da6282001-02-19 19:30:05 +0000167 default, an empty string. The elements of b must be hashable. See
Tim Peters9ae21482001-02-10 08:00:53 +0000168 also .set_seqs() and .set_seq2().
Terry Reedy99f96372010-11-25 06:12:34 +0000169
170 Optional arg autojunk should be set to False to disable the
171 "automatic junk heuristic" that treats popular elements as junk
172 (see module documentation for more information).
Tim Peters9ae21482001-02-10 08:00:53 +0000173 """
174
175 # Members:
176 # a
177 # first sequence
178 # b
179 # second sequence; differences are computed as "what do
180 # we need to do to 'a' to change it into 'b'?"
181 # b2j
182 # for x in b, b2j[x] is a list of the indices (into b)
Terry Reedybcd89882010-12-03 22:29:40 +0000183 # at which x appears; junk and popular elements do not appear
Tim Peters9ae21482001-02-10 08:00:53 +0000184 # fullbcount
185 # for x in b, fullbcount[x] == the number of times x
186 # appears in b; only materialized if really needed (used
187 # only for computing quick_ratio())
188 # matching_blocks
189 # a list of (i, j, k) triples, where a[i:i+k] == b[j:j+k];
190 # ascending & non-overlapping in i and in j; terminated by
191 # a dummy (len(a), len(b), 0) sentinel
192 # opcodes
193 # a list of (tag, i1, i2, j1, j2) tuples, where tag is
194 # one of
195 # 'replace' a[i1:i2] should be replaced by b[j1:j2]
196 # 'delete' a[i1:i2] should be deleted
197 # 'insert' b[j1:j2] should be inserted
198 # 'equal' a[i1:i2] == b[j1:j2]
199 # isjunk
200 # a user-supplied function taking a sequence element and
201 # returning true iff the element is "junk" -- this has
202 # subtle but helpful effects on the algorithm, which I'll
203 # get around to writing up someday <0.9 wink>.
Florent Xicluna7f1c15b2011-12-10 13:02:17 +0100204 # DON'T USE! Only __chain_b uses this. Use "in self.bjunk".
Terry Reedy74a7c672010-12-03 18:57:42 +0000205 # bjunk
206 # the items in b for which isjunk is True.
207 # bpopular
208 # nonjunk items in b treated as junk by the heuristic (if used).
Tim Peters9ae21482001-02-10 08:00:53 +0000209
210 self.isjunk = isjunk
211 self.a = self.b = None
Terry Reedy99f96372010-11-25 06:12:34 +0000212 self.autojunk = autojunk
Tim Peters9ae21482001-02-10 08:00:53 +0000213 self.set_seqs(a, b)
214
215 def set_seqs(self, a, b):
216 """Set the two sequences to be compared.
217
218 >>> s = SequenceMatcher()
219 >>> s.set_seqs("abcd", "bcde")
220 >>> s.ratio()
221 0.75
222 """
223
224 self.set_seq1(a)
225 self.set_seq2(b)
226
227 def set_seq1(self, a):
228 """Set the first sequence to be compared.
229
230 The second sequence to be compared is not changed.
231
232 >>> s = SequenceMatcher(None, "abcd", "bcde")
233 >>> s.ratio()
234 0.75
235 >>> s.set_seq1("bcde")
236 >>> s.ratio()
237 1.0
238 >>>
239
240 SequenceMatcher computes and caches detailed information about the
241 second sequence, so if you want to compare one sequence S against
242 many sequences, use .set_seq2(S) once and call .set_seq1(x)
243 repeatedly for each of the other sequences.
244
245 See also set_seqs() and set_seq2().
246 """
247
248 if a is self.a:
249 return
250 self.a = a
251 self.matching_blocks = self.opcodes = None
252
253 def set_seq2(self, b):
254 """Set the second sequence to be compared.
255
256 The first sequence to be compared is not changed.
257
258 >>> s = SequenceMatcher(None, "abcd", "bcde")
259 >>> s.ratio()
260 0.75
261 >>> s.set_seq2("abcd")
262 >>> s.ratio()
263 1.0
264 >>>
265
266 SequenceMatcher computes and caches detailed information about the
267 second sequence, so if you want to compare one sequence S against
268 many sequences, use .set_seq2(S) once and call .set_seq1(x)
269 repeatedly for each of the other sequences.
270
271 See also set_seqs() and set_seq1().
272 """
273
274 if b is self.b:
275 return
276 self.b = b
277 self.matching_blocks = self.opcodes = None
278 self.fullbcount = None
279 self.__chain_b()
280
281 # For each element x in b, set b2j[x] to a list of the indices in
282 # b where x appears; the indices are in increasing order; note that
283 # the number of times x appears in b is len(b2j[x]) ...
284 # when self.isjunk is defined, junk elements don't show up in this
285 # map at all, which stops the central find_longest_match method
286 # from starting any matching block at a junk element ...
Tim Peters81b92512002-04-29 01:37:32 +0000287 # b2j also does not contain entries for "popular" elements, meaning
Terry Reedy99f96372010-11-25 06:12:34 +0000288 # elements that account for more than 1 + 1% of the total elements, and
Tim Peters81b92512002-04-29 01:37:32 +0000289 # when the sequence is reasonably large (>= 200 elements); this can
290 # be viewed as an adaptive notion of semi-junk, and yields an enormous
291 # speedup when, e.g., comparing program files with hundreds of
292 # instances of "return NULL;" ...
Tim Peters9ae21482001-02-10 08:00:53 +0000293 # note that this is only called when b changes; so for cross-product
294 # kinds of matches, it's best to call set_seq2 once, then set_seq1
295 # repeatedly
296
297 def __chain_b(self):
298 # Because isjunk is a user-defined (not C) function, and we test
299 # for junk a LOT, it's important to minimize the number of calls.
300 # Before the tricks described here, __chain_b was by far the most
301 # time-consuming routine in the whole module! If anyone sees
302 # Jim Roskind, thank him again for profile.py -- I never would
303 # have guessed that.
304 # The first trick is to build b2j ignoring the possibility
305 # of junk. I.e., we don't call isjunk at all yet. Throwing
306 # out the junk later is much cheaper than building b2j "right"
307 # from the start.
308 b = self.b
309 self.b2j = b2j = {}
Terry Reedy99f96372010-11-25 06:12:34 +0000310
Tim Peters81b92512002-04-29 01:37:32 +0000311 for i, elt in enumerate(b):
Terry Reedy99f96372010-11-25 06:12:34 +0000312 indices = b2j.setdefault(elt, [])
313 indices.append(i)
Tim Peters9ae21482001-02-10 08:00:53 +0000314
Terry Reedy99f96372010-11-25 06:12:34 +0000315 # Purge junk elements
Terry Reedy74a7c672010-12-03 18:57:42 +0000316 self.bjunk = junk = set()
Tim Peters81b92512002-04-29 01:37:32 +0000317 isjunk = self.isjunk
Tim Peters9ae21482001-02-10 08:00:53 +0000318 if isjunk:
Terry Reedy17a59252010-12-15 20:18:10 +0000319 for elt in b2j.keys():
Terry Reedy99f96372010-11-25 06:12:34 +0000320 if isjunk(elt):
321 junk.add(elt)
Terry Reedy17a59252010-12-15 20:18:10 +0000322 for elt in junk: # separate loop avoids separate list of keys
323 del b2j[elt]
Tim Peters9ae21482001-02-10 08:00:53 +0000324
Terry Reedy99f96372010-11-25 06:12:34 +0000325 # Purge popular elements that are not junk
Terry Reedy74a7c672010-12-03 18:57:42 +0000326 self.bpopular = popular = set()
Terry Reedy99f96372010-11-25 06:12:34 +0000327 n = len(b)
328 if self.autojunk and n >= 200:
329 ntest = n // 100 + 1
Terry Reedy17a59252010-12-15 20:18:10 +0000330 for elt, idxs in b2j.items():
Terry Reedy99f96372010-11-25 06:12:34 +0000331 if len(idxs) > ntest:
332 popular.add(elt)
Terry Reedy17a59252010-12-15 20:18:10 +0000333 for elt in popular: # ditto; as fast for 1% deletion
334 del b2j[elt]
Terry Reedy99f96372010-11-25 06:12:34 +0000335
Tim Peters9ae21482001-02-10 08:00:53 +0000336 def find_longest_match(self, alo, ahi, blo, bhi):
337 """Find longest matching block in a[alo:ahi] and b[blo:bhi].
338
339 If isjunk is not defined:
340
341 Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where
342 alo <= i <= i+k <= ahi
343 blo <= j <= j+k <= bhi
344 and for all (i',j',k') meeting those conditions,
345 k >= k'
346 i <= i'
347 and if i == i', j <= j'
348
349 In other words, of all maximal matching blocks, return one that
350 starts earliest in a, and of all those maximal matching blocks that
351 start earliest in a, return the one that starts earliest in b.
352
353 >>> s = SequenceMatcher(None, " abcd", "abcd abcd")
354 >>> s.find_longest_match(0, 5, 0, 9)
Christian Heimes25bb7832008-01-11 16:17:00 +0000355 Match(a=0, b=4, size=5)
Tim Peters9ae21482001-02-10 08:00:53 +0000356
357 If isjunk is defined, first the longest matching block is
358 determined as above, but with the additional restriction that no
359 junk element appears in the block. Then that block is extended as
360 far as possible by matching (only) junk elements on both sides. So
361 the resulting block never matches on junk except as identical junk
362 happens to be adjacent to an "interesting" match.
363
364 Here's the same example as before, but considering blanks to be
365 junk. That prevents " abcd" from matching the " abcd" at the tail
366 end of the second sequence directly. Instead only the "abcd" can
367 match, and matches the leftmost "abcd" in the second sequence:
368
369 >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
370 >>> s.find_longest_match(0, 5, 0, 9)
Christian Heimes25bb7832008-01-11 16:17:00 +0000371 Match(a=1, b=0, size=4)
Tim Peters9ae21482001-02-10 08:00:53 +0000372
373 If no blocks match, return (alo, blo, 0).
374
375 >>> s = SequenceMatcher(None, "ab", "c")
376 >>> s.find_longest_match(0, 2, 0, 1)
Christian Heimes25bb7832008-01-11 16:17:00 +0000377 Match(a=0, b=0, size=0)
Tim Peters9ae21482001-02-10 08:00:53 +0000378 """
379
380 # CAUTION: stripping common prefix or suffix would be incorrect.
381 # E.g.,
382 # ab
383 # acab
384 # Longest matching block is "ab", but if common prefix is
385 # stripped, it's "a" (tied with "b"). UNIX(tm) diff does so
386 # strip, so ends up claiming that ab is changed to acab by
387 # inserting "ca" in the middle. That's minimal but unintuitive:
388 # "it's obvious" that someone inserted "ac" at the front.
389 # Windiff ends up at the same place as diff, but by pairing up
390 # the unique 'b's and then matching the first two 'a's.
391
Terry Reedybcd89882010-12-03 22:29:40 +0000392 a, b, b2j, isbjunk = self.a, self.b, self.b2j, self.bjunk.__contains__
Tim Peters9ae21482001-02-10 08:00:53 +0000393 besti, bestj, bestsize = alo, blo, 0
394 # find longest junk-free match
395 # during an iteration of the loop, j2len[j] = length of longest
396 # junk-free match ending with a[i-1] and b[j]
397 j2len = {}
398 nothing = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000399 for i in range(alo, ahi):
Tim Peters9ae21482001-02-10 08:00:53 +0000400 # look at all instances of a[i] in b; note that because
401 # b2j has no junk keys, the loop is skipped if a[i] is junk
402 j2lenget = j2len.get
403 newj2len = {}
404 for j in b2j.get(a[i], nothing):
405 # a[i] matches b[j]
406 if j < blo:
407 continue
408 if j >= bhi:
409 break
410 k = newj2len[j] = j2lenget(j-1, 0) + 1
411 if k > bestsize:
412 besti, bestj, bestsize = i-k+1, j-k+1, k
413 j2len = newj2len
414
Tim Peters81b92512002-04-29 01:37:32 +0000415 # Extend the best by non-junk elements on each end. In particular,
416 # "popular" non-junk elements aren't in b2j, which greatly speeds
417 # the inner loop above, but also means "the best" match so far
418 # doesn't contain any junk *or* popular non-junk elements.
419 while besti > alo and bestj > blo and \
420 not isbjunk(b[bestj-1]) and \
421 a[besti-1] == b[bestj-1]:
422 besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
423 while besti+bestsize < ahi and bestj+bestsize < bhi and \
424 not isbjunk(b[bestj+bestsize]) and \
425 a[besti+bestsize] == b[bestj+bestsize]:
426 bestsize += 1
427
Tim Peters9ae21482001-02-10 08:00:53 +0000428 # Now that we have a wholly interesting match (albeit possibly
429 # empty!), we may as well suck up the matching junk on each
430 # side of it too. Can't think of a good reason not to, and it
431 # saves post-processing the (possibly considerable) expense of
432 # figuring out what to do with it. In the case of an empty
433 # interesting match, this is clearly the right thing to do,
434 # because no other kind of match is possible in the regions.
435 while besti > alo and bestj > blo and \
436 isbjunk(b[bestj-1]) and \
437 a[besti-1] == b[bestj-1]:
438 besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
439 while besti+bestsize < ahi and bestj+bestsize < bhi and \
440 isbjunk(b[bestj+bestsize]) and \
441 a[besti+bestsize] == b[bestj+bestsize]:
442 bestsize = bestsize + 1
443
Christian Heimes25bb7832008-01-11 16:17:00 +0000444 return Match(besti, bestj, bestsize)
Tim Peters9ae21482001-02-10 08:00:53 +0000445
446 def get_matching_blocks(self):
447 """Return list of triples describing matching subsequences.
448
449 Each triple is of the form (i, j, n), and means that
450 a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000451 i and in j. New in Python 2.5, it's also guaranteed that if
452 (i, j, n) and (i', j', n') are adjacent triples in the list, and
453 the second is not the last triple in the list, then i+n != i' or
454 j+n != j'. IOW, adjacent triples never describe adjacent equal
455 blocks.
Tim Peters9ae21482001-02-10 08:00:53 +0000456
457 The last triple is a dummy, (len(a), len(b), 0), and is the only
458 triple with n==0.
459
460 >>> s = SequenceMatcher(None, "abxcd", "abcd")
Christian Heimes25bb7832008-01-11 16:17:00 +0000461 >>> list(s.get_matching_blocks())
462 [Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)]
Tim Peters9ae21482001-02-10 08:00:53 +0000463 """
464
465 if self.matching_blocks is not None:
466 return self.matching_blocks
Tim Peters9ae21482001-02-10 08:00:53 +0000467 la, lb = len(self.a), len(self.b)
Gustavo Niemeyer548148812006-01-31 18:34:13 +0000468
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000469 # This is most naturally expressed as a recursive algorithm, but
470 # at least one user bumped into extreme use cases that exceeded
471 # the recursion limit on their box. So, now we maintain a list
472 # ('queue`) of blocks we still need to look at, and append partial
473 # results to `matching_blocks` in a loop; the matches are sorted
474 # at the end.
Gustavo Niemeyer548148812006-01-31 18:34:13 +0000475 queue = [(0, la, 0, lb)]
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000476 matching_blocks = []
Gustavo Niemeyer548148812006-01-31 18:34:13 +0000477 while queue:
Gustavo Niemeyer548148812006-01-31 18:34:13 +0000478 alo, ahi, blo, bhi = queue.pop()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000479 i, j, k = x = self.find_longest_match(alo, ahi, blo, bhi)
Gustavo Niemeyer548148812006-01-31 18:34:13 +0000480 # a[alo:i] vs b[blo:j] unknown
481 # a[i:i+k] same as b[j:j+k]
482 # a[i+k:ahi] vs b[j+k:bhi] unknown
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000483 if k: # if k is 0, there was no matching block
484 matching_blocks.append(x)
Gustavo Niemeyer548148812006-01-31 18:34:13 +0000485 if alo < i and blo < j:
486 queue.append((alo, i, blo, j))
Gustavo Niemeyer548148812006-01-31 18:34:13 +0000487 if i+k < ahi and j+k < bhi:
488 queue.append((i+k, ahi, j+k, bhi))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000489 matching_blocks.sort()
Gustavo Niemeyer548148812006-01-31 18:34:13 +0000490
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000491 # It's possible that we have adjacent equal blocks in the
492 # matching_blocks list now. Starting with 2.5, this code was added
493 # to collapse them.
494 i1 = j1 = k1 = 0
495 non_adjacent = []
496 for i2, j2, k2 in matching_blocks:
497 # Is this block adjacent to i1, j1, k1?
498 if i1 + k1 == i2 and j1 + k1 == j2:
499 # Yes, so collapse them -- this just increases the length of
500 # the first block by the length of the second, and the first
501 # block so lengthened remains the block to compare against.
502 k1 += k2
503 else:
504 # Not adjacent. Remember the first block (k1==0 means it's
505 # the dummy we started with), and make the second block the
506 # new block to compare against.
507 if k1:
508 non_adjacent.append((i1, j1, k1))
509 i1, j1, k1 = i2, j2, k2
510 if k1:
511 non_adjacent.append((i1, j1, k1))
512
513 non_adjacent.append( (la, lb, 0) )
Raymond Hettingerfabefc32014-06-21 11:57:36 -0700514 self.matching_blocks = list(map(Match._make, non_adjacent))
515 return self.matching_blocks
Tim Peters9ae21482001-02-10 08:00:53 +0000516
Tim Peters9ae21482001-02-10 08:00:53 +0000517 def get_opcodes(self):
518 """Return list of 5-tuples describing how to turn a into b.
519
520 Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple
521 has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the
522 tuple preceding it, and likewise for j1 == the previous j2.
523
524 The tags are strings, with these meanings:
525
526 'replace': a[i1:i2] should be replaced by b[j1:j2]
527 'delete': a[i1:i2] should be deleted.
528 Note that j1==j2 in this case.
529 'insert': b[j1:j2] should be inserted at a[i1:i1].
530 Note that i1==i2 in this case.
531 'equal': a[i1:i2] == b[j1:j2]
532
533 >>> a = "qabxcd"
534 >>> b = "abycdf"
535 >>> s = SequenceMatcher(None, a, b)
536 >>> for tag, i1, i2, j1, j2 in s.get_opcodes():
Guido van Rossumfff80df2007-02-09 20:33:44 +0000537 ... print(("%7s a[%d:%d] (%s) b[%d:%d] (%s)" %
538 ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2])))
Tim Peters9ae21482001-02-10 08:00:53 +0000539 delete a[0:1] (q) b[0:0] ()
540 equal a[1:3] (ab) b[0:2] (ab)
541 replace a[3:4] (x) b[2:3] (y)
542 equal a[4:6] (cd) b[3:5] (cd)
543 insert a[6:6] () b[5:6] (f)
544 """
545
546 if self.opcodes is not None:
547 return self.opcodes
548 i = j = 0
549 self.opcodes = answer = []
550 for ai, bj, size in self.get_matching_blocks():
551 # invariant: we've pumped out correct diffs to change
552 # a[:i] into b[:j], and the next matching block is
553 # a[ai:ai+size] == b[bj:bj+size]. So we need to pump
554 # out a diff to change a[i:ai] into b[j:bj], pump out
555 # the matching block, and move (i,j) beyond the match
556 tag = ''
557 if i < ai and j < bj:
558 tag = 'replace'
559 elif i < ai:
560 tag = 'delete'
561 elif j < bj:
562 tag = 'insert'
563 if tag:
564 answer.append( (tag, i, ai, j, bj) )
565 i, j = ai+size, bj+size
566 # the list of matching blocks is terminated by a
567 # sentinel with size 0
568 if size:
569 answer.append( ('equal', ai, i, bj, j) )
570 return answer
571
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +0000572 def get_grouped_opcodes(self, n=3):
573 """ Isolate change clusters by eliminating ranges with no changes.
574
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300575 Return a generator of groups with up to n lines of context.
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +0000576 Each group is in the same format as returned by get_opcodes().
577
578 >>> from pprint import pprint
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000579 >>> a = list(map(str, range(1,40)))
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +0000580 >>> b = a[:]
581 >>> b[8:8] = ['i'] # Make an insertion
582 >>> b[20] += 'x' # Make a replacement
583 >>> b[23:28] = [] # Make a deletion
584 >>> b[30] += 'y' # Make another replacement
585 >>> pprint(list(SequenceMatcher(None,a,b).get_grouped_opcodes()))
586 [[('equal', 5, 8, 5, 8), ('insert', 8, 8, 8, 9), ('equal', 8, 11, 9, 12)],
587 [('equal', 16, 19, 17, 20),
588 ('replace', 19, 20, 20, 21),
589 ('equal', 20, 22, 21, 23),
590 ('delete', 22, 27, 23, 23),
591 ('equal', 27, 30, 23, 26)],
592 [('equal', 31, 34, 27, 30),
593 ('replace', 34, 35, 30, 31),
594 ('equal', 35, 38, 31, 34)]]
595 """
596
597 codes = self.get_opcodes()
Brett Cannond2c5b4b2004-07-10 23:54:07 +0000598 if not codes:
599 codes = [("equal", 0, 1, 0, 1)]
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +0000600 # Fixup leading and trailing groups if they show no changes.
601 if codes[0][0] == 'equal':
602 tag, i1, i2, j1, j2 = codes[0]
603 codes[0] = tag, max(i1, i2-n), i2, max(j1, j2-n), j2
604 if codes[-1][0] == 'equal':
605 tag, i1, i2, j1, j2 = codes[-1]
606 codes[-1] = tag, i1, min(i2, i1+n), j1, min(j2, j1+n)
607
608 nn = n + n
609 group = []
610 for tag, i1, i2, j1, j2 in codes:
611 # End the current group and start a new one whenever
612 # there is a large range with no changes.
613 if tag == 'equal' and i2-i1 > nn:
614 group.append((tag, i1, min(i2, i1+n), j1, min(j2, j1+n)))
615 yield group
616 group = []
617 i1, j1 = max(i1, i2-n), max(j1, j2-n)
618 group.append((tag, i1, i2, j1 ,j2))
619 if group and not (len(group)==1 and group[0][0] == 'equal'):
620 yield group
621
Tim Peters9ae21482001-02-10 08:00:53 +0000622 def ratio(self):
623 """Return a measure of the sequences' similarity (float in [0,1]).
624
625 Where T is the total number of elements in both sequences, and
Tim Petersbcc95cb2004-07-31 00:19:43 +0000626 M is the number of matches, this is 2.0*M / T.
Tim Peters9ae21482001-02-10 08:00:53 +0000627 Note that this is 1 if the sequences are identical, and 0 if
628 they have nothing in common.
629
630 .ratio() is expensive to compute if you haven't already computed
631 .get_matching_blocks() or .get_opcodes(), in which case you may
632 want to try .quick_ratio() or .real_quick_ratio() first to get an
633 upper bound.
634
635 >>> s = SequenceMatcher(None, "abcd", "bcde")
636 >>> s.ratio()
637 0.75
638 >>> s.quick_ratio()
639 0.75
640 >>> s.real_quick_ratio()
641 1.0
642 """
643
Guido van Rossum89da5d72006-08-22 00:21:25 +0000644 matches = sum(triple[-1] for triple in self.get_matching_blocks())
Neal Norwitze7dfe212003-07-01 14:59:46 +0000645 return _calculate_ratio(matches, len(self.a) + len(self.b))
Tim Peters9ae21482001-02-10 08:00:53 +0000646
647 def quick_ratio(self):
648 """Return an upper bound on ratio() relatively quickly.
649
650 This isn't defined beyond that it is an upper bound on .ratio(), and
651 is faster to compute.
652 """
653
654 # viewing a and b as multisets, set matches to the cardinality
655 # of their intersection; this counts the number of matches
656 # without regard to order, so is clearly an upper bound
657 if self.fullbcount is None:
658 self.fullbcount = fullbcount = {}
659 for elt in self.b:
660 fullbcount[elt] = fullbcount.get(elt, 0) + 1
661 fullbcount = self.fullbcount
662 # avail[x] is the number of times x appears in 'b' less the
663 # number of times we've seen it in 'a' so far ... kinda
664 avail = {}
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000665 availhas, matches = avail.__contains__, 0
Tim Peters9ae21482001-02-10 08:00:53 +0000666 for elt in self.a:
667 if availhas(elt):
668 numb = avail[elt]
669 else:
670 numb = fullbcount.get(elt, 0)
671 avail[elt] = numb - 1
672 if numb > 0:
673 matches = matches + 1
Neal Norwitze7dfe212003-07-01 14:59:46 +0000674 return _calculate_ratio(matches, len(self.a) + len(self.b))
Tim Peters9ae21482001-02-10 08:00:53 +0000675
676 def real_quick_ratio(self):
677 """Return an upper bound on ratio() very quickly.
678
679 This isn't defined beyond that it is an upper bound on .ratio(), and
680 is faster to compute than either .ratio() or .quick_ratio().
681 """
682
683 la, lb = len(self.a), len(self.b)
684 # can't have more matches than the number of elements in the
685 # shorter sequence
Neal Norwitze7dfe212003-07-01 14:59:46 +0000686 return _calculate_ratio(min(la, lb), la + lb)
Tim Peters9ae21482001-02-10 08:00:53 +0000687
688def get_close_matches(word, possibilities, n=3, cutoff=0.6):
689 """Use SequenceMatcher to return list of the best "good enough" matches.
690
691 word is a sequence for which close matches are desired (typically a
692 string).
693
694 possibilities is a list of sequences against which to match word
695 (typically a list of strings).
696
697 Optional arg n (default 3) is the maximum number of close matches to
698 return. n must be > 0.
699
700 Optional arg cutoff (default 0.6) is a float in [0, 1]. Possibilities
701 that don't score at least that similar to word are ignored.
702
703 The best (no more than n) matches among the possibilities are returned
704 in a list, sorted by similarity score, most similar first.
705
706 >>> get_close_matches("appel", ["ape", "apple", "peach", "puppy"])
707 ['apple', 'ape']
Tim Peters5e824c32001-08-12 22:25:01 +0000708 >>> import keyword as _keyword
709 >>> get_close_matches("wheel", _keyword.kwlist)
Tim Peters9ae21482001-02-10 08:00:53 +0000710 ['while']
Guido van Rossum486364b2007-06-30 05:01:58 +0000711 >>> get_close_matches("Apple", _keyword.kwlist)
Tim Peters9ae21482001-02-10 08:00:53 +0000712 []
Tim Peters5e824c32001-08-12 22:25:01 +0000713 >>> get_close_matches("accept", _keyword.kwlist)
Tim Peters9ae21482001-02-10 08:00:53 +0000714 ['except']
715 """
716
717 if not n > 0:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000718 raise ValueError("n must be > 0: %r" % (n,))
Tim Peters9ae21482001-02-10 08:00:53 +0000719 if not 0.0 <= cutoff <= 1.0:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000720 raise ValueError("cutoff must be in [0.0, 1.0]: %r" % (cutoff,))
Tim Peters9ae21482001-02-10 08:00:53 +0000721 result = []
722 s = SequenceMatcher()
723 s.set_seq2(word)
724 for x in possibilities:
725 s.set_seq1(x)
726 if s.real_quick_ratio() >= cutoff and \
727 s.quick_ratio() >= cutoff and \
728 s.ratio() >= cutoff:
729 result.append((s.ratio(), x))
Tim Peters9ae21482001-02-10 08:00:53 +0000730
Raymond Hettinger6b59f5f2003-10-16 05:53:16 +0000731 # Move the best scorers to head of list
Raymond Hettingerae39fbd2014-08-03 22:40:59 -0700732 result = _nlargest(n, result)
Raymond Hettinger6b59f5f2003-10-16 05:53:16 +0000733 # Strip scores for the best n matches
Raymond Hettingerbb6b7342004-06-13 09:57:33 +0000734 return [x for score, x in result]
Tim Peters5e824c32001-08-12 22:25:01 +0000735
Tim Peters5e824c32001-08-12 22:25:01 +0000736
Anthony Sottilee1c638d2019-08-21 11:59:26 -0700737def _keep_original_ws(s, tag_s):
738 """Replace whitespace with the original whitespace characters in `s`"""
739 return ''.join(
740 c if tag_c == " " and c.isspace() else tag_c
741 for c, tag_c in zip(s, tag_s)
742 )
Tim Peters5e824c32001-08-12 22:25:01 +0000743
Tim Peters5e824c32001-08-12 22:25:01 +0000744
Tim Peters5e824c32001-08-12 22:25:01 +0000745
746class Differ:
747 r"""
748 Differ is a class for comparing sequences of lines of text, and
749 producing human-readable differences or deltas. Differ uses
750 SequenceMatcher both to compare sequences of lines, and to compare
751 sequences of characters within similar (near-matching) lines.
752
753 Each line of a Differ delta begins with a two-letter code:
754
755 '- ' line unique to sequence 1
756 '+ ' line unique to sequence 2
757 ' ' line common to both sequences
758 '? ' line not present in either input sequence
759
760 Lines beginning with '? ' attempt to guide the eye to intraline
761 differences, and were not present in either input sequence. These lines
762 can be confusing if the sequences contain tab characters.
763
764 Note that Differ makes no claim to produce a *minimal* diff. To the
765 contrary, minimal diffs are often counter-intuitive, because they synch
766 up anywhere possible, sometimes accidental matches 100 pages apart.
767 Restricting synch points to contiguous matches preserves some notion of
768 locality, at the occasional cost of producing a longer diff.
769
770 Example: Comparing two texts.
771
772 First we set up the texts, sequences of individual single-line strings
773 ending with newlines (such sequences can also be obtained from the
774 `readlines()` method of file-like objects):
775
776 >>> text1 = ''' 1. Beautiful is better than ugly.
777 ... 2. Explicit is better than implicit.
778 ... 3. Simple is better than complex.
779 ... 4. Complex is better than complicated.
Ezio Melottid8b509b2011-09-28 17:37:55 +0300780 ... '''.splitlines(keepends=True)
Tim Peters5e824c32001-08-12 22:25:01 +0000781 >>> len(text1)
782 4
783 >>> text1[0][-1]
784 '\n'
785 >>> text2 = ''' 1. Beautiful is better than ugly.
786 ... 3. Simple is better than complex.
787 ... 4. Complicated is better than complex.
788 ... 5. Flat is better than nested.
Ezio Melottid8b509b2011-09-28 17:37:55 +0300789 ... '''.splitlines(keepends=True)
Tim Peters5e824c32001-08-12 22:25:01 +0000790
791 Next we instantiate a Differ object:
792
793 >>> d = Differ()
794
795 Note that when instantiating a Differ object we may pass functions to
796 filter out line and character 'junk'. See Differ.__init__ for details.
797
798 Finally, we compare the two:
799
Tim Peters8a9c2842001-09-22 21:30:22 +0000800 >>> result = list(d.compare(text1, text2))
Tim Peters5e824c32001-08-12 22:25:01 +0000801
802 'result' is a list of strings, so let's pretty-print it:
803
804 >>> from pprint import pprint as _pprint
805 >>> _pprint(result)
806 [' 1. Beautiful is better than ugly.\n',
807 '- 2. Explicit is better than implicit.\n',
808 '- 3. Simple is better than complex.\n',
809 '+ 3. Simple is better than complex.\n',
810 '? ++\n',
811 '- 4. Complex is better than complicated.\n',
812 '? ^ ---- ^\n',
813 '+ 4. Complicated is better than complex.\n',
814 '? ++++ ^ ^\n',
815 '+ 5. Flat is better than nested.\n']
816
817 As a single multi-line string it looks like this:
818
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000819 >>> print(''.join(result), end="")
Tim Peters5e824c32001-08-12 22:25:01 +0000820 1. Beautiful is better than ugly.
821 - 2. Explicit is better than implicit.
822 - 3. Simple is better than complex.
823 + 3. Simple is better than complex.
824 ? ++
825 - 4. Complex is better than complicated.
826 ? ^ ---- ^
827 + 4. Complicated is better than complex.
828 ? ++++ ^ ^
829 + 5. Flat is better than nested.
830
831 Methods:
832
833 __init__(linejunk=None, charjunk=None)
834 Construct a text differencer, with optional filters.
835
836 compare(a, b)
Tim Peters8a9c2842001-09-22 21:30:22 +0000837 Compare two sequences of lines; generate the resulting delta.
Tim Peters5e824c32001-08-12 22:25:01 +0000838 """
839
840 def __init__(self, linejunk=None, charjunk=None):
841 """
842 Construct a text differencer, with optional filters.
843
844 The two optional keyword parameters are for filter functions:
845
846 - `linejunk`: A function that should accept a single string argument,
847 and return true iff the string is junk. The module-level function
848 `IS_LINE_JUNK` may be used to filter out lines without visible
Tim Peters81b92512002-04-29 01:37:32 +0000849 characters, except for at most one splat ('#'). It is recommended
Andrew Kuchlingc51da2b2014-03-19 16:43:06 -0400850 to leave linejunk None; the underlying SequenceMatcher class has
851 an adaptive notion of "noise" lines that's better than any static
852 definition the author has ever been able to craft.
Tim Peters5e824c32001-08-12 22:25:01 +0000853
854 - `charjunk`: A function that should accept a string of length 1. The
855 module-level function `IS_CHARACTER_JUNK` may be used to filter out
856 whitespace characters (a blank or tab; **note**: bad idea to include
Tim Peters81b92512002-04-29 01:37:32 +0000857 newline in this!). Use of IS_CHARACTER_JUNK is recommended.
Tim Peters5e824c32001-08-12 22:25:01 +0000858 """
859
860 self.linejunk = linejunk
861 self.charjunk = charjunk
Tim Peters5e824c32001-08-12 22:25:01 +0000862
863 def compare(self, a, b):
864 r"""
Tim Peters8a9c2842001-09-22 21:30:22 +0000865 Compare two sequences of lines; generate the resulting delta.
Tim Peters5e824c32001-08-12 22:25:01 +0000866
867 Each sequence must contain individual single-line strings ending with
868 newlines. Such sequences can be obtained from the `readlines()` method
Tim Peters8a9c2842001-09-22 21:30:22 +0000869 of file-like objects. The delta generated also consists of newline-
870 terminated strings, ready to be printed as-is via the writeline()
Tim Peters5e824c32001-08-12 22:25:01 +0000871 method of a file-like object.
872
873 Example:
874
Ezio Melottid8b509b2011-09-28 17:37:55 +0300875 >>> print(''.join(Differ().compare('one\ntwo\nthree\n'.splitlines(True),
876 ... 'ore\ntree\nemu\n'.splitlines(True))),
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000877 ... end="")
Tim Peters5e824c32001-08-12 22:25:01 +0000878 - one
879 ? ^
880 + ore
881 ? ^
882 - two
883 - three
884 ? -
885 + tree
886 + emu
887 """
888
889 cruncher = SequenceMatcher(self.linejunk, a, b)
890 for tag, alo, ahi, blo, bhi in cruncher.get_opcodes():
891 if tag == 'replace':
Tim Peters8a9c2842001-09-22 21:30:22 +0000892 g = self._fancy_replace(a, alo, ahi, b, blo, bhi)
Tim Peters5e824c32001-08-12 22:25:01 +0000893 elif tag == 'delete':
Tim Peters8a9c2842001-09-22 21:30:22 +0000894 g = self._dump('-', a, alo, ahi)
Tim Peters5e824c32001-08-12 22:25:01 +0000895 elif tag == 'insert':
Tim Peters8a9c2842001-09-22 21:30:22 +0000896 g = self._dump('+', b, blo, bhi)
Tim Peters5e824c32001-08-12 22:25:01 +0000897 elif tag == 'equal':
Tim Peters8a9c2842001-09-22 21:30:22 +0000898 g = self._dump(' ', a, alo, ahi)
Tim Peters5e824c32001-08-12 22:25:01 +0000899 else:
Collin Winterce36ad82007-08-30 01:19:48 +0000900 raise ValueError('unknown tag %r' % (tag,))
Tim Peters8a9c2842001-09-22 21:30:22 +0000901
Philip Jenvey4993cc02012-10-01 12:53:43 -0700902 yield from g
Tim Peters5e824c32001-08-12 22:25:01 +0000903
904 def _dump(self, tag, x, lo, hi):
Tim Peters8a9c2842001-09-22 21:30:22 +0000905 """Generate comparison results for a same-tagged range."""
Guido van Rossum805365e2007-05-07 22:24:25 +0000906 for i in range(lo, hi):
Tim Peters8a9c2842001-09-22 21:30:22 +0000907 yield '%s %s' % (tag, x[i])
Tim Peters5e824c32001-08-12 22:25:01 +0000908
909 def _plain_replace(self, a, alo, ahi, b, blo, bhi):
910 assert alo < ahi and blo < bhi
911 # dump the shorter block first -- reduces the burden on short-term
912 # memory if the blocks are of very different sizes
913 if bhi - blo < ahi - alo:
Tim Peters8a9c2842001-09-22 21:30:22 +0000914 first = self._dump('+', b, blo, bhi)
915 second = self._dump('-', a, alo, ahi)
Tim Peters5e824c32001-08-12 22:25:01 +0000916 else:
Tim Peters8a9c2842001-09-22 21:30:22 +0000917 first = self._dump('-', a, alo, ahi)
918 second = self._dump('+', b, blo, bhi)
919
920 for g in first, second:
Philip Jenvey4993cc02012-10-01 12:53:43 -0700921 yield from g
Tim Peters5e824c32001-08-12 22:25:01 +0000922
923 def _fancy_replace(self, a, alo, ahi, b, blo, bhi):
924 r"""
925 When replacing one block of lines with another, search the blocks
926 for *similar* lines; the best-matching pair (if any) is used as a
927 synch point, and intraline difference marking is done on the
928 similar pair. Lots of work, but often worth it.
929
930 Example:
931
932 >>> d = Differ()
Raymond Hettinger83325e92003-07-16 04:32:32 +0000933 >>> results = d._fancy_replace(['abcDefghiJkl\n'], 0, 1,
934 ... ['abcdefGhijkl\n'], 0, 1)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000935 >>> print(''.join(results), end="")
Tim Peters5e824c32001-08-12 22:25:01 +0000936 - abcDefghiJkl
937 ? ^ ^ ^
938 + abcdefGhijkl
939 ? ^ ^ ^
940 """
941
Tim Peters5e824c32001-08-12 22:25:01 +0000942 # don't synch up unless the lines have a similarity score of at
943 # least cutoff; best_ratio tracks the best score seen so far
944 best_ratio, cutoff = 0.74, 0.75
945 cruncher = SequenceMatcher(self.charjunk)
946 eqi, eqj = None, None # 1st indices of equal lines (if any)
947
948 # search for the pair that matches best without being identical
949 # (identical lines must be junk lines, & we don't want to synch up
950 # on junk -- unless we have to)
Guido van Rossum805365e2007-05-07 22:24:25 +0000951 for j in range(blo, bhi):
Tim Peters5e824c32001-08-12 22:25:01 +0000952 bj = b[j]
953 cruncher.set_seq2(bj)
Guido van Rossum805365e2007-05-07 22:24:25 +0000954 for i in range(alo, ahi):
Tim Peters5e824c32001-08-12 22:25:01 +0000955 ai = a[i]
956 if ai == bj:
957 if eqi is None:
958 eqi, eqj = i, j
959 continue
960 cruncher.set_seq1(ai)
961 # computing similarity is expensive, so use the quick
962 # upper bounds first -- have seen this speed up messy
963 # compares by a factor of 3.
964 # note that ratio() is only expensive to compute the first
965 # time it's called on a sequence pair; the expensive part
966 # of the computation is cached by cruncher
967 if cruncher.real_quick_ratio() > best_ratio and \
968 cruncher.quick_ratio() > best_ratio and \
969 cruncher.ratio() > best_ratio:
970 best_ratio, best_i, best_j = cruncher.ratio(), i, j
971 if best_ratio < cutoff:
972 # no non-identical "pretty close" pair
973 if eqi is None:
974 # no identical pair either -- treat it as a straight replace
Philip Jenvey4993cc02012-10-01 12:53:43 -0700975 yield from self._plain_replace(a, alo, ahi, b, blo, bhi)
Tim Peters5e824c32001-08-12 22:25:01 +0000976 return
977 # no close pair, but an identical pair -- synch up on that
978 best_i, best_j, best_ratio = eqi, eqj, 1.0
979 else:
980 # there's a close pair, so forget the identical pair (if any)
981 eqi = None
982
983 # a[best_i] very similar to b[best_j]; eqi is None iff they're not
984 # identical
Tim Peters5e824c32001-08-12 22:25:01 +0000985
986 # pump out diffs from before the synch point
Philip Jenvey4993cc02012-10-01 12:53:43 -0700987 yield from self._fancy_helper(a, alo, best_i, b, blo, best_j)
Tim Peters5e824c32001-08-12 22:25:01 +0000988
989 # do intraline marking on the synch pair
990 aelt, belt = a[best_i], b[best_j]
991 if eqi is None:
992 # pump out a '-', '?', '+', '?' quad for the synched lines
993 atags = btags = ""
994 cruncher.set_seqs(aelt, belt)
995 for tag, ai1, ai2, bj1, bj2 in cruncher.get_opcodes():
996 la, lb = ai2 - ai1, bj2 - bj1
997 if tag == 'replace':
998 atags += '^' * la
999 btags += '^' * lb
1000 elif tag == 'delete':
1001 atags += '-' * la
1002 elif tag == 'insert':
1003 btags += '+' * lb
1004 elif tag == 'equal':
1005 atags += ' ' * la
1006 btags += ' ' * lb
1007 else:
Collin Winterce36ad82007-08-30 01:19:48 +00001008 raise ValueError('unknown tag %r' % (tag,))
Philip Jenvey4993cc02012-10-01 12:53:43 -07001009 yield from self._qformat(aelt, belt, atags, btags)
Tim Peters5e824c32001-08-12 22:25:01 +00001010 else:
1011 # the synch pair is identical
Tim Peters8a9c2842001-09-22 21:30:22 +00001012 yield ' ' + aelt
Tim Peters5e824c32001-08-12 22:25:01 +00001013
1014 # pump out diffs from after the synch point
Philip Jenvey4993cc02012-10-01 12:53:43 -07001015 yield from self._fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi)
Tim Peters5e824c32001-08-12 22:25:01 +00001016
1017 def _fancy_helper(self, a, alo, ahi, b, blo, bhi):
Tim Peters8a9c2842001-09-22 21:30:22 +00001018 g = []
Tim Peters5e824c32001-08-12 22:25:01 +00001019 if alo < ahi:
1020 if blo < bhi:
Tim Peters8a9c2842001-09-22 21:30:22 +00001021 g = self._fancy_replace(a, alo, ahi, b, blo, bhi)
Tim Peters5e824c32001-08-12 22:25:01 +00001022 else:
Tim Peters8a9c2842001-09-22 21:30:22 +00001023 g = self._dump('-', a, alo, ahi)
Tim Peters5e824c32001-08-12 22:25:01 +00001024 elif blo < bhi:
Tim Peters8a9c2842001-09-22 21:30:22 +00001025 g = self._dump('+', b, blo, bhi)
1026
Philip Jenvey4993cc02012-10-01 12:53:43 -07001027 yield from g
Tim Peters5e824c32001-08-12 22:25:01 +00001028
1029 def _qformat(self, aline, bline, atags, btags):
1030 r"""
Anthony Sottilee1c638d2019-08-21 11:59:26 -07001031 Format "?" output and deal with tabs.
Tim Peters5e824c32001-08-12 22:25:01 +00001032
1033 Example:
1034
1035 >>> d = Differ()
Senthil Kumaran758025c2009-11-23 19:02:52 +00001036 >>> results = d._qformat('\tabcDefghiJkl\n', '\tabcdefGhijkl\n',
1037 ... ' ^ ^ ^ ', ' ^ ^ ^ ')
Guido van Rossumfff80df2007-02-09 20:33:44 +00001038 >>> for line in results: print(repr(line))
1039 ...
Tim Peters5e824c32001-08-12 22:25:01 +00001040 '- \tabcDefghiJkl\n'
1041 '? \t ^ ^ ^\n'
Senthil Kumaran758025c2009-11-23 19:02:52 +00001042 '+ \tabcdefGhijkl\n'
1043 '? \t ^ ^ ^\n'
Tim Peters5e824c32001-08-12 22:25:01 +00001044 """
Anthony Sottilee1c638d2019-08-21 11:59:26 -07001045 atags = _keep_original_ws(aline, atags).rstrip()
1046 btags = _keep_original_ws(bline, btags).rstrip()
Tim Peters5e824c32001-08-12 22:25:01 +00001047
Tim Peters8a9c2842001-09-22 21:30:22 +00001048 yield "- " + aline
Tim Peters5e824c32001-08-12 22:25:01 +00001049 if atags:
Anthony Sottilee1c638d2019-08-21 11:59:26 -07001050 yield f"? {atags}\n"
Tim Peters5e824c32001-08-12 22:25:01 +00001051
Tim Peters8a9c2842001-09-22 21:30:22 +00001052 yield "+ " + bline
Tim Peters5e824c32001-08-12 22:25:01 +00001053 if btags:
Anthony Sottilee1c638d2019-08-21 11:59:26 -07001054 yield f"? {btags}\n"
Tim Peters5e824c32001-08-12 22:25:01 +00001055
1056# With respect to junk, an earlier version of ndiff simply refused to
1057# *start* a match with a junk element. The result was cases like this:
1058# before: private Thread currentThread;
1059# after: private volatile Thread currentThread;
1060# If you consider whitespace to be junk, the longest contiguous match
1061# not starting with junk is "e Thread currentThread". So ndiff reported
1062# that "e volatil" was inserted between the 't' and the 'e' in "private".
1063# While an accurate view, to people that's absurd. The current version
1064# looks for matching blocks that are entirely junk-free, then extends the
1065# longest one of those as far as possible but only with matching junk.
1066# So now "currentThread" is matched, then extended to suck up the
1067# preceding blank; then "private" is matched, and extended to suck up the
1068# following blank; then "Thread" is matched; and finally ndiff reports
1069# that "volatile " was inserted before "Thread". The only quibble
1070# remaining is that perhaps it was really the case that " volatile"
1071# was inserted after "private". I can live with that <wink>.
1072
1073import re
1074
Jamie Davis0e6c8ee2018-03-04 00:33:32 -05001075def IS_LINE_JUNK(line, pat=re.compile(r"\s*(?:#\s*)?$").match):
Tim Peters5e824c32001-08-12 22:25:01 +00001076 r"""
Serhiy Storchaka138ccbb2019-11-12 16:57:03 +02001077 Return True for ignorable line: iff `line` is blank or contains a single '#'.
Tim Peters5e824c32001-08-12 22:25:01 +00001078
1079 Examples:
1080
1081 >>> IS_LINE_JUNK('\n')
Guido van Rossum77f6a652002-04-03 22:41:51 +00001082 True
Tim Peters5e824c32001-08-12 22:25:01 +00001083 >>> IS_LINE_JUNK(' # \n')
Guido van Rossum77f6a652002-04-03 22:41:51 +00001084 True
Tim Peters5e824c32001-08-12 22:25:01 +00001085 >>> IS_LINE_JUNK('hello\n')
Guido van Rossum77f6a652002-04-03 22:41:51 +00001086 False
Tim Peters5e824c32001-08-12 22:25:01 +00001087 """
1088
1089 return pat(line) is not None
1090
1091def IS_CHARACTER_JUNK(ch, ws=" \t"):
1092 r"""
Serhiy Storchaka138ccbb2019-11-12 16:57:03 +02001093 Return True for ignorable character: iff `ch` is a space or tab.
Tim Peters5e824c32001-08-12 22:25:01 +00001094
1095 Examples:
1096
1097 >>> IS_CHARACTER_JUNK(' ')
Guido van Rossum77f6a652002-04-03 22:41:51 +00001098 True
Tim Peters5e824c32001-08-12 22:25:01 +00001099 >>> IS_CHARACTER_JUNK('\t')
Guido van Rossum77f6a652002-04-03 22:41:51 +00001100 True
Tim Peters5e824c32001-08-12 22:25:01 +00001101 >>> IS_CHARACTER_JUNK('\n')
Guido van Rossum77f6a652002-04-03 22:41:51 +00001102 False
Tim Peters5e824c32001-08-12 22:25:01 +00001103 >>> IS_CHARACTER_JUNK('x')
Guido van Rossum77f6a652002-04-03 22:41:51 +00001104 False
Tim Peters5e824c32001-08-12 22:25:01 +00001105 """
1106
1107 return ch in ws
1108
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001109
Raymond Hettinger9180deb2011-04-12 15:25:30 -07001110########################################################################
1111### Unified Diff
1112########################################################################
1113
1114def _format_range_unified(start, stop):
Raymond Hettinger49353d02011-04-11 12:40:58 -07001115 'Convert range to the "ed" format'
1116 # Per the diff spec at http://www.unix.org/single_unix_specification/
1117 beginning = start + 1 # lines start numbering with one
1118 length = stop - start
1119 if length == 1:
1120 return '{}'.format(beginning)
1121 if not length:
1122 beginning -= 1 # empty ranges begin at line just before the range
1123 return '{},{}'.format(beginning, length)
1124
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001125def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
1126 tofiledate='', n=3, lineterm='\n'):
1127 r"""
1128 Compare two sequences of lines; generate the delta as a unified diff.
1129
1130 Unified diffs are a compact way of showing line changes and a few
1131 lines of context. The number of context lines is set by 'n' which
1132 defaults to three.
1133
Raymond Hettinger0887c732003-06-17 16:53:25 +00001134 By default, the diff control lines (those with ---, +++, or @@) are
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001135 created with a trailing newline. This is helpful so that inputs
1136 created from file.readlines() result in diffs that are suitable for
1137 file.writelines() since both the inputs and outputs have trailing
1138 newlines.
1139
1140 For inputs that do not have trailing newlines, set the lineterm
1141 argument to "" so that the output will be uniformly newline free.
1142
1143 The unidiff format normally has a header for filenames and modification
1144 times. Any or all of these may be specified using strings for
R. David Murrayb2416e52010-04-12 16:58:02 +00001145 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
1146 The modification times are normally expressed in the ISO 8601 format.
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001147
1148 Example:
1149
1150 >>> for line in unified_diff('one two three four'.split(),
1151 ... 'zero one tree four'.split(), 'Original', 'Current',
R. David Murrayb2416e52010-04-12 16:58:02 +00001152 ... '2005-01-26 23:30:50', '2010-04-02 10:20:52',
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001153 ... lineterm=''):
R. David Murrayb2416e52010-04-12 16:58:02 +00001154 ... print(line) # doctest: +NORMALIZE_WHITESPACE
1155 --- Original 2005-01-26 23:30:50
1156 +++ Current 2010-04-02 10:20:52
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001157 @@ -1,4 +1,4 @@
1158 +zero
1159 one
1160 -two
1161 -three
1162 +tree
1163 four
1164 """
1165
Greg Ward4d9d2562015-04-20 20:21:21 -04001166 _check_types(a, b, fromfile, tofile, fromfiledate, tofiledate, lineterm)
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001167 started = False
1168 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
1169 if not started:
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001170 started = True
Raymond Hettinger47e120e2011-04-10 17:14:56 -07001171 fromdate = '\t{}'.format(fromfiledate) if fromfiledate else ''
1172 todate = '\t{}'.format(tofiledate) if tofiledate else ''
1173 yield '--- {}{}{}'.format(fromfile, fromdate, lineterm)
1174 yield '+++ {}{}{}'.format(tofile, todate, lineterm)
Raymond Hettinger49353d02011-04-11 12:40:58 -07001175
Raymond Hettinger47e120e2011-04-10 17:14:56 -07001176 first, last = group[0], group[-1]
Raymond Hettinger9180deb2011-04-12 15:25:30 -07001177 file1_range = _format_range_unified(first[1], last[2])
1178 file2_range = _format_range_unified(first[3], last[4])
Raymond Hettinger49353d02011-04-11 12:40:58 -07001179 yield '@@ -{} +{} @@{}'.format(file1_range, file2_range, lineterm)
1180
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001181 for tag, i1, i2, j1, j2 in group:
1182 if tag == 'equal':
1183 for line in a[i1:i2]:
1184 yield ' ' + line
1185 continue
Raymond Hettinger47e120e2011-04-10 17:14:56 -07001186 if tag in {'replace', 'delete'}:
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001187 for line in a[i1:i2]:
1188 yield '-' + line
Raymond Hettinger47e120e2011-04-10 17:14:56 -07001189 if tag in {'replace', 'insert'}:
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001190 for line in b[j1:j2]:
1191 yield '+' + line
1192
Raymond Hettinger9180deb2011-04-12 15:25:30 -07001193
1194########################################################################
1195### Context Diff
1196########################################################################
1197
1198def _format_range_context(start, stop):
1199 'Convert range to the "ed" format'
1200 # Per the diff spec at http://www.unix.org/single_unix_specification/
1201 beginning = start + 1 # lines start numbering with one
1202 length = stop - start
1203 if not length:
1204 beginning -= 1 # empty ranges begin at line just before the range
1205 if length <= 1:
1206 return '{}'.format(beginning)
1207 return '{},{}'.format(beginning, beginning + length - 1)
1208
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001209# See http://www.unix.org/single_unix_specification/
1210def context_diff(a, b, fromfile='', tofile='',
1211 fromfiledate='', tofiledate='', n=3, lineterm='\n'):
1212 r"""
1213 Compare two sequences of lines; generate the delta as a context diff.
1214
1215 Context diffs are a compact way of showing line changes and a few
1216 lines of context. The number of context lines is set by 'n' which
1217 defaults to three.
1218
1219 By default, the diff control lines (those with *** or ---) are
1220 created with a trailing newline. This is helpful so that inputs
1221 created from file.readlines() result in diffs that are suitable for
1222 file.writelines() since both the inputs and outputs have trailing
1223 newlines.
1224
1225 For inputs that do not have trailing newlines, set the lineterm
1226 argument to "" so that the output will be uniformly newline free.
1227
1228 The context diff format normally has a header for filenames and
1229 modification times. Any or all of these may be specified using
1230 strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
R. David Murrayb2416e52010-04-12 16:58:02 +00001231 The modification times are normally expressed in the ISO 8601 format.
1232 If not specified, the strings default to blanks.
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001233
1234 Example:
1235
Ezio Melottid8b509b2011-09-28 17:37:55 +03001236 >>> print(''.join(context_diff('one\ntwo\nthree\nfour\n'.splitlines(True),
1237 ... 'zero\none\ntree\nfour\n'.splitlines(True), 'Original', 'Current')),
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001238 ... end="")
R. David Murrayb2416e52010-04-12 16:58:02 +00001239 *** Original
1240 --- Current
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001241 ***************
1242 *** 1,4 ****
1243 one
1244 ! two
1245 ! three
1246 four
1247 --- 1,4 ----
1248 + zero
1249 one
1250 ! tree
1251 four
1252 """
1253
Greg Ward4d9d2562015-04-20 20:21:21 -04001254 _check_types(a, b, fromfile, tofile, fromfiledate, tofiledate, lineterm)
Raymond Hettinger47e120e2011-04-10 17:14:56 -07001255 prefix = dict(insert='+ ', delete='- ', replace='! ', equal=' ')
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001256 started = False
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001257 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
1258 if not started:
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001259 started = True
Raymond Hettinger47e120e2011-04-10 17:14:56 -07001260 fromdate = '\t{}'.format(fromfiledate) if fromfiledate else ''
1261 todate = '\t{}'.format(tofiledate) if tofiledate else ''
1262 yield '*** {}{}{}'.format(fromfile, fromdate, lineterm)
1263 yield '--- {}{}{}'.format(tofile, todate, lineterm)
Raymond Hettinger7f2d3022003-06-08 19:38:42 +00001264
Raymond Hettinger47e120e2011-04-10 17:14:56 -07001265 first, last = group[0], group[-1]
Raymond Hettinger49353d02011-04-11 12:40:58 -07001266 yield '***************' + lineterm
Raymond Hettinger47e120e2011-04-10 17:14:56 -07001267
Raymond Hettinger9180deb2011-04-12 15:25:30 -07001268 file1_range = _format_range_context(first[1], last[2])
Raymond Hettinger49353d02011-04-11 12:40:58 -07001269 yield '*** {} ****{}'.format(file1_range, lineterm)
1270
Raymond Hettinger47e120e2011-04-10 17:14:56 -07001271 if any(tag in {'replace', 'delete'} for tag, _, _, _, _ in group):
Raymond Hettinger7f2d3022003-06-08 19:38:42 +00001272 for tag, i1, i2, _, _ in group:
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001273 if tag != 'insert':
1274 for line in a[i1:i2]:
Raymond Hettinger47e120e2011-04-10 17:14:56 -07001275 yield prefix[tag] + line
Raymond Hettinger7f2d3022003-06-08 19:38:42 +00001276
Raymond Hettinger9180deb2011-04-12 15:25:30 -07001277 file2_range = _format_range_context(first[3], last[4])
Raymond Hettinger49353d02011-04-11 12:40:58 -07001278 yield '--- {} ----{}'.format(file2_range, lineterm)
1279
Raymond Hettinger47e120e2011-04-10 17:14:56 -07001280 if any(tag in {'replace', 'insert'} for tag, _, _, _, _ in group):
Raymond Hettinger7f2d3022003-06-08 19:38:42 +00001281 for tag, _, _, j1, j2 in group:
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001282 if tag != 'delete':
1283 for line in b[j1:j2]:
Raymond Hettinger47e120e2011-04-10 17:14:56 -07001284 yield prefix[tag] + line
Raymond Hettingerf0b1a1f2003-06-08 11:07:08 +00001285
Greg Ward4d9d2562015-04-20 20:21:21 -04001286def _check_types(a, b, *args):
1287 # Checking types is weird, but the alternative is garbled output when
1288 # someone passes mixed bytes and str to {unified,context}_diff(). E.g.
1289 # without this check, passing filenames as bytes results in output like
1290 # --- b'oldfile.txt'
1291 # +++ b'newfile.txt'
1292 # because of how str.format() incorporates bytes objects.
1293 if a and not isinstance(a[0], str):
1294 raise TypeError('lines to compare must be str, not %s (%r)' %
1295 (type(a[0]).__name__, a[0]))
1296 if b and not isinstance(b[0], str):
1297 raise TypeError('lines to compare must be str, not %s (%r)' %
1298 (type(b[0]).__name__, b[0]))
1299 for arg in args:
1300 if not isinstance(arg, str):
1301 raise TypeError('all arguments must be str, not: %r' % (arg,))
1302
1303def diff_bytes(dfunc, a, b, fromfile=b'', tofile=b'',
1304 fromfiledate=b'', tofiledate=b'', n=3, lineterm=b'\n'):
1305 r"""
1306 Compare `a` and `b`, two sequences of lines represented as bytes rather
1307 than str. This is a wrapper for `dfunc`, which is typically either
1308 unified_diff() or context_diff(). Inputs are losslessly converted to
1309 strings so that `dfunc` only has to worry about strings, and encoded
1310 back to bytes on return. This is necessary to compare files with
1311 unknown or inconsistent encoding. All other inputs (except `n`) must be
1312 bytes rather than str.
1313 """
1314 def decode(s):
1315 try:
1316 return s.decode('ascii', 'surrogateescape')
1317 except AttributeError as err:
1318 msg = ('all arguments must be bytes, not %s (%r)' %
1319 (type(s).__name__, s))
1320 raise TypeError(msg) from err
1321 a = list(map(decode, a))
1322 b = list(map(decode, b))
1323 fromfile = decode(fromfile)
1324 tofile = decode(tofile)
1325 fromfiledate = decode(fromfiledate)
1326 tofiledate = decode(tofiledate)
1327 lineterm = decode(lineterm)
1328
1329 lines = dfunc(a, b, fromfile, tofile, fromfiledate, tofiledate, n, lineterm)
1330 for line in lines:
1331 yield line.encode('ascii', 'surrogateescape')
1332
Tim Peters81b92512002-04-29 01:37:32 +00001333def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK):
Tim Peters5e824c32001-08-12 22:25:01 +00001334 r"""
1335 Compare `a` and `b` (lists of strings); return a `Differ`-style delta.
1336
1337 Optional keyword parameters `linejunk` and `charjunk` are for filter
Andrew Kuchlingc51da2b2014-03-19 16:43:06 -04001338 functions, or can be None:
Tim Peters5e824c32001-08-12 22:25:01 +00001339
Andrew Kuchlingc51da2b2014-03-19 16:43:06 -04001340 - linejunk: A function that should accept a single string argument and
Tim Peters81b92512002-04-29 01:37:32 +00001341 return true iff the string is junk. The default is None, and is
Andrew Kuchlingc51da2b2014-03-19 16:43:06 -04001342 recommended; the underlying SequenceMatcher class has an adaptive
1343 notion of "noise" lines.
Tim Peters5e824c32001-08-12 22:25:01 +00001344
Andrew Kuchlingc51da2b2014-03-19 16:43:06 -04001345 - charjunk: A function that accepts a character (string of length
1346 1), and returns true iff the character is junk. The default is
1347 the module-level function IS_CHARACTER_JUNK, which filters out
1348 whitespace characters (a blank or tab; note: it's a bad idea to
1349 include newline in this!).
Tim Peters5e824c32001-08-12 22:25:01 +00001350
1351 Tools/scripts/ndiff.py is a command-line front-end to this function.
1352
1353 Example:
1354
Ezio Melottid8b509b2011-09-28 17:37:55 +03001355 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True),
1356 ... 'ore\ntree\nemu\n'.splitlines(keepends=True))
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001357 >>> print(''.join(diff), end="")
Tim Peters5e824c32001-08-12 22:25:01 +00001358 - one
1359 ? ^
1360 + ore
1361 ? ^
1362 - two
1363 - three
1364 ? -
1365 + tree
1366 + emu
1367 """
1368 return Differ(linejunk, charjunk).compare(a, b)
1369
Martin v. Löwise064b412004-08-29 16:34:40 +00001370def _mdiff(fromlines, tolines, context=None, linejunk=None,
1371 charjunk=IS_CHARACTER_JUNK):
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001372 r"""Returns generator yielding marked up from/to side by side differences.
Martin v. Löwise064b412004-08-29 16:34:40 +00001373
1374 Arguments:
1375 fromlines -- list of text lines to compared to tolines
1376 tolines -- list of text lines to be compared to fromlines
1377 context -- number of context lines to display on each side of difference,
1378 if None, all from/to text lines will be generated.
1379 linejunk -- passed on to ndiff (see ndiff documentation)
1380 charjunk -- passed on to ndiff (see ndiff documentation)
Tim Peters48bd7f32004-08-29 22:38:38 +00001381
Ezio Melotti30b9d5d2013-08-17 15:50:46 +03001382 This function returns an iterator which returns a tuple:
Martin v. Löwise064b412004-08-29 16:34:40 +00001383 (from line tuple, to line tuple, boolean flag)
1384
1385 from/to line tuple -- (line num, line text)
Mark Dickinson934896d2009-02-21 20:59:32 +00001386 line num -- integer or None (to indicate a context separation)
Martin v. Löwise064b412004-08-29 16:34:40 +00001387 line text -- original line text with following markers inserted:
1388 '\0+' -- marks start of added text
1389 '\0-' -- marks start of deleted text
1390 '\0^' -- marks start of changed text
1391 '\1' -- marks end of added/deleted/changed text
Tim Peters48bd7f32004-08-29 22:38:38 +00001392
Martin v. Löwise064b412004-08-29 16:34:40 +00001393 boolean flag -- None indicates context separation, True indicates
1394 either "from" or "to" line contains a change, otherwise False.
1395
1396 This function/iterator was originally developed to generate side by side
1397 file difference for making HTML pages (see HtmlDiff class for example
1398 usage).
1399
1400 Note, this function utilizes the ndiff function to generate the side by
1401 side difference markup. Optional ndiff arguments may be passed to this
Tim Peters48bd7f32004-08-29 22:38:38 +00001402 function and they in turn will be passed to ndiff.
Martin v. Löwise064b412004-08-29 16:34:40 +00001403 """
Tim Peters48bd7f32004-08-29 22:38:38 +00001404 import re
Martin v. Löwise064b412004-08-29 16:34:40 +00001405
1406 # regular expression for finding intraline change indices
R David Murray44b548d2016-09-08 13:59:53 -04001407 change_re = re.compile(r'(\++|\-+|\^+)')
Tim Peters48bd7f32004-08-29 22:38:38 +00001408
Martin v. Löwise064b412004-08-29 16:34:40 +00001409 # create the difference iterator to generate the differences
1410 diff_lines_iterator = ndiff(fromlines,tolines,linejunk,charjunk)
1411
1412 def _make_line(lines, format_key, side, num_lines=[0,0]):
1413 """Returns line of text with user's change markup and line formatting.
1414
1415 lines -- list of lines from the ndiff generator to produce a line of
1416 text from. When producing the line of text to return, the
1417 lines used are removed from this list.
1418 format_key -- '+' return first line in list with "add" markup around
1419 the entire line.
1420 '-' return first line in list with "delete" markup around
1421 the entire line.
1422 '?' return first line in list with add/delete/change
1423 intraline markup (indices obtained from second line)
1424 None return first line in list with no markup
1425 side -- indice into the num_lines list (0=from,1=to)
1426 num_lines -- from/to current line number. This is NOT intended to be a
1427 passed parameter. It is present as a keyword argument to
1428 maintain memory of the current line numbers between calls
1429 of this function.
1430
1431 Note, this function is purposefully not defined at the module scope so
1432 that data it needs from its parent function (within whose context it
1433 is defined) does not need to be of module scope.
1434 """
1435 num_lines[side] += 1
1436 # Handle case where no user markup is to be added, just return line of
1437 # text with user's line format to allow for usage of the line number.
1438 if format_key is None:
1439 return (num_lines[side],lines.pop(0)[2:])
1440 # Handle case of intraline changes
1441 if format_key == '?':
1442 text, markers = lines.pop(0), lines.pop(0)
1443 # find intraline changes (store change type and indices in tuples)
1444 sub_info = []
1445 def record_sub_info(match_object,sub_info=sub_info):
1446 sub_info.append([match_object.group(1)[0],match_object.span()])
1447 return match_object.group(1)
1448 change_re.sub(record_sub_info,markers)
1449 # process each tuple inserting our special marks that won't be
1450 # noticed by an xml/html escaper.
Raymond Hettingerf25a38e2014-08-03 22:36:32 -07001451 for key,(begin,end) in reversed(sub_info):
Martin v. Löwise064b412004-08-29 16:34:40 +00001452 text = text[0:begin]+'\0'+key+text[begin:end]+'\1'+text[end:]
1453 text = text[2:]
1454 # Handle case of add/delete entire line
1455 else:
1456 text = lines.pop(0)[2:]
1457 # if line of text is just a newline, insert a space so there is
1458 # something for the user to highlight and see.
Tim Peters0ca0c642004-11-12 16:12:15 +00001459 if not text:
1460 text = ' '
Martin v. Löwise064b412004-08-29 16:34:40 +00001461 # insert marks that won't be noticed by an xml/html escaper.
1462 text = '\0' + format_key + text + '\1'
Georg Brandl7eb4b7d2005-07-22 21:49:32 +00001463 # Return line of text, first allow user's line formatter to do its
Martin v. Löwise064b412004-08-29 16:34:40 +00001464 # thing (such as adding the line number) then replace the special
1465 # marks with what the user's change markup.
1466 return (num_lines[side],text)
Tim Peters48bd7f32004-08-29 22:38:38 +00001467
Martin v. Löwise064b412004-08-29 16:34:40 +00001468 def _line_iterator():
1469 """Yields from/to lines of text with a change indication.
1470
1471 This function is an iterator. It itself pulls lines from a
1472 differencing iterator, processes them and yields them. When it can
1473 it yields both a "from" and a "to" line, otherwise it will yield one
1474 or the other. In addition to yielding the lines of from/to text, a
1475 boolean flag is yielded to indicate if the text line(s) have
1476 differences in them.
1477
1478 Note, this function is purposefully not defined at the module scope so
1479 that data it needs from its parent function (within whose context it
1480 is defined) does not need to be of module scope.
1481 """
1482 lines = []
1483 num_blanks_pending, num_blanks_to_yield = 0, 0
Tim Peters48bd7f32004-08-29 22:38:38 +00001484 while True:
Martin v. Löwise064b412004-08-29 16:34:40 +00001485 # Load up next 4 lines so we can look ahead, create strings which
1486 # are a concatenation of the first character of each of the 4 lines
1487 # so we can do some very readable comparisons.
1488 while len(lines) < 4:
Raymond Hettingerbbeac6e2014-08-03 22:49:07 -07001489 lines.append(next(diff_lines_iterator, 'X'))
Martin v. Löwise064b412004-08-29 16:34:40 +00001490 s = ''.join([line[0] for line in lines])
1491 if s.startswith('X'):
1492 # When no more lines, pump out any remaining blank lines so the
1493 # corresponding add/delete lines get a matching blank line so
1494 # all line pairs get yielded at the next level.
1495 num_blanks_to_yield = num_blanks_pending
1496 elif s.startswith('-?+?'):
1497 # simple intraline change
1498 yield _make_line(lines,'?',0), _make_line(lines,'?',1), True
1499 continue
1500 elif s.startswith('--++'):
1501 # in delete block, add block coming: we do NOT want to get
1502 # caught up on blank lines yet, just process the delete line
1503 num_blanks_pending -= 1
1504 yield _make_line(lines,'-',0), None, True
1505 continue
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001506 elif s.startswith(('--?+', '--+', '- ')):
Martin Panter7462b6492015-11-02 03:37:02 +00001507 # in delete block and see an intraline change or unchanged line
Martin v. Löwise064b412004-08-29 16:34:40 +00001508 # coming: yield the delete line and then blanks
1509 from_line,to_line = _make_line(lines,'-',0), None
1510 num_blanks_to_yield,num_blanks_pending = num_blanks_pending-1,0
1511 elif s.startswith('-+?'):
1512 # intraline change
1513 yield _make_line(lines,None,0), _make_line(lines,'?',1), True
1514 continue
1515 elif s.startswith('-?+'):
1516 # intraline change
1517 yield _make_line(lines,'?',0), _make_line(lines,None,1), True
1518 continue
1519 elif s.startswith('-'):
1520 # delete FROM line
1521 num_blanks_pending -= 1
1522 yield _make_line(lines,'-',0), None, True
1523 continue
1524 elif s.startswith('+--'):
1525 # in add block, delete block coming: we do NOT want to get
1526 # caught up on blank lines yet, just process the add line
1527 num_blanks_pending += 1
1528 yield None, _make_line(lines,'+',1), True
1529 continue
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001530 elif s.startswith(('+ ', '+-')):
Martin v. Löwise064b412004-08-29 16:34:40 +00001531 # will be leaving an add block: yield blanks then add line
1532 from_line, to_line = None, _make_line(lines,'+',1)
1533 num_blanks_to_yield,num_blanks_pending = num_blanks_pending+1,0
1534 elif s.startswith('+'):
1535 # inside an add block, yield the add line
1536 num_blanks_pending += 1
1537 yield None, _make_line(lines,'+',1), True
1538 continue
1539 elif s.startswith(' '):
1540 # unchanged text, yield it to both sides
1541 yield _make_line(lines[:],None,0),_make_line(lines,None,1),False
1542 continue
1543 # Catch up on the blank lines so when we yield the next from/to
1544 # pair, they are lined up.
1545 while(num_blanks_to_yield < 0):
1546 num_blanks_to_yield += 1
1547 yield None,('','\n'),True
1548 while(num_blanks_to_yield > 0):
1549 num_blanks_to_yield -= 1
1550 yield ('','\n'),None,True
1551 if s.startswith('X'):
Raymond Hettingerbbeac6e2014-08-03 22:49:07 -07001552 return
Martin v. Löwise064b412004-08-29 16:34:40 +00001553 else:
1554 yield from_line,to_line,True
1555
1556 def _line_pair_iterator():
1557 """Yields from/to lines of text with a change indication.
1558
1559 This function is an iterator. It itself pulls lines from the line
Georg Brandl7eb4b7d2005-07-22 21:49:32 +00001560 iterator. Its difference from that iterator is that this function
Martin v. Löwise064b412004-08-29 16:34:40 +00001561 always yields a pair of from/to text lines (with the change
1562 indication). If necessary it will collect single from/to lines
1563 until it has a matching pair from/to pair to yield.
1564
1565 Note, this function is purposefully not defined at the module scope so
1566 that data it needs from its parent function (within whose context it
1567 is defined) does not need to be of module scope.
1568 """
1569 line_iterator = _line_iterator()
1570 fromlines,tolines=[],[]
1571 while True:
1572 # Collecting lines of text until we have a from/to pair
1573 while (len(fromlines)==0 or len(tolines)==0):
Yury Selivanov68333392015-05-22 11:16:47 -04001574 try:
1575 from_line, to_line, found_diff = next(line_iterator)
1576 except StopIteration:
1577 return
Martin v. Löwise064b412004-08-29 16:34:40 +00001578 if from_line is not None:
1579 fromlines.append((from_line,found_diff))
1580 if to_line is not None:
1581 tolines.append((to_line,found_diff))
1582 # Once we have a pair, remove them from the collection and yield it
1583 from_line, fromDiff = fromlines.pop(0)
1584 to_line, to_diff = tolines.pop(0)
1585 yield (from_line,to_line,fromDiff or to_diff)
1586
1587 # Handle case where user does not want context differencing, just yield
1588 # them up without doing anything else with them.
1589 line_pair_iterator = _line_pair_iterator()
1590 if context is None:
Yury Selivanov8170e8c2015-05-09 11:44:30 -04001591 yield from line_pair_iterator
Martin v. Löwise064b412004-08-29 16:34:40 +00001592 # Handle case where user wants context differencing. We must do some
1593 # storage of lines until we know for sure that they are to be yielded.
1594 else:
1595 context += 1
1596 lines_to_write = 0
1597 while True:
1598 # Store lines up until we find a difference, note use of a
1599 # circular queue because we only need to keep around what
1600 # we need for context.
1601 index, contextLines = 0, [None]*(context)
1602 found_diff = False
1603 while(found_diff is False):
Yury Selivanov68333392015-05-22 11:16:47 -04001604 try:
1605 from_line, to_line, found_diff = next(line_pair_iterator)
1606 except StopIteration:
1607 return
Martin v. Löwise064b412004-08-29 16:34:40 +00001608 i = index % context
1609 contextLines[i] = (from_line, to_line, found_diff)
1610 index += 1
1611 # Yield lines that we have collected so far, but first yield
1612 # the user's separator.
1613 if index > context:
1614 yield None, None, None
1615 lines_to_write = context
1616 else:
1617 lines_to_write = index
1618 index = 0
1619 while(lines_to_write):
1620 i = index % context
1621 index += 1
1622 yield contextLines[i]
1623 lines_to_write -= 1
1624 # Now yield the context lines after the change
1625 lines_to_write = context-1
Raymond Hettinger01b731f2018-04-05 11:19:57 -07001626 try:
1627 while(lines_to_write):
1628 from_line, to_line, found_diff = next(line_pair_iterator)
1629 # If another change within the context, extend the context
1630 if found_diff:
1631 lines_to_write = context-1
1632 else:
1633 lines_to_write -= 1
1634 yield from_line, to_line, found_diff
1635 except StopIteration:
1636 # Catch exception from next() and return normally
1637 return
Martin v. Löwise064b412004-08-29 16:34:40 +00001638
1639
1640_file_template = """
1641<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1642 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1643
1644<html>
1645
1646<head>
Tim Peters48bd7f32004-08-29 22:38:38 +00001647 <meta http-equiv="Content-Type"
Berker Peksag102029d2015-03-15 01:18:47 +02001648 content="text/html; charset=%(charset)s" />
Martin v. Löwise064b412004-08-29 16:34:40 +00001649 <title></title>
1650 <style type="text/css">%(styles)s
1651 </style>
1652</head>
1653
1654<body>
1655 %(table)s%(legend)s
1656</body>
1657
1658</html>"""
1659
1660_styles = """
1661 table.diff {font-family:Courier; border:medium;}
1662 .diff_header {background-color:#e0e0e0}
1663 td.diff_header {text-align:right}
1664 .diff_next {background-color:#c0c0c0}
1665 .diff_add {background-color:#aaffaa}
1666 .diff_chg {background-color:#ffff77}
1667 .diff_sub {background-color:#ffaaaa}"""
1668
1669_table_template = """
Tim Peters48bd7f32004-08-29 22:38:38 +00001670 <table class="diff" id="difflib_chg_%(prefix)s_top"
1671 cellspacing="0" cellpadding="0" rules="groups" >
1672 <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
Martin v. Löwise064b412004-08-29 16:34:40 +00001673 <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
1674 %(header_row)s
1675 <tbody>
1676%(data_rows)s </tbody>
1677 </table>"""
1678
1679_legend = """
1680 <table class="diff" summary="Legends">
1681 <tr> <th colspan="2"> Legends </th> </tr>
1682 <tr> <td> <table border="" summary="Colors">
1683 <tr><th> Colors </th> </tr>
1684 <tr><td class="diff_add">&nbsp;Added&nbsp;</td></tr>
1685 <tr><td class="diff_chg">Changed</td> </tr>
1686 <tr><td class="diff_sub">Deleted</td> </tr>
1687 </table></td>
1688 <td> <table border="" summary="Links">
1689 <tr><th colspan="2"> Links </th> </tr>
1690 <tr><td>(f)irst change</td> </tr>
1691 <tr><td>(n)ext change</td> </tr>
1692 <tr><td>(t)op</td> </tr>
1693 </table></td> </tr>
1694 </table>"""
1695
1696class HtmlDiff(object):
1697 """For producing HTML side by side comparison with change highlights.
1698
1699 This class can be used to create an HTML table (or a complete HTML file
Andrew M. Kuchling55be9ea2004-09-10 12:59:54 +00001700 containing the table) showing a side by side, line by line comparison
Tim Peters48bd7f32004-08-29 22:38:38 +00001701 of text with inter-line and intra-line change highlights. The table can
Martin v. Löwise064b412004-08-29 16:34:40 +00001702 be generated in either full or contextual difference mode.
Tim Peters48bd7f32004-08-29 22:38:38 +00001703
Martin v. Löwise064b412004-08-29 16:34:40 +00001704 The following methods are provided for HTML generation:
1705
1706 make_table -- generates HTML for a single side by side table
1707 make_file -- generates complete HTML file with a single side by side table
1708
Tim Peters48bd7f32004-08-29 22:38:38 +00001709 See tools/scripts/diff.py for an example usage of this class.
Martin v. Löwise064b412004-08-29 16:34:40 +00001710 """
1711
1712 _file_template = _file_template
1713 _styles = _styles
1714 _table_template = _table_template
1715 _legend = _legend
1716 _default_prefix = 0
Tim Peters48bd7f32004-08-29 22:38:38 +00001717
Martin v. Löwise064b412004-08-29 16:34:40 +00001718 def __init__(self,tabsize=8,wrapcolumn=None,linejunk=None,
1719 charjunk=IS_CHARACTER_JUNK):
1720 """HtmlDiff instance initializer
1721
1722 Arguments:
1723 tabsize -- tab stop spacing, defaults to 8.
1724 wrapcolumn -- column number where lines are broken and wrapped,
1725 defaults to None where lines are not wrapped.
Andrew Kuchlingc51da2b2014-03-19 16:43:06 -04001726 linejunk,charjunk -- keyword arguments passed into ndiff() (used by
Tim Peters48bd7f32004-08-29 22:38:38 +00001727 HtmlDiff() to generate the side by side HTML differences). See
Martin v. Löwise064b412004-08-29 16:34:40 +00001728 ndiff() documentation for argument default values and descriptions.
1729 """
1730 self._tabsize = tabsize
1731 self._wrapcolumn = wrapcolumn
1732 self._linejunk = linejunk
1733 self._charjunk = charjunk
1734
Berker Peksag102029d2015-03-15 01:18:47 +02001735 def make_file(self, fromlines, tolines, fromdesc='', todesc='',
1736 context=False, numlines=5, *, charset='utf-8'):
Martin v. Löwise064b412004-08-29 16:34:40 +00001737 """Returns HTML file of side by side comparison with change highlights
1738
1739 Arguments:
1740 fromlines -- list of "from" lines
1741 tolines -- list of "to" lines
1742 fromdesc -- "from" file column header string
1743 todesc -- "to" file column header string
1744 context -- set to True for contextual differences (defaults to False
1745 which shows full differences).
1746 numlines -- number of context lines. When context is set True,
1747 controls number of lines displayed before and after the change.
1748 When context is False, controls the number of lines to place
1749 the "next" link anchors before the next change (so click of
1750 "next" link jumps to just before the change).
Berker Peksag102029d2015-03-15 01:18:47 +02001751 charset -- charset of the HTML document
Martin v. Löwise064b412004-08-29 16:34:40 +00001752 """
Tim Peters48bd7f32004-08-29 22:38:38 +00001753
Berker Peksag102029d2015-03-15 01:18:47 +02001754 return (self._file_template % dict(
1755 styles=self._styles,
1756 legend=self._legend,
1757 table=self.make_table(fromlines, tolines, fromdesc, todesc,
1758 context=context, numlines=numlines),
1759 charset=charset
1760 )).encode(charset, 'xmlcharrefreplace').decode(charset)
Tim Peters48bd7f32004-08-29 22:38:38 +00001761
Martin v. Löwise064b412004-08-29 16:34:40 +00001762 def _tab_newline_replace(self,fromlines,tolines):
1763 """Returns from/to line lists with tabs expanded and newlines removed.
1764
1765 Instead of tab characters being replaced by the number of spaces
1766 needed to fill in to the next tab stop, this function will fill
1767 the space with tab characters. This is done so that the difference
1768 algorithms can identify changes in a file when tabs are replaced by
1769 spaces and vice versa. At the end of the HTML generation, the tab
1770 characters will be replaced with a nonbreakable space.
1771 """
1772 def expand_tabs(line):
1773 # hide real spaces
1774 line = line.replace(' ','\0')
1775 # expand tabs into spaces
1776 line = line.expandtabs(self._tabsize)
Ezio Melotti13925002011-03-16 11:05:33 +02001777 # replace spaces from expanded tabs back into tab characters
Martin v. Löwise064b412004-08-29 16:34:40 +00001778 # (we'll replace them with markup after we do differencing)
1779 line = line.replace(' ','\t')
1780 return line.replace('\0',' ').rstrip('\n')
1781 fromlines = [expand_tabs(line) for line in fromlines]
1782 tolines = [expand_tabs(line) for line in tolines]
1783 return fromlines,tolines
1784
1785 def _split_line(self,data_list,line_num,text):
1786 """Builds list of text lines by splitting text lines at wrap point
1787
1788 This function will determine if the input text line needs to be
1789 wrapped (split) into separate lines. If so, the first wrap point
1790 will be determined and the first line appended to the output
1791 text line list. This function is used recursively to handle
1792 the second part of the split line to further split it.
1793 """
1794 # if blank line or context separator, just add it to the output list
1795 if not line_num:
1796 data_list.append((line_num,text))
1797 return
1798
1799 # if line text doesn't need wrapping, just add it to the output list
1800 size = len(text)
1801 max = self._wrapcolumn
1802 if (size <= max) or ((size -(text.count('\0')*3)) <= max):
1803 data_list.append((line_num,text))
1804 return
1805
1806 # scan text looking for the wrap point, keeping track if the wrap
1807 # point is inside markers
1808 i = 0
1809 n = 0
1810 mark = ''
1811 while n < max and i < size:
1812 if text[i] == '\0':
1813 i += 1
1814 mark = text[i]
1815 i += 1
1816 elif text[i] == '\1':
1817 i += 1
1818 mark = ''
1819 else:
1820 i += 1
1821 n += 1
1822
1823 # wrap point is inside text, break it up into separate lines
1824 line1 = text[:i]
1825 line2 = text[i:]
1826
1827 # if wrap point is inside markers, place end marker at end of first
1828 # line and start marker at beginning of second line because each
1829 # line will have its own table tag markup around it.
1830 if mark:
1831 line1 = line1 + '\1'
1832 line2 = '\0' + mark + line2
1833
Tim Peters48bd7f32004-08-29 22:38:38 +00001834 # tack on first line onto the output list
Martin v. Löwise064b412004-08-29 16:34:40 +00001835 data_list.append((line_num,line1))
1836
Tim Peters48bd7f32004-08-29 22:38:38 +00001837 # use this routine again to wrap the remaining text
Martin v. Löwise064b412004-08-29 16:34:40 +00001838 self._split_line(data_list,'>',line2)
1839
1840 def _line_wrapper(self,diffs):
1841 """Returns iterator that splits (wraps) mdiff text lines"""
1842
1843 # pull from/to data and flags from mdiff iterator
1844 for fromdata,todata,flag in diffs:
1845 # check for context separators and pass them through
1846 if flag is None:
1847 yield fromdata,todata,flag
1848 continue
1849 (fromline,fromtext),(toline,totext) = fromdata,todata
1850 # for each from/to line split it at the wrap column to form
1851 # list of text lines.
1852 fromlist,tolist = [],[]
1853 self._split_line(fromlist,fromline,fromtext)
1854 self._split_line(tolist,toline,totext)
1855 # yield from/to line in pairs inserting blank lines as
1856 # necessary when one side has more wrapped lines
1857 while fromlist or tolist:
1858 if fromlist:
1859 fromdata = fromlist.pop(0)
1860 else:
1861 fromdata = ('',' ')
1862 if tolist:
1863 todata = tolist.pop(0)
1864 else:
1865 todata = ('',' ')
1866 yield fromdata,todata,flag
1867
1868 def _collect_lines(self,diffs):
1869 """Collects mdiff output into separate lists
1870
1871 Before storing the mdiff from/to data into a list, it is converted
1872 into a single line of text with HTML markup.
1873 """
1874
1875 fromlist,tolist,flaglist = [],[],[]
Tim Peters48bd7f32004-08-29 22:38:38 +00001876 # pull from/to data and flags from mdiff style iterator
Martin v. Löwise064b412004-08-29 16:34:40 +00001877 for fromdata,todata,flag in diffs:
1878 try:
1879 # store HTML markup of the lines into the lists
1880 fromlist.append(self._format_line(0,flag,*fromdata))
1881 tolist.append(self._format_line(1,flag,*todata))
1882 except TypeError:
1883 # exceptions occur for lines where context separators go
1884 fromlist.append(None)
1885 tolist.append(None)
1886 flaglist.append(flag)
1887 return fromlist,tolist,flaglist
Tim Peters48bd7f32004-08-29 22:38:38 +00001888
Martin v. Löwise064b412004-08-29 16:34:40 +00001889 def _format_line(self,side,flag,linenum,text):
1890 """Returns HTML markup of "from" / "to" text lines
1891
1892 side -- 0 or 1 indicating "from" or "to" text
1893 flag -- indicates if difference on line
1894 linenum -- line number (used for line number column)
1895 text -- line text to be marked up
1896 """
1897 try:
1898 linenum = '%d' % linenum
1899 id = ' id="%s%s"' % (self._prefix[side],linenum)
1900 except TypeError:
1901 # handle blank lines where linenum is '>' or ''
Tim Peters48bd7f32004-08-29 22:38:38 +00001902 id = ''
Martin v. Löwise064b412004-08-29 16:34:40 +00001903 # replace those things that would get confused with HTML symbols
1904 text=text.replace("&","&amp;").replace(">","&gt;").replace("<","&lt;")
1905
1906 # make space non-breakable so they don't get compressed or line wrapped
1907 text = text.replace(' ','&nbsp;').rstrip()
1908
1909 return '<td class="diff_header"%s>%s</td><td nowrap="nowrap">%s</td>' \
1910 % (id,linenum,text)
1911
1912 def _make_prefix(self):
1913 """Create unique anchor prefixes"""
1914
1915 # Generate a unique anchor prefix so multiple tables
1916 # can exist on the same HTML page without conflicts.
1917 fromprefix = "from%d_" % HtmlDiff._default_prefix
1918 toprefix = "to%d_" % HtmlDiff._default_prefix
1919 HtmlDiff._default_prefix += 1
1920 # store prefixes so line format method has access
1921 self._prefix = [fromprefix,toprefix]
1922
1923 def _convert_flags(self,fromlist,tolist,flaglist,context,numlines):
1924 """Makes list of "next" links"""
Tim Peters48bd7f32004-08-29 22:38:38 +00001925
Martin v. Löwise064b412004-08-29 16:34:40 +00001926 # all anchor names will be generated using the unique "to" prefix
1927 toprefix = self._prefix[1]
Tim Peters48bd7f32004-08-29 22:38:38 +00001928
Martin v. Löwise064b412004-08-29 16:34:40 +00001929 # process change flags, generating middle column of next anchors/links
1930 next_id = ['']*len(flaglist)
1931 next_href = ['']*len(flaglist)
1932 num_chg, in_change = 0, False
1933 last = 0
1934 for i,flag in enumerate(flaglist):
1935 if flag:
1936 if not in_change:
1937 in_change = True
1938 last = i
1939 # at the beginning of a change, drop an anchor a few lines
Tim Peters48bd7f32004-08-29 22:38:38 +00001940 # (the context lines) before the change for the previous
Martin v. Löwise064b412004-08-29 16:34:40 +00001941 # link
1942 i = max([0,i-numlines])
1943 next_id[i] = ' id="difflib_chg_%s_%d"' % (toprefix,num_chg)
Tim Peters48bd7f32004-08-29 22:38:38 +00001944 # at the beginning of a change, drop a link to the next
Martin v. Löwise064b412004-08-29 16:34:40 +00001945 # change
1946 num_chg += 1
1947 next_href[last] = '<a href="#difflib_chg_%s_%d">n</a>' % (
1948 toprefix,num_chg)
1949 else:
1950 in_change = False
1951 # check for cases where there is no content to avoid exceptions
1952 if not flaglist:
1953 flaglist = [False]
1954 next_id = ['']
1955 next_href = ['']
1956 last = 0
1957 if context:
1958 fromlist = ['<td></td><td>&nbsp;No Differences Found&nbsp;</td>']
1959 tolist = fromlist
1960 else:
1961 fromlist = tolist = ['<td></td><td>&nbsp;Empty File&nbsp;</td>']
1962 # if not a change on first line, drop a link
1963 if not flaglist[0]:
1964 next_href[0] = '<a href="#difflib_chg_%s_0">f</a>' % toprefix
1965 # redo the last link to link to the top
1966 next_href[last] = '<a href="#difflib_chg_%s_top">t</a>' % (toprefix)
1967
1968 return fromlist,tolist,flaglist,next_href,next_id
1969
1970 def make_table(self,fromlines,tolines,fromdesc='',todesc='',context=False,
1971 numlines=5):
1972 """Returns HTML table of side by side comparison with change highlights
1973
1974 Arguments:
1975 fromlines -- list of "from" lines
1976 tolines -- list of "to" lines
1977 fromdesc -- "from" file column header string
1978 todesc -- "to" file column header string
1979 context -- set to True for contextual differences (defaults to False
1980 which shows full differences).
1981 numlines -- number of context lines. When context is set True,
1982 controls number of lines displayed before and after the change.
1983 When context is False, controls the number of lines to place
1984 the "next" link anchors before the next change (so click of
1985 "next" link jumps to just before the change).
1986 """
1987
1988 # make unique anchor prefixes so that multiple tables may exist
1989 # on the same page without conflict.
1990 self._make_prefix()
Tim Peters48bd7f32004-08-29 22:38:38 +00001991
Martin v. Löwise064b412004-08-29 16:34:40 +00001992 # change tabs to spaces before it gets more difficult after we insert
Ezio Melotti30b9d5d2013-08-17 15:50:46 +03001993 # markup
Martin v. Löwise064b412004-08-29 16:34:40 +00001994 fromlines,tolines = self._tab_newline_replace(fromlines,tolines)
Tim Peters48bd7f32004-08-29 22:38:38 +00001995
Martin v. Löwise064b412004-08-29 16:34:40 +00001996 # create diffs iterator which generates side by side from/to data
1997 if context:
1998 context_lines = numlines
1999 else:
2000 context_lines = None
2001 diffs = _mdiff(fromlines,tolines,context_lines,linejunk=self._linejunk,
2002 charjunk=self._charjunk)
2003
2004 # set up iterator to wrap lines that exceed desired width
2005 if self._wrapcolumn:
2006 diffs = self._line_wrapper(diffs)
Tim Peters48bd7f32004-08-29 22:38:38 +00002007
Martin v. Löwise064b412004-08-29 16:34:40 +00002008 # collect up from/to lines and flags into lists (also format the lines)
2009 fromlist,tolist,flaglist = self._collect_lines(diffs)
2010
2011 # process change flags, generating middle column of next anchors/links
2012 fromlist,tolist,flaglist,next_href,next_id = self._convert_flags(
2013 fromlist,tolist,flaglist,context,numlines)
2014
Guido van Rossumd8faa362007-04-27 19:54:29 +00002015 s = []
Martin v. Löwise064b412004-08-29 16:34:40 +00002016 fmt = ' <tr><td class="diff_next"%s>%s</td>%s' + \
2017 '<td class="diff_next">%s</td>%s</tr>\n'
2018 for i in range(len(flaglist)):
2019 if flaglist[i] is None:
2020 # mdiff yields None on separator lines skip the bogus ones
2021 # generated for the first line
2022 if i > 0:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002023 s.append(' </tbody> \n <tbody>\n')
Martin v. Löwise064b412004-08-29 16:34:40 +00002024 else:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002025 s.append( fmt % (next_id[i],next_href[i],fromlist[i],
Martin v. Löwise064b412004-08-29 16:34:40 +00002026 next_href[i],tolist[i]))
2027 if fromdesc or todesc:
2028 header_row = '<thead><tr>%s%s%s%s</tr></thead>' % (
2029 '<th class="diff_next"><br /></th>',
2030 '<th colspan="2" class="diff_header">%s</th>' % fromdesc,
2031 '<th class="diff_next"><br /></th>',
2032 '<th colspan="2" class="diff_header">%s</th>' % todesc)
2033 else:
2034 header_row = ''
2035
2036 table = self._table_template % dict(
Guido van Rossumd8faa362007-04-27 19:54:29 +00002037 data_rows=''.join(s),
Martin v. Löwise064b412004-08-29 16:34:40 +00002038 header_row=header_row,
2039 prefix=self._prefix[1])
2040
2041 return table.replace('\0+','<span class="diff_add">'). \
2042 replace('\0-','<span class="diff_sub">'). \
2043 replace('\0^','<span class="diff_chg">'). \
2044 replace('\1','</span>'). \
2045 replace('\t','&nbsp;')
Tim Peters48bd7f32004-08-29 22:38:38 +00002046
Martin v. Löwise064b412004-08-29 16:34:40 +00002047del re
2048
Tim Peters5e824c32001-08-12 22:25:01 +00002049def restore(delta, which):
2050 r"""
Tim Peters8a9c2842001-09-22 21:30:22 +00002051 Generate one of the two sequences that generated a delta.
Tim Peters5e824c32001-08-12 22:25:01 +00002052
2053 Given a `delta` produced by `Differ.compare()` or `ndiff()`, extract
2054 lines originating from file 1 or 2 (parameter `which`), stripping off line
2055 prefixes.
2056
2057 Examples:
2058
Ezio Melottid8b509b2011-09-28 17:37:55 +03002059 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True),
2060 ... 'ore\ntree\nemu\n'.splitlines(keepends=True))
Tim Peters8a9c2842001-09-22 21:30:22 +00002061 >>> diff = list(diff)
Guido van Rossumbe19ed72007-02-09 05:37:30 +00002062 >>> print(''.join(restore(diff, 1)), end="")
Tim Peters5e824c32001-08-12 22:25:01 +00002063 one
2064 two
2065 three
Guido van Rossumbe19ed72007-02-09 05:37:30 +00002066 >>> print(''.join(restore(diff, 2)), end="")
Tim Peters5e824c32001-08-12 22:25:01 +00002067 ore
2068 tree
2069 emu
2070 """
2071 try:
2072 tag = {1: "- ", 2: "+ "}[int(which)]
2073 except KeyError:
Collin Winterce36ad82007-08-30 01:19:48 +00002074 raise ValueError('unknown delta choice (must be 1 or 2): %r'
Serhiy Storchaka5affd232017-04-05 09:37:24 +03002075 % which) from None
Tim Peters5e824c32001-08-12 22:25:01 +00002076 prefixes = (" ", tag)
Tim Peters5e824c32001-08-12 22:25:01 +00002077 for line in delta:
2078 if line[:2] in prefixes:
Tim Peters8a9c2842001-09-22 21:30:22 +00002079 yield line[2:]
Tim Peters5e824c32001-08-12 22:25:01 +00002080
Tim Peters9ae21482001-02-10 08:00:53 +00002081def _test():
2082 import doctest, difflib
2083 return doctest.testmod(difflib)
2084
2085if __name__ == "__main__":
2086 _test()