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.
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):
: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):
: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):
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.
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}
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