From eac5ad50a8ff47b24d74bdb3c878c171e37fceed Mon Sep 17 00:00:00 2001 From: Nicoco K Date: Tue, 9 Mar 2021 19:24:43 +0100 Subject: XEP-0050 Make handle_command_xxx async --- slixmpp/plugins/xep_0050/adhoc.py | 42 ++-- tests/test_stream_xep_0050.py | 448 +++++++++++++++++++++++++++++++++++++- 2 files changed, 471 insertions(+), 19 deletions(-) diff --git a/slixmpp/plugins/xep_0050/adhoc.py b/slixmpp/plugins/xep_0050/adhoc.py index 5f3bc81c..072ec5aa 100644 --- a/slixmpp/plugins/xep_0050/adhoc.py +++ b/slixmpp/plugins/xep_0050/adhoc.py @@ -3,6 +3,7 @@ # Copyright (C) 2011 Nathanael C. Fritz, Lance J.T. Stout # This file is part of Slixmpp. # See the file LICENSE for copying permission. +import asyncio import logging import time @@ -164,25 +165,25 @@ class XEP_0050(BasePlugin): self.xmpp.event('command', iq) self.xmpp.event('command_%s' % iq['command']['action'], iq) - def _handle_command_all(self, iq: Iq) -> None: + async def _handle_command_all(self, iq: Iq) -> None: action = iq['command']['action'] sessionid = iq['command']['sessionid'] session = self.sessions.get(sessionid) if session is None: - return self._handle_command_start(iq) + return await self._handle_command_start(iq) if action in ('next', 'execute'): - return self._handle_command_next(iq) + return await self._handle_command_next(iq) if action == 'prev': - return self._handle_command_prev(iq) + return await self._handle_command_prev(iq) if action == 'complete': - return self._handle_command_complete(iq) + return await self._handle_command_complete(iq) if action == 'cancel': - return self._handle_command_cancel(iq) + return await self._handle_command_cancel(iq) return None - def _handle_command_start(self, iq): + async def _handle_command_start(self, iq): """ Process an initial request to execute a command. @@ -222,11 +223,11 @@ class XEP_0050(BasePlugin): 'prev': None, 'cancel': None} - session = handler(iq, initial_session) + session = await _await_if_needed(handler, iq, initial_session) self._process_command_response(iq, session) - def _handle_command_next(self, iq): + async def _handle_command_next(self, iq): """ Process a request for the next step in the workflow for a command with multiple steps. @@ -246,13 +247,13 @@ class XEP_0050(BasePlugin): if len(results) == 1: results = results[0] - session = handler(results, session) + session = await _await_if_needed(handler, results, session) self._process_command_response(iq, session) else: raise XMPPError('item-not-found') - def _handle_command_prev(self, iq): + async def _handle_command_prev(self, iq): """ Process a request for the prev step in the workflow for a command with multiple steps. @@ -272,7 +273,7 @@ class XEP_0050(BasePlugin): if len(results) == 1: results = results[0] - session = handler(results, session) + session = await _await_if_needed(handler, results, session) self._process_command_response(iq, session) else: @@ -334,7 +335,7 @@ class XEP_0050(BasePlugin): iq.send() - def _handle_command_cancel(self, iq): + async def _handle_command_cancel(self, iq): """ Process a request to cancel a command's execution. @@ -348,7 +349,7 @@ class XEP_0050(BasePlugin): if session: handler = session['cancel'] if handler: - handler(iq, session) + await _await_if_needed(handler, iq, session) del self.sessions[sessionid] iq = iq.reply() iq['command']['node'] = node @@ -360,7 +361,7 @@ class XEP_0050(BasePlugin): raise XMPPError('item-not-found') - def _handle_command_complete(self, iq): + async def _handle_command_complete(self, iq): """ Process a request to finish the execution of command and terminate the workflow. @@ -385,7 +386,7 @@ class XEP_0050(BasePlugin): results = results[0] if handler: - handler(results, session) + await _await_if_needed(handler, results, session) del self.sessions[sessionid] @@ -616,3 +617,12 @@ class XEP_0050(BasePlugin): if iq['command']['status'] == 'completed': self.terminate_command(session) + + +async def _await_if_needed(handler, *args): + if asyncio.iscoroutinefunction(handler): + log.debug(f"%s is async", handler) + return await handler(*args) + else: + log.debug(f"%s is sync", handler) + return handler(*args) diff --git a/tests/test_stream_xep_0050.py b/tests/test_stream_xep_0050.py index 37cd233d..519c860e 100644 --- a/tests/test_stream_xep_0050.py +++ b/tests/test_stream_xep_0050.py @@ -47,7 +47,6 @@ class TestAdHocCommands(SlixTest): session['has_next'] = False return session - self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', 'Do Foo', handle_command) @@ -418,8 +417,6 @@ class TestAdHocCommands(SlixTest): """) - - def testMultiPayloads(self): """Test using commands with multiple payloads.""" results = [] @@ -519,6 +516,451 @@ class TestAdHocCommands(SlixTest): self.assertEqual(results, [['form_1'], ['form_2']], "Command handler was not executed: %s" % results) + def testZeroStepCommandAsync(self): + """Test running a command with no steps.""" + + async def handle_command(iq, session): + form = self.xmpp['xep_0004'].make_form(ftype='result') + form.addField(var='foo', ftype='text-single', + label='Foo', value='bar') + + session['payload'] = form + session['next'] = None + session['has_next'] = False + + return session + + self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', + 'Do Foo', handle_command) + + self.recv(""" + + + + """) + + self.send(""" + + + + + bar + + + + + """) + + def testOneStepCommandAsync(self): + """Test running a single step command.""" + results = [] + + async def handle_command(iq, session): + + async def handle_form(form, session): + results.append(form.get_values()['foo']) + session['payload'] = None + + form = self.xmpp['xep_0004'].make_form('form') + form.addField(var='foo', ftype='text-single', label='Foo') + + session['payload'] = form + session['next'] = handle_form + session['has_next'] = False + + return session + + self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', + 'Do Foo', handle_command) + + self.recv(""" + + + + """) + + self.send(""" + + + + + + + + + + + """) + + self.recv(""" + + + + + blah + + + + + """) + + self.send(""" + + + + """) + + self.assertEqual(results, ['blah'], + "Command handler was not executed: %s" % results) + + def testTwoStepCommandAsync(self): + """Test using a two-stage command.""" + results = [] + + async def handle_command(iq, session): + + async def handle_step2(form, session): + results.append(form.get_values()['bar']) + session['payload'] = None + + async def handle_step1(form, session): + results.append(form.get_values()['foo']) + + form = self.xmpp['xep_0004'].make_form('form') + form.addField(var='bar', ftype='text-single', label='Bar') + + session['payload'] = form + session['next'] = handle_step2 + session['has_next'] = False + + return session + + form = self.xmpp['xep_0004'].make_form('form') + form.addField(var='foo', ftype='text-single', label='Foo') + + session['payload'] = form + session['next'] = handle_step1 + session['has_next'] = True + + return session + + self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', + 'Do Foo', handle_command) + + self.recv(""" + + + + """) + + self.send(""" + + + + + + + + + + + """) + + self.recv(""" + + + + + blah + + + + + """) + + self.send(""" + + + + + + + + + + + """) + + self.recv(""" + + + + + meh + + + + + """) + self.send(""" + + + + """) + + self.assertEqual(results, ['blah', 'meh'], + "Command handler was not executed: %s" % results) + + def testCancelCommandAsync(self): + """Test canceling command.""" + results = [] + + async def handle_command(iq, session): + + async def handle_form(form, session): + results.append(form['values']['foo']) + + async def handle_cancel(iq, session): + results.append('canceled') + + form = self.xmpp['xep_0004'].make_form('form') + form.addField(var='foo', ftype='text-single', label='Foo') + + session['payload'] = form + session['next'] = handle_form + session['cancel'] = handle_cancel + session['has_next'] = False + + return session + + self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', + 'Do Foo', handle_command) + + self.recv(""" + + + + """) + + self.send(""" + + + + + + + + + + + """) + + self.recv(""" + + + + + blah + + + + + """) + + self.send(""" + + + + """) + + self.assertEqual(results, ['canceled'], + "Cancelation handler not executed: %s" % results) + + def testCommandNoteAsync(self): + """Test adding notes to commands.""" + + async def handle_command(iq, session): + form = self.xmpp['xep_0004'].make_form(ftype='result') + form.addField(var='foo', ftype='text-single', + label='Foo', value='bar') + + session['payload'] = form + session['next'] = None + session['has_next'] = False + session['notes'] = [('info', 'testing notes')] + + return session + + self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', + 'Do Foo', handle_command) + + self.recv(""" + + + + """) + + self.send(""" + + + testing notes + + + bar + + + + + """) + + def testMultiPayloadsAsync(self): + """Test using commands with multiple payloads.""" + results = [] + + async def handle_command(iq, session): + + async def handle_form(forms, session): + for form in forms: + results.append(form.get_values()['FORM_TYPE']) + session['payload'] = None + + form1 = self.xmpp['xep_0004'].make_form('form') + form1.addField(var='FORM_TYPE', ftype='hidden', value='form_1') + form1.addField(var='foo', ftype='text-single', label='Foo') + + form2 = self.xmpp['xep_0004'].make_form('form') + form2.addField(var='FORM_TYPE', ftype='hidden', value='form_2') + form2.addField(var='foo', ftype='text-single', label='Foo') + + session['payload'] = [form1, form2] + session['next'] = handle_form + session['has_next'] = False + + return session + + self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', + 'Do Foo', handle_command) + + self.recv(""" + + + + """) + + self.send(""" + + + + + + + + form_1 + + + + + + form_2 + + + + + + """) + + self.recv(""" + + + + + form_1 + + + bar + + + + + form_2 + + + bar + + + + + """) + + self.send(""" + + + + """) + + self.assertEqual(results, [['form_1'], ['form_2']], + "Command handler was not executed: %s" % results) + def testClientAPI(self): """Test using client-side API for commands.""" results = [] -- cgit v1.2.3