]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Mostly finished ConfigDiffer
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 7 Apr 2017 12:56:04 +0000 (14:56 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 7 Apr 2017 12:56:04 +0000 (14:56 +0200)
pp_lib/differ.py

index 33fc4611a3458097da316a6e9b8479724462a72d..98609e8e8eb9d9d511b5e6e582e7902386400066 100644 (file)
@@ -23,7 +23,7 @@ import six
 
 # Own modules
 
-__version__ = '0.2.2'
+__version__ = '0.2.3'
 LOG = logging.getLogger(__name__)
 
 DEFAULT_COMMENT_CHAR = '#'
@@ -94,6 +94,8 @@ class ConfigDiffer(Differ):
         pat = r'^(\s*(?:[^\s"' + r"'" + r']+|\\["' + r"'" + r'])+)(?#token-wo-quote)'
         self.re_token_list.append(re.compile(pat, re_flags))
 
+        self.re_whitespace = re.compile(r'\s+', re_flags)
+
         self.re_empty = re.compile(r'^(\s*)$')
 
         if self.comment_chars:
@@ -115,20 +117,6 @@ class ConfigDiffer(Differ):
 
                 i += 1
 
-    # -------------------------------------------------------------------------
-    def is_equal(self, a, b):
-
-        equal = True
-        for line in self.compare(a, b):
-            if line.startswith('+') or line.startswith('-'):
-                subline = line[1:]
-                if self.is_line_junk(subline):
-                    LOG.debug("Line {!r} is junk.".format(subline))
-                else:
-                    equal = False
-
-        return equal
-
     # -------------------------------------------------------------------------
     def remove_comments(self, line):
 
@@ -207,6 +195,59 @@ class ConfigDiffer(Differ):
         out += ", ".join(fields) + ")>"
         return out
 
+    # -------------------------------------------------------------------------
+    def _mangle_lines(self, lines):
+
+        if isinstance(lines, (list, set, tuple)):
+            line_list = copy.copy(lines)
+        else:
+            line_list = [str(lines)]
+
+        if ( not self.ignore_empty and not self.ignore_whitespace and
+                not self.ignore_comment and not self.case_insensitive):
+            return line_list
+
+        reult_list = []
+        for item in line_list:
+            if self.ignore_empty and self.re_empty.search(item):
+                continue
+            item_cp = str(item)
+            if self.ignore_whitespace:
+                item_cp = self.re_whitespace.sub(' ', item_cp)
+            if self.ignore_comment:
+                item_cp = = self.remove_comments(item_cp)
+            if self.case_insensitive:
+                item_cp = item_cp.lower()
+            reult_list.append(item_cp)
+
+        return reult_list
+
+    # -------------------------------------------------------------------------
+    def compare(self, a, b):
+
+        list_a = self._mangle_lines(a)
+        list_b = self._mangle_lines(b)
+
+        return super(ConfigDiffer, self).compare(list_a, list_b)
+
+    # -------------------------------------------------------------------------
+    def unified_diff(self, a, b, n=3, lineterm='\n'):
+
+        list_a = self._mangle_lines(a)
+        list_b = self._mangle_lines(b)
+
+        return difflib.unified_diff(list_a, list_b, n=n, lineterm=lineterm)
+
+    # -------------------------------------------------------------------------
+    def is_equal(self, a, b):
+
+        equal = True
+        for line in self.compare(a, b):
+            if not line.startswith(' '):
+                LOG.debug("Difference line: {}".format(line))
+                equal = False
+
+        return equal
 
 
 # =============================================================================