[node-patches] Change in ovirt-node[master]: add parse_varfile into __init__.py of ovirt.node.utils

hadong0720 at gmail.com hadong0720 at gmail.com
Wed Mar 5 04:44:44 UTC 2014


hadong has uploaded a new change for review.

Change subject: add parse_varfile into __init__.py of ovirt.node.utils
......................................................................

add parse_varfile into __init__.py of ovirt.node.utils

Change-Id: Idead1cfd1c52c94c4cae7dbd1502a9b3d59ed3b1
Signed-off-by: hadong <hadong0720 at gmail.com>
---
M src/ovirt/node/utils/__init__.py
1 file changed, 27 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/66/25366/1

diff --git a/src/ovirt/node/utils/__init__.py b/src/ovirt/node/utils/__init__.py
index 08c8dc7..349dc27 100644
--- a/src/ovirt/node/utils/__init__.py
+++ b/src/ovirt/node/utils/__init__.py
@@ -382,3 +382,30 @@
     """
     return hasattr(filec, "read") \
         and hasattr(filec, "write")
+
+
+def parse_varfile(txt):
+    """Parse a simple shell-var-style lines into a dict:
+
+    >>> import StringIO
+    >>> txt = "# A comment\\n"
+    >>> txt += "A=ah\\n"
+    >>> txt += "B=beh\\n"
+    >>> txt += "C=\\"ceh\\"\\n"
+    >>> txt += "D=\\"more=less\\"\\n"
+    >>> sorted(parse_varfile(txt).items())
+    [('A', 'ah'), ('B', 'beh'), ('C', 'ceh'), ('D', 'more=less')]
+    """
+    cfg = {}
+    for line in txt.split("\n"):
+        line = line.strip()
+        if line == "" or line.startswith("#"):
+            continue
+        try:
+            key, value = line.split("=", 1)
+            cfg[key] = value.strip("\"' \n")
+        except:
+            pass
+            # BAAAAD
+            #raise RuntimeError("Failed to parse line: %s" % line)
+    return cfg


-- 
To view, visit http://gerrit.ovirt.org/25366
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idead1cfd1c52c94c4cae7dbd1502a9b3d59ed3b1
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: hadong <hadong0720 at gmail.com>



More information about the node-patches mailing list