cli: refactor get_prop

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

Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
This commit is contained in:
Marc Hartmayer 2019-02-26 10:56:31 +01:00 committed by Cole Robinson
parent 1856c1fa65
commit adf30349c3
2 changed files with 18 additions and 7 deletions

View File

@ -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
@ -1225,12 +1225,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):

View File

@ -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,