Attempt to add traceback logging
[redakcja.git] / scripts / nice_diff.py
1 import difflib
2 import re
3
4 def read_whole(path):
5         with open(path) as f:
6                 return f.readlines()
7
8 DIFF_RE = re.compile(r"""\x00([+^-])""" ,re.UNICODE)
9 NAMES = { '+': 'added', '-': 'removed', '^': 'changed' }
10
11 def diff_replace(match):
12         return """<span class="diff_mark diff_mark_%s">""" %  NAMES[match.group(1)]
13
14
15 def filter_line(line):
16         return DIFF_RE.sub(diff_replace, line).replace('\x01', '</span>')
17
18 def group(diffs):
19         group_a = []
20         group_b = []
21
22         a, b, _ = diffs.next()
23         group_a.append(a[1])
24         group_b.append(b[1])
25
26
27         for _ in range(10):
28                 a, b, _ = diffs.next()
29                 group_a.append(a[1])
30                 group_b.append(b[1])    
31
32                 while a[0] == '':
33                         a, b, _ = diffs.next()
34                         group_a.append(a[1])
35                         group_b.append(b[1])
36
37                 yield group_a[:-1], group_b[:-1]
38                 group_a, group_b = group_a[-1:], group_b[-1:]
39
40 def join_to_html(diffs):
41         for group_a, group_b in group(diffs):
42                 yield """
43 <div class="change_block">
44         <div class="old_block">%s</div>
45         <div class="new_block">%s</div>
46 </div>""" % ( 
47         '\n'.join( filter_line(line) for line in group_a ),
48         '\n'.join( filter_line(line) for line in group_b ),
49         )
50
51 fa = read_whole("file_a")
52 fb = read_whole("file_b")
53
54 print '\n'.join( repr(x) for x in  difflib._mdiff(fa, fb)  )
55 print "**************************"
56 print '\n'.join( join_to_html( difflib._mdiff(fa, fb) ) )
57 # print '\n'.join( repr(x) for x in group(difflib._mdiff(fa, fb)) )