[patch][python] ExportedGObject, a convenience class to export GObjects

Simon McVittie simon.mcvittie at collabora.co.uk
Fri Mar 2 10:56:44 PST 2007


Also for review:

Implement ExportedGObject, a convenience class to export GObjects on the bus.
This is non-trivial because dbus.service.Object and GObject both use
metaclasses, so we need to implement a metaclass inheriting from both their
metaclasses - it might as well go in dbus-python to avoid everyone having to
reinvent this solution.

>From a7110d5ee20f04f17346079a6a824a7c017fe124 Mon Sep 17 00:00:00 2001
From: Simon McVittie <simon.mcvittie at collabora.co.uk>
Date: Fri, 2 Mar 2007 18:46:12 +0000
Subject: [PATCH] Implement ExportedGObject, a convenience class to export GObjects on the bus.
This is non-trivial because dbus.service.Object and GObject both use
metaclasses, so we need to implement a metaclass inheriting from both their
metaclasses - it might as well go in dbus-python to avoid everyone having to
reinvent this solution.
---
 dbus/gobject_service.py |   37 +++++++++++++++++++++++++++++++++++++
 1 files changed, 37 insertions(+), 0 deletions(-)
 create mode 100644 dbus/gobject_service.py

diff --git a/dbus/gobject_service.py b/dbus/gobject_service.py
new file mode 100644
index 0000000..2a9fbd8
--- /dev/null
+++ b/dbus/gobject_service.py
@@ -0,0 +1,37 @@
+"""Support code for implementing D-Bus services via GObjects."""
+
+# Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
+#
+# Licensed under the Academic Free License version 2.1
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Library General Public License as published by
+# the Free Software Foundation; either version 2.1 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 Library General Public License for more details.
+#
+# You should have received a copy of the GNU Library General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+import gobject
+import dbus.service
+
+class ExportedGObjectType(dbus.service.InterfaceType, gobject.GObjectMeta):
+    """A metaclass which inherits from both GObjectMeta and
+    `dbus.service.InterfaceType`. Used as the metaclass for `ExportedGObject`.
+    """
+
+class ExportedGObject(dbus.service.Object, gobject.GObject):
+    """A GObject which is exported on the D-Bus.
+
+    Because GObject and `dbus.service.Object` both have custom metaclasses,
+    the naive approach using simple multiple inheritance won't work. This
+    class has `ExportedGObjectType` as its metaclass, which is sufficient
+    to make it work correctly.
+    """
+    __metaclass__ = ExportedGObjectType
-- 
1.5.0.1



More information about the dbus mailing list