pass over config in json format
[pinmux.git] / src / ifacebase.py
1 import json
2 import os.path
3
4 try:
5 from UserDict import UserDict
6 except ImportError:
7 from collections import UserDict
8
9 def _decode_list(data):
10 rv = []
11 for item in data:
12 if isinstance(item, unicode):
13 item = item.encode('utf-8')
14 elif isinstance(item, list):
15 item = _decode_list(item)
16 elif isinstance(item, dict):
17 item = _decode_dict(item)
18 rv.append(item)
19 return rv
20
21 def _decode_dict(data):
22 rv = {}
23 for key, value in data.iteritems():
24 if isinstance(key, unicode):
25 key = key.encode('utf-8')
26 if isinstance(value, unicode):
27 value = value.encode('utf-8')
28 elif isinstance(value, list):
29 value = _decode_list(value)
30 elif isinstance(value, dict):
31 value = _decode_dict(value)
32 rv[key] = value
33 return rv
34
35 class InterfacesBase(UserDict):
36 """ contains a list of interface definitions
37 """
38
39 def __init__(self, ifacekls, pth=None, ifaceklsdict=None):
40 self.pth = pth
41 self.fastbus = []
42 self.ifacecount = []
43 self.fastbus = []
44 if ifaceklsdict is None:
45 ifaceklsdict = {}
46 UserDict.__init__(self, {})
47 if not pth:
48 return
49 ift = 'interfaces.txt'
50 cfg = 'configs.txt'
51 if pth:
52 ift = os.path.join(pth, ift)
53 cfg = os.path.join(pth, cfg)
54
55 with open(cfg, 'r') as ifile:
56 self.configs = json.loads(ifile.read(), object_hook=_decode_dict)
57
58 print self.configs
59
60 exit(0)
61 with open(ift, 'r') as ifile:
62 for ln in ifile.readlines():
63 ln = ln.strip()
64 ln = ln.split("\t")
65 name = ln[0] # will have uart
66 count = int(ln[1]) # will have count of uart
67 # spec looks like this:
68 """
69 [{'name': 'sda', 'outen': True},
70 {'name': 'scl', 'outen': True},
71 ]
72 """
73 ikls = ifacekls
74 for k, v in ifaceklsdict.items():
75 if name.startswith(k):
76 ikls = v
77 break
78 spec, ganged = self.read_spec(pth, name)
79 # XXX HORRIBLE hack!!!
80 if name == 'pwm' and count == 1 and len(spec) != 1:
81 #print "read", name, count, spec, ganged
82 #print "multi pwm", spec[:1], len(spec)
83 spec[0]['name'] = 'out'
84 iface = ikls(name, spec[:1], ganged, False)
85 self.ifaceadd(name, len(spec), iface)
86 else:
87 iface = ikls(name, spec, ganged, count == 1)
88 self.ifaceadd(name, count, iface)
89
90 def getifacetype(self, fname):
91 # finds the interface type, e.g sd_d0 returns "inout"
92 for iface in self.values():
93 typ = iface.getifacetype(fname)
94 # if fname.startswith('pwm'):
95 # print fname, iface.ifacename, typ
96 if typ:
97 return typ
98 return None
99
100 def ifaceadd(self, name, count, iface, at=None):
101 if at is None:
102 at = len(self.ifacecount) # ifacecount is a list
103 self.ifacecount.insert(at, (name, count)) # appends the list
104 # with (name,count) *at* times
105 self[name] = iface
106
107 """
108 will check specific files of kind peripheral.txt like spi.txt,
109 uart.txt in test directory
110 """
111
112 def read_spec(self, pth, name):
113 spec = []
114 ganged = {}
115 fname = '%s.txt' % name
116 if pth:
117 ift = os.path.join(pth, fname)
118 with open(ift, 'r') as sfile:
119 for ln in sfile.readlines():
120 ln = ln.strip()
121 ln = ln.split("\t")
122 name = ln[0]
123 d = {'name': name, # here we start to make the dictionary
124 'type': ln[1]}
125 if ln[1] == 'out':
126 d['action'] = True # adding element to the dict
127 elif ln[1] == 'inout':
128 d['outen'] = True
129 if len(ln) == 3:
130 bus = ln[2]
131 if bus not in ganged:
132 ganged[bus] = []
133 ganged[bus].append(name)
134 spec.append(d)
135 return spec, ganged