[virt-tools-list] [RFC PATCH 07/10] baseclass: Make gtk/gobject dep optional

Cole Robinson crobinso at redhat.com
Mon Apr 18 18:06:49 UTC 2011


Add a new file 'guidiff' that sets up the enviroment for TUI vs GUI.

Signed-off-by: Cole Robinson <crobinso at redhat.com>
---
 src/virt-manager-tui.py.in   |    3 ++
 src/virt-manager.py.in       |    3 ++
 src/virtManager/baseclass.py |   42 +++++++++++++++++++++++---------
 src/virtManager/guidiff.py   |   53 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 89 insertions(+), 12 deletions(-)
 create mode 100644 src/virtManager/guidiff.py

diff --git a/src/virt-manager-tui.py.in b/src/virt-manager-tui.py.in
index 6a055fa..3026cc4 100644
--- a/src/virt-manager-tui.py.in
+++ b/src/virt-manager-tui.py.in
@@ -136,6 +136,9 @@ def main():
                             (virtinst.__version__) +
                            "\n\n" + msg)
 
+    import virtManager.guidiff
+    virtManager.guidiff.is_gui(False)
+
     # Hack in the default URI for this instance of the tui
     if options.uri:
         import virtManagerTui.libvirtworker
diff --git a/src/virt-manager.py.in b/src/virt-manager.py.in
index 75a2cb5..4df4fa1 100755
--- a/src/virt-manager.py.in
+++ b/src/virt-manager.py.in
@@ -354,6 +354,9 @@ def main():
     config.hv_packages = hv_packages
     config.libvirt_packages = libvirt_packages
 
+    import virtManager.guidiff
+    virtManager.guidiff.is_gui(True)
+
     # Now we've got basic environment up & running we can fork
     if not options.nofork and not options.debug:
         drop_tty()
diff --git a/src/virtManager/baseclass.py b/src/virtManager/baseclass.py
index 86a231e..30bc627 100644
--- a/src/virtManager/baseclass.py
+++ b/src/virtManager/baseclass.py
@@ -22,11 +22,9 @@ import os
 import sys
 import logging
 
-import gtk
-import gobject
-
 import virtManager
-import virtManager.util as util
+
+running_config, gobject, GObject, gtk = virtManager.guidiff.get_imports()
 
 def _safe_wrapper(func, *args):
     gtk.gdk.threads_enter()
@@ -35,15 +33,24 @@ def _safe_wrapper(func, *args):
     finally:
         gtk.gdk.threads_leave()
 
-class vmmGObject(gobject.GObject):
+class vmmGObject(GObject):
 
     @staticmethod
     def type_register(*args, **kwargs):
-        gobject.type_register(*args, **kwargs)
+        if hasattr(gobject, "type_register"):
+            gobject.type_register(*args, **kwargs)
+
+    @staticmethod
+    def signal_new(klass, signal, args):
+        if hasattr(gobject, "signal_new"):
+            gobject.signal_new(signal, klass,
+                                    gobject.SIGNAL_RUN_FIRST,
+                                    gobject.TYPE_NONE,
+                                    args)
 
     def __init__(self):
-        gobject.GObject.__init__(self)
-        self.config = util.running_config
+        GObject.__init__(self)
+        self.config = running_config
 
         self._gobject_handles = []
         self._gobject_timeouts = []
@@ -69,11 +76,15 @@ class vmmGObject(gobject.GObject):
             logging.exception("Error cleaning up %s" % self)
 
     def connect(self, name, callback, *args):
-        ret = gobject.GObject.connect(self, name, callback, *args)
+        if not hasattr(GObject, "connect"):
+            return
+        ret = GObject.connect(self, name, callback, *args)
         self._gobject_handles.append(ret)
         return ret
     def disconnect(self, handle):
-        ret = gobject.GObject.disconnect(self, handle)
+        if not hasattr(GObject, "disconnect"):
+            return
+        ret = GObject.disconnect(self, handle)
         self._gobject_handles.remove(handle)
         return ret
 
@@ -86,6 +97,8 @@ class vmmGObject(gobject.GObject):
     def add_gobject_timeout(self, handle):
         self._gobject_timeouts.append(handle)
     def remove_gobject_timeout(self, handle):
+        if not hasattr(gobject, "source_remove"):
+            return
         gobject.source_remove(handle)
         self._gobject_timeouts.remove(handle)
 
@@ -152,9 +165,14 @@ class vmmGObject(gobject.GObject):
         """
         return gobject.timeout_add(timeout, _safe_wrapper, func, *args)
 
+    def emit(self, *args, **kwargs):
+        if not hasattr(GObject, "emit"):
+            return
+        return GObject.emit(self, *args, **kwargs)
+
     def __del__(self):
-        if hasattr(gobject.GObject, "__del__"):
-            getattr(gobject.GObject, "__del__")(self)
+        if hasattr(GObject, "__del__"):
+            getattr(GObject, "__del__")(self)
 
         try:
             if self.config:
diff --git a/src/virtManager/guidiff.py b/src/virtManager/guidiff.py
new file mode 100644
index 0000000..bba61b1
--- /dev/null
+++ b/src/virtManager/guidiff.py
@@ -0,0 +1,53 @@
+#
+# Copyright (C) 2011 Red Hat, Inc.
+# Copyright (C) 2011 Cole Robinson <crobinso at redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301 USA.
+#
+
+_is_gui = True
+
+class stubclass(object):
+    def __init__(self, *args, **kwargs):
+        ignore = args
+        ignore = kwargs
+
+    def __getattr__(self, attr):
+        def stub(*args, **kwargs):
+            ignore = args
+            ignore = kwargs
+        return stub
+
+    def __setattr__(self, attr, val):
+        ignore = attr
+        ignore = val
+
+def is_gui(isgui):
+    global _is_gui
+    _is_gui = isgui
+
+def get_imports():
+    if _is_gui:
+        import gobject
+        import gtk
+        import virtManager.util
+
+        return (virtManager.util.running_config,
+                gobject,
+                gobject.GObject,
+                gtk)
+    else:
+        return (stubclass(), None, object, None)
-- 
1.7.4




More information about the virt-tools-list mailing list