]> git.karo-electronics.de Git - karo-tx-uboot.git/commitdiff
dtoc: Move a few more common functions into fdt.py
authorSimon Glass <sjg@chromium.org>
Tue, 26 Jul 2016 00:59:07 +0000 (18:59 -0600)
committerSimon Glass <sjg@chromium.org>
Mon, 19 Sep 2016 03:04:38 +0000 (21:04 -0600)
Some functions have the same code in the subclasses. Move these into the
superclass to avoid duplication.

Signed-off-by: Simon Glass <sjg@chromium.org>
tools/dtoc/fdt.py
tools/dtoc/fdt_fallback.py
tools/dtoc/fdt_normal.py

index 964ef7cbb461dc7f1e6ef51e6226a7e97d129221..c0ce5af8ac50ee74bd758c17779747611fd16e48 100644 (file)
@@ -164,6 +164,26 @@ class NodeBase:
         self.subnodes = []
         self.props = {}
 
+    def _FindNode(self, name):
+        """Find a node given its name
+
+        Args:
+            name: Node name to look for
+        Returns:
+            Node object if found, else None
+        """
+        for subnode in self.subnodes:
+            if subnode.name == name:
+                return subnode
+        return None
+
+    def Scan(self):
+        """Scan the subnodes of a node
+
+        This should be implemented by subclasses
+        """
+        raise NotImplementedError()
+
 class Fdt:
     """Provides simple access to a flat device tree blob.
 
@@ -173,3 +193,40 @@ class Fdt:
     """
     def __init__(self, fname):
         self._fname = fname
+
+    def Scan(self, root='/'):
+        """Scan a device tree, building up a tree of Node objects
+
+        This fills in the self._root property
+
+        Args:
+            root: Ignored
+
+        TODO(sjg@chromium.org): Implement the 'root' parameter
+        """
+        self._root = self.Node(self, 0, '/', '/')
+        self._root.Scan()
+
+    def GetRoot(self):
+        """Get the root Node of the device tree
+
+        Returns:
+            The root Node object
+        """
+        return self._root
+
+    def GetNode(self, path):
+        """Look up a node from its path
+
+        Args:
+            path: Path to look up, e.g. '/microcode/update@0'
+        Returns:
+            Node object, or None if not found
+        """
+        node = self._root
+        for part in path.split('/')[1:]:
+            node = node._FindNode(part)
+            if not node:
+                return None
+        return node
+
index 84a3db1e78fbf3ca04724a7531c0afb655a79360..5b0f2a181b7cfd786888b4fa77325602dcf29759 100644 (file)
@@ -81,22 +81,6 @@ class FdtFallback(Fdt):
     def __init__(self, fname):
         Fdt.__init__(self, fname)
 
-    def Scan(self):
-        """Scan a device tree, building up a tree of Node objects
-
-        This fills in the self._root property
-        """
-        self._root = Node(self, 0, '/', '/')
-        self._root.Scan()
-
-    def GetRoot(self):
-        """Get the root Node of the device tree
-
-        Returns:
-            The root Node object
-        """
-        return self._root
-
     def GetSubNodes(self, node):
         """Returns a list of sub-nodes of a given node
 
index 6f019c1f128785d43d8a66230f0a8bcf6c73e8a6..861f60c7d04815c526150edaf7b8dc053d528538 100644 (file)
@@ -94,22 +94,6 @@ class FdtNormal(Fdt):
         """
         return self._fdt
 
-    def Scan(self):
-        """Scan a device tree, building up a tree of Node objects
-
-        This fills in the self._root property
-        """
-        self._root = Node(self, 0, '/', '/')
-        self._root.Scan()
-
-    def GetRoot(self):
-        """Get the root Node of the device tree
-
-        Returns:
-            The root Node object
-        """
-        return self._root
-
     def GetProps(self, node):
         """Get all properties from a node.