2013-03-18 05:06:52 +08:00
|
|
|
#
|
2013-10-28 04:59:47 +08:00
|
|
|
# Copyright 2006, 2013 Red Hat, Inc.
|
2013-03-18 05:06:52 +08:00
|
|
|
#
|
2018-04-04 21:35:41 +08:00
|
|
|
# This work is licensed under the GNU GPLv2 or later.
|
2018-03-21 03:00:02 +08:00
|
|
|
# See the COPYING file in the top-level directory.
|
2013-04-11 22:27:02 +08:00
|
|
|
#
|
2013-03-18 05:06:52 +08:00
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-04-11 22:27:02 +08:00
|
|
|
def listify(l):
|
|
|
|
if l is None:
|
|
|
|
return []
|
2017-10-11 19:35:41 +08:00
|
|
|
elif not isinstance(l, list):
|
2013-04-14 02:34:52 +08:00
|
|
|
return [l]
|
2013-04-11 22:27:02 +08:00
|
|
|
else:
|
|
|
|
return l
|
|
|
|
|
2013-04-14 02:34:52 +08:00
|
|
|
|
2013-04-12 20:26:21 +08:00
|
|
|
def xml_escape(xml):
|
|
|
|
"""
|
|
|
|
Replaces chars ' " < > & with xml safe counterparts
|
|
|
|
"""
|
2019-06-10 04:39:15 +08:00
|
|
|
if xml:
|
|
|
|
xml = xml.replace("&", "&")
|
|
|
|
xml = xml.replace("'", "'")
|
|
|
|
xml = xml.replace("\"", """)
|
|
|
|
xml = xml.replace("<", "<")
|
|
|
|
xml = xml.replace(">", ">")
|
2013-04-12 20:26:21 +08:00
|
|
|
return xml
|
2013-03-18 05:06:52 +08:00
|
|
|
|
|
|
|
|
2019-05-11 00:54:30 +08:00
|
|
|
def get_prop_path(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])
|
|
|
|
|
|
|
|
|
|
|
|
def set_prop_path(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)
|
2019-06-10 06:16:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def raise_programming_error(cond, msg):
|
|
|
|
"""
|
|
|
|
Small helper to raise a consistent error message for coding issues
|
|
|
|
"""
|
|
|
|
if cond:
|
|
|
|
raise RuntimeError("programming error: %s" % msg)
|