Mercurial > libervia-backend
annotate src/plugins/plugin_adhoc_dbus.py @ 918:1597fb8b9108
bridge: remove unecessary signals/methods declarations
author | souliane <souliane@mailoo.org> |
---|---|
date | Fri, 21 Mar 2014 09:09:51 +0100 |
parents | 1a759096ccbd |
children | 301b342c697a |
rev | line source |
---|---|
822 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin for adding D-Bus to Ad-Hoc Commands | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from sat.core.i18n import _, D_ | |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
822
diff
changeset
|
21 from sat.core.constants import Const as C |
822 | 22 from logging import debug, info, warning, error |
23 from twisted.words.protocols.jabber import jid | |
24 from twisted.internet import defer, reactor | |
25 from wokkel import data_form | |
26 from lxml import etree | |
27 from os import path | |
28 import uuid | |
29 import dbus | |
30 from dbus.mainloop.glib import DBusGMainLoop | |
31 DBusGMainLoop(set_as_default=True) | |
32 | |
33 FD_NAME = "org.freedesktop.DBus" | |
34 FD_PATH = "/org/freedekstop/DBus" | |
35 INTROSPECT_IFACE = "org.freedesktop.DBus.Introspectable" | |
36 | |
37 INTROSPECT_METHOD = "Introspect" | |
38 IGNORED_IFACES_START = ('org.freedesktop', 'org.qtproject', 'org.kde.KMainWindow') # commands in interface starting with these values will be ignored | |
39 FLAG_LOOP = 'LOOP' | |
40 | |
41 PLUGIN_INFO = { | |
42 "name": "Ad-Hoc Commands - D-Bus", | |
43 "import_name": "AD_HOC_DBUS", | |
44 "type": "Misc", | |
45 "protocols": [], | |
46 "dependencies": ["XEP-0050"], | |
47 "main": "AdHocDBus", | |
48 "handler": "no", | |
49 "description": _("""Add D-Bus management to Ad-Hoc commands""") | |
50 } | |
51 | |
52 | |
53 class AdHocDBus(object): | |
54 | |
55 def __init__(self, host): | |
56 info(_("plugin Ad-Hoc D-Bus initialization")) | |
57 self.host = host | |
58 host.bridge.addMethod("adHocDBusAddAuto", ".plugin", in_sign='sasasasasasass', out_sign='(sa(sss))', | |
59 method=self._adHocDBusAddAuto, | |
60 async=True) | |
61 self.session_bus = dbus.SessionBus() | |
62 self.fd_object = self.session_bus.get_object(FD_NAME, FD_PATH, introspect=False) | |
63 self.XEP_0050 = host.plugins['XEP-0050'] | |
64 | |
65 def _DBusAsyncCall(self, proxy, method, *args, **kwargs): | |
66 """ Call a DBus method asynchronously and return a deferred | |
67 @param proxy: DBus object proxy, as returner by get_object | |
68 @param method: name of the method to call | |
69 @param args: will be transmitted to the method | |
70 @param kwargs: will be transmetted to the method, except for the following poped values: | |
71 - interface: name of the interface to use | |
72 @return: a deferred | |
73 | |
74 """ | |
75 d = defer.Deferred() | |
76 interface = kwargs.pop('interface', None) | |
77 kwargs['reply_handler'] = lambda ret=None: d.callback(ret) | |
78 kwargs['error_handler'] = d.errback | |
79 proxy.get_dbus_method(method, dbus_interface=interface)(*args, **kwargs) | |
80 return d | |
81 | |
82 def _DBusListNames(self): | |
83 return self._DBusAsyncCall(self.fd_object, "ListNames") | |
84 | |
85 def _DBusIntrospect(self, proxy): | |
86 return self._DBusAsyncCall(proxy, INTROSPECT_METHOD, interface=INTROSPECT_IFACE) | |
87 | |
88 def _acceptMethod(self, method): | |
89 """ Return True if we accept the method for a command | |
90 @param method: etree.Element | |
91 @return: True if the method is acceptable | |
92 | |
93 """ | |
94 if method.xpath("arg[@direction='in']"): # we don't accept method with argument for the moment | |
95 return False | |
96 return True | |
97 | |
98 @defer.inlineCallbacks | |
99 def _introspect(self, methods, bus_name, proxy): | |
100 debug("introspecting path [%s]" % proxy.object_path) | |
101 introspect_xml = yield self._DBusIntrospect(proxy) | |
102 el = etree.fromstring(introspect_xml) | |
103 for node in el.iterchildren('node', 'interface'): | |
104 if node.tag == 'node': | |
105 new_path = path.join(proxy.object_path, node.get('name')) | |
106 new_proxy = self.session_bus.get_object(bus_name, new_path, introspect=False) | |
107 yield self._introspect(methods, bus_name, new_proxy) | |
108 elif node.tag == 'interface': | |
109 name = node.get('name') | |
110 if any(name.startswith(ignored) for ignored in IGNORED_IFACES_START): | |
111 debug('interface [%s] is ignored' % name) | |
112 continue | |
113 debug("introspecting interface [%s]" % name) | |
114 for method in node.iterchildren('method'): | |
115 if self._acceptMethod(method): | |
116 method_name = method.get('name') | |
117 debug("method accepted: [%s]" % method_name) | |
118 methods.add((proxy.object_path, name, method_name)) | |
119 | |
120 def _adHocDBusAddAuto(self, prog_name, allowed_jids, allowed_groups, allowed_magics, forbidden_jids, forbidden_groups, flags, profile_key): | |
121 return self.adHocDBusAddAuto(prog_name, allowed_jids, allowed_groups, allowed_magics, forbidden_jids, forbidden_groups, flags, profile_key) | |
122 | |
123 @defer.inlineCallbacks | |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
822
diff
changeset
|
124 def adHocDBusAddAuto(self, prog_name, allowed_jids=None, allowed_groups=None, allowed_magics=None, forbidden_jids=None, forbidden_groups=None, flags=None, profile_key=C.PROF_KEY_NONE): |
822 | 125 bus_names = yield self._DBusListNames() |
126 bus_names = [bus_name for bus_name in bus_names if '.' + prog_name in bus_name] | |
127 if not bus_names: | |
128 info("Can't find any bus for [%s]" % prog_name) | |
129 return | |
130 bus_names.sort() | |
131 for bus_name in bus_names: | |
132 if bus_name.endswith(prog_name): | |
133 break | |
134 info("bus name found: [%s]" % bus_name) | |
135 proxy = self.session_bus.get_object(bus_name, '/', introspect=False) | |
136 methods = set() | |
137 | |
138 yield self._introspect(methods, bus_name, proxy) | |
139 | |
140 if methods: | |
141 self._addCommand(prog_name, bus_name, methods, | |
142 allowed_jids = allowed_jids, | |
143 allowed_groups = allowed_groups, | |
144 allowed_magics = allowed_magics, | |
145 forbidden_jids = forbidden_jids, | |
146 forbidden_groups = forbidden_groups, | |
147 flags = flags, | |
148 profile_key = profile_key) | |
149 | |
150 defer.returnValue((bus_name, methods)) | |
151 | |
152 | |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
822
diff
changeset
|
153 def _addCommand(self, adhoc_name, bus_name, methods, allowed_jids=None, allowed_groups=None, allowed_magics=None, forbidden_jids=None, forbidden_groups=None, flags=None, profile_key=C.PROF_KEY_NONE): |
822 | 154 if flags is None: |
155 flags = set() | |
156 | |
157 def DBusCallback(command_elt, session_data, action, node, profile): | |
158 actions = session_data.setdefault('actions',[]) | |
159 names_map = session_data.setdefault('names_map', {}) | |
160 actions.append(action) | |
161 | |
162 if len(actions) == 1: | |
163 # it's our first request, we ask the desired new status | |
164 status = self.XEP_0050.STATUS.EXECUTING | |
165 form = data_form.Form('form', title=_('Command selection')) | |
166 options = [] | |
167 for path, iface, command in methods: | |
168 label = command.rsplit('.',1)[-1] | |
169 name = str(uuid.uuid4()) | |
170 names_map[name] = (path, iface, command) | |
171 options.append(data_form.Option(name, label)) | |
172 | |
173 field = data_form.Field('list-single', 'command', options=options, required=True) | |
174 form.addField(field) | |
175 | |
176 payload = form.toElement() | |
177 note = None | |
178 | |
179 elif len(actions) == 2: | |
180 # we should have the answer here | |
181 try: | |
182 x_elt = command_elt.elements(data_form.NS_X_DATA,'x').next() | |
183 answer_form = data_form.Form.fromElement(x_elt) | |
184 command = answer_form['command'] | |
185 except KeyError, StopIteration: | |
186 raise self.XEP_0050.AdHocError(self.XEP_0050.ERROR.BAD_PAYLOAD) | |
187 | |
188 if command not in names_map: | |
189 raise self.XEP_0050.AdHocError(self.XEP_0050.ERROR.BAD_PAYLOAD) | |
190 | |
191 path, iface, command = names_map[command] | |
192 proxy = self.session_bus.get_object(bus_name, path) | |
193 | |
194 d = self._DBusAsyncCall(proxy, command, interface=iface) | |
195 | |
196 # job done, we can end the session, except if we have FLAG_LOOP | |
197 if FLAG_LOOP in flags: | |
198 # We have a loop, so we clear everything and we execute again the command as we had a first call (command_elt is not used, so None is OK) | |
199 del actions[:] | |
200 names_map.clear() | |
201 return DBusCallback(None, session_data, self.XEP_0050.ACTION.EXECUTE, node, profile) | |
202 form = data_form.Form('form', title=_(u'Updated')) | |
203 form.addField(data_form.Field('fixed', u'Command sent')) | |
204 status = self.XEP_0050.STATUS.COMPLETED | |
205 payload = None | |
206 note = (self.XEP_0050.NOTE.INFO, _(u"Command sent")) | |
207 else: | |
208 raise self.XEP_0050.AdHocError(self.XEP_0050.ERROR.INTERNAL) | |
209 | |
210 return (payload, status, None, note) | |
211 | |
212 self.XEP_0050.addAdHocCommand(DBusCallback, adhoc_name, | |
213 allowed_jids = allowed_jids, | |
214 allowed_groups = allowed_groups, | |
215 allowed_magics = allowed_magics, | |
216 forbidden_jids = forbidden_jids, | |
217 forbidden_groups = forbidden_groups, | |
218 profile_key = profile_key) |