mirror of
https://github.com/virt-manager/virt-manager.git
synced 2025-02-02 09:47:16 +03:00
storage: Use XMLBuilder for pool enumerate parsing
And kill the parse_node_helper function, since that paradigm is dead.
This commit is contained in:
parent
d4dfbd41b5
commit
45ae269950
@ -165,30 +165,33 @@ class StoragePool(_StorageObject):
|
||||
return []
|
||||
raise
|
||||
|
||||
def source_parser(node):
|
||||
ret = []
|
||||
class _EnumerateSource(XMLBuilder):
|
||||
_XML_ROOT_NAME = "source"
|
||||
class _EnumerateSources(XMLBuilder):
|
||||
_XML_ROOT_NAME = "sources"
|
||||
sources = XMLChildProperty(_EnumerateSource)
|
||||
|
||||
child = node.children
|
||||
while child:
|
||||
if child.name == "source":
|
||||
xml = "<pool>\n%s\n</pool>" % (
|
||||
util.xml_indent(child.serialize(format=1), 2))
|
||||
parseobj = StoragePool(conn, parsexml=xml)
|
||||
parseobj.type = pool_type
|
||||
|
||||
obj = StoragePool(conn)
|
||||
obj.type = pool_type
|
||||
obj.source_path = parseobj.source_path
|
||||
for host in parseobj.hosts:
|
||||
parseobj.remove_host(host)
|
||||
obj.add_host_obj(host)
|
||||
obj.source_name = parseobj.source_name
|
||||
obj.format = parseobj.format
|
||||
ret.append(obj)
|
||||
child = child.next
|
||||
return ret
|
||||
ret = []
|
||||
sources = _EnumerateSources(conn, xml)
|
||||
for source in sources.sources:
|
||||
source_xml = source.get_xml_config()
|
||||
|
||||
return util.parse_node_helper(xml, "sources", source_parser)
|
||||
pool_xml = "<pool>\n%s\n</pool>" % (util.xml_indent(source_xml, 2))
|
||||
parseobj = StoragePool(conn, parsexml=pool_xml)
|
||||
parseobj.type = pool_type
|
||||
|
||||
obj = StoragePool(conn)
|
||||
obj.type = pool_type
|
||||
obj.source_path = parseobj.source_path
|
||||
for host in parseobj.hosts:
|
||||
parseobj.remove_host(host)
|
||||
obj.add_host_obj(host)
|
||||
obj.source_name = parseobj.source_name
|
||||
obj.format = parseobj.format
|
||||
|
||||
ret.append(obj)
|
||||
return ret
|
||||
|
||||
@staticmethod
|
||||
def build_default_pool(conn):
|
||||
|
@ -25,7 +25,6 @@ import re
|
||||
import stat
|
||||
|
||||
import libvirt
|
||||
import libxml2
|
||||
|
||||
|
||||
_host_blktap_capable = None
|
||||
@ -248,42 +247,6 @@ def default_bridge(conn):
|
||||
return None
|
||||
|
||||
|
||||
def parse_node_helper(xml, root_name, callback, exec_class=ValueError):
|
||||
"""
|
||||
Parse the passed XML, expecting root as root_name, and pass the
|
||||
root node to callback
|
||||
"""
|
||||
class ErrorHandler(object):
|
||||
def __init__(self):
|
||||
self.msg = ""
|
||||
def handler(self, ignore, s):
|
||||
self.msg += s
|
||||
error = ErrorHandler()
|
||||
libxml2.registerErrorHandler(error.handler, None)
|
||||
|
||||
try:
|
||||
try:
|
||||
doc = libxml2.readMemory(xml, len(xml),
|
||||
None, None,
|
||||
libxml2.XML_PARSE_NOBLANKS)
|
||||
except (libxml2.parserError, libxml2.treeError), e:
|
||||
raise exec_class("%s\n%s" % (e, error.msg))
|
||||
finally:
|
||||
libxml2.registerErrorHandler(None, None)
|
||||
|
||||
ret = None
|
||||
try:
|
||||
root = doc.getRootElement()
|
||||
if root.name != root_name:
|
||||
raise ValueError("Root element is not '%s'" % root_name)
|
||||
|
||||
ret = callback(root)
|
||||
finally:
|
||||
doc.freeDoc()
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def generate_uuid(conn):
|
||||
for ignore in range(256):
|
||||
uuid = randomUUID(conn)
|
||||
|
Loading…
x
Reference in New Issue
Block a user