Martin Polednik has uploaded a new change for review.

Change subject: caps: specialize and modernize _parseKeyVal
......................................................................

caps: specialize and modernize _parseKeyVal

_parseKeyVal was used as a very generic function to parse lines
including delimiter (in a key=value sense). This is easily implemented
either project-wide or without using parse that generic. Since it is
now only used to parse release and version of rhev-h/node, the function
can be specialized and modernized.

Change-Id: I04b6f813c01fd8745510234ddb9bbb69f4bc67f4
Signed-off-by: Martin Polednik <[email protected]>
---
M tests/capsTests.py
M vdsm/caps.py
2 files changed, 13 insertions(+), 22 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/41/54541/1

diff --git a/tests/capsTests.py b/tests/capsTests.py
index 6249a57..35cd39b 100644
--- a/tests/capsTests.py
+++ b/tests/capsTests.py
@@ -112,14 +112,6 @@
                             'pc-0.10', 'pc-0.11', 'pc-0.14', 'pc-0.15']
         self.assertEqual(machines, expectedMachines)
 
-    def test_parseKeyVal(self):
-        lines = ["x=&2", "y& = 2", " z = 2 ", " s=3=&'5", " w=", "4&"]
-        expectedRes = [{'x': '&2', 'y&': '2', 'z': '2', 's': "3=&'5", 'w': ''},
-                       {'x=': '2', 'y': '= 2', 's=3=': "'5", '4': ''}]
-        sign = ["=", "&"]
-        for res, s in zip(expectedRes, sign):
-            self.assertEqual(res, caps._parseKeyVal(lines, s))
-
     @MonkeyPatch(numa, 'memory_by_cell', lambda x: {
         'total': '49141', 'free': '46783'})
     @MonkeyPatch(numa, '_get_libvirt_caps', lambda: _getTestData(
diff --git a/vdsm/caps.py b/vdsm/caps.py
index db7f532..3820d1e 100644
--- a/vdsm/caps.py
+++ b/vdsm/caps.py
@@ -164,17 +164,6 @@
     return True
 
 
-def _parseKeyVal(lines, delim='='):
-    d = {}
-    for line in lines:
-        kv = line.split(delim, 1)
-        if len(kv) != 2:
-            continue
-        k, v = map(str.strip, kv)
-        d[k] = v
-    return d
-
-
 def _getKdumpStatus():
     try:
         # check if kdump service is running
@@ -216,6 +205,18 @@
         return OSName.UNKNOWN
 
 
+def _parse_node_version(path):
+    data = {}
+    with open(path) as f:
+        for line in f:
+            try:
+                key, value = [kv.strip() for kv in line.split('=', 1)]
+            except ValueError:
+                continue
+
+    return data.get('VERSION', ''), data.get('RELEASE', '')
+
+
 @utils.memoized
 def osversion():
     version = release = ''
@@ -223,9 +224,7 @@
     osname = getos()
     try:
         if osname == OSName.RHEVH or osname == OSName.OVIRT:
-            d = _parseKeyVal(file('/etc/default/version'))
-            version = d.get('VERSION', '')
-            release = d.get('RELEASE', '')
+            version, release = _parse_node_version('/etc/default/version')
         elif osname == OSName.DEBIAN:
             version = linecache.getline('/etc/debian_version', 1).strip("\n")
             release = ""  # Debian just has a version entry


-- 
To view, visit https://gerrit.ovirt.org/54541
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I04b6f813c01fd8745510234ddb9bbb69f4bc67f4
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Martin Polednik <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to