xmlapi: add set_prop

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

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:32 +01:00 committed by Cole Robinson
parent adf30349c3
commit 60c7e778e3

View File

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