def _xpath(xml, path=None, func=None, return_list=False, register_namespace=None): """ Return the content from the passed xml xpath, or return the result of a passed function (receives xpathContext as its only arg) """ def _getter(doc, ctx, path): ignore = doc if func: return func(ctx) if not path: raise ValueError("'path' or 'func' is required.") ret = ctx.xpathEval(path) if type(ret) is list: if len(ret) >= 1: if return_list: return ret else: return ret[0].content else: ret = None return ret return util.xml_parse_wrapper(xml, _getter, path, register_namespace=register_namespace)
def import_file(input_file): """ Import a configuration file. Raises if the file couldn't be opened, or parsing otherwise failed. """ infile = open(input_file, "r") xml = infile.read() infile.close() logging.debug("Importing OVF XML:\n%s", xml) return util.xml_parse_wrapper(xml, ovf_parser._import_file, register_namespace=register_namespace)
def _redefine(self, xml_func, *args): """ Helper function for altering a redefining VM xml @param xml_func: Function to alter the running XML. Takes the original XML as its first argument. @param args: Extra arguments to pass to xml_func """ origxml = self._xml_to_redefine() # Sanitize origxml to be similar to what we will get back origxml = util.xml_parse_wrapper(origxml, lambda d, c: d.serialize()) newxml = xml_func(origxml, *args) self._redefine_xml(newxml)