normalize cnfvars for JSON output too
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Thu, 7 Feb 2019 13:16:44 +0000 (14:16 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 7 Feb 2019 15:10:53 +0000 (16:10 +0100)
Reported by Christian: The normalization pass that ensures
varnames are uppercase was missing from the JSON based routines
which caused them to be rejected by set_cnf(). Implement it by
passing a normalized copy of the CNF tree to the json writer.

src/cnfvar.py

index ffd967d..833a606 100644 (file)
@@ -673,6 +673,29 @@ def cnf_root (root):
     return cnf
 
 
+def normalize_cnf (cnf):
+    """
+    Ensure the output conforms to set_cnf()’s expectations.
+    """
+    if isinstance (cnf, list) is False:
+        raise MalformedCNF ("expected list of CNF_VARs, got [%s]" % type (cnf))
+    def norm (var):
+        vvar = \
+            { "number"   : var ["number"]
+            , "varname"  : var ["varname"].upper ()
+            , "instance" : var ["instance"]
+            , "data"     : var ["data"]
+            }
+
+        children = var.get ("children", None)
+        if children is not None:
+            vvar ["children"] = normalize_cnf (children)
+
+        return vvar
+
+    return [ norm (var) for var in cnf ]
+
+
 def print_cnf_raw(root, out=None):
     if root is not None:
         out.write(root)
@@ -728,7 +751,7 @@ def dump_cnf_string(root, renumber=False):
 
 def print_cnf(root, out=None, renumber=False):
     if root is not None:
-        output_cnf(root, out or sys.stdout, renumber=renumber)
+        output_cnf(root, out or sys.stdout, renumber=renumber, bytes=False)
 
 
 def write_cnf(*argv, **kw_argv):
@@ -742,6 +765,7 @@ def output_json(root, out, renumber=False):
     if renumber is True:
         _count = renumber_vars(root)
     if is_cnf(root) is True:
+        root ["cnf"] = normalize_cnf(cnf_root (root))
         data = json.dumps(root)
         out.write(data)
         out.write("\n")