From: Michael Goldish Subject: [KVM-AUTOTEST PATCH 1/4] KVM test: introduce Params, a dict-like object containing test parameters Date: Tue, 28 Dec 2010 01:00:45 +0200 Message-ID: <1293490848-28376-1-git-send-email-mgoldish@redhat.com> Cc: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:15871 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751527Ab0L0XA1 (ORCPT ); Mon, 27 Dec 2010 18:00:27 -0500 Sender: kvm-owner@vger.kernel.org List-ID: This object should replace get_sub_dict_names() and get_sub_dict(). Signed-off-by: Michael Goldish --- client/tests/kvm/kvm_utils.py | 38 +++++++++++++++++++++++++++++++++++++- 1 files changed, 37 insertions(+), 1 deletions(-) diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py index 57c9951..1e32e30 100644 --- a/client/tests/kvm/kvm_utils.py +++ b/client/tests/kvm/kvm_utils.py @@ -5,7 +5,7 @@ KVM test utility functions. """ import time, string, random, socket, os, signal, re, logging, commands, cPickle -import fcntl, shelve, ConfigParser, rss_file_transfer, threading, sys +import fcntl, shelve, ConfigParser, rss_file_transfer, threading, sys, UserDict from autotest_lib.client.bin import utils, os_dep from autotest_lib.client.common_lib import error, logging_config import kvm_subprocess @@ -97,6 +97,42 @@ def get_sub_dict_names(dict, keyword): return [] +class Params(UserDict.IterableUserDict): + """ + A dict-like object passed to every test. + """ + def objects(self, key): + """ + Return the names of objects defined using a given key. + + @param key: The name of the key whose value lists the objects + (e.g. 'nics'). + """ + return self.get(key, "").split() + + + def object_params(self, obj_name): + """ + Return a dict-like object containing the parameters of an individual + object. + + This method behaves as follows: the suffix '_' + obj_name is removed + from all key names that have it. Other key names are left unchanged. + The values of keys with the suffix overwrite the values of their + suffixless versions. + + @param obj_name: The name of the object (objects are listed by the + objects() method). + """ + suffix = "_" + obj_name + new_dict = self.copy() + for key in self: + if key.endswith(suffix): + new_key = key.split(suffix)[0] + new_dict[new_key] = self[key] + return new_dict + + # Functions related to MAC/IP addresses def _open_mac_pool(lock_mode): -- 1.7.3.4