util: Have set_xml_path use xml_parse_wrapper
This commit is contained in:
parent
3cf7679beb
commit
43d15d67ac
|
@ -156,46 +156,6 @@ def validate_macaddr(val):
|
|||
"AA:BB:CC:DD:EE:FF"))
|
||||
|
||||
|
||||
def xml_append(orig, new):
|
||||
"""
|
||||
Little function that helps generate consistent xml
|
||||
"""
|
||||
if not new:
|
||||
return orig
|
||||
if orig:
|
||||
orig += "\n"
|
||||
return orig + new
|
||||
|
||||
|
||||
def set_xml_path(xml, path, newval):
|
||||
"""
|
||||
Set the passed xml xpath to the new value
|
||||
"""
|
||||
doc = None
|
||||
ctx = None
|
||||
result = None
|
||||
|
||||
try:
|
||||
doc = libxml2.parseDoc(xml)
|
||||
ctx = doc.xpathNewContext()
|
||||
|
||||
ret = ctx.xpathEval(path)
|
||||
if ret is not None:
|
||||
if type(ret) == list:
|
||||
if len(ret) == 1:
|
||||
ret[0].setContent(newval)
|
||||
else:
|
||||
ret.setContent(newval)
|
||||
|
||||
result = doc.serialize()
|
||||
finally:
|
||||
if doc:
|
||||
doc.freeDoc()
|
||||
if ctx:
|
||||
ctx.xpathFreeContext()
|
||||
return result
|
||||
|
||||
|
||||
def generate_name(base, collision_cb, suffix="", lib_collision=True,
|
||||
start_num=0, sep="-", force_num=False, collidelist=None):
|
||||
"""
|
||||
|
@ -330,6 +290,34 @@ def xml_parse_wrapper(xml, parse_func, *args, **kwargs):
|
|||
return ret
|
||||
|
||||
|
||||
def set_xml_path(xml, path, newval):
|
||||
"""
|
||||
Set the passed xml xpath to the new value
|
||||
"""
|
||||
def cb(doc, ctx):
|
||||
ret = ctx.xpathEval(path)
|
||||
if ret is not None:
|
||||
if type(ret) == list:
|
||||
if len(ret) == 1:
|
||||
ret[0].setContent(newval)
|
||||
else:
|
||||
ret.setContent(newval)
|
||||
|
||||
return doc.serialize()
|
||||
return xml_parse_wrapper(xml, cb)
|
||||
|
||||
|
||||
def xml_append(orig, new):
|
||||
"""
|
||||
Little function that helps generate consistent xml
|
||||
"""
|
||||
if not new:
|
||||
return orig
|
||||
if orig:
|
||||
orig += "\n"
|
||||
return orig + new
|
||||
|
||||
|
||||
def generate_uuid(conn):
|
||||
for ignore in range(256):
|
||||
uuid = randomUUID(conn=conn)
|
||||
|
|
Loading…
Reference in New Issue