Commit 03e0dcca authored by Christophe Dumez's avatar Christophe Dumez

- code cleanup


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@6479 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 15369bb0
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# #
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved. # Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
# Yoshinori Okuji <yo@nexedi.com> # Yoshinori Okuji <yo@nexedi.com>
# Christophe Dumez <christophe@nexedi.com>
# #
# WARNING: This program as such is intended to be used by professional # WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential # programmers who take the whole responsability of assessing all potential
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# #
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved. # Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
# Yoshinori Okuji <yo@nexedi.com> # Yoshinori Okuji <yo@nexedi.com>
# Christophe Dumez <christophe@nexedi.com>
# #
# WARNING: This program as such is intended to be used by professional # WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential # programmers who take the whole responsability of assessing all potential
...@@ -115,7 +116,6 @@ class DiffFile: ...@@ -115,7 +116,6 @@ class DiffFile:
Escape &, <, and > in a string of data. Escape &, <, and > in a string of data.
This is a copy of the xml.sax.saxutils.escape function. This is a copy of the xml.sax.saxutils.escape function.
""" """
# must do ampersand first
if data: if data:
#data = data.replace("&", "&amp;") #data = data.replace("&", "&amp;")
data = data.replace(">", "&gt;") data = data.replace(">", "&gt;")
...@@ -132,36 +132,23 @@ class DiffFile: ...@@ -132,36 +132,23 @@ class DiffFile:
<td style="background-color: black;" width="2"></td> <td style="background-color: black;" width="2"></td>
<td style="background-color: grey"><b><center>%s</center></b></td> <td style="background-color: grey"><b><center>%s</center></b></td>
</tr>'''%(self.old_revision, self.new_revision) </tr>'''%(self.old_revision, self.new_revision)
First = True header_color = 'grey'
for child in self.children: for child in self.children:
# Adding line number of the modification # Adding line number of the modification
if First: html += '''<tr height="18px"><td style="background-color: %s">&nbsp;</td><td style="background-color: black;" width="2"></td><td style="background-color: %s">&nbsp;</td></tr> <tr height="18px">
html += '''<tr height="18px"><td style="background-color: grey">&nbsp;</td><td style="background-color: black;" width="2"></td><td style="background-color: grey">&nbsp;</td></tr> <tr height="18px">
<td style="background-color: rgb(68, 132, 255);"><b>Line %s</b></td> <td style="background-color: rgb(68, 132, 255);"><b>Line %s</b></td>
<td style="background-color: black;" width="2"></td> <td style="background-color: black;" width="2"></td>
<td style="background-color: rgb(68, 132, 255);"><b>Line %s</b></td> <td style="background-color: rgb(68, 132, 255);"><b>Line %s</b></td>
</tr>'''%(child.old_line, child.new_line) </tr>'''%(header_color, header_color, child.old_line, child.new_line)
First = False header_color = 'white'
else:
html += '''<tr height="18px"><td style="background-color: white">&nbsp;</td><td style="background-color: black;" width="2"></td><td style="background-color: white">&nbsp;</td></tr> <tr height="18px">
<td style="background-color: rgb(68, 132, 255);"><b>Line %s</b></td>
<td style="background-color: black;" width="2"></td>
<td style="background-color: rgb(68, 132, 255);"><b>Line %s</b></td>
</tr>'''%(child.old_line, child.new_line)
# Adding diff of the modification # Adding diff of the modification
old_code_list = child.getOldCodeList() old_code_list = child.getOldCodeList()
new_code_list = child.getNewCodeList() new_code_list = child.getNewCodeList()
i=0 i=0
for old_line_tuple in old_code_list: for old_line_tuple in old_code_list:
new_line_tuple = new_code_list[i] new_line_tuple = new_code_list[i]
if new_line_tuple[0]: new_line = new_line_tuple[0] or ' '
new_line = new_line_tuple[0] old_line = old_line_tuple[0] or ' '
else:
new_line = ' '
if old_line_tuple[0]:
old_line = old_line_tuple[0]
else:
old_line = ' '
i+=1 i+=1
html += ''' <tr height="18px"> html += ''' <tr height="18px">
<td style="background-color: %s">%s</td> <td style="background-color: %s">%s</td>
...@@ -169,7 +156,7 @@ class DiffFile: ...@@ -169,7 +156,7 @@ class DiffFile:
<td style="background-color: %s">%s</td> <td style="background-color: %s">%s</td>
</tr>'''%(old_line_tuple[1], self._escape(old_line).replace(' ', '&nbsp;').replace('\t', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'), new_line_tuple[1], self._escape(new_line).replace(' ', '&nbsp;').replace('\t', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')) </tr>'''%(old_line_tuple[1], self._escape(old_line).replace(' ', '&nbsp;').replace('\t', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'), new_line_tuple[1], self._escape(new_line).replace(' ', '&nbsp;').replace('\t', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'))
html += ''' </tbody> html += ''' </tbody>
</table></font><br><br>''' </table><br><br>'''
return html return html
...@@ -234,6 +221,8 @@ class SubCodeBlock: ...@@ -234,6 +221,8 @@ class SubCodeBlock:
def __init__(self, code): def __init__(self, code):
self.body = code self.body = code
self.modification = self._getModif() self.modification = self._getModif()
self.old_code_length = self._getOldCodeLength()
self.new_code_length = self._getNewCodeLength()
# Choosing background color # Choosing background color
if self.modification == 'none': if self.modification == 'none':
self.color = 'white' self.color = 'white'
...@@ -241,7 +230,7 @@ class SubCodeBlock: ...@@ -241,7 +230,7 @@ class SubCodeBlock:
self.color = 'rgb(253, 228, 6);'#light orange self.color = 'rgb(253, 228, 6);'#light orange
elif self.modification == 'deletion': elif self.modification == 'deletion':
self.color = 'rgb(253, 117, 74);'#light red self.color = 'rgb(253, 117, 74);'#light red
else: else: # addition
self.color = 'rgb(83, 253, 74);'#light green self.color = 'rgb(83, 253, 74);'#light green
def _getModif(self): def _getModif(self):
...@@ -252,18 +241,38 @@ class SubCodeBlock: ...@@ -252,18 +241,38 @@ class SubCodeBlock:
nb_minus-=1 nb_minus-=1
elif line.startswith("+"): elif line.startswith("+"):
nb_plus+=1 nb_plus+=1
if (nb_plus!=0 and nb_minus==0):
return 'addition'
if (nb_plus==0 and nb_minus!=0):
return 'deletion'
if (nb_plus==0 and nb_minus==0): if (nb_plus==0 and nb_minus==0):
return 'none' return 'none'
if (nb_minus==0):
return 'addition'
if (nb_plus==0):
return 'deletion'
return 'change' return 'change'
def _getOldCodeLength(self):
nb_lines = 0
for line in self.body.split('\n'):
if not line.startswith("+"):
nb_lines+=1
return nb_lines
def _getNewCodeLength(self):
nb_lines = 0
for line in self.body.split('\n'):
if not line.startswith("-"):
nb_lines+=1
return nb_lines
# Return code before modification # Return code before modification
def getOldCodeList(self): def getOldCodeList(self):
if self.modification=='none': if self.modification=='none':
old_code = [(x, 'white') for x in self.body.split('\n')] old_code = [(x, 'white') for x in self.body.split('\n')]
elif self.modification=='change':
old_code = [self._getOldCodeList(x) for x in self.body.split('\n') if self._getOldCodeList(x)[0]]
# we want old_code_list and new_code_list to have the same length
if(self.old_code_length < self.new_code_length):
filling = [(None, self.color)]*(self.new_code_length-self.old_code_length)
old_code.extend(filling)
else: # deletion or addition else: # deletion or addition
old_code = [self._getOldCodeList(x) for x in self.body.split('\n')] old_code = [self._getOldCodeList(x) for x in self.body.split('\n')]
return old_code return old_code
...@@ -279,6 +288,12 @@ class SubCodeBlock: ...@@ -279,6 +288,12 @@ class SubCodeBlock:
def getNewCodeList(self): def getNewCodeList(self):
if self.modification=='none': if self.modification=='none':
new_code = [(x, 'white') for x in self.body.split('\n')] new_code = [(x, 'white') for x in self.body.split('\n')]
elif self.modification=='change':
new_code = [self._getNewCodeList(x) for x in self.body.split('\n') if self._getNewCodeList(x)[0]]
# we want old_code_list and new_code_list to have the same length
if(self.new_code_length < self.old_code_length):
filling = [(None, self.color)]*(self.old_code_length-self.new_code_length)
new_code.extend(filling)
else: # deletion or addition else: # deletion or addition
new_code = [self._getNewCodeList(x) for x in self.body.split('\n')] new_code = [self._getNewCodeList(x) for x in self.body.split('\n')]
return new_code return new_code
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment