From: Frank Brehm Date: Fri, 7 Apr 2017 12:56:04 +0000 (+0200) Subject: Mostly finished ConfigDiffer X-Git-Tag: 0.1.2~208 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=32a95282e94f4d2b1876683a9a98ad7b5abd231b;p=pixelpark%2Fadmin-tools.git Mostly finished ConfigDiffer --- diff --git a/pp_lib/differ.py b/pp_lib/differ.py index 33fc461..98609e8 100644 --- a/pp_lib/differ.py +++ b/pp_lib/differ.py @@ -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 # =============================================================================