[libvirt] [PATCH libvirt-python v2 08/22] event-test.py: Add class for event descriptions

Philipp Hahn posted 22 patches 6 years, 2 months ago
[libvirt] [PATCH libvirt-python v2 08/22] event-test.py: Add class for event descriptions
Posted by Philipp Hahn 6 years, 2 months ago
Signed-off-by: Philipp Hahn <hahn@univention.de>
---
 examples/event-test.py | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/examples/event-test.py b/examples/event-test.py
index 91a7cb7..d2d2c60 100755
--- a/examples/event-test.py
+++ b/examples/event-test.py
@@ -456,6 +456,31 @@ def virEventLoopNativeStart():
 ##########################################################################
 # Everything that now follows is a simple demo of domain lifecycle events
 ##########################################################################
+class Description(object):
+    __slots__ = ('desc', 'args')
+
+    def __init__(self, *args, **kwargs):
+        self.desc = kwargs.get('desc')
+        self.args = args
+
+    def __str__(self):  # type: () -> str
+        return self.desc
+
+    def __getitem__(self, item):  # type: (int) -> str
+        try:
+            data = self.args[item]
+        except IndexError:
+            return self.__class__(desc=str(item))
+
+        if isinstance(data, str):
+            return self.__class__(desc=data)
+        elif isinstance(data, (list, tuple)):
+            desc, args = data
+            return self.__class__(*args, desc=desc)
+
+        raise TypeError(args)
+
+
 def domEventToString(event):
     domEventStrings = ( "Defined",
                      "Undefined",
-- 
2.11.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list