Skip to content

Commit

Permalink
Merge pull request #14 from sifive/create-node-from-string
Browse files Browse the repository at this point in the history
Create node from string
  • Loading branch information
nategraff-sifive authored Nov 8, 2019
2 parents 062d733 + bde2f32 commit 3bca450
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
16 changes: 15 additions & 1 deletion pydevicetree/ast/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ def __eq__(self, other) -> bool:
def __hash__(self):
return hash((self.name, self.address))

@staticmethod
def from_dts(source: str) -> 'Node':
"""Create a node from Devicetree Source"""
# pylint: disable=import-outside-toplevel,cyclic-import
from pydevicetree.source import parseNode
return parseNode(source)

def add_child(self, node: 'Node', merge: bool = True):
"""Add a child node and merge it into the tree"""
node.parent = self
self.children.append(node)
if merge:
self.merge_tree()

def to_dts(self, level: int = 0) -> str:
"""Format the subtree starting at the node as Devicetree Source"""
out = ""
Expand Down Expand Up @@ -195,7 +209,7 @@ def __get_child_by_handle(self, handle: str) -> Optional['Node']:
"""Get a child node by name or name and unit address"""
if '@' in handle:
name, addr_s = handle.split('@')
address = int(addr_s)
address = int(addr_s, base=16)
nodes = list(filter(lambda n: n.name == name and n.address == address, self.children))
else:
name = handle
Expand Down
2 changes: 1 addition & 1 deletion pydevicetree/source/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Copyright (c) 2019 SiFive Inc.
# SPDX-License-Identifier: Apache-2.0

from pydevicetree.source.parser import parseTree
from pydevicetree.source.parser import parseTree, parseNode
4 changes: 4 additions & 0 deletions pydevicetree/source/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def parseTree(dts, pwd="", followIncludes=False):
"""Parses a string into a full Devicetree"""
return Devicetree(parseElements(dts, pwd, followIncludes))

def parseNode(dts):
"""Parses a string into a Devictreee Node"""
return grammar.node_definition.parseString(dts)[0]

if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
Expand Down
25 changes: 25 additions & 0 deletions tests/test_devicetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,30 @@ def func(values):
self.assertEqual(cpu.get_path(), "/cpus/cpu@0")
self.assertEqual(cpu.get_field("reg"), 0)

def test_node_from_dts(self):
node = Node.from_dts("uart0: uart@10013000 { compatible = \"sifive,uart0\"; };")

self.assertEqual(type(node), Node)
self.assertEqual(node.label, "uart0")
self.assertEqual(node.name, "uart")
self.assertEqual(node.address, 0x10013000)
self.assertEqual(node.get_field("compatible"), "sifive,uart0")

def test_add_child(self):
tree = parseTree(self.source)

new_node = Node.from_dts("uart0: uart@10013000 { compatible = \"sifive,uart0\"; };")

soc = tree.get_by_path("/soc")
soc.add_child(new_node)

uart = tree.get_by_path("/soc/uart@10013000")
self.assertEqual(type(uart), Node)
self.assertEqual(uart.label, "uart0")
self.assertEqual(uart.name, "uart")
self.assertEqual(uart.address, 0x10013000)
self.assertEqual(uart.get_field("compatible"), "sifive,uart0")


if __name__ == "__main__":
unittest.main()

0 comments on commit 3bca450

Please sign in to comment.