5 from UserDict
import UserDict
7 from collections
import UserDict
9 def _decode_list(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
)
21 def _decode_dict(data
):
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
)
35 class InterfacesBase(UserDict
):
36 """ contains a list of interface definitions
39 def __init__(self
, ifacekls
, pth
=None, ifaceklsdict
=None):
43 if ifaceklsdict
is None:
45 UserDict
.__init
__(self
, {})
48 ift
= 'interfaces.txt'
51 ift
= os
.path
.join(pth
, ift
)
52 cfg
= os
.path
.join(pth
, cfg
)
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
)
58 # process the configs, look for "bus" type... XXX TODO; make this
59 # a bit more sophisticated
61 for (ifacename
, v
) in self
.configs
.items():
62 if v
.get('bus', "") == "fastbus":
63 self
.fastbus
.append(ifacename
)
65 # reads the interfaces, name and quantity of each
66 with
open(ift
, 'r') as ifile
:
67 for ln
in ifile
.readlines():
70 name
= ln
[0] # will have uart
71 count
= int(ln
[1]) # will have count of uart
72 # spec looks like this:
74 [{'name': 'sda', 'outen': True},
75 {'name': 'scl', 'outen': True},
79 for k
, v
in ifaceklsdict
.items():
80 if name
.startswith(k
):
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
)
92 iface
= ikls(name
, spec
, ganged
, count
== 1)
93 self
.ifaceadd(name
, count
, iface
)
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
105 def ifaceadd(self
, name
, count
, iface
, at
=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
113 will check specific files of kind peripheral.txt like spi.txt,
114 uart.txt in test directory
117 def read_spec(self
, pth
, name
):
120 fname
= '%s.txt' % name
122 ift
= os
.path
.join(pth
, fname
)
123 with
open(ift
, 'r') as sfile
:
124 for ln
in sfile
.readlines():
128 d
= {'name': name
, # here we start to make the dictionary
131 d
['action'] = True # adding element to the dict
132 elif ln
[1] == 'inout':
136 if bus
not in ganged
:
138 ganged
[bus
].append(name
)