From: Ralf Ramsauer <[email protected]>

Move the next part to the new class: the whole file parser.

For the moment, this leaves an ugly one-liner in parse_iomem_file, but let's
keep it for the moment -- we'll clean that up later.

Signed-off-by: Ralf Ramsauer <[email protected]>
---
 pyjailhouse/sysfs_parser.py | 50 ++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 23 deletions(-)

diff --git a/pyjailhouse/sysfs_parser.py b/pyjailhouse/sysfs_parser.py
index a179461d..b72be367 100644
--- a/pyjailhouse/sysfs_parser.py
+++ b/pyjailhouse/sysfs_parser.py
@@ -834,6 +834,32 @@ class IORegionTree:
 
         return level, TargetClass(int(region[0], 16), int(region[1], 16), type)
 
+    @staticmethod
+    def parse_io_file(filename, TargetClass):
+        root = IOMemRegionTree(None, 0)
+        f = input_open(filename)
+        lastlevel = 0
+        lastnode = root
+        for line in f:
+            level, r = IORegionTree.parse_io_line(line, TargetClass)
+            t = IOMemRegionTree(r, level)
+            if t.level > lastlevel:
+                t.parent = lastnode
+            if t.level == lastlevel:
+                t.parent = lastnode.parent
+            if t.level < lastlevel:
+                p = lastnode.parent
+                while t.level < p.level:
+                    p = p.parent
+                t.parent = p.parent
+
+            t.parent.children.append(t)
+            lastnode = t
+            lastlevel = t.level
+        f.close()
+
+        return root
+
 
 class IOMemRegionTree:
     def __init__(self, region, level):
@@ -877,29 +903,7 @@ class IOMemRegionTree:
 
     @staticmethod
     def parse_iomem_file():
-        root = IOMemRegionTree(None, 0)
-        f = input_open('/proc/iomem')
-        lastlevel = 0
-        lastnode = root
-        for line in f:
-            level, r = IORegionTree.parse_io_line(line, MemRegion)
-            t = IOMemRegionTree(r, level)
-            if (t.level > lastlevel):
-                t.parent = lastnode
-            if (t.level == lastlevel):
-                t.parent = lastnode.parent
-            if (t.level < lastlevel):
-                p = lastnode.parent
-                while(t.level < p.level):
-                    p = p.parent
-                t.parent = p.parent
-
-            t.parent.children.append(t)
-            lastnode = t
-            lastlevel = t.level
-        f.close()
-
-        return root
+        return IORegionTree.parse_io_file('/proc/iomem', MemRegion)
 
     # find specific regions in tree
     @staticmethod
-- 
2.23.0

-- 
You received this message because you are subscribed to the Google Groups 
"Jailhouse" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jailhouse-dev/20190930191323.32266-8-andrej.utz%40st.oth-regensburg.de.

Reply via email to