From 0dcfcf2057ceb5eca76382bc232ccc4bc267e33f Mon Sep 17 00:00:00 2001 From: Kevin Xiwei Zheng Date: Tue, 4 Aug 2015 19:13:52 -0400 Subject: [PATCH] Refactor and fix command buffering It's still a bit hairy and needs better tests. --- omnipresence/connection.py | 10 ++++------ omnipresence/plugins/more/__init__.py | 7 +++++-- omnipresence/plugins/more/test_more.py | 2 +- omnipresence/test/test_command.py | 1 - 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/omnipresence/connection.py b/omnipresence/connection.py index 320be79..e525b63 100644 --- a/omnipresence/connection.py +++ b/omnipresence/connection.py @@ -418,7 +418,7 @@ def respond_to(self, msg): deferred = maybeDeferred(plugin.respond_to, msg) if msg.action == 'command': deferred.addCallback(self.buffer_reply, msg) - deferred.addCallback(self.reply_from_buffer, msg) + deferred.addCallback(lambda _: self.reply_from_buffer(msg)) deferred.addErrback(self.reply_from_error, msg) else: deferred.addErrback(log.err, @@ -463,7 +463,6 @@ def buffer_reply(self, response, request): raise TypeError('invalid command reply type ' + type(response).__name__) self.message_buffers[venue][request.actor.nick] = buf - return request.actor.nick def copy_buffer(self, venue, source, target): """Copy a reply buffer from the *source* nick to the *target* @@ -481,15 +480,14 @@ def copy_buffer(self, venue, source, target): self.message_buffers[venue][target] = two return two - def reply_from_buffer(self, nick, request, reply_when_empty=False): + def reply_from_buffer(self, request, reply_when_empty=False): """Call `.reply` with the next reply from the reply buffer - belonging to *nick* in the `~.Message.venue` of the invocation - `~.Message` *request*. Return a + corresponding to the invocation `~.Message` *request*. Return a `~twisted.internet.defer.Deferred` yielding either the reply's contents, or `None` if no reply was made because of an empty reply buffer.""" venue = PRIVATE_CHANNEL if request.private else request.venue - buf = self.copy_buffer(venue, nick, request.actor.nick) + buf = self.message_buffers[venue].get(request.actor.nick, []) if isinstance(buf, collections.Sequence): next_reply = None if buf: diff --git a/omnipresence/plugins/more/__init__.py b/omnipresence/plugins/more/__init__.py index 2c7d6b3..1cebea0 100644 --- a/omnipresence/plugins/more/__init__.py +++ b/omnipresence/plugins/more/__init__.py @@ -4,12 +4,15 @@ from ...message import collapse from ...plugin import EventPlugin +from ...settings import PRIVATE_CHANNEL class Default(EventPlugin): def on_command(self, msg): - msg.connection.reply_from_buffer(msg.content or msg.actor.nick, - msg, reply_when_empty=True) + venue = PRIVATE_CHANNEL if msg.private else msg.venue + response = msg.connection.copy_buffer( + venue, msg.content or msg.actor.nick, msg.actor.nick) + return response or u'No text in buffer.' def on_cmdhelp(self, msg): return collapse("""\ diff --git a/omnipresence/plugins/more/test_more.py b/omnipresence/plugins/more/test_more.py index 35e8cf6..458facb 100644 --- a/omnipresence/plugins/more/test_more.py +++ b/omnipresence/plugins/more/test_more.py @@ -22,7 +22,7 @@ def setUp(self): def assert_reply(self, content, expected, **kwargs): kwargs.setdefault('venue', '#foo') msg = self.command_message(content, **kwargs) - self.command.respond_to(msg) + self.connection.respond_to(msg) self.assertEqual(self.outgoing.last_seen.content.split(': ', 1)[-1], expected) diff --git a/omnipresence/test/test_command.py b/omnipresence/test/test_command.py index 93a57eb..36f8fda 100644 --- a/omnipresence/test/test_command.py +++ b/omnipresence/test/test_command.py @@ -25,7 +25,6 @@ def setUp(self): def more(self, **kwargs): return self.connection.reply_from_buffer( - self.other_user.nick, self.command_message( '', subaction='more', target=self.other_user.nick, **kwargs), reply_when_empty=True)