From patchwork Wed Oct 13 14:13:17 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Zupka X-Patchwork-Id: 250711 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id o9DECrwG002623 for ; Wed, 13 Oct 2010 14:12:53 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752304Ab0JMOMo (ORCPT ); Wed, 13 Oct 2010 10:12:44 -0400 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 Received: from jzupka.local.com (dhcp-27-161.brq.redhat.com [10.34.27.161]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o9DECb2H009027; Wed, 13 Oct 2010 10:12:41 -0400 From: =?UTF-8?q?Ji=C5=99=C3=AD=20=C5=BDupka?= To: kvm-autotest@redhat.com, kvm@vger.kernel.org Cc: jzupka@redhat.com, akong@redhat.com, ldoktor@redhat.com 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> In-Reply-To: <1286979199-13109-1-git-send-email-jzupka@redhat.com> References: <1286979199-13109-1-git-send-email-jzupka@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Wed, 13 Oct 2010 14:12:53 +0000 (UTC) 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')