All of lore.kernel.org
 help / color / mirror / Atom feed
From: OpenBMC Patches <openbmc-patches@stwcx.xyz>
To: openbmc@lists.ozlabs.org
Cc: Brad Bishop <bradleyb@us.ibm.com>
Subject: [PATCH pyphosphor 3/4] Added python to dts encoder
Date: Fri, 15 Apr 2016 07:50:46 -0500	[thread overview]
Message-ID: <1460724647-4184-4-git-send-email-openbmc-patches@stwcx.xyz> (raw)
In-Reply-To: <1460724647-4184-1-git-send-email-openbmc-patches@stwcx.xyz>

From: Brad Bishop <bradleyb@us.ibm.com>

This is a rudimentary python to device tree encoder.

It supports nested nodes and, cell, cell array,
string, string array, and mixed array properties.
At the moment there is no support for binary properties.
---
 obmc/utils/dtree.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 obmc/utils/dtree.py

diff --git a/obmc/utils/dtree.py b/obmc/utils/dtree.py
new file mode 100644
index 0000000..ce349ee
--- /dev/null
+++ b/obmc/utils/dtree.py
@@ -0,0 +1,63 @@
+# Contributors Listed Below - COPYRIGHT 2016
+# [+] International Business Machines Corp.
+#
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied. See the License for the specific language governing
+# permissions and limitations under the License.
+
+
+def dts_encode(obj, fd, **kw):
+    ''' A rudimentary python to dts encoder.
+    '''
+    indent = kw.get('indent', 0)
+    depth = kw.setdefault('depth', 0)
+    tab = indent * depth * ' '
+    kw['depth'] += 1
+    newline = '\n' if indent else ' '
+    context = kw.get('context')
+
+    if(isinstance(obj, dict)):
+        nodes = []
+        for k, v in obj.iteritems():
+            if(isinstance(v, dict)):
+                nodes.append((k, v))
+                continue
+            fd.write('%s%s = ' % (tab, k))
+            dts_encode(v, fd, **kw)
+            fd.write(";%s" % newline)
+
+        for k, v in nodes:
+            fd.write('%s%s {%s' % (tab, k, newline))
+            dts_encode(v, fd, **kw)
+            fd.write('%s};%s' % (tab, newline))
+
+    if(isinstance(obj, int)):
+        if context == 'int_list':
+            fd.write("%d" % obj)
+        else:
+            fd.write("<%d>" % obj)
+
+    if(isinstance(obj, basestring)):
+        fd.write("\"%s\"" % obj)
+
+    if(isinstance(obj, list)):
+        ctx = 'int_list' if all((type(x) is int) for x in iter(obj)) else ''
+        delim = ' ' if ctx is 'int_list' else ','
+        closure = ('<', '>') if ctx is 'int_list' else ('', '')
+
+        fd.write(closure[0])
+        for v in obj[:-1]:
+            dts_encode(v, fd, context=ctx, **kw)
+            fd.write(delim)
+
+        dts_encode(obj[-1], fd, context=ctx)
+        fd.write(closure[1])
-- 
2.7.1

  parent reply	other threads:[~2016-04-15 12:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-15 12:50 [PATCH pyphosphor 0/4] Pull OpenBMC Patches
2016-04-15 12:50 ` [PATCH pyphosphor 1/4] Introducing pyobmc OpenBMC Patches
2016-04-15 12:50 ` [PATCH pyphosphor 2/4] Add dictionary export to PathTree OpenBMC Patches
2016-04-15 12:50 ` OpenBMC Patches [this message]
2016-04-19  4:24   ` [PATCH pyphosphor 3/4] Added python to dts encoder Andrew Jeffery
2016-04-15 12:50 ` [PATCH pyphosphor 4/4] Move common code from REST server OpenBMC Patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1460724647-4184-4-git-send-email-openbmc-patches@stwcx.xyz \
    --to=openbmc-patches@stwcx.xyz \
    --cc=bradleyb@us.ibm.com \
    --cc=openbmc@lists.ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.