From: =?UTF-8?q?Ji=C5=99=C3=AD=20=C5=BDupka?= Subject: [Autotest][PATCH 2/4] Parse value from string. Date: Wed, 13 Oct 2010 16:13:17 +0200 Message-ID: <1286979199-13109-3-git-send-email-jzupka@redhat.com> References: <1286979199-13109-1-git-send-email-jzupka@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: jzupka@redhat.com, akong@redhat.com, ldoktor@redhat.com To: kvm-autotest@redhat.com, kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:50132 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751936Ab0JMOMm (ORCPT ); Wed, 13 Oct 2010 10:12:42 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o9DECgiV008662 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 13 Oct 2010 10:12:42 -0400 In-Reply-To: <1286979199-13109-1-git-send-email-jzupka@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: 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", " +") =3D> 34 get_field(data,4,"cpu", " +") =3D> 345 Signed-off-by: Ji=C5=99=C3=AD =C5=BDupka --- 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() =20 =20 +def get_field(data, param, linestart=3D"", sep=3D" "): + """ + 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 =3D re.compile(r"(?<=3D^%s)\s*(.*)" % linestart, re.MULTILI= NE) + find =3D search.search(data) + if find !=3D None: + return re.split("%s" % sep, find.group(1))[param] + else: + print "There is no line which starts with %s in data." % lines= tart + return None + + def write_one_line(filename, line): open_write_close(filename, line.rstrip('\n') + '\n') =20 --=20 1.7.2.3