|
Packit |
130fc8 |
"""Support code for implementing D-Bus services via PyGI."""
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
# Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
|
|
Packit |
130fc8 |
#
|
|
Packit |
130fc8 |
# Permission is hereby granted, free of charge, to any person
|
|
Packit |
130fc8 |
# obtaining a copy of this software and associated documentation
|
|
Packit |
130fc8 |
# files (the "Software"), to deal in the Software without
|
|
Packit |
130fc8 |
# restriction, including without limitation the rights to use, copy,
|
|
Packit |
130fc8 |
# modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
Packit |
130fc8 |
# of the Software, and to permit persons to whom the Software is
|
|
Packit |
130fc8 |
# furnished to do so, subject to the following conditions:
|
|
Packit |
130fc8 |
#
|
|
Packit |
130fc8 |
# The above copyright notice and this permission notice shall be
|
|
Packit |
130fc8 |
# included in all copies or substantial portions of the Software.
|
|
Packit |
130fc8 |
#
|
|
Packit |
130fc8 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
Packit |
130fc8 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
Packit |
130fc8 |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
Packit |
130fc8 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
Packit |
130fc8 |
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
Packit |
130fc8 |
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
Packit |
130fc8 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
Packit |
130fc8 |
# DEALINGS IN THE SOFTWARE.
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
__all__ = ['ExportedGObject']
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
from gi.repository import GObject
|
|
Packit |
130fc8 |
import dbus.service
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
# The odd syntax used here is required so that the code is compatible with
|
|
Packit |
130fc8 |
# both Python 2 and Python 3. It essentially creates a new class called
|
|
Packit |
130fc8 |
# ExportedGObject with a metaclass of ExportGObjectType and an __init__()
|
|
Packit |
130fc8 |
# function.
|
|
Packit |
130fc8 |
#
|
|
Packit |
130fc8 |
# Because GObject and `dbus.service.Object` both have custom metaclasses, the
|
|
Packit |
130fc8 |
# naive approach using simple multiple inheritance won't work. This class has
|
|
Packit |
130fc8 |
# `ExportedGObjectType` as its metaclass, which is sufficient to make it work
|
|
Packit |
130fc8 |
# correctly.
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
class ExportedGObjectType(GObject.GObject.__class__, dbus.service.InterfaceType):
|
|
Packit |
130fc8 |
"""A metaclass which inherits from both GObjectMeta and
|
|
Packit |
130fc8 |
`dbus.service.InterfaceType`. Used as the metaclass for `ExportedGObject`.
|
|
Packit |
130fc8 |
"""
|
|
Packit |
130fc8 |
def __init__(cls, name, bases, dct):
|
|
Packit |
130fc8 |
GObject.GObject.__class__.__init__(cls, name, bases, dct)
|
|
Packit |
130fc8 |
dbus.service.InterfaceType.__init__(cls, name, bases, dct)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
def ExportedGObject__init__(self, conn=None, object_path=None, **kwargs):
|
|
Packit |
130fc8 |
"""Initialize an exported GObject.
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
:Parameters:
|
|
Packit |
130fc8 |
`conn` : dbus.connection.Connection
|
|
Packit |
130fc8 |
The D-Bus connection or bus
|
|
Packit |
130fc8 |
`object_path` : str
|
|
Packit |
130fc8 |
The object path at which to register this object.
|
|
Packit |
130fc8 |
:Keywords:
|
|
Packit |
130fc8 |
`bus_name` : dbus.service.BusName
|
|
Packit |
130fc8 |
A bus name to be held on behalf of this object, or None.
|
|
Packit |
130fc8 |
`gobject_properties` : dict
|
|
Packit |
130fc8 |
GObject properties to be set on the constructed object.
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
Any unrecognised keyword arguments will also be interpreted
|
|
Packit |
130fc8 |
as GObject properties.
|
|
Packit |
130fc8 |
"""
|
|
Packit |
130fc8 |
bus_name = kwargs.pop('bus_name', None)
|
|
Packit |
130fc8 |
gobject_properties = kwargs.pop('gobject_properties', None)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
if gobject_properties is not None:
|
|
Packit |
130fc8 |
kwargs.update(gobject_properties)
|
|
Packit |
130fc8 |
GObject.GObject.__init__(self, **kwargs)
|
|
Packit |
130fc8 |
dbus.service.Object.__init__(self, conn=conn,
|
|
Packit |
130fc8 |
object_path=object_path,
|
|
Packit |
130fc8 |
bus_name=bus_name)
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
ExportedGObject__doc__ = 'A GObject which is exported on the D-Bus.'
|
|
Packit |
130fc8 |
|
|
Packit |
130fc8 |
ExportedGObject = ExportedGObjectType(
|
|
Packit |
130fc8 |
'ExportedGObject',
|
|
Packit |
130fc8 |
(GObject.GObject, dbus.service.Object),
|
|
Packit |
130fc8 |
{'__init__': ExportedGObject__init__,
|
|
Packit |
130fc8 |
'__doc__': ExportedGObject__doc__,
|
|
Packit |
130fc8 |
})
|