annotate sat/bridge/bridge_constructor/constructors/dbus/dbus_core_template.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/bridge/bridge_constructor/constructors/dbus/dbus_core_template.py@0046283a285d
children 56f94936df1e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 #-*- coding: utf-8 -*-
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 595
diff changeset
4 # SAT: a jabber client
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 595
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 595
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 595
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 595
diff changeset
10 # (at your option) any later version.
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 595
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 595
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 595
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 595
diff changeset
15 # GNU Affero General Public License for more details.
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 595
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 595
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
20 from sat.core.i18n import _
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import dbus
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import dbus.service
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 import dbus.mainloop.glib
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
24 import inspect
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
25 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
26 log = getLogger(__name__)
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
27 from twisted.internet.defer import Deferred
627
d207c2186519 core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents: 609
diff changeset
28 from sat.core.exceptions import BridgeInitError
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
30 const_INT_PREFIX = "org.goffi.SAT" # Interface prefix
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
31 const_ERROR_PREFIX = const_INT_PREFIX + ".error"
359
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
32 const_OBJ_PATH = '/org/goffi/SAT/bridge'
371
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
33 const_CORE_SUFFIX = ".core"
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
34 const_PLUGIN_SUFFIX = ".plugin"
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
36
477
031b0e0aaab8 plugin groupblog: subscriptions/notifications
Goffi <goffi@goffi.org>
parents: 469
diff changeset
37 class ParseError(Exception):
031b0e0aaab8 plugin groupblog: subscriptions/notifications
Goffi <goffi@goffi.org>
parents: 469
diff changeset
38 pass
031b0e0aaab8 plugin groupblog: subscriptions/notifications
Goffi <goffi@goffi.org>
parents: 469
diff changeset
39
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
40
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
41 class MethodNotRegistered(dbus.DBusException):
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
42 _dbus_error_name = const_ERROR_PREFIX + ".MethodNotRegistered"
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
43
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
44
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
45 class InternalError(dbus.DBusException):
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
46 _dbus_error_name = const_ERROR_PREFIX + ".InternalError"
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
47
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
48
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
49 class AsyncNotDeferred(dbus.DBusException):
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
50 _dbus_error_name = const_ERROR_PREFIX + ".AsyncNotDeferred"
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
51
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
52
424
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
53 class DeferredNotAsync(dbus.DBusException):
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
54 _dbus_error_name = const_ERROR_PREFIX + ".DeferredNotAsync"
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
55
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
56
419
6c167a2e04b8 bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents: 371
diff changeset
57 class GenericException(dbus.DBusException):
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
58 def __init__(self, twisted_error):
1073
f094583732de bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents: 1062
diff changeset
59 """
f094583732de bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents: 1062
diff changeset
60
f094583732de bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents: 1062
diff changeset
61 @param twisted_error (Failure): instance of twisted Failure
f094583732de bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents: 1062
diff changeset
62 @return: DBusException
f094583732de bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents: 1062
diff changeset
63 """
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
64 super(GenericException, self).__init__()
751
1def5b7edf9f core, bridge: better GenericException handling
Goffi <goffi@goffi.org>
parents: 627
diff changeset
65 try:
1def5b7edf9f core, bridge: better GenericException handling
Goffi <goffi@goffi.org>
parents: 627
diff changeset
66 # twisted_error.value is a class
1062
95758ef3faa8 bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents: 993
diff changeset
67 class_ = twisted_error.value().__class__
751
1def5b7edf9f core, bridge: better GenericException handling
Goffi <goffi@goffi.org>
parents: 627
diff changeset
68 except TypeError:
1def5b7edf9f core, bridge: better GenericException handling
Goffi <goffi@goffi.org>
parents: 627
diff changeset
69 # twisted_error.value is an instance
1062
95758ef3faa8 bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents: 993
diff changeset
70 class_ = twisted_error.value.__class__
1073
f094583732de bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents: 1062
diff changeset
71 message = twisted_error.getErrorMessage()
f094583732de bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents: 1062
diff changeset
72 try:
f094583732de bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents: 1062
diff changeset
73 self.args = (message, twisted_error.value.condition)
f094583732de bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents: 1062
diff changeset
74 except AttributeError:
f094583732de bridge: DBusException also transports the twisted failure condition
souliane <souliane@mailoo.org>
parents: 1062
diff changeset
75 self.args = (message,)
1062
95758ef3faa8 bridge: async failures are more detailed (full class name + error message)
souliane <souliane@mailoo.org>
parents: 993
diff changeset
76 self._dbus_error_name = '.'.join([const_ERROR_PREFIX, class_.__module__, class_.__name__])
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
77
419
6c167a2e04b8 bridge: added generic D-Bus exception management + asyncCreateProfile method
Goffi <goffi@goffi.org>
parents: 371
diff changeset
78
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 class DbusObject(dbus.service.Object):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
80
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 def __init__(self, bus, path):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 dbus.service.Object.__init__(self, bus, path)
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
83 log.debug("Init DbusObject...")
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
84 self.cb = {}
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
2086
4633cfcbcccb bridge (D-Bus): bad design fixes:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
86 def register_method(self, name, cb):
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
87 self.cb[name] = cb
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
89 def _callback(self, name, *args, **kwargs):
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
90 """call the callback if it exists, raise an exception else
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
91 if the callback return a deferred, use async methods"""
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
92 if not name in self.cb:
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
93 raise MethodNotRegistered
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
94
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
95 if "callback" in kwargs:
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
96 #we must have errback too
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
97 if not "errback" in kwargs:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
98 log.error("errback is missing in method call [%s]" % name)
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
99 raise InternalError
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
100 callback = kwargs.pop("callback")
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
101 errback = kwargs.pop("errback")
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
102 async = True
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
103 else:
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
104 async = False
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
105 result = self.cb[name](*args, **kwargs)
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
106 if async:
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
107 if not isinstance(result, Deferred):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
108 log.error("Asynchronous method [%s] does not return a Deferred." % name)
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
109 raise AsyncNotDeferred
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
110 result.addCallback(lambda result: callback() if result is None else callback(result))
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
111 result.addErrback(lambda err: errback(GenericException(err)))
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
112 else:
424
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
113 if isinstance(result, Deferred):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
114 log.error("Synchronous method [%s] return a Deferred." % name)
424
72c13313b6d6 bridge: added check that synchronous method does not return a deferred
Goffi <goffi@goffi.org>
parents: 423
diff changeset
115 raise DeferredNotAsync
423
6c20c76abdcc backend: - bridge async D-Bus method now automatically manage callback and errback, we just have to return a deferred
Goffi <goffi@goffi.org>
parents: 419
diff changeset
116 return result
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
117 ### signals ###
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
118
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
119 @dbus.service.signal(const_INT_PREFIX + const_PLUGIN_SUFFIX,
371
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
120 signature='')
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
121 def dummySignal(self):
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
122 #FIXME: workaround for addSignal (doesn't work if one method doensn't
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
123 # already exist for plugins), probably missing some initialisation, need
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
124 # further investigations
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
125 pass
3ea41a199b36 bridge refactoring: categories are now core and plugin instead of communication and request
Goffi <goffi@goffi.org>
parents: 359
diff changeset
126
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 ##SIGNALS_PART##
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
128 ### methods ###
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
129
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 ##METHODS_PART##
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
131 def __attributes(self, in_sign):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 """Return arguments to user given a in_sign
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 @param in_sign: in_sign in the short form (using s,a,i,b etc)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
134 @return: list of arguments that correspond to a in_sign (e.g.: "sss" return "arg1, arg2, arg3")"""
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
135 i = 0
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
136 idx = 0
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
137 attr = []
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
138 while i < len(in_sign):
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
139 if in_sign[i] not in ['b', 'y', 'n', 'i', 'x', 'q', 'u', 't', 'd', 's', 'a']:
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 raise ParseError("Unmanaged attribute type [%c]" % in_sign[i])
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
141
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
142 attr.append("arg_%i" % idx)
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
143 idx += 1
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
144
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 if in_sign[i] == 'a':
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
146 i += 1
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
147 if in_sign[i] != '{' and in_sign[i] != '(': # FIXME: must manage tuples out of arrays
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
148 i += 1
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
149 continue # we have a simple type for the array
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 opening_car = in_sign[i]
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
151 assert(opening_car in ['{', '('])
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 closing_car = '}' if opening_car == '{' else ')'
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 opening_count = 1
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
154 while (True): # we have a dict or a list of tuples
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
155 i += 1
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
156 if i >= len(in_sign):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 raise ParseError("missing }")
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 if in_sign[i] == opening_car:
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
159 opening_count += 1
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 if in_sign[i] == closing_car:
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
161 opening_count -= 1
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 if opening_count == 0:
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 break
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
164 i += 1
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
165 return attr
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
166
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
167 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 """Dynamically add a method to Dbus Bridge"""
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
169 inspect_args = inspect.getargspec(method)
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
170
469
db4c2b82bab6 D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 468
diff changeset
171 _arguments = inspect_args.args
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
172 _defaults = list(inspect_args.defaults or [])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
173
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
174 if inspect.ismethod(method):
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
175 #if we have a method, we don't want the first argument (usually 'self')
469
db4c2b82bab6 D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 468
diff changeset
176 del(_arguments[0])
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
177
469
db4c2b82bab6 D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 468
diff changeset
178 #first arguments are for the _callback method
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
179 arguments_callback = ', '.join([repr(name)] + ((_arguments + ['callback=callback', 'errback=errback']) if async else _arguments))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
180
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
181 if async:
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
182 _arguments.extend(['callback', 'errback'])
469
db4c2b82bab6 D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 468
diff changeset
183 _defaults.extend([None, None])
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
184
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
185 #now we create a second list with default values
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
186 for i in range(1, len(_defaults) + 1):
469
db4c2b82bab6 D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 468
diff changeset
187 _arguments[-i] = "%s = %s" % (_arguments[-i], repr(_defaults[-i]))
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
188
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
189 arguments_defaults = ', '.join(_arguments)
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
190
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
191 code = compile('def %(name)s (self,%(arguments_defaults)s): return self._callback(%(arguments_callback)s)' %
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
192 {'name': name, 'arguments_defaults': arguments_defaults, 'arguments_callback': arguments_callback}, '<DBus bridge>', 'exec')
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
193 exec (code) # FIXME: to the same thing in a cleaner way, without compile/exec
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 method = locals()[name]
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
195 async_callbacks = ('callback', 'errback') if async else None
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 setattr(DbusObject, name, dbus.service.method(
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
197 const_INT_PREFIX + int_suffix, in_signature=in_sign, out_signature=out_sign,
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
198 async_callbacks=async_callbacks)(method))
284
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
199 function = getattr(self, name)
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
200 func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface]
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
201 func_table[function.__name__] = function # Needed for introspection
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
202
298
15c8916317d0 dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents: 284
diff changeset
203 def addSignal(self, name, int_suffix, signature, doc={}):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 """Dynamically add a signal to Dbus Bridge"""
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
205 attributes = ', '.join(self.__attributes(signature))
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
206 #TODO: use doc parameter to name attributes
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
207
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
208 #code = compile ('def '+name+' (self,'+attributes+'): log.debug ("'+name+' signal")', '<DBus bridge>','exec') #XXX: the log.debug is too annoying with xmllog
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
209 code = compile('def ' + name + ' (self,' + attributes + '): pass', '<DBus bridge>', 'exec')
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 exec (code)
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 signal = locals()[name]
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 setattr(DbusObject, name, dbus.service.signal(
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
213 const_INT_PREFIX + int_suffix, signature=signature)(signal))
284
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
214 function = getattr(self, name)
c25371424090 dbus bridge: fixed introspection for dynamically added methods and signals
Goffi <goffi@goffi.org>
parents: 272
diff changeset
215 func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface]
595
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
216 func_table[function.__name__] = function # Needed for introspection
1f160467f5de Fix pep8 support in src/bridge.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
217
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
218
2086
4633cfcbcccb bridge (D-Bus): bad design fixes:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
219 class Bridge(object):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 def __init__(self):
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
222 log.info("Init DBus...")
627
d207c2186519 core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents: 609
diff changeset
223 try:
d207c2186519 core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents: 609
diff changeset
224 self.session_bus = dbus.SessionBus()
d207c2186519 core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents: 609
diff changeset
225 except dbus.DBusException as e:
d207c2186519 core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents: 609
diff changeset
226 if e._dbus_error_name == 'org.freedesktop.DBus.Error.NotSupported':
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
227 log.error(_(u"D-Bus is not launched, please see README to see instructions on how to launch it"))
627
d207c2186519 core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents: 609
diff changeset
228 raise BridgeInitError
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus)
359
eb9d33ba4e36 bridge: templates' constants can now be overrided
Goffi <goffi@goffi.org>
parents: 337
diff changeset
230 self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
231
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
232 ##SIGNAL_DIRECT_CALLS_PART##
2086
4633cfcbcccb bridge (D-Bus): bad design fixes:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
233 def register_method(self, name, callback):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
234 log.debug("registering DBus bridge method [%s]" % name)
2086
4633cfcbcccb bridge (D-Bus): bad design fixes:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
235 self.dbus_bridge.register_method(name, callback)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
236
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
237 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}):
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 """Dynamically add a method to Dbus Bridge"""
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
239 #FIXME: doc parameter is kept only temporary, the time to remove it from calls
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
240 log.debug("Adding method [%s] to DBus bridge" % name)
468
c97640c90a94 D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents: 459
diff changeset
241 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, method, async)
2086
4633cfcbcccb bridge (D-Bus): bad design fixes:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
242 self.register_method(name, method)
267
bdcd535e179e Bridge constructor:
Goffi <goffi@goffi.org>
parents:
diff changeset
243
298
15c8916317d0 dbus bridge: added doc parameter, unmanaged yet
Goffi <goffi@goffi.org>
parents: 284
diff changeset
244 def addSignal(self, name, int_suffix, signature, doc={}):
299
e044d1dc37d1 dbus bridge: asynchrone methods management
Goffi <goffi@goffi.org>
parents: 298
diff changeset
245 self.dbus_bridge.addSignal(name, int_suffix, signature, doc)
2086
4633cfcbcccb bridge (D-Bus): bad design fixes:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
246 setattr(Bridge, name, getattr(self.dbus_bridge, name))