annotate src/plugins/plugin_adhoc_dbus.py @ 2331:01877048c94a

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