xref: /openbmc/pyphosphor/obmc/utils/dtree.py (revision d641c086b5cc5f873c69aab454d1d3297e178192)
1e2b65e90SBrad Bishop# Contributors Listed Below - COPYRIGHT 2016
2e2b65e90SBrad Bishop# [+] International Business Machines Corp.
3e2b65e90SBrad Bishop#
4e2b65e90SBrad Bishop#
5e2b65e90SBrad Bishop# Licensed under the Apache License, Version 2.0 (the "License");
6e2b65e90SBrad Bishop# you may not use this file except in compliance with the License.
7e2b65e90SBrad Bishop# You may obtain a copy of the License at
8e2b65e90SBrad Bishop#
9e2b65e90SBrad Bishop#     http://www.apache.org/licenses/LICENSE-2.0
10e2b65e90SBrad Bishop#
11e2b65e90SBrad Bishop# Unless required by applicable law or agreed to in writing, software
12e2b65e90SBrad Bishop# distributed under the License is distributed on an "AS IS" BASIS,
13e2b65e90SBrad Bishop# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14e2b65e90SBrad Bishop# implied. See the License for the specific language governing
15e2b65e90SBrad Bishop# permissions and limitations under the License.
16e2b65e90SBrad Bishop
17e2b65e90SBrad Bishop
18e2b65e90SBrad Bishopdef dts_encode(obj, fd, **kw):
19e2b65e90SBrad Bishop    ''' A rudimentary python to dts encoder.
20e2b65e90SBrad Bishop    '''
21e2b65e90SBrad Bishop    indent = kw.get('indent', 0)
22e2b65e90SBrad Bishop    depth = kw.setdefault('depth', 0)
23e2b65e90SBrad Bishop    tab = indent * depth * ' '
24e2b65e90SBrad Bishop    kw['depth'] += 1
25e2b65e90SBrad Bishop    newline = '\n' if indent else ' '
26e2b65e90SBrad Bishop    context = kw.get('context')
27e2b65e90SBrad Bishop
28e2b65e90SBrad Bishop    if(isinstance(obj, dict)):
29e2b65e90SBrad Bishop        nodes = []
30*d641c086SBrad Bishop        for k, v in obj.iteritems():
31e2b65e90SBrad Bishop            if(isinstance(v, dict)):
32e2b65e90SBrad Bishop                nodes.append((k, v))
33e2b65e90SBrad Bishop                continue
34*d641c086SBrad Bishop            if(isinstance(v, basestring) and v.lower() == 'true'):
35e2b65e90SBrad Bishop                fd.write('%s%s' % (tab, k))
36*d641c086SBrad Bishop            elif(isinstance(v, basestring) and v.lower() == 'false'):
37e2b65e90SBrad Bishop                continue
38e2b65e90SBrad Bishop            else:
39e2b65e90SBrad Bishop                fd.write('%s%s = ' % (tab, k))
40e2b65e90SBrad Bishop                dts_encode(v, fd, **kw)
41e2b65e90SBrad Bishop            fd.write(";%s" % newline)
42e2b65e90SBrad Bishop
43e2b65e90SBrad Bishop        for k, v in nodes:
44e2b65e90SBrad Bishop            fd.write('%s%s {%s' % (tab, k, newline))
45e2b65e90SBrad Bishop            dts_encode(v, fd, **kw)
46e2b65e90SBrad Bishop            fd.write('%s};%s' % (tab, newline))
47e2b65e90SBrad Bishop
48e2b65e90SBrad Bishop    if(isinstance(obj, int)):
49e2b65e90SBrad Bishop        if context == 'int_list':
50e2b65e90SBrad Bishop            fd.write("%d" % obj)
51e2b65e90SBrad Bishop        else:
52e2b65e90SBrad Bishop            fd.write("<%d>" % obj)
53e2b65e90SBrad Bishop
54*d641c086SBrad Bishop    if(isinstance(obj, basestring)):
55e2b65e90SBrad Bishop        fd.write("\"%s\"" % obj)
56e2b65e90SBrad Bishop
57e2b65e90SBrad Bishop    if(isinstance(obj, list)):
58e2b65e90SBrad Bishop        ctx = 'int_list' if all((type(x) is int) for x in iter(obj)) else ''
59e2b65e90SBrad Bishop        if ctx is 'int_list':
60e2b65e90SBrad Bishop            delim = ' '
61e2b65e90SBrad Bishop            closure = ('<', '>')
62e2b65e90SBrad Bishop        else:
63e2b65e90SBrad Bishop            delim = ','
64e2b65e90SBrad Bishop            closure = ('', '')
65e2b65e90SBrad Bishop
66e2b65e90SBrad Bishop        fd.write(closure[0])
67e2b65e90SBrad Bishop        if obj:
68e2b65e90SBrad Bishop            for v in obj[:-1]:
69e2b65e90SBrad Bishop                dts_encode(v, fd, context=ctx, **kw)
70e2b65e90SBrad Bishop                fd.write(delim)
71e2b65e90SBrad Bishop
72e2b65e90SBrad Bishop            dts_encode(obj[-1], fd, context=ctx)
73e2b65e90SBrad Bishop
74e2b65e90SBrad Bishop        fd.write(closure[1])
75