How about this? Good start? What about LibvirtXML class for capabilities. I feel a little awkward about that use, but I couldn't find anything else to use it for. Maybe we don't need it?

On 08/31/2012 12:09 PM, Chris Evich wrote:
* Improved LibvirtXMLError exception with optional string argument
* Added module docstring
* Added LibvirtXMLBase base-class for common methods/attributes
* Added simple LibvirtXML class

Signed-off-by: Chris Evich<[email protected]>
---
  client/virt/libvirt_xml.py |   63 +++++++++++++++++++++++++++++++++++++++-----
  1 files changed, 56 insertions(+), 7 deletions(-)

diff --git a/client/virt/libvirt_xml.py b/client/virt/libvirt_xml.py
index cb577c6..a0a179d 100644
--- a/client/virt/libvirt_xml.py
+++ b/client/virt/libvirt_xml.py
@@ -1,15 +1,64 @@
+"""
+    Intermediate module for working with XML-related virsh functions/methods.
+
+    All classes defined here should inherrit from LibvirtXMLBase and utilize
+    the XMLTreeFile interface to recover external source XML in case internal
+    errors are detected.  Errors originating within this module should raise
+    LibvirtXMLError or subclasses of this exception.  Pleae refer to the
+    xml_utils module documentation for more information on working with
+    XMLTreeFile instances.
+"""
+
  import logging, os.path
  from autotest.client.shared import error, xml_utils
-from autotest.client.virt import libvirt_vm
-
+from autotest.client.virt import libvirt_vm, virsh

  class LibvirtXMLError(Exception):
-    pass
+    """
+    Error originating within libvirt_xml module
+    """

+    def __init__(self, details=''):
+        self.details = details
+        super(LibvirtXMLError, self).__init__()

-class LibvirtXMLVMNameError(LibvirtXMLError):
-    pass

+    def __str__(self):
+        return str(self.details)
+
+
+class LibvirtXMLBase(xml_utils.XMLTreeFile):
+    """
+    Base class for common attributes/methods applying to all sub-classes
+    """
+
+    @classmethod
+    def generate_uuid(cls):
+        """
+        Returns generated uuid value
+        """
+        try:
+            return open("/proc/sys/kernel/random/uuid").read().strip()
+        except IOError:
+            return "" #assume libvirt will fill in empty uuids
+
+
+class LibvirtXML(LibvirtXMLBase):
+    """
+    Represents capabilities of libvirt
+    """
+
+    # Cache this data upon first __new__ call
+    _XML = None
+
+    def __new__(cls):
+        if cls._XML is None:
+            cls._XML = virsh.capabilities()
+        # older python super doesn't work on class objects
+        return LibvirtXMLBase.__new__(cls)
+
+    def __init__(self):
+        """Returns copy of libvirtd capabilities XML"""
+        # protect against accidental modification
+        super(LibvirtXML, self).__init__(self._XML)

-class LibvirtXML(xml_utils.XMLTreeFile):
-    pass
-- 1.7.1



--
Chris Evich, RHCA, RHCE, RHCDS, RHCSS
Quality Assurance Engineer
e-mail: cevich + `@' + redhat.com o: 1-888-RED-HAT1 x44214

_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel

Reply via email to