add comments
[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.ifacecount = []
42 self.fastbus = []
43 if ifaceklsdict is None:
44 ifaceklsdict = {}
45 UserDict.__init__(self, {})
46 if not pth:
47 return
48 ift = 'interfaces.txt'
49 cfg = 'configs.txt'
50 if pth:
51 ift = os.path.join(pth, ift)
52 cfg = os.path.join(pth, cfg)
53
54 # read in configs in JSON format, but strip out unicode
55 with open(cfg, 'r') as ifile:
56 self.configs = json.loads(ifile.read(), object_hook=_decode_dict)
57
58 # process the configs, look for "bus" type... XXX TODO; make this
59 # a bit more sophisticated
60 self.fastbus = []
61 for (ifacename, v) in self.configs.items():
62 if v.get('bus', "") == "fastbus":
63 self.fastbus.append(ifacename)
64
65 # reads the interfaces, name and quantity of each
66 with open(ift, 'r') as ifile:
67 for ln in ifile.readlines():
68 ln = ln.strip()
69 ln = ln.split("\t")
70 name = ln[0] # will have uart
71 count = int(ln[1]) # will have count of uart
72 # spec looks like this:
73 """
74 [{'name': 'sda', 'outen': True},
75 {'name': 'scl', 'outen': True},
76 ]
77 """
78 ikls = ifacekls
79 for k, v in ifaceklsdict.items():
80 if name.startswith(k):
81 ikls = v
82 break
83 spec, ganged = self.read_spec(pth, name)
84 # XXX HORRIBLE hack!!!
85 if name == 'pwm' and count == 1 and len(spec) != 1:
86 #print "read", name, count, spec, ganged
87 #print "multi pwm", spec[:1], len(spec)
88 spec[0]['name'] = 'out'
89 iface = ikls(name, spec[:1], ganged, False)
90 self.ifaceadd(name, len(spec), iface)
91 else:
92 iface = ikls(name, spec, ganged, count == 1)
93 self.ifaceadd(name, count, iface)
94
95 def getifacetype(self, fname):
96 # finds the interface type, e.g sd_d0 returns "inout"
97 for iface in self.values():
98 typ = iface.getifacetype(fname)
99 # if fname.startswith('pwm'):
100 # print fname, iface.ifacename, typ
101 if typ:
102 return typ
103 return None
104
105 def ifaceadd(self, name, count, iface, at=None):
106 if at is None:
107 at = len(self.ifacecount) # ifacecount is a list
108 self.ifacecount.insert(at, (name, count)) # appends the list
109 # with (name,count) *at* times
110 self[name] = iface
111
112 """
113 will check specific files of kind peripheral.txt like spi.txt,
114 uart.txt in test directory
115 """
116
117 def read_spec(self, pth, name):
118 spec = []
119 ganged = {}
120 fname = '%s.txt' % name
121 if pth:
122 ift = os.path.join(pth, fname)
123 with open(ift, 'r') as sfile:
124 for ln in sfile.readlines():
125 ln = ln.strip()
126 ln = ln.split("\t")
127 name = ln[0]
128 d = {'name': name, # here we start to make the dictionary
129 'type': ln[1]}
130 if ln[1] == 'out':
131 d['action'] = True # adding element to the dict
132 elif ln[1] == 'inout':
133 d['outen'] = True
134 if len(ln) == 3:
135 bus = ln[2]
136 if bus not in ganged:
137 ganged[bus] = []
138 ganged[bus].append(name)
139 spec.append(d)
140 return spec, ganged