pass over config in json format
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Thu, 2 Aug 2018 06:13:30 +0000 (07:13 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Thu, 2 Aug 2018 06:13:30 +0000 (07:13 +0100)
src/ifacebase.py
src/spec/gen.py

index 81e4e9d2fcaa8da932a5bd04206744effcbd5052..cdbfc4d63d23e279bca6a988b0f7df533c2d1cf5 100644 (file)
@@ -1,3 +1,4 @@
+import json
 import os.path
 
 try:
@@ -5,6 +6,31 @@ try:
 except ImportError:
     from collections import UserDict
 
+def _decode_list(data):
+    rv = []
+    for item in data:
+        if isinstance(item, unicode):
+            item = item.encode('utf-8')
+        elif isinstance(item, list):
+            item = _decode_list(item)
+        elif isinstance(item, dict):
+            item = _decode_dict(item)
+        rv.append(item)
+    return rv
+
+def _decode_dict(data):
+    rv = {}
+    for key, value in data.iteritems():
+        if isinstance(key, unicode):
+            key = key.encode('utf-8')
+        if isinstance(value, unicode):
+            value = value.encode('utf-8')
+        elif isinstance(value, list):
+            value = _decode_list(value)
+        elif isinstance(value, dict):
+            value = _decode_dict(value)
+        rv[key] = value
+    return rv
 
 class InterfacesBase(UserDict):
     """ contains a list of interface definitions
@@ -14,21 +40,30 @@ class InterfacesBase(UserDict):
         self.pth = pth
         self.fastbus = []
         self.ifacecount = []
+        self.fastbus = []
         if ifaceklsdict is None:
             ifaceklsdict = {}
         UserDict.__init__(self, {})
         if not pth:
             return
         ift = 'interfaces.txt'
+        cfg = 'configs.txt'
         if pth:
             ift = os.path.join(pth, ift)
+            cfg = os.path.join(pth, cfg)
+
+        with open(cfg, 'r') as ifile:
+            self.configs = json.loads(ifile.read(), object_hook=_decode_dict)
+
+        print self.configs
+
+        exit(0)
         with open(ift, 'r') as ifile:
             for ln in ifile.readlines():
                 ln = ln.strip()
                 ln = ln.split("\t")
                 name = ln[0]  # will have uart
                 count = int(ln[1])  # will have count of uart
-                self.fastbus += ln[2:]
                 # spec looks like this:
                 """
                 [{'name': 'sda', 'outen': True},
index e556ae1523bad7d12ad5c443ee7caaf7d4b68f80..492ba535074087c93ffb4a8d879082ba6b2c64d7 100644 (file)
@@ -1,4 +1,5 @@
 import os
+import json
 import os.path
 from spec.interfaces import Pinouts
 
@@ -30,15 +31,7 @@ def specgen(of, pth, pinouts, bankspec, muxwidths, pinbanks, fixedpins,
     if not os.path.exists(pth):
         os.makedirs(pth)
     with open(os.path.join(pth, 'configs.txt'), 'w') as f:
-        for (name, d) in configs.items():
-            d = d.items()
-            d.sort()
-            vals = []
-            for (k, v) in d:
-                vals.append("%s=%s" % (k, repr(v)))
-            line = [name.lower(), '\t'.join(vals)]
-            line = '\t'.join(line)
-            f.write("%s\n" % line)
+        f.write("%s\n" % json.dumps(configs))
 
     with open(os.path.join(pth, 'interfaces.txt'), 'w') as f:
         for k in pinouts.fnspec.keys():