annotate MoinMoin/util/diff_text.py @ 6100:e9ef58bdad15

use difflib from stdlib
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 06 Sep 2016 00:20:17 +0200
parents 01f05e74aa9c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1019
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
2 """
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
3 MoinMoin - simple text diff (uses difflib)
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4
1918
bb2e053067fb fixing copyright headers: remove umlauts (encoding troubles), make epydoc compatible, reformat
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1866
diff changeset
5 @copyright: 2006 MoinMoin:ThomasWaldmann
1019
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
7 """
6100
e9ef58bdad15 use difflib from stdlib
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
8 import difflib
1019
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10 def diff(oldlines, newlines, **kw):
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11 """
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12 Find changes between oldlines and newlines.
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
13
1019
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14 @param oldlines: list of old text lines
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15 @param newlines: list of new text lines
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16 @keyword ignorews: if 1: ignore whitespace
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17 @rtype: list
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18 @return: lines like diff tool does output.
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19 """
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20 false = lambda s: None
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21 if kw.get('ignorews', 0):
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
22 d = difflib.Differ(false)
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23 else:
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
24 d = difflib.Differ(false, false)
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
26 lines = list(d.compare(oldlines, newlines))
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
28 # return empty list if there were no changes
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
29 changed = 0
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
30 for l in lines:
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
31 if l[0] != ' ':
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32 changed = 1
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
33 break
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
34 if not changed: return []
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
35
1866
0194beaf511e reduce reduce, filter and map usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1019
diff changeset
36 # if not "we want the unchanged lines, too":
0194beaf511e reduce reduce, filter and map usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1019
diff changeset
37 # if "no questionmark lines":
0194beaf511e reduce reduce, filter and map usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1019
diff changeset
38 # lines = [line for line in lines if line[0] != '?']
0194beaf511e reduce reduce, filter and map usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1019
diff changeset
39 # return lines
1019
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
40
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
41
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
42 # calculate the hunks and remove the unchanged lines between them
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
43 i = 0 # actual index in lines
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
44 count = 0 # number of unchanged lines
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
45 lcount_old = 0 # line count old file
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
46 lcount_new = 0 # line count new file
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
47 while i < len(lines):
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
48 marker = lines[i][0]
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
49 if marker == ' ':
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
50 count = count + 1
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
51 i = i + 1
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
52 lcount_old = lcount_old + 1
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
53 lcount_new = lcount_new + 1
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
54 elif marker in ['-', '+']:
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
55 if (count == i) and count > 3:
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
56 lines[:i-3] = []
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
57 i = 4
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
58 count = 0
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
59 elif count > 6:
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
60 # remove lines and insert new hunk indicator
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
61 lines[i-count+3:i-3] = ['@@ -%i, +%i @@\n' %
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
62 (lcount_old, lcount_new)]
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63 i = i - count + 8
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
64 count = 0
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65 else:
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66 count = 0
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67 i += 1
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68 if marker == '-': lcount_old = lcount_old + 1
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69 else: lcount_new = lcount_new + 1
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
70 elif marker == '?':
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 lines[i:i+1] = []
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
73 # remove unchanged lines a the end
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74 if count > 3:
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75 lines[-count+3:] = []
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
77 return lines
6488692b1eb8 have all diff implementations at one place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78