MCPcopy
hub / github.com/retspen/webvirtcloud / get_xml_path

Function get_xml_path

vrtManager/util.py:78–110  ·  view source on GitHub ↗

Return the content from the passed xml xpath, or return the result of a passed function (receives xpathContext as its only arg)

(xml, path=None, func=None)

Source from the content-addressed store, hash-verified

76
77
78def get_xml_path(xml, path=None, func=None):
79 """
80 Return the content from the passed xml xpath, or return the result
81 of a passed function (receives xpathContext as its only arg)
82 """
83 doc = None
84 ctx = None
85 result = None
86
87 try:
88 doc = libxml2.parseDoc(xml)
89 ctx = doc.xpathNewContext()
90
91 if path:
92 ret = ctx.xpathEval(path)
93 if ret is not None:
94 if type(ret) == list:
95 if len(ret) >= 1:
96 result = ret[0].content
97 else:
98 result = ret
99
100 elif func:
101 result = func(ctx)
102
103 else:
104 raise ValueError("'path' or 'func' is required.")
105 finally:
106 if doc:
107 doc.freeDoc()
108 if ctx:
109 ctx.xpathFreeContext()
110 return result
111
112
113def pretty_mem(val):

Callers 2

get_node_infoMethod · 0.90
hypervisor_typeMethod · 0.90

Calls 1

funcFunction · 0.85

Tested by

no test coverage detected