comparison sat/bridge/bridge_constructor/constructors/dbus/constructor.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
1 #!/usr/bin/env python2 1 #!/usr/bin/env python2
2 #-*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SàT: a XMPP client 4 # SàT: a XMPP client
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6 6
7 # This program is free software: you can redistribute it and/or modify 7 # This program is free software: you can redistribute it and/or modify
23 class DbusConstructor(base_constructor.Constructor): 23 class DbusConstructor(base_constructor.Constructor):
24 NAME = "dbus" 24 NAME = "dbus"
25 CORE_TEMPLATE = "dbus_core_template.py" 25 CORE_TEMPLATE = "dbus_core_template.py"
26 CORE_DEST = "dbus_bridge.py" 26 CORE_DEST = "dbus_bridge.py"
27 CORE_FORMATS = { 27 CORE_FORMATS = {
28 'signals': """\ 28 "signals": """\
29 @dbus.service.signal(const_INT_PREFIX+const_{category}_SUFFIX, 29 @dbus.service.signal(const_INT_PREFIX+const_{category}_SUFFIX,
30 signature='{sig_in}') 30 signature='{sig_in}')
31 def {name}(self, {args}): 31 def {name}(self, {args}):
32 {body}\n""", 32 {body}\n""",
33 33 "methods": """\
34 'methods': """\
35 @dbus.service.method(const_INT_PREFIX+const_{category}_SUFFIX, 34 @dbus.service.method(const_INT_PREFIX+const_{category}_SUFFIX,
36 in_signature='{sig_in}', out_signature='{sig_out}', 35 in_signature='{sig_in}', out_signature='{sig_out}',
37 async_callbacks={async_callbacks}) 36 async_callbacks={async_callbacks})
38 def {name}(self, {args}{async_comma}{async_args_def}): 37 def {name}(self, {args}{async_comma}{async_args_def}):
39 {debug}return self._callback("{name}", {args_result}{async_comma}{async_args_call})\n""", 38 {debug}return self._callback("{name}", {args_result}{async_comma}{async_args_call})\n""",
40 39 "signal_direct_calls": """\
41 'signal_direct_calls': """\
42 def {name}(self, {args}): 40 def {name}(self, {args}):
43 self.dbus_bridge.{name}({args})\n""", 41 self.dbus_bridge.{name}({args})\n""",
44 } 42 }
45 43
46 FRONTEND_TEMPLATE = "dbus_frontend_template.py" 44 FRONTEND_TEMPLATE = "dbus_frontend_template.py"
47 FRONTEND_DEST = CORE_DEST 45 FRONTEND_DEST = CORE_DEST
48 FRONTEND_FORMATS = { 46 FRONTEND_FORMATS = {
49 'methods': """\ 47 "methods": """\
50 def {name}(self, {args}{async_comma}{async_args}): 48 def {name}(self, {args}{async_comma}{async_args}):
51 {error_handler}{blocking_call}{debug}return {result}\n""", 49 {error_handler}{blocking_call}{debug}return {result}\n"""
52 } 50 }
53 51
54 def core_completion_signal(self, completion, function, default, arg_doc, async_): 52 def core_completion_signal(self, completion, function, default, arg_doc, async_):
55 completion['category'] = completion['category'].upper() 53 completion["category"] = completion["category"].upper()
56 completion['body'] = "pass" if not self.args.debug else 'log.debug ("{}")'.format(completion['name']) 54 completion["body"] = (
55 "pass"
56 if not self.args.debug
57 else 'log.debug ("{}")'.format(completion["name"])
58 )
57 59
58 def core_completion_method(self, completion, function, default, arg_doc, async_): 60 def core_completion_method(self, completion, function, default, arg_doc, async_):
59 completion.update({ 61 completion.update(
60 'debug': "" if not self.args.debug else 'log.debug ("%s")\n%s' % (completion['name'], 8 * ' '), 62 {
61 'args_result': self.getArguments(function['sig_in'], name=arg_doc, unicode_protect=self.args.unicode), 63 "debug": ""
62 'async_comma': ', ' if async_ and function['sig_in'] else '', 64 if not self.args.debug
63 'async_args_def': 'callback=None, errback=None' if async_ else '', 65 else 'log.debug ("%s")\n%s' % (completion["name"], 8 * " "),
64 'async_args_call': 'callback=callback, errback=errback' if async_ else '', 66 "args_result": self.getArguments(
65 'async_callbacks': "('callback', 'errback')" if async_ else "None", 67 function["sig_in"], name=arg_doc, unicode_protect=self.args.unicode
66 'category': completion['category'].upper(), 68 ),
67 }) 69 "async_comma": ", " if async_ and function["sig_in"] else "",
70 "async_args_def": "callback=None, errback=None" if async_ else "",
71 "async_args_call": "callback=callback, errback=errback" if async_ else "",
72 "async_callbacks": "('callback', 'errback')" if async_ else "None",
73 "category": completion["category"].upper(),
74 }
75 )
68 76
69 def frontend_completion_method(self, completion, function, default, arg_doc, async_): 77 def frontend_completion_method(self, completion, function, default, arg_doc, async_):
70 completion.update({ 78 completion.update(
71 # XXX: we can manage blocking call in the same way as async one: if callback is None the call will be blocking 79 {
72 'debug': "" if not self.args.debug else 'log.debug ("%s")\n%s' % (completion['name'], 8 * ' '), 80 # XXX: we can manage blocking call in the same way as async one: if callback is None the call will be blocking
73 'args_result': self.getArguments(function['sig_in'], name=arg_doc), 81 "debug": ""
74 'async_args': 'callback=None, errback=None', 82 if not self.args.debug
75 'async_comma': ', ' if function['sig_in'] else '', 83 else 'log.debug ("%s")\n%s' % (completion["name"], 8 * " "),
76 'error_handler': """if callback is None: 84 "args_result": self.getArguments(function["sig_in"], name=arg_doc),
85 "async_args": "callback=None, errback=None",
86 "async_comma": ", " if function["sig_in"] else "",
87 "error_handler": """if callback is None:
77 error_handler = None 88 error_handler = None
78 else: 89 else:
79 if errback is None: 90 if errback is None:
80 errback = log.error 91 errback = log.error
81 error_handler = lambda err:errback(dbus_to_bridge_exception(err)) 92 error_handler = lambda err:errback(dbus_to_bridge_exception(err))
82 """, 93 """,
83 }) 94 }
95 )
84 if async_: 96 if async_:
85 completion['blocking_call'] = '' 97 completion["blocking_call"] = ""
86 completion['async_args_result'] = 'timeout=const_TIMEOUT, reply_handler=callback, error_handler=error_handler' 98 completion[
99 "async_args_result"
100 ] = "timeout=const_TIMEOUT, reply_handler=callback, error_handler=error_handler"
87 else: 101 else:
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 102 # 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
89 completion['blocking_call'] = """kwargs={} 103 completion[
104 "blocking_call"
105 ] = """kwargs={}
90 if callback is not None: 106 if callback is not None:
91 kwargs['timeout'] = const_TIMEOUT 107 kwargs['timeout'] = const_TIMEOUT
92 kwargs['reply_handler'] = callback 108 kwargs['reply_handler'] = callback
93 kwargs['error_handler'] = error_handler 109 kwargs['error_handler'] = error_handler
94 """ 110 """
95 completion['async_args_result'] = '**kwargs' 111 completion["async_args_result"] = "**kwargs"
96 result = "self.db_%(category)s_iface.%(name)s(%(args_result)s%(async_comma)s%(async_args_result)s)" % completion 112 result = (
97 completion['result'] = ("unicode(%s)" if self.args.unicode and function['sig_out'] == 's' else "%s") % result 113 "self.db_%(category)s_iface.%(name)s(%(args_result)s%(async_comma)s%(async_args_result)s)"
114 % completion
115 )
116 completion["result"] = (
117 "unicode(%s)" if self.args.unicode and function["sig_out"] == "s" else "%s"
118 ) % result