annotate src/bridge/bridge_constructor/constructors/dbus/constructor.py @ 2186:9061c7247964

jp (std output): force unicode on simple output
author Goffi <goffi@goffi.org>
date Sun, 12 Mar 2017 23:29:09 +0100
parents 4633cfcbcccb
children 8b37a62336c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 #-*- coding: utf-8 -*-
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SàT: a XMPP client
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.bridge.bridge_constructor import base_constructor
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 class DbusConstructor(base_constructor.Constructor):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 NAME = "dbus"
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 CORE_TEMPLATE = "dbus_core_template.py"
2086
4633cfcbcccb bridge (D-Bus): bad design fixes:
Goffi <goffi@goffi.org>
parents: 2085
diff changeset
26 CORE_DEST = "dbus_bridge.py"
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 CORE_FORMATS = {
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 'signals': """\
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 @dbus.service.signal(const_INT_PREFIX+const_{category}_SUFFIX,
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 signature='{sig_in}')
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 def {name}(self, {args}):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 {body}\n""",
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 'methods': """\
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 @dbus.service.method(const_INT_PREFIX+const_{category}_SUFFIX,
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 in_signature='{sig_in}', out_signature='{sig_out}',
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 async_callbacks={async_callbacks})
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 def {name}(self, {args}{async_comma}{async_args_def}):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 {debug}return self._callback("{name}", {args_result}{async_comma}{async_args_call})\n""",
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 'signal_direct_calls': """\
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 def {name}(self, {args}):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.dbus_bridge.{name}({args})\n""",
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 }
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 FRONTEND_TEMPLATE = "dbus_frontend_template.py"
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 FRONTEND_DEST = CORE_DEST
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 FRONTEND_FORMATS = {
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 'methods': """\
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def {name}(self, {args}{async_comma}{async_args}):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 {error_handler}{blocking_call}{debug}return {result}\n""",
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 }
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def core_completion_signal(self, completion, function, default, arg_doc, async_):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 completion['category'] = completion['category'].upper()
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 completion['body'] = "pass" if not self.args.debug else 'log.debug ("{}")'.format(completion['name'])
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
57
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 def core_completion_method(self, completion, function, default, arg_doc, async_):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 completion.update({
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 'debug': "" if not self.args.debug else 'log.debug ("%s")\n%s' % (completion['name'], 8 * ' '),
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 'args_result': self.getArguments(function['sig_in'], name=arg_doc, unicode_protect=self.args.unicode),
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 'async_comma': ', ' if async_ and function['sig_in'] else '',
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 'async_args_def': 'callback=None, errback=None' if async_ else '',
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 'async_args_call': 'callback=callback, errback=errback' if async_ else '',
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 'async_callbacks': "('callback', 'errback')" if async_ else "None",
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 'category': completion['category'].upper(),
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 })
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 def frontend_completion_method(self, completion, function, default, arg_doc, async_):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 completion.update({
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 # XXX: we can manage blocking call in the same way as async one: if callback is None the call will be blocking
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 'debug': "" if not self.args.debug else 'log.debug ("%s")\n%s' % (completion['name'], 8 * ' '),
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 'args_result': self.getArguments(function['sig_in'], name=arg_doc),
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 'async_args': 'callback=None, errback=None',
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 'async_comma': ', ' if function['sig_in'] else '',
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 'error_handler': """if callback is None:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 error_handler = None
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 else:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 if errback is None:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 errback = log.error
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 error_handler = lambda err:errback(dbus_to_bridge_exception(err))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 """,
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 })
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 if async_:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 completion['blocking_call'] = ''
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 completion['async_args_result'] = 'timeout=const_TIMEOUT, reply_handler=callback, error_handler=error_handler'
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 else:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 # XXX: To have a blocking call, we must have not reply_handler, so we test if callback exists, and add reply_handler only in this case
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 completion['blocking_call'] = """kwargs={}
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 if callback is not None:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 kwargs['timeout'] = const_TIMEOUT
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 kwargs['reply_handler'] = callback
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 kwargs['error_handler'] = error_handler
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 """
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 completion['async_args_result'] = '**kwargs'
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 result = "self.db_%(category)s_iface.%(name)s(%(args_result)s%(async_comma)s%(async_args_result)s)" % completion
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 completion['result'] = ("unicode(%s)" if self.args.unicode and function['sig_out'] == 's' else "%s") % result