diff mbox

[KVM-AUTOTEST,4/6] KVM test: allow for regex matching of keys in the config parser

Message ID dadb541c36d9c646003f805a47703f7dffa0e385.1249835043.git.mgoldish@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Michael Goldish Aug. 9, 2009, 4:33 p.m. UTC
Allow for statements with the following syntax:
regex ?= value         (set)
regex ?+= value        (append)
regex ?<= value        (prepend)

These operations are performed only for keys that match regex.
The whole key name must match the expression, so if regex is a regular string,
only the key whose name equals this string is modified.

This is useful for modifying all parameters of a certain type regardless of the
objects they apply to.  For example, the following parameters specify the cdrom
filenames for different objects: cdrom_vm1, cdrom_vm2, cdrom.  It is now
possible to modify all of them with a statement such
cdrom.* ?<= shared_
which would prepend the string 'shared_' to all cdrom filenames.

Signed-off-by: Michael Goldish <mgoldish@redhat.com>
---
 client/tests/kvm/kvm_config.py |   34 +++++++++++++++++++++++-----------
 1 files changed, 23 insertions(+), 11 deletions(-)
diff mbox

Patch

diff --git a/client/tests/kvm/kvm_config.py b/client/tests/kvm/kvm_config.py
index 7ff7a07..b7bddbd 100755
--- a/client/tests/kvm/kvm_config.py
+++ b/client/tests/kvm/kvm_config.py
@@ -316,20 +316,32 @@  class config:
                 for filter in filters:
                     filtered_list = self.filter(filter, filtered_list)
                 # Apply the operation to the filtered list
-                for dict in filtered_list:
-                    if op_found == "=":
+                if op_found == "=":
+                    for dict in filtered_list:
                         dict[key] = value
-                    elif op_found == "+=":
+                elif op_found == "+=":
+                    for dict in filtered_list:
                         dict[key] = dict.get(key, "") + value
-                    elif op_found == "<=":
+                elif op_found == "<=":
+                    for dict in filtered_list:
                         dict[key] = value + dict.get(key, "")
-                    elif op_found.startswith("?") and dict.has_key(key):
-                        if op_found == "?=":
-                            dict[key] = value
-                        elif op_found == "?+=":
-                            dict[key] = dict.get(key, "") + value
-                        elif op_found == "?<=":
-                            dict[key] = value + dict.get(key, "")
+                elif op_found.startswith("?"):
+                    exp = re.compile("^(%s)$" % key)
+                    if op_found == "?=":
+                        for dict in filtered_list:
+                            for key in dict.keys():
+                                if exp.match(key):
+                                    dict[key] = value
+                    elif op_found == "?+=":
+                        for dict in filtered_list:
+                            for key in dict.keys():
+                                if exp.match(key):
+                                    dict[key] = dict.get(key, "") + value
+                    elif op_found == "?<=":
+                        for dict in filtered_list:
+                            for key in dict.keys():
+                                if exp.match(key):
+                                    dict[key] = value + dict.get(key, "")
 
             # Parse 'no' and 'only' statements
             elif words[0] == "no" or words[0] == "only":