enhance coding style.
Signed-off-by: Sergey Satskiy <sergey.satskiy@gmail.com>
Signed-off-by: Olivier Matz <zer0@droids-corp.org>
-html_hdr="""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
+html_hdr = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<meta name="generator" content="diff2html.rb" />
<title>HTML Diff</title>
<html><head>
<meta name="generator" content="diff2html.rb" />
<title>HTML Diff</title>
-DIFFON="\x01"
-DIFFOFF="\x02"
+DIFFON = "\x01"
+DIFFOFF = "\x02"
-buf=[]
-add_cpt, del_cpt = 0,0
-line1, line2 = 0,0
-hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0,0,0,0
+buf = []
+add_cpt, del_cpt = 0, 0
+line1, line2 = 0, 0
+hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0, 0, 0, 0
# Characters we're willing to word wrap on
# Characters we're willing to word wrap on
for i in x:
j = ord(i)
if i not in ['\t', '\n'] and ((j < 32) or (j >= 127)):
for i in x:
j = ord(i)
if i not in ['\t', '\n'] and ((j < 32) or (j >= 127)):
return r
def linediff(s, t):
if len(s):
return r
def linediff(s, t):
if len(s):
- s=str(reduce(lambda x,y:x+y, [ sane(c) for c in s ]))
+ s = str(reduce(lambda x, y:x+y, [ sane(c) for c in s ]))
- t=str(reduce(lambda x,y:x+y, [ sane(c) for c in t ]))
-
- m,n = len(s), len(t)
- d=[[(0,0) for i in range(n+1)] for i in range(m+1)]
- x=[[(0,0) for i in range(n+1)] for i in range(m+1)]
-
-
- d[0][0] = (0, (0,0))
+ t = str(reduce(lambda x, y:x+y, [ sane(c) for c in t ]))
+
+ m, n = len(s), len(t)
+ d = [[(0, 0) for i in range(n+1)] for i in range(m+1)]
+ x = [[(0, 0) for i in range(n+1)] for i in range(m+1)]
+
+
+ d[0][0] = (0, (0, 0))
for i in range(m+1)[1:]:
for j in range(n+1)[1:]:
for i in range(m+1)[1:]:
for j in range(n+1)[1:]:
- d[i][j] = min((d[i-1][j][0] + 1, (i-1,j)),
- (d[i][j-1][0] + 1, (i,j-1)),
- (d[i-1][j-1][0] + cost, (i-1,j-1)))
-
- l=[]
- coord = (m,n)
- while coord != (0,0):
+ d[i][j] = min((d[i-1][j][0] + 1, (i-1, j)),
+ (d[i][j-1][0] + 1, (i, j-1)),
+ (d[i-1][j-1][0] + cost, (i-1, j-1)))
+
+ l = []
+ coord = (m, n)
+ while coord != (0, 0):
coord = d[x][y][1]
l1 = []
l2 = []
for coord in l:
coord = d[x][y][1]
l1 = []
l2 = []
for coord in l:
father_coord = d[cx][cy][1]
father_coord = d[cx][cy][1]
father_val = d[fx][fy][0]
diff = (cx-fx, cy-fy)
father_val = d[fx][fy][0]
diff = (cx-fx, cy-fy)
l1.append("")
l2.append(DIFFON + t[fy] + DIFFOFF)
l1.append("")
l2.append(DIFFON + t[fy] + DIFFOFF)
l1.append(DIFFON + s[fx] + DIFFOFF)
l2.append("")
elif child_val-father_val == 1:
l1.append(DIFFON + s[fx] + DIFFOFF)
l2.append("")
elif child_val-father_val == 1:
l1.append(s[fx])
l2.append(t[fy])
l1.append(s[fx])
l2.append(t[fy])
- r1,r2 = (reduce(lambda x,y:x+y, l1), reduce(lambda x,y:x+y, l2))
- return r1,r2
+ r1, r2 = (reduce(lambda x, y:x+y, l1), reduce(lambda x, y:x+y, l2))
+ return r1, r2
def convert(s, linesize=0, ponct=0):
def convert(s, linesize=0, ponct=0):
l=[]
for c in s:
# used by diffs
l=[]
for c in s:
# used by diffs
t += '<span class="diffchanged2">'
t += '<span class="diffchanged2">'
t += "</span>"
# special html chars
elif htmlentitydefs.codepoint2name.has_key(ord(c)):
t += "</span>"
# special html chars
elif htmlentitydefs.codepoint2name.has_key(ord(c)):
- t += "&%s;"%(htmlentitydefs.codepoint2name[ord(c)])
+ t += "&%s;" % (htmlentitydefs.codepoint2name[ord(c)])
i += 1
# special highlighted chars
i += 1
# special highlighted chars
- elif c=="\t" and ponct==1:
+ elif c == "\t" and ponct == 1:
+ if n == 0:
+ n = tabsize
t += ('<span class="diffponct">»</span>'+' '*(n-1))
t += ('<span class="diffponct">»</span>'+' '*(n-1))
- elif c==" " and ponct==1:
+ elif c == " " and ponct == 1:
t += '<span class="diffponct">·</span>'
t += '<span class="diffponct">·</span>'
- elif c=="\n" and ponct==1:
+ elif c == "\n" and ponct == 1:
if show_CR:
t += '<span class="diffponct">\</span>'
else:
t += c
i += 1
if show_CR:
t += '<span class="diffponct">\</span>'
else:
t += c
i += 1
- if linesize and (WORDBREAK.count(c)==1):
+ if linesize and (WORDBREAK.count(c) == 1):
- i=0
- if linesize and i>linesize:
- i=0
+ i = 0
+ if linesize and i > linesize:
+ i = 0
outputfile.write('<td colspan="2">Offset %d, %d lines modified</td></tr>\n'%(hunk_off2, hunk_size2))
else:
# ⋮ - vertical ellipsis
outputfile.write('<td colspan="2">Offset %d, %d lines modified</td></tr>\n'%(hunk_off2, hunk_size2))
else:
# ⋮ - vertical ellipsis
- outputfile.write('<tr class="diffhunk"><td colspan="2">⋮</td><td colspan="2">⋮</td></tr>');
+ outputfile.write('<tr class="diffhunk"><td colspan="2">⋮</td><td colspan="2">⋮</td></tr>')
def add_line(s1, s2):
global line1
global line2
def add_line(s1, s2):
global line1
global line2
- if s1==None and s2==None:
- type_name="unmodified"
- elif s1==None or s1=="":
- type_name="added"
- elif s2==None or s1=="":
- type_name="deleted"
- elif s1==s2:
- type_name="unmodified"
+ if s1 == None and s2 == None:
+ type_name = "unmodified"
+ elif s1 == None or s1 == "":
+ type_name = "added"
+ elif s2 == None or s1 == "":
+ type_name = "deleted"
+ elif s1 == s2:
+ type_name = "unmodified"
- type_name="changed"
- s1,s2 = linediff(s1, s2)
+ type_name = "changed"
+ s1, s2 = linediff(s1, s2)
- outputfile.write('<tr class="diff%s">'%type_name)
- if s1!=None and s1!="":
- outputfile.write('<td class="diffline">%d </td>'%line1)
+ outputfile.write('<tr class="diff%s">' % type_name)
+ if s1 != None and s1 != "":
+ outputfile.write('<td class="diffline">%d </td>' % line1)
outputfile.write('<td class="diffpresent">')
outputfile.write(convert(s1, linesize=linesize, ponct=1))
outputfile.write('</td>')
else:
outputfile.write('<td class="diffpresent">')
outputfile.write(convert(s1, linesize=linesize, ponct=1))
outputfile.write('</td>')
else:
outputfile.write('<td colspan="2"> </td>')
outputfile.write('<td colspan="2"> </td>')
-
- if s2!=None and s2!="":
+
+ if s2 != None and s2 != "":
outputfile.write('<td class="diffline">%d </td>'%line2)
outputfile.write('<td class="diffpresent">')
outputfile.write(convert(s2, linesize=linesize, ponct=1))
outputfile.write('</td>')
else:
outputfile.write('<td class="diffline">%d </td>'%line2)
outputfile.write('<td class="diffpresent">')
outputfile.write(convert(s2, linesize=linesize, ponct=1))
outputfile.write('</td>')
else:
outputfile.write('<td colspan="2"></td>')
outputfile.write('</tr>\n')
outputfile.write('<td colspan="2"></td>')
outputfile.write('</tr>\n')
max_len = (len(l0) > len(l1)) and len(l0) or len(l1)
for i in range(max_len):
s0, s1 = "", ""
max_len = (len(l0) > len(l1)) and len(l0) or len(l1)
for i in range(max_len):
s0, s1 = "", ""
s1 = l1[i]
add_line(s0, s1)
s1 = l1[i]
add_line(s0, s1)
-
- add_cpt, del_cpt = 0,0
+
+ add_cpt, del_cpt = 0, 0
if not exclude_headers:
outputfile.write(html_hdr)
outputfile.write(table_hdr)
if not exclude_headers:
outputfile.write(html_hdr)
outputfile.write(table_hdr)
- l=inputfile.readline()
- if l=="":
+ l = inputfile.readline()
+ if l == "":
- m=re.match('^--- ([^\s]*)', l)
+ m = re.match('^--- ([^\s]*)', l)
- file1=m.groups()[0]
- l=inputfile.readline()
- m=re.match('^\+\+\+ ([^\s]*)', l)
+ file1 = m.groups()[0]
+ l = inputfile.readline()
+ m = re.match('^\+\+\+ ([^\s]*)', l)
add_filename(file1, file2)
add_filename(file1, file2)
- hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0,0,0,0
+ hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0, 0, 0, 0
- m=re.match("@@ -(\d+),?(\d*) \+(\d+),?(\d*)", l)
+ m = re.match("@@ -(\d+),?(\d*) \+(\d+),?(\d*)", l)
if m:
empty_buffer()
hunk_data = map(lambda x:x=="" and 1 or int(x), m.groups())
if m:
empty_buffer()
hunk_data = map(lambda x:x=="" and 1 or int(x), m.groups())