[virt-tools-list] [PATCH virt-manager 02/14] xmlapi: add set_prop

Marc Hartmayer mhartmay at linux.ibm.com
Tue Feb 26 09:56:32 UTC 2019


Introduce set_prop helper function. It will be used in the next patch.

Signed-off-by: Marc Hartmayer <mhartmay at linux.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy at linux.ibm.com>
---
 virtinst/xmlapi.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/virtinst/xmlapi.py b/virtinst/xmlapi.py
index 569e47f7498d..49ca0fa395cb 100644
--- a/virtinst/xmlapi.py
+++ b/virtinst/xmlapi.py
@@ -27,6 +27,22 @@ def get_prop(obj, prop_path):
     return getattr(parent, pieces[-1])
 
 
+def set_prop(obj, prop_path, value):
+    """Set value of attribute identified by `prop_path`
+
+    Set the attribute of `obj` identified by `prop_path` (separated by
+    ".") to `value`. If any component along the path is missing an
+    `AttributeError` is raised.
+
+    """
+    parent = obj
+    pieces = prop_path.split(".")
+    for piece in pieces[:-1]:
+        parent = getattr(parent, piece)
+
+    return setattr(parent, pieces[-1], value)
+
+
 class _XPathSegment(object):
     """
     Class representing a single 'segment' of an xpath string. For example,
-- 
2.17.0




More information about the virt-tools-list mailing list