Permalink
Browse files

Added restart method to runner. Added unit tests to talkbackbot. Adde…

…d Mock and coverage dependencies. Fixed a few malformed quotes.
  • Loading branch information...
1 parent 0acd90f commit 8abb9b4b64d4fada56bce55b06d9381d9638fb06 @jessamynsmith committed Mar 27, 2012
Showing with 99 additions and 25 deletions.
  1. +6 −2 bot_runner.py
  2. +1 −3 quotes.txt
  3. +2 −0 requirements.txt
  4. +3 −0 talkbackbot
  5. +23 −20 talkbackbot.py
  6. +1 −0 tests/test_quote.txt
  7. +19 −0 tests/test_settings.py
  8. +44 −0 tests/test_talkbackbot.py
View
8 bot_runner.py
@@ -14,7 +14,7 @@ def __init__(self, pid_file, bot_factory):
self.factory = bot_factory
def run(self):
- factory = self.factory(settings.CHANNEL)
+ factory = self.factory(settings)
if settings.USE_SSL:
reactor.connectSSL(settings.HOST, settings.PORT, factory,
ssl.ClientContextFactory())
@@ -43,4 +43,8 @@ def stop(self):
pid = pid_file.read()
pid_file.close()
os.kill(int(pid), signal.SIGHUP)
- os.remove(self.pid_file)
+ os.remove(self.pid_file)
+
+ def restart(self):
+ self.stop()
+ self.start()
View
4 quotes.txt
@@ -25,7 +25,7 @@ Men of sense in all ages abhor those customs which treat us only as the vassals
The only chance for much intellectual improvement in the female sex, was to be found in the families of the educated class and in occasional intercourse with the learned. (1817) ~ Abigail Adams
I regret the trifling narrow contracted education of the females of my own country. ~ Abigail Adams
The natural tenderness and delicacy of our constitution, added to the many dangers we are subject to from your sex, renders it almost impossible for a single lady to travel without injury to her character. And those who have a protector in a husband have, generally speaking, obstacles to prevent their roving. ~ Abigail Adams
-If much depends as is allowed upon the early Education of youth and the first principals which are instill'd take the deepest root, great benefit must arise from literary accomplishments in women. ~ Abigail Adams
+If much depends as is allowed upon the early Education of youth and the first principles which are instill'd take the deepest root, great benefit must arise from literary accomplishments in women. ~ Abigail Adams
These are times in which a genius would wish to live. It is not in the still calm of life, or the repose of a pacific station, that great characters are formed. ~ Abigail Adams
To be good, and do good, is the whole duty of man comprised in a few words. ~ Abigail Adams
Learning is not to be attained by chance, it must be sought with ardor and attended to with diligence. ~ Abigail Adams
@@ -724,10 +724,8 @@ Take away the violence and who will hear the men of peace? ~ Lorraine Hansberry
Seems like God don't see fit to give the black man nothing but dreams -- but He did give us children to make them dreams seem worthwhile. ~ Lorraine Hansberry
If you worry about who is going to get credit, you don't get much work done. ~ Dorothy Height
Greatness is not measured by what a man or woman accomplishes, but by the opposition he or she has overcome to reach his goals. ~ Dorothy Height
-I was inspired by ~ Dorothy Height
I want to be remembered as someone who used herself and anything she could touch to work for justice and freedom.... I want to be remembered as one who tried. ~ Dorothy Height
A Negro woman has the same kind of problems as other women, but she can't take the same things for granted. ~ Dorothy Height
- ~ Dorothy Height
It was not easy for those of us who had become symbols of the struggle for equality to see our children raising their fists in defiant contradiction of all we had fought for. ~ Dorothy Height
No one will do for you what you need to do for yourself. We cannot afford to be separate. ~ Dorothy Height
We have to see that all of us are in the same boat. ~ Dorothy Height
View
2 requirements.txt
@@ -1,3 +1,5 @@
+coverage
+Mock
nose
pyopenssl
twisted
View
3 talkbackbot
@@ -1,10 +1,13 @@
#!./tbenv/bin/python
+import logging
import sys
from bot_runner import BotRunner
import settings
from talkbackbot import TalkBackBotFactory
+logging.basicConfig(filename=settings.LOG_FILE, level=logging.DEBUG)
+
if __name__ == '__main__':
command = ""
View
43 talkbackbot.py
@@ -4,16 +4,9 @@
from twisted.internet import reactor, protocol
from quotation_selector import QuotationSelector
-import settings
-
-
-logging.basicConfig(filename=settings.LOG_FILE, level=logging.DEBUG)
class TalkBackBot(irc.IRCClient):
- password = settings.PASSWORD
- nickname = settings.NICKNAME
- realname = settings.REALNAME
def connectionMade(self):
irc.IRCClient.connectionMade(self)
@@ -37,29 +30,39 @@ def joined(self, channel):
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
-
+
trigger_found = False
- for trigger in settings.TRIGGERS:
- if msg.lower().find(trigger) >= 0:
- trigger_found = True
- break
+ send_to = channel
+ if self.factory.settings.NICKNAME.startswith(channel) or \
+ channel.startswith(self.factory.settings.NICKNAME):
+ trigger_found = True
+ send_to = user.split('!')[0]
+ else:
+ for trigger in self.factory.settings.TRIGGERS:
+ if msg.lower().find(trigger) >= 0:
+ trigger_found = True
+ break
if trigger_found:
quote = self.factory.quotation.select()
- self.msg(channel, quote)
- logging.info("sent message:\n\t%s" % (quote))
+ self.msg(send_to, quote)
+ logging.info("sent message to %s:\n\t%s" % (send_to, quote))
class TalkBackBotFactory(protocol.ClientFactory):
- def __init__(self, channel):
- self.channel = channel
- self.quotation = QuotationSelector(settings.QUOTES_FILE)
+ def __init__(self, settings):
+ self.settings = settings
+ self.channel = self.settings.CHANNEL
+ self.quotation = QuotationSelector(self.settings.QUOTES_FILE)
def buildProtocol(self, addr):
- p = TalkBackBot()
- p.factory = self
- return p
+ bot = TalkBackBot()
+ bot.factory = self
+ bot.password = self.settings.PASSWORD
+ bot.nickname = self.settings.NICKNAME
+ bot.realname = self.settings.REALNAME
+ return bot
def clientConnectionLost(self, connector, reason):
logging.info("connection lost, reconnecting")
View
1 tests/test_quote.txt
@@ -0,0 +1 @@
+Nobody minds having what is too good for them. ~ Jane Austen
View
19 tests/test_settings.py
@@ -0,0 +1,19 @@
+# IRC settings
+HOST = "test.example.com"
+PORT = 6667
+USE_SSL = False
+PASSWORD = None
+NICKNAME = "shesaidbot"
+REALNAME = "bot: provides quotations from notable women"
+
+CHANNEL = "#test"
+
+# Trigger phrases, in lowercase
+TRIGGERS = (
+ "twss",
+ )
+
+# Process settings
+PID_FILE = "./talkbackbot.pid"
+LOG_FILE = "./talkbackbot.log"
+QUOTES_FILE = "tests/test_quote.txt"
View
44 tests/test_talkbackbot.py
@@ -0,0 +1,44 @@
+import os
+import unittest
+import mock
+
+from talkbackbot import TalkBackBotFactory
+import test_settings
+
+class TestTalkBackBot(unittest.TestCase):
+
+ CHANNEL = "#testchannel"
+ QUOTE = "Nobody minds having what is too good for them. ~ Jane Austen"
+ USERNAME = "tester"
+
+ def setUp(self):
+ super(TestTalkBackBot, self).setUp()
+ factory = TalkBackBotFactory(test_settings)
+ self.bot = factory.buildProtocol(None)
+ self.bot.msg = mock.MagicMock()
+
+ def test_privmsg__no_trigger(self):
+ """Shouldn't send a quote if message does not match trigger"""
+ self.bot.privmsg(self.USERNAME, self.CHANNEL, "hi")
+ self.assertFalse(self.bot.msg.called)
+
+ def test_privmsg__with_trigger(self):
+ """Should send a quote if message matches trigger"""
+ self.bot.privmsg(self.USERNAME, self.CHANNEL, "twss")
+ self.bot.msg.assert_called_with(self.CHANNEL, self.QUOTE)
+
+ def test_privmsg__private_message(self):
+ """ For private messages, should send quote directly to user """
+ self.bot.privmsg(self.USERNAME, test_settings.NICKNAME, "hi")
+ self.bot.msg.assert_called_with(self.USERNAME, self.QUOTE)
+
+ def test_privmsg__private_message_truncated_nickname(self):
+ """ Send quote directly to user even if name is truncated """
+ self.bot.privmsg(self.USERNAME, test_settings.NICKNAME[:-2], "hi")
+ self.bot.msg.assert_called_with(self.USERNAME, self.QUOTE)
+
+ def test_privmsg__private_message_alternate_nickname(self):
+ """ Send quote directly to user even if using alternate nickname """
+ self.bot.privmsg(self.USERNAME, test_settings.NICKNAME + '_', "hi")
+ self.bot.msg.assert_called_with(self.USERNAME, self.QUOTE)
+

0 comments on commit 8abb9b4

Please sign in to comment.