cdbfc4d63d23e279bca6a988b0f7df533c2d1cf5
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):
44 if ifaceklsdict
is None:
46 UserDict
.__init
__(self
, {})
49 ift
= 'interfaces.txt'
52 ift
= os
.path
.join(pth
, ift
)
53 cfg
= os
.path
.join(pth
, cfg
)
55 with
open(cfg
, 'r') as ifile
:
56 self
.configs
= json
.loads(ifile
.read(), object_hook
=_decode_dict
)
61 with
open(ift
, 'r') as ifile
:
62 for ln
in ifile
.readlines():
65 name
= ln
[0] # will have uart
66 count
= int(ln
[1]) # will have count of uart
67 # spec looks like this:
69 [{'name': 'sda', 'outen': True},
70 {'name': 'scl', 'outen': True},
74 for k
, v
in ifaceklsdict
.items():
75 if name
.startswith(k
):
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
)
87 iface
= ikls(name
, spec
, ganged
, count
== 1)
88 self
.ifaceadd(name
, count
, iface
)
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
100 def ifaceadd(self
, name
, count
, iface
, at
=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
108 will check specific files of kind peripheral.txt like spi.txt,
109 uart.txt in test directory
112 def read_spec(self
, pth
, name
):
115 fname
= '%s.txt' % name
117 ift
= os
.path
.join(pth
, fname
)
118 with
open(ift
, 'r') as sfile
:
119 for ln
in sfile
.readlines():
123 d
= {'name': name
, # here we start to make the dictionary
126 d
['action'] = True # adding element to the dict
127 elif ln
[1] == 'inout':
131 if bus
not in ganged
:
133 ganged
[bus
].append(name
)