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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

Signed-off-by: Marc Hartmayer <mhartmay@xxxxxxxxxxxxx>
Reviewed-by: Boris Fiuczynski <fiuczy@xxxxxxxxxxxxx>
---
 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

_______________________________________________
virt-tools-list mailing list
virt-tools-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/virt-tools-list



[Index of Archives]     [Linux Virtualization]     [KVM Development]     [CentOS Virtualization]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite Forum]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]     [Video 4 Linux]

  Powered by Linux