[meld] Introduce namedtuple chunk representation to improve clarity



commit 92295b5f64082709c01ec799b06c348b2ecb2d77
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Wed Jun 20 06:36:12 2012 +1000

    Introduce namedtuple chunk representation to improve clarity
    
    This commit also adapts a couple of existing chunk manipulation methods
    to correctly return namedtuple-style chunks.

 meld/diffutil.py |   14 ++++++++------
 meld/matchers.py |   10 +++++++++-
 2 files changed, 17 insertions(+), 7 deletions(-)
---
diff --git a/meld/diffutil.py b/meld/diffutil.py
index 6630094..517afce 100644
--- a/meld/diffutil.py
+++ b/meld/diffutil.py
@@ -19,7 +19,7 @@ import difflib
 
 import gobject
 
-from matchers import MyersSequenceMatcher
+from matchers import DiffChunk, MyersSequenceMatcher
 
 ################################################################################
 #
@@ -62,7 +62,8 @@ opcode_reverse = {
 }
 
 def reverse_chunk(chunk):
-    return opcode_reverse[chunk[0]], chunk[3], chunk[4], chunk[1], chunk[2]
+    tag = opcode_reverse[chunk[0]]
+    return DiffChunk._make((tag, chunk[3], chunk[4], chunk[1], chunk[2]))
 
 ################################################################################
 #
@@ -174,16 +175,17 @@ class Differ(gobject.GObject):
     def _consume_blank_lines(self, c, texts, pane1, pane2):
         if c is None:
             return None
-        c0 = c[0]
+
+        tag = c.tag
         c1, c2 = self._find_blank_lines(texts[pane1], c[1], c[2])
         c3, c4 = self._find_blank_lines(texts[pane2], c[3], c[4])
         if c1 == c2 and c3 == c4:
             return None
-        if c1 == c2 and c[0] == "replace":
+        if c1 == c2 and tag == "replace":
             c0 = "insert"
-        elif c3 == c4 and c[0] == "replace":
+        elif c3 == c4 and tag == "replace":
             c0 = "delete"
-        return (c0, c1, c2, c3, c4)
+        return DiffChunk._make((tag, c1, c2, c3, c4))
 
     def _find_blank_lines(self, txt, lo, hi):
         for line in range(lo, hi):
diff --git a/meld/matchers.py b/meld/matchers.py
index 1b145fb..1b7c152 100644
--- a/meld/matchers.py
+++ b/meld/matchers.py
@@ -14,6 +14,7 @@
 ### along with this program; if not, write to the Free Software
 ### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
+from collections import namedtuple
 import difflib
 
 
@@ -51,6 +52,9 @@ def find_common_suffix(a, b):
     return 0
 
 
+DiffChunk = namedtuple('DiffChunk', 'tag, start_a, end_a, start_b, end_b')
+
+
 class MyersSequenceMatcher(difflib.SequenceMatcher):
 
     def __init__(self, isjunk=None, a="", b=""):
@@ -71,8 +75,12 @@ class MyersSequenceMatcher(difflib.SequenceMatcher):
                 pass
         return self.matching_blocks
 
+    def get_opcodes(self):
+        opcodes = difflib.SequenceMatcher.get_opcodes(self)
+        return [DiffChunk._make(chunk) for chunk in opcodes]
+
     def get_difference_opcodes(self):
-        return filter(lambda x: x[0] != "equal", self.get_opcodes())
+        return [chunk for chunk in self.get_opcodes() if chunk.tag != "equal"]
 
     def preprocess_remove_prefix_suffix(self, a, b):
         # remove common prefix and common suffix



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]