[Kimchi-devel] [PATCH 3/5] cdrom: Add convinient functions to deal with xml parse

lvroyce at linux.vnet.ibm.com lvroyce at linux.vnet.ibm.com
Mon Jan 6 09:32:13 UTC 2014


From: Royce Lv <lvroyce at linux.vnet.ibm.com>

To format xml used to create cdrom, and to parse and filter cdrom
query results, add three helper function to do this.

Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
---
 src/kimchi/model.py    | 42 ++++++++++++++++++++++++++++++++++++++++++
 src/kimchi/xmlutils.py |  5 +++++
 2 files changed, 47 insertions(+)

diff --git a/src/kimchi/model.py b/src/kimchi/model.py
index a21fcf7..c74c973 100644
--- a/src/kimchi/model.py
+++ b/src/kimchi/model.py
@@ -1672,6 +1672,48 @@ def _get_volume_xml(**kwargs):
     return xml
 
 
+def _get_disk_xml(**kwargs):
+    kwargs['readonly'] = "<readonly/>" \
+                         if kwargs['target']['type'] == 'cdrom' else None
+    xml = """
+    <disk type='{source[type]}' device='{target[type]}'>
+        <source file='{source[path]}'/>
+        <target dev='{target[name]}' bus='{target[bus]}'/>
+        {readonly}
+    </disk>
+    """.format(**kwargs)
+    return dict(xml=xml)
+
+
+def _parse_vm_disks(xml_str, filter_path, filter_name, filter_val):
+    # xml_str: xml_str of all disks
+    # filter_path: xpath of the filter attribute,
+    #              such as './disk' or './disk/target'
+    # filter_name: relative path of filter attr of disks etree element
+    #              such as: type, bus
+    # filter_val: expected value of filter name
+    root = ElementTree.fromstring(xml_str)
+    ret = []
+    xpath = "%s/[@%s='%s']" % (filter_path, filter_name, filter_val)
+    for disk in root.findall(xpath):
+        name = disk.find('./target').attrib['dev']
+        ret.append(name)
+
+    return ret
+
+
+def _parse_device(xml_str, dev_name):
+    xpath = "./disk/target[@dev='%s']/.." % dev_name
+    root = ElementTree.fromstring(xml_str)
+    ret = []
+    disk = root.find(xpath)
+    if disk:
+        path = disk.find('./source').attrib['file']
+        return dict(name=dev_name, path=path)
+    else:
+        raise NotFoundError
+
+
 class LibvirtConnection(object):
     def __init__(self, uri):
         self.uri = uri
diff --git a/src/kimchi/xmlutils.py b/src/kimchi/xmlutils.py
index 51ff0ec..176ceb1 100644
--- a/src/kimchi/xmlutils.py
+++ b/src/kimchi/xmlutils.py
@@ -40,3 +40,8 @@ def xml_item_update(xml, xpath, value):
     item = root.find(xpath)
     item.text = value
     return ElementTree.tostring(root, encoding="utf-8")
+
+def xml_get_child(xml, xpath):
+    root = ElementTree.fromstring(xml)
+    item = root.find(xpath)
+    return ElementTree.tostring(item, encoding="utf-8")
-- 
1.8.1.2




More information about the Kimchi-devel mailing list