Skip to content
This repository has been archived by the owner on Apr 19, 2019. It is now read-only.

Commit

Permalink
Refactor and fix command buffering
Browse files Browse the repository at this point in the history
It's still a bit hairy and needs better tests.
  • Loading branch information
kxz committed Aug 4, 2015
1 parent 85853be commit 0dcfcf2
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
10 changes: 4 additions & 6 deletions omnipresence/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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*
Expand All @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions omnipresence/plugins/more/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("""\
Expand Down
2 changes: 1 addition & 1 deletion omnipresence/plugins/more/test_more.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 0 additions & 1 deletion omnipresence/test/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0dcfcf2

Please sign in to comment.