Make CnfVar lists comparable
[pyi2ncommon] / src / cnfvar / model.py
index 6e46bdd..7944d78 100644 (file)
@@ -34,6 +34,7 @@ extend them with extra functionality.
 """
 
 import json
+from typing import Sequence, Callable, Any, Tuple, cast as type_hints_pseudo_cast
 
 from . import string
 from .. import arnied_api
@@ -945,6 +946,79 @@ class CnfListQueryingMixin(BaseCnfList):
         return max([c.instance for c in self]) if len(self) > 0 else -1
 
 
+class CnfDiff(list):
+    """A list of differences between :py:class:`BaseCnfList`s"""
+
+    def add_missing(self, cnf: BaseCnf, ancestry: Sequence[BaseCnf]):
+        self.append(("-", cnf, ancestry))
+
+    def add_excess(self, cnf: BaseCnf, ancestry: Sequence[BaseCnf]):
+        self.append(("+", cnf, ancestry))
+
+    def print(self, output_func: Callable[[str], Any] = print):
+        """
+        Create a string representation of this diff and "print" it, using given function
+
+        :param output_func: Function to use for printing
+        :return: Iterator over text lines
+        """
+        for diff_type, cnf, ancestry in self:
+            ancestral_string = ' > '.join(f"{anc.name}={anc.value}" for anc in ancestry)
+            output_func(f"cnf diff: {diff_type} {cnf.name} ({cnf.instance}) = {cnf.value!r} in "
+                        + ancestral_string)
+
+
+class CnfCompareMixin(BaseCnfList):
+    """Mixin to add a `compare()` function."""
+
+    def compare(self, other: BaseCnfList, ignore_list: Sequence[str] = None,
+                ancestry: Tuple[BaseCnf, ...] = None) -> CnfDiff:
+        """
+        Compare this list of config variables to another list, return differences.
+
+        :param other: another list of configuration variables
+        :param ignore_list: names of variables to ignore
+        :param ancestry: when comparing recursively, we call this function on children (of children ...).
+                         This is the "path" of parent cnf vars that lead to the list of children we
+                         currently compare.
+        :return: difference between self and other
+        """
+        if ancestry is None:
+            ancestry = ()
+        if ignore_list is None:
+            ignore_list = ()
+        diff = CnfDiff()
+        # check whether all own values also appear in other config
+        for c_own in self:
+            c_own = type_hints_pseudo_cast(BaseCnf, c_own)
+            if c_own.name in ignore_list:
+                continue
+            c_other = other.where(lambda c: c_own == c)
+            if len(c_other) == 0:
+                diff.add_missing(c_own, ancestry)
+            elif len(c_other) == 1:
+                # found matching entry. Recurse into children
+                diff.extend(c_own.children.compare(c_other[0].children, ignore_list,
+                                                   ancestry + (c_own, )))
+            else:
+                # several variables in other have the same name, value, parent, and instance as c_own?!
+                raise NotImplementedError("This should not be possible!")
+
+        # reverse check: all other values also appear in own config?
+        for c_other in other:
+            c_other = type_hints_pseudo_cast(BaseCnf, c_other)
+            if c_other.name in ignore_list:
+                continue
+            c_own = self.where(lambda c: c_other == c)
+            if len(c_own) == 0:
+                diff.add_excess(c_other, ancestry)
+            elif len(c_own) == 1:
+                pass   # no need to descend into children again
+            else:
+                # several variables in self have the same name, value, parent, and instance as c_other?!
+                raise NotImplementedError("This should not be possible!")
+        return diff
+
 ###############################################################################
 # PUBLIC CLASSES
 ###############################################################################
@@ -953,7 +1027,7 @@ class CnfListQueryingMixin(BaseCnfList):
 #
 
 
-class CnfList(CnfListSerializationMixin, CnfListArniedApiMixin, CnfListQueryingMixin):
+class CnfList(CnfListSerializationMixin, CnfListArniedApiMixin, CnfListQueryingMixin, CnfCompareMixin):
     """Collection of Cnf variables."""
 
     pass
@@ -965,4 +1039,4 @@ class Cnf(CnfSerializationMixin, CnfArniedApiMixin, CnfShortcutsMixin):
     pass
 
 
-__all__ = ["CnfList", "Cnf"]
+__all__ = ["CnfList", "Cnf", "CnfDiff"]