root/sx-dbus/sxdbus.py

Revision 2fe4ced70542fe57d3063c7f7a6f7bc15a39811b, 4.5 KB (checked in by dunk <dunk@…>, 3 years ago)

sx-dbus: keep up with yaydbus

  • Property mode set to 100644
Line 
1# Copyright (c) 2008-2009, samurai-x.org
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
6#     * Redistributions of source code must retain the above copyright
7#       notice, this list of conditions and the following disclaimer.
8#     * Redistributions in binary form must reproduce the above copyright
9#       notice, this list of conditions and the following disclaimer in the
10#       documentation and/or other materials provided with the distribution.
11#     * Neither the name of the samurai-x.org nor the
12#       names of its contributors may be used to endorse or promote products
13#       derived from this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY SAMURAI-X.ORG ``AS IS'' AND ANY
16# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18# DISCLAIMED. IN NO EVENT SHALL SAMURAI-X.ORG  BE LIABLE FOR ANY
19# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26"""
27    sx-dbus is a plugin that enables dbus inside samurai-x.
28
29    .. warning::
30       
31        :ref:`sx-actions` exposes a method that is able to execute
32        arbitrary actions, including the "spawn" action. That means you
33        can execute arbitrary commands in the context of the `sx-wm`
34        process over dbus. Be careful.
35
36    Dependencies
37    ------------
38
39    sx-dbus depends on :ref:`sx-gobject`.
40
41"""
42
43import functools
44
45import gobject
46
47try:
48    from yaydbus.bus import SessionBus
49    from yaydbus import service
50    YAYDBUS = True
51except Exception, e:
52    print e
53    import dbus
54    from dbus import service
55    import dbus.mainloop.glib
56    YAYDBUS = False
57
58from samuraix.plugin import Plugin
59
60import logging 
61log = logging.getLogger(__name__)
62
63
64DEFAULT_BUS_NAME = 'org.samuraix'
65
66
67def sxmethod(interface, **kwargs):
68    """
69        decorator like dbus.service.method() but automatically adds in the sx bus name
70    """
71    return service.method('%s.%s' % (DEFAULT_BUS_NAME, interface), **kwargs)
72
73def sxsignal(interface, **kwargs):
74    """
75        decorator like dbus.service.signal() but automatically adds in the sx bus name
76    """
77    return service.signal('%s.%s' % (DEFAULT_BUS_NAME, interface), **kwargs)
78
79
80class DBusObject(service.Object):
81    def __init__(self, app, *args, **kwargs):
82        self.app = app
83
84        service.Object.__init__(self, *args, **kwargs)
85       
86    @sxmethod("DBusInterface", in_signature='s', out_signature='s')
87    def hello(self, name):   
88        return "hello %s" % name
89
90
91class SXDBus(Plugin):
92    key = 'dbus'
93
94    def __init__(self, app):
95        self.objects = {}
96        if YAYDBUS:
97            self.session_bus = SessionBus()
98            app.add_fd_handler('read', self.session_bus, self.process_dbus)
99        else:
100            dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
101
102            self.session_bus = dbus.SessionBus()
103            self.name = service.BusName(DEFAULT_BUS_NAME, self.session_bus)
104
105        if YAYDBUS:
106            self.session_bus.request_name(DEFAULT_BUS_NAME)
107            self.session_bus.request_name(DEFAULT_BUS_NAME+'.DBusService')
108        self.register('dbus', functools.partial(DBusObject, app))
109
110    def process_dbus(self):
111        self.session_bus.receive_one()
112
113    def register(self, name, cls, path=None):
114        """ register a new dbus object """
115        log.info('registering %s: %s', name, cls)
116       
117        if path is None:
118            path = "/%s" % name
119
120        if YAYDBUS:
121            self.objects[name] = self.session_bus.add_object(
122                    cls(self.session_bus, path),
123            )
124        else:
125            self.objects[name] = cls(
126                    conn=self.session_bus, 
127                    object_path=path,
128                    bus_name=self.name,
129            )
130
131
132if __name__ == '__main__':
133    plugin = SXDBus(None)
134    if YAYDBUS:
135        from yaydbus.mainloop import Mainloop
136        m = Mainloop((plugin.session_bus,))
137        m.run()
138    else:
139        mainloop = gobject.MainLoop()
140        mainloop.run()
Note: See TracBrowser for help on using the browser.