From: Menno Smits Date: Tue, 8 Nov 2011 20:32:59 +0000 (+0000) Subject: NOOP support (#74) X-Git-Tag: 0.8~16 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=b3e613e937be42a34d3d597f64566c031986f06b;p=imapclient.git NOOP support (#74) Untagged response handling shared with IDLE. --- diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 664b7e7..d65c688 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -316,6 +316,25 @@ class IMAPClient(object): out[key] = value return out + def noop(self): + """Execute the NOOP command. + + This command returns immediately, returning any server side + status updates. It can also be used to reset any auto-logout + timers. + + The return value is the server command response message + followed by a list of status responses. For example:: + + ('NOOP completed.', + [(4, 'EXISTS'), + (3, 'FETCH', ('FLAGS', ('bar', 'sne'))), + (6, 'FETCH', ('FLAGS', ('sne',)))]) + + """ + tag = self._imap._command('NOOP') + return self._consume_until_tagged_response(tag, 'NOOP') + def idle(self): """Put the server into IDLE mode. @@ -372,7 +391,7 @@ class IMAPClient(object): except (socket.timeout, socket.error): break else: - resps.append(_parse_idle_response(line)) + resps.append(_parse_untagged_response(line)) return resps finally: sock.setblocking(1) @@ -392,19 +411,7 @@ class IMAPClient(object): ``idle_check()``. """ self._imap.send('DONE\r\n') - # Slurp up any remaining IDLE responses until the IDLE is done - tag = self._idle_tag - tagged_commands = self._imap.tagged_commands - resps = [] - while True: - line = self._imap._get_response() - if tagged_commands[tag]: - break - resps.append(_parse_idle_response(line)) - self._idle_tag = None - typ, data = tagged_commands.pop(tag) - self._checkok('idle', typ, data) - return data[0], resps + return self._consume_until_tagged_response(self._idle_tag, 'IDLE') def folder_status(self, folder, what=None): """Return the status of *folder*. @@ -753,6 +760,18 @@ class IMAPClient(object): if typ != expected: raise self.Error('%s failed: %r' % (command, data[0])) + def _consume_until_tagged_response(self, tag, command): + tagged_commands = self._imap.tagged_commands + resps = [] + while True: + line = self._imap._get_response() + if tagged_commands[tag]: + break + resps.append(_parse_untagged_response(line)) + typ, data = tagged_commands.pop(tag) + self._checkok(command, typ, data) + return data[0], resps + def _checkok(self, command, typ, data): self._check_resp('OK', command, typ, data) @@ -858,7 +877,7 @@ def _quote_arg(arg): return '"%s"' % arg -def _parse_idle_response(text): +def _parse_untagged_response(text): assert text.startswith('* ') text = text[2:] if text.startswith(('OK ', 'NO ')): diff --git a/imapclient/test/test_IMAPClient.py b/imapclient/test/test_IMAPClient.py index da6fbf7..1367437 100644 --- a/imapclient/test/test_IMAPClient.py +++ b/imapclient/test/test_IMAPClient.py @@ -168,7 +168,7 @@ class TestAclMethods(IMAPClientTest): self.assertSequenceEqual(acl, [('Fred', 'rwipslda'), ('Sally', 'rwip')]) -class TestIdle(IMAPClientTest): +class TestIdleAndNoop(IMAPClientTest): def test_idle(self): self.client._imap._command.return_value = sentinel.tag @@ -241,23 +241,50 @@ class TestIdle(IMAPClientTest): def test_idle_done(self): self.client._idle_tag = sentinel.tag - self.client._imap.tagged_commands = {sentinel.tag: None} + + mockSend = Mock() + self.client._imap.send = mockSend + mockConsume = Mock(return_value=sentinel.out) + self.client._consume_until_tagged_response = mockConsume + + result = self.client.idle_done() + + mockSend.assert_called_with('DONE\r\n') + mockConsume.assert_called_with(sentinel.tag, 'IDLE') + self.assertEquals(result, sentinel.out) + + def test_noop(self): + mockCommand = Mock(return_value=sentinel.tag) + self.client._imap._command = mockCommand + mockConsume = Mock(return_value=sentinel.out) + self.client._consume_until_tagged_response = mockConsume + + result = self.client.noop() + + mockCommand.assert_called_with('NOOP') + mockConsume.assert_called_with(sentinel.tag, 'NOOP') + self.assertEquals(result, sentinel.out) + + def test_consume_until_tagged_response(self): + client = self.client + client._imap.tagged_commands = {sentinel.tag: None} counter = itertools.count() def fake_get_response(): count = counter.next() if count == 0: return '* 99 EXISTS' - self.client._imap.tagged_commands[sentinel.tag] = ('OK', ['Idle done']) - self.client._imap._get_response = fake_get_response + client._imap.tagged_commands[sentinel.tag] = ('OK', ['Idle done']) + client._imap._get_response = fake_get_response - text, responses = self.client.idle_done() - - self.assertEqual(self.client._imap.tagged_commands, {}) + text, responses = client._consume_until_tagged_response(sentinel.tag, + 'IDLE') + self.assertEqual(client._imap.tagged_commands, {}) self.assertEqual(text, 'Idle done') self.assertListEqual([(99, 'EXISTS')], responses) + class TestDebugLogging(IMAPClientTest): def test_default_is_stderr(self): diff --git a/livetest.py b/livetest.py index 43ce3cd..852a1f7 100755 --- a/livetest.py +++ b/livetest.py @@ -506,6 +506,26 @@ def createLiveTestClass(conf, use_uid): self.assertTrue(isinstance(text, str)) self.assertGreater(len(text), 0) + def test_noop(self): + self.client.select_folder('INBOX') + + # Initially there should be no responses + text, resps = self.client.noop() + self.assertTrue(isinstance(text, str)) + self.assertGreater(len(text), 0) + self.assertEquals(resps, []) + + # Start a new connection and upload a new message + client2 = create_client_from_config(conf) + client2.select_folder('INBOX') + client2.append('INBOX', SIMPLE_MESSAGE) + + # Check for this addition in the NOOP data + msg, resps = self.client.noop() + self.assertTrue(isinstance(text, str)) + self.assertGreater(len(text), 0) + self.assertTrue(isinstance(resps, list)) + self.assertIn((1, 'EXISTS'), resps) return LiveTest