Deprecate "to" and privatize "from" cnf structure methods
authorPlamen Dimitrov <plamen.dimitrov@intra2net.com>
Sat, 16 Apr 2022 15:18:22 +0000 (18:18 +0300)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 19 May 2022 09:13:27 +0000 (11:13 +0200)
These are no longer officially used by any client code and are now
restricted only to one directional internal use when it comes to
the old functionality of reading cnfvars from strings or JSON data
which were originally developed to use the same format (cnf structure).

src/cnfvar/model.py

index 83bea0a..d04e8dd 100644 (file)
@@ -508,18 +508,6 @@ class BaseCnf:
 class CnfListSerializationMixin(BaseCnfList):
     """Add serialization support to BaseCnfList."""
 
-    def to_cnf_structure(self, renumber=True):
-        """
-        Convert this list to an object meaningful to :py:mod:`cnfvar`.
-
-        :param bool renumber: whether to fix up the number/ids of the CNFs
-        :returns: a dictionary with the converted values
-        :rtype: {str, {str, str or int}}
-        """
-        if renumber:
-            self.renumber()
-        return {"cnf": [x.to_cnf_structure() for x in self]}
-
     def to_cnf_file(self, path, renumber=True, encoding=ENCODING):
         """
         Dump a string representation of this list in the cnfvar format to a file.
@@ -571,16 +559,16 @@ class CnfListSerializationMixin(BaseCnfList):
             fp.write(self.to_json_string(renumber=renumber))
 
     @classmethod
-    def from_cnf_structure(cls, obj):
+    def _from_cnf_structure(cls, obj):
         """
-        Create a list from a cnfvar object from the :py:mod:`cnfvar` module.
+        Create a list from a JSON structure obtainable from `get_cnf --json`.
 
         :param obj: an object as defined in the :py:mod:`cnfvar`
         :type obj: {str, {str, str or int}}
         :returns: a list of cnfvars
         :rtype: :py:class:`CnfList`
         """
-        return cls(map(Cnf.from_cnf_structure, obj["cnf"]))
+        return cls(map(Cnf._from_cnf_structure, obj["cnf"]))
 
     @classmethod
     def from_cnf_string(cls, data):
@@ -592,7 +580,7 @@ class CnfListSerializationMixin(BaseCnfList):
         :rtype: :py:class:`CnfList`
         """
         cnf_obj = string.read_cnf(data)
-        return CnfList.from_cnf_structure(cnf_obj)
+        return CnfList._from_cnf_structure(cnf_obj)
 
     @classmethod
     def from_json_string(cls, data):
@@ -604,7 +592,7 @@ class CnfListSerializationMixin(BaseCnfList):
         :rtype: :py:class:`CnfList`
         """
         cnf_obj = json.loads(data)
-        return CnfList.from_cnf_structure(cnf_obj)
+        return CnfList._from_cnf_structure(cnf_obj)
 
     @classmethod
     def from_cnf_file(cls, path, encoding=ENCODING):
@@ -636,30 +624,6 @@ class CnfListSerializationMixin(BaseCnfList):
 class CnfSerializationMixin(BaseCnf):
     """Add serialization support to BaseCnf."""
 
-    def to_cnf_structure(self):
-        """
-        Convert this instance to dictionary from the :py:mod:`cnfvar` module.
-
-        :returns: the dictionary created
-        :rtype: {str, str or int}
-
-        .. todo:: this method is still needed because dumping cnf variables
-        to strings (json or not) is still delegated to the old cnfvar module.
-        """
-        d = {
-            "number": self.lineno,
-            "varname": self.name,
-            "data": self.value,
-            "instance": self.instance
-        }
-        if self.parent and self.parent.lineno:
-            d["parent"] = self.parent.lineno
-        if self.comment is not None:
-            d["comment"] = self.comment
-        if len(self.children) > 0:
-            d["children"] = [c.to_cnf_structure() for c in self.children]
-        return d
-
     def to_json_string(self, renumber=True):
         """
         Convert this instance to a JSON string.
@@ -690,9 +654,9 @@ class CnfSerializationMixin(BaseCnf):
         CnfList([self]).to_json_file(path, renumber=renumber)
 
     @classmethod
-    def from_cnf_structure(cls, obj):
+    def _from_cnf_structure(cls, obj):
         """
-        Create an instance from a dictionary from the :py:mod:`cnfvar` module.
+        Create an instance from a JSON structure obtainable from `get_cnf --json`.
 
         :param obj: dictionary to convert to this instance
         :type obj: {str, str or int}
@@ -703,7 +667,7 @@ class CnfSerializationMixin(BaseCnf):
                   instance=obj["instance"], lineno=obj["number"],
                   comment=obj.get("comment", None))
         for ch_obj in obj.get("children", []):
-            child_cnf = Cnf.from_cnf_structure(ch_obj)
+            child_cnf = Cnf._from_cnf_structure(ch_obj)
             cnf.add_child(child_cnf)
         return cnf