5 from UserDict
import UserDict
7 from collections
import UserDict
10 def _decode_list(data
):
13 if isinstance(item
, unicode):
14 item
= item
.encode('utf-8')
15 elif isinstance(item
, list):
16 item
= _decode_list(item
)
17 elif isinstance(item
, dict):
18 item
= _decode_dict(item
)
23 def _decode_dict(data
):
25 for key
, value
in data
.iteritems():
26 if isinstance(key
, unicode):
27 key
= key
.encode('utf-8')
28 if isinstance(value
, unicode):
29 value
= value
.encode('utf-8')
30 elif isinstance(value
, list):
31 value
= _decode_list(value
)
32 elif isinstance(value
, dict):
33 value
= _decode_dict(value
)
38 class InterfacesBase(UserDict
):
39 """ contains a list of interface definitions
42 def __init__(self
, ifacekls
, pth
=None, ifaceklsdict
=None):
46 if ifaceklsdict
is None:
48 UserDict
.__init
__(self
, {})
51 ift
= 'interfaces.txt'
54 ift
= os
.path
.join(pth
, ift
)
55 cfg
= os
.path
.join(pth
, cfg
)
57 # read in configs in JSON format, but strip out unicode
58 with
open(cfg
, 'r') as ifile
:
59 self
.configs
= json
.loads(ifile
.read(), object_hook
=_decode_dict
)
61 # process the configs, look for "bus" type... XXX TODO; make this
62 # a bit more sophisticated
64 for (ifacename
, v
) in self
.configs
.items():
65 if v
.get('bus', "") == "fastbus":
66 self
.fastbus
.append(ifacename
)
68 # reads the interfaces, name and quantity of each
69 with
open(ift
, 'r') as ifile
:
70 for ln
in ifile
.readlines():
73 name
= ln
[0] # will have uart
74 count
= int(ln
[1]) # will have count of uart
76 # spec looks like this:
78 [{'name': 'sda', 'outen': True},
79 {'name': 'scl', 'outen': True},
83 for k
, v
in ifaceklsdict
.items():
84 if name
.startswith(k
):
87 spec
, ganged
= self
.read_spec(pth
, name
)
88 # XXX HORRIBLE hack!!!
89 if name
== 'pwm' and count
== 1 and len(spec
) != 1:
90 #print "read", name, count, spec, ganged
91 #print "multi pwm", spec[:1], len(spec)
92 spec
[0]['name'] = 'out'
93 iface
= ikls(name
, spec
[:1], ganged
, False)
94 self
.ifaceadd(name
, len(spec
), iface
)
96 iface
= ikls(name
, spec
, ganged
, count
== 1)
97 self
.ifaceadd(name
, count
, iface
)
98 cfgs
= self
.getconfigs(name
, count
)
100 print name
, count
, cfgs
102 def getconfigs(self
, fname
, count
):
104 for i
in range(count
):
108 name
= "%s%d" % (fname
, i
)
109 cfgs
.append(self
.configs
.get(name
, {}))
112 def getifacetype(self
, fname
):
113 # finds the interface type, e.g sd_d0 returns "inout"
114 for iface
in self
.values():
115 typ
= iface
.getifacetype(fname
)
116 # if fname.startswith('pwm'):
117 # print fname, iface.ifacename, typ
122 def ifaceadd(self
, name
, count
, iface
, at
=None):
124 at
= len(self
.ifacecount
) # ifacecount is a list
125 self
.ifacecount
.insert(at
, (name
, count
)) # appends the list
126 # with (name,count) *at* times
130 will check specific files of kind peripheral.txt like spi.txt,
131 uart.txt in test directory
134 def read_spec(self
, pth
, name
):
137 fname
= '%s.txt' % name
139 ift
= os
.path
.join(pth
, fname
)
140 with
open(ift
, 'r') as sfile
:
141 for ln
in sfile
.readlines():
145 d
= {'name': name
, # here we start to make the dictionary
148 d
['action'] = True # adding element to the dict
149 elif ln
[1] == 'inout':
153 if bus
not in ganged
:
155 ganged
[bus
].append(name
)