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