From: Ralf Ramsauer <ralf.ramsa...@oth-regensburg.de>

Do this step by step. First of all, let's create a new routine that is able to
parse a line from /proc/iomem or /proc/ioports. Both files share the same
layout, so we can use a common parser.

Passing the destination type of the entry to the parser allows to share code.

Signed-off-by: Ralf Ramsauer <ralf.ramsa...@oth-regensburg.de>
---
 pyjailhouse/sysfs_parser.py | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/pyjailhouse/sysfs_parser.py b/pyjailhouse/sysfs_parser.py
index b0a9bf44..a179461d 100644
--- a/pyjailhouse/sysfs_parser.py
+++ b/pyjailhouse/sysfs_parser.py
@@ -824,6 +824,17 @@ class IOAPIC:
         return (self.iommu << 16) | self.bdf
 
 
+class IORegionTree:
+    @staticmethod
+    def parse_io_line(line, TargetClass):
+        region, type = line.split(' : ', 1)
+        level = int(region.count(' ') / 2) + 1
+        type = type.strip()
+        region = [r.strip() for r in region.split('-', 1)]
+
+        return level, TargetClass(int(region[0], 16), int(region[1], 16), type)
+
+
 class IOMemRegionTree:
     def __init__(self, region, level):
         self.region = region
@@ -864,14 +875,6 @@ class IOMemRegionTree:
 
         return [before_kernel, kernel_region, after_kernel]
 
-    @staticmethod
-    def parse_iomem_line(line):
-        a = line.split(':', 1)
-        level = int(a[0].count(' ') / 2) + 1
-        region = a[0].split('-', 1)
-        a[1] = a[1].strip()
-        return level, MemRegion(int(region[0], 16), int(region[1], 16), a[1])
-
     @staticmethod
     def parse_iomem_file():
         root = IOMemRegionTree(None, 0)
@@ -879,7 +882,7 @@ class IOMemRegionTree:
         lastlevel = 0
         lastnode = root
         for line in f:
-            (level, r) = IOMemRegionTree.parse_iomem_line(line)
+            level, r = IORegionTree.parse_io_line(line, MemRegion)
             t = IOMemRegionTree(r, level)
             if (t.level > lastlevel):
                 t.parent = lastnode
-- 
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 jailhouse-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jailhouse-dev/20190930191323.32266-7-andrej.utz%40st.oth-regensburg.de.

Reply via email to