Skip to content

Commit

Permalink
Merge pull request sifive#31 from sifive/fix-include
Browse files Browse the repository at this point in the history
Fixes to includes and include directives
  • Loading branch information
nategraff-sifive authored Dec 2, 2019
2 parents f81dcce + aa53824 commit ee5f156
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
11 changes: 8 additions & 3 deletions pydevicetree/ast/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from typing import Any

from pydevicetree.ast.helpers import formatLevel
from pydevicetree.ast.helpers import formatLevel, wrapStrings

class Directive:
"""Represents a Devicetree directive
Expand Down Expand Up @@ -36,6 +36,11 @@ def __str__(self) -> str:

def to_dts(self, level: int = 0) -> str:
"""Format the Directive in Devicetree Source format"""
if self.option:
return formatLevel(level, "%s %s;\n" % (self.directive, self.option))
if isinstance(self.option, list):
return formatLevel(level, "%s %s;\n" % (self.directive,
wrapStrings(self.option)))
if isinstance(self.option, str):
if self.directive == "/include/":
return formatLevel(level, "%s \"%s\"\n" % (self.directive, self.option))
return formatLevel(level, "%s \"%s\";\n" % (self.directive, self.option))
return formatLevel(level, "%s;\n" % self.directive)
11 changes: 7 additions & 4 deletions pydevicetree/ast/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,9 @@ def __init__(self, elements: ElementList):
for node in self.children:
node.parent = self

reference_nodes = filter(lambda node: isinstance(node, NodeReference), self.all_nodes())
for refnode in cast(List[NodeReference], reference_nodes):
reference_nodes = self.filter(lambda n: isinstance(n, NodeReference))
for refnode in reference_nodes:
refnode = cast(NodeReference, refnode)
node = refnode.resolve_reference(self)

if refnode.parent:
Expand Down Expand Up @@ -460,8 +461,10 @@ def parseFile(filename: str, followIncludes: bool = False) -> 'Devicetree':
from pydevicetree.source import parseTree
with open(filename, 'r') as f:
contents = f.read()
pwd = os.path.dirname(filename) + "/"
return parseTree(contents, pwd, followIncludes)
dirname = os.path.dirname(filename)
if dirname != "":
dirname += "/"
return parseTree(contents, dirname, followIncludes)

def all_nodes(self) -> Iterable[Node]:
"""Get an iterable over all nodes in the tree"""
Expand Down
8 changes: 5 additions & 3 deletions pydevicetree/source/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
path_reference = p.Literal("&{").suppress() + node_path + p.Literal("}").suppress()
label_reference = p.Literal("&").suppress() + label
reference = path_reference ^ label_reference
directive = p.QuotedString(quoteChar="/", unquoteResults=False) + \
p.Optional(string ^ property_name ^ node_name ^ reference ^ \
(integer * 2)) + p.Literal(";").suppress()
include_directive = p.Literal("/include/") + p.QuotedString(quoteChar='"')
generic_directive = p.QuotedString(quoteChar="/", unquoteResults=False) + \
p.Optional(string ^ property_name ^ node_name ^ reference ^ (integer * 2)) + \
p.Literal(";").suppress()
directive = include_directive ^ generic_directive

operator = p.oneOf("~ ! * / + - << >> < <= > >= == != & ^ | && ||")
arith_expr = p.Forward()
Expand Down
2 changes: 2 additions & 0 deletions pydevicetree/source/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ def recurseIncludeFiles(elements, pwd):

elements += parseElements(contents)

del elements[elements.asList().index(e)]

def parseElements(dts, pwd="", followIncludes=False):
"""Parses a string into a list of elements"""
elements = grammar.devicetree.parseString(dts, parseAll=True)
Expand Down
2 changes: 1 addition & 1 deletion tests/devicetrees/include/base.dts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
/dts-v1/;

/include/ "overlay.dtsi";
/include/ "overlay.dtsi"

/ {
#address-cells = <1>;
Expand Down
4 changes: 4 additions & 0 deletions tests/test_devicetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def test_delete_directive(self):
self.assertEqual(type(delete_property), Node)
self.assertEqual(delete_property.get_field("delete-me"), None)

def test_include_directive(self):
tree = Devicetree.from_dts("/include/ \"foo.dtsi\"")
self.assertEqual(tree.directives[0].to_dts(), "/include/ \"foo.dtsi\"\n")

def test_get_by_label(self):
cpu0 = self.tree.get_by_label("cpu0")
self.assertEqual(cpu0.name, "cpu")
Expand Down

0 comments on commit ee5f156

Please sign in to comment.