[virt-tools-list] [PATCH virt-manager 01/14] cli: refactor get_prop

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


Refactor get_prop since it will be used in the next patches at other
places as well.

Signed-off-by: Marc Hartmayer <mhartmay at linux.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy at linux.ibm.com>
---
 virtinst/cli.py    |  9 ++-------
 virtinst/xmlapi.py | 16 ++++++++++++++++
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/virtinst/cli.py b/virtinst/cli.py
index 06c6d33e6089..d50a0391ae73 100644
--- a/virtinst/cli.py
+++ b/virtinst/cli.py
@@ -22,7 +22,7 @@ import libvirt
 
 from virtcli import CLIConfig
 
-from . import util
+from . import util, xmlapi
 from .devices import (Device, DeviceController, DeviceDisk, DeviceGraphics,
         DeviceInterface, DevicePanic)
 from .domain import DomainClock, DomainOs
@@ -1223,12 +1223,7 @@ class VirtCLIParser(metaclass=InitClass):
         """
         if not cls.propname:
             return None
-        parent = obj
-        pieces = cls.propname.split(".")
-        for piece in pieces[:-1]:
-            parent = getattr(parent, piece)
-
-        return getattr(parent, pieces[-1])
+        return xmlapi.get_prop(obj, cls.propname)
 
     @classmethod
     def prop_is_list(cls, obj):
diff --git a/virtinst/xmlapi.py b/virtinst/xmlapi.py
index 00b551468d03..569e47f7498d 100644
--- a/virtinst/xmlapi.py
+++ b/virtinst/xmlapi.py
@@ -11,6 +11,22 @@ from . import util
 # pylint: disable=protected-access
 
 
+def get_prop(obj, prop_path):
+    """Return value of attribute identified by `prop_path`
+
+    Look up the attribute of `obj` identified by `prop_path`
+    (separated by "."). 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 getattr(parent, pieces[-1])
+
+
 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