Back to index

moin  1.9.0~rc2
Functions
MoinMoin.util.diff_text Namespace Reference

Functions

def diff

Function Documentation

def MoinMoin.util.diff_text.diff (   oldlines,
  newlines,
  kw 
)
Find changes between oldlines and newlines.

@param oldlines: list of old text lines
@param newlines: list of new text lines
@keyword ignorews: if 1: ignore whitespace
@rtype: list
@return: lines like diff tool does output.

Definition at line 10 of file diff_text.py.

00010 
00011 def diff(oldlines, newlines, **kw):
00012     """
00013     Find changes between oldlines and newlines.
00014 
00015     @param oldlines: list of old text lines
00016     @param newlines: list of new text lines
00017     @keyword ignorews: if 1: ignore whitespace
00018     @rtype: list
00019     @return: lines like diff tool does output.
00020     """
00021     false = lambda s: None
00022     if kw.get('ignorews', 0):
00023         d = difflib.Differ(false)
00024     else:
00025         d = difflib.Differ(false, false)
00026 
00027     lines = list(d.compare(oldlines, newlines))
00028 
00029     # return empty list if there were no changes
00030     changed = 0
00031     for l in lines:
00032         if l[0] != ' ':
00033             changed = 1
00034             break
00035     if not changed: return []
00036 
00037 #    if not "we want the unchanged lines, too":
00038 #        if "no questionmark lines":
00039 #            lines = [line for line in lines if line[0] != '?']
00040 #        return lines
00041 
00042 
00043     # calculate the hunks and remove the unchanged lines between them
00044     i = 0              # actual index in lines
00045     count = 0          # number of unchanged lines
00046     lcount_old = 0     # line count old file
00047     lcount_new = 0     # line count new file
00048     while i < len(lines):
00049         marker = lines[i][0]
00050         if marker == ' ':
00051             count = count + 1
00052             i = i + 1
00053             lcount_old = lcount_old + 1
00054             lcount_new = lcount_new + 1
00055         elif marker in ['-', '+']:
00056             if (count == i) and count > 3:
00057                 lines[:i-3] = []
00058                 i = 4
00059                 count = 0
00060             elif count > 6:
00061                 # remove lines and insert new hunk indicator
00062                 lines[i-count+3:i-3] = ['@@ -%i, +%i @@\n' %
00063                                         (lcount_old, lcount_new)]
00064                 i = i - count + 8
00065                 count = 0
00066             else:
00067                 count = 0
00068                 i += 1
00069             if marker == '-': lcount_old = lcount_old + 1
00070             else: lcount_new = lcount_new + 1
00071         elif marker == '?':
00072             lines[i:i+1] = []
00073 
00074     # remove unchanged lines a the end
00075     if count > 3:
00076         lines[-count+3:] = []
00077 
00078     return lines
00079