-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from sifive/split-up-ast-module
Split up the pydevicetree.ast module for maintainability
- Loading branch information
Showing
8 changed files
with
160 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) 2019 SiFive Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from typing import List, Any | ||
|
||
from pydevicetree.ast.helpers import formatLevel | ||
|
||
class Directive: | ||
def __init__(self, directive: str, options: List[Any] = None): | ||
self.directive = directive | ||
self.options = options | ||
|
||
def __repr__(self) -> str: | ||
return "<Directive %s>" % self.directive | ||
|
||
def __str__(self) -> str: | ||
return self.to_dts() | ||
|
||
def to_dts(self, level: int = 0) -> str: | ||
if self.options: | ||
return formatLevel(level, "%s %s;\n" % (self.directive, self.options)) | ||
return formatLevel(level, "%s;\n" % self.directive) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) 2019 SiFive Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from typing import List, Any | ||
|
||
def formatLevel(level: int, s: str) -> str: | ||
return "\t" * level + s | ||
|
||
def wrapStrings(values: List[Any], formatHex: bool = False) -> List[Any]: | ||
wrapped = [] | ||
for v in values: | ||
if isinstance(v, str): | ||
if v[0] != '&': | ||
wrapped.append("\"%s\"" % v) | ||
else: | ||
wrapped.append(v) | ||
elif isinstance(v, int): | ||
if formatHex: | ||
wrapped.append("0x%x" % v) | ||
else: | ||
wrapped.append(str(v)) | ||
else: | ||
wrapped.append(str(v)) | ||
return wrapped |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) 2019 SiFive Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from typing import List, Any | ||
|
||
from pydevicetree.ast.helpers import wrapStrings, formatLevel | ||
|
||
class PropertyValues: | ||
def __init__(self, values: List[Any] = None): | ||
self.values = values | ||
|
||
def __repr__(self) -> str: | ||
return "<PropertyValues " + self.values.__repr__() + ">" | ||
|
||
def __str__(self) -> str: | ||
return self.to_dts() | ||
|
||
def __iter__(self): | ||
return iter(self.values) | ||
|
||
def __len__(self) -> int: | ||
return len(self.values) | ||
|
||
def to_dts(self, formatHex: bool = False) -> str: | ||
return " ".join(wrapStrings(self.values, formatHex)) | ||
|
||
def __getitem__(self, key) -> Any: | ||
return self.values[key] | ||
|
||
def __eq__(self, other) -> bool: | ||
if isinstance(other, PropertyValues): | ||
return self.values == other.values | ||
return self.values == other | ||
|
||
class Bytestring(PropertyValues): | ||
def __init__(self, bytelist: List[int] = None): | ||
PropertyValues.__init__(self, bytearray(bytelist)) | ||
|
||
def __repr__(self) -> str: | ||
return "<Bytestring " + str(self.values) + ">" | ||
|
||
def to_dts(self, formatHex: bool = False) -> str: | ||
return "[" + " ".join("%02x" % v for v in self.values) + "]" | ||
|
||
class CellArray(PropertyValues): | ||
def __init__(self, cells: List[Any] = None): | ||
PropertyValues.__init__(self, cells) | ||
|
||
def __repr__(self) -> str: | ||
return "<CellArray " + self.values.__repr__() + ">" | ||
|
||
def to_dts(self, formatHex: bool = False) -> str: | ||
return "<" + " ".join(wrapStrings(self.values, formatHex)) + ">" | ||
|
||
class StringList(PropertyValues): | ||
def __init__(self, strings: List[str] = None): | ||
PropertyValues.__init__(self, strings) | ||
|
||
def __repr__(self) -> str: | ||
return "<StringList " + self.values.__repr__() + ">" | ||
|
||
def to_dts(self, formatHex: bool = False) -> str: | ||
return ", ".join(wrapStrings(self.values)) | ||
|
||
class Property: | ||
def __init__(self, name: str, values: PropertyValues): | ||
self.name = name | ||
self.values = values | ||
|
||
def __repr__(self) -> str: | ||
return "<Property %s>" % self.name | ||
|
||
def __str__(self) -> str: | ||
return self.to_dts() | ||
|
||
def to_dts(self, level: int = 0) -> str: | ||
if self.name in ["reg", "ranges"]: | ||
value = self.values.to_dts(formatHex=True) | ||
else: | ||
value = self.values.to_dts(formatHex=False) | ||
|
||
if value != "": | ||
return formatLevel(level, "%s = %s;\n" % (self.name, value)) | ||
return formatLevel(level, "%s;\n" % self.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters