+import json
import os.path
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
"""
- def __init__(self, ifacekls, pth=None):
+ def __init__(self, ifacekls, pth=None, ifaceklsdict=None):
self.pth = pth
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)
+
+ # read in configs in JSON format, but strip out unicode
+ with open(cfg, 'r') as ifile:
+ self.configs = json.loads(ifile.read(), object_hook=_decode_dict)
+
+ # process the configs, look for "bus" type... XXX TODO; make this
+ # a bit more sophisticated
+ self.fastbus = []
+ for (ifacename, v) in self.configs.items():
+ if v.get('bus', "") == "fastbus":
+ self.fastbus.append(ifacename)
+
+ # reads the interfaces, name and quantity of each
with open(ift, 'r') as ifile:
for ln in ifile.readlines():
ln = ln.strip()
{'name': 'scl', 'outen': True},
]
"""
+ ikls = ifacekls
+ for k, v in ifaceklsdict.items():
+ if name.startswith(k):
+ ikls = v
+ break
spec, ganged = self.read_spec(pth, name)
# XXX HORRIBLE hack!!!
if name == 'pwm' and count == 1 and len(spec) != 1:
#print "read", name, count, spec, ganged
#print "multi pwm", spec[:1], len(spec)
spec[0]['name'] = 'out'
- iface = ifacekls(name, spec[:1], ganged, False)
+ iface = ikls(name, spec[:1], ganged, False)
self.ifaceadd(name, len(spec), iface)
else:
- iface = ifacekls(name, spec, ganged, count == 1)
+ iface = ikls(name, spec, ganged, count == 1)
self.ifaceadd(name, count, iface)
def getifacetype(self, fname):