format: [(start_of_line,position in params)] example:

  data:
     cpu    90  45 24  5 345
     cpu0   14  11 83 34  33
     ^^^^
     start of line
     params  0   1  2  3   4

  get_field(data,3,"cpu0", " +") => 34
  get_field(data,4,"cpu", " +") => 345

Signed-off-by: Jiří Župka <jzu...@redhat.com>
---
 client/common_lib/utils.py |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/client/common_lib/utils.py b/client/common_lib/utils.py
index 8d6c1f7..c126e58 100644
--- a/client/common_lib/utils.py
+++ b/client/common_lib/utils.py
@@ -190,6 +190,30 @@ def read_file(filename):
         f.close()
 
 
+def get_field(data, param, linestart="", sep=" "):
+    """
+    Parse data from string.
+    @param data: Data to parse.
+        example:
+          data:
+             cpu   324 345 34  5 345
+             cpu0  34  11  34 34  33
+             ^^^^
+             start of line
+             params 0   1   2  3   4
+    @param param: Position of parameter after linestart marker.
+    @param linestart: String to which start line with parameters.
+    @param sep: Separator between parameters regular expression.
+    """
+    search = re.compile(r"(?<=^%s)\s*(.*)" % linestart, re.MULTILINE)
+    find = search.search(data)
+    if find != None:
+        return re.split("%s" % sep, find.group(1))[param]
+    else:
+        print "There is no line which starts with %s in data." % linestart
+        return None
+
+
 def write_one_line(filename, line):
     open_write_close(filename, line.rstrip('\n') + '\n')
 
-- 
1.7.2.3

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to