More work on XML parsing to provide nice domain output

This commit is contained in:
Joshua Boniface 2018-06-05 12:27:41 -04:00
parent b63f6a2958
commit 9d8845dee3
1 changed files with 33 additions and 4 deletions

37
pvcf.py
View File

@ -4,6 +4,7 @@ import os, sys, libvirt, uuid
import kazoo.client import kazoo.client
import lxml import lxml
import click import click
import operator
#from click import command, option, Option, UsageError #from click import command, option, Option, UsageError
# #
@ -70,10 +71,9 @@ def getInformationFromXML(zk, uuid):
# Parse XML using lxml.objectify # Parse XML using lxml.objectify
parsed_xml = lxml.objectify.fromstring(xml) parsed_xml = lxml.objectify.fromstring(xml)
# Now get the information we want from it # Now get the information we want from it
print(lxml.objectify.dump(parsed_xml))
dmemory = parsed_xml.memory dmemory = parsed_xml.memory
dmemory_unit = parsed_xml.memory.attrib['unit'] dmemory_unit = parsed_xml.memory.attrib['unit']
dcurrentMemory = parsed_xml.currentMemory
dcurrentMemory_unit = parsed_xml.currentMemory.attrib['unit']
dvcpu = parsed_xml.vcpu dvcpu = parsed_xml.vcpu
dtype = parsed_xml.os.type dtype = parsed_xml.os.type
darch = parsed_xml.os.type.attrib['arch'] darch = parsed_xml.os.type.attrib['arch']
@ -81,11 +81,40 @@ def getInformationFromXML(zk, uuid):
dfeatures = [] dfeatures = []
for feature in parsed_xml.features.getchildren(): for feature in parsed_xml.features.getchildren():
dfeatures.append(feature.tag) dfeatures.append(feature.tag)
dconsole = parsed_xml.devices.console.attrib['type']
ddisks = [] ddisks = []
for disk in parsed_xml.devices.disk.getchildren(): dnets = []
ddisks.append(disk.attrib) dcontrollers = []
for device in parsed_xml.devices.getchildren():
if device.tag == 'disk':
disk_attrib = device.source.attrib
disk_type = device.attrib['type']
if disk_type == 'network':
disk_obj = { 'type': disk_attrib.get('protocol'), 'name': disk_attrib.get('name') }
elif disk_type == 'file':
disk_obj = { 'type': 'file', 'name': disk_attrib.get('file') }
else:
disk_obj = {}
ddisks.append(disk_obj)
if device.tag == 'interface':
net_type = device.attrib['type']
net_mac = device.mac.attrib['address']
net_bridge = device.source.attrib[net_type]
net_model = device.model.attrib['type']
net_obj = { 'type': net_type, 'mac': net_mac, 'source': net_bridge, 'model': net_model }
dnets.append(net_obj)
if device.tag == 'controller':
controller_type = device.attrib['type']
try:
controller_model = device.attrib['model']
except KeyError:
controller_model = 'none'
controller_obj = { 'type': controller_type, 'model': controller_model }
dcontrollers.append(controller_obj)
print(ddisks) print(ddisks)
print(dnets)
print(dcontrollers)
return None return None