From 21607e73156e1da3194a5b60e829d30b7243cf6e Mon Sep 17 00:00:00 2001 From: Mathieu Agopian Date: Tue, 19 Mar 2013 11:05:30 +0100 Subject: [PATCH] callbacks triggering --- docs/source/index.rst | 8 ++++---- mailbot/callback.py | 11 +++++++++++ mailbot/mailbot.py | 2 +- mailbot/tests/test_callback.py | 13 ++++++++++++- mailbot/tests/test_mailbot.py | 4 ++-- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/docs/source/index.rst b/docs/source/index.rst index e99dfd3..df77169 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -54,7 +54,7 @@ Registering callbacks class MyCallback(Callback): - def callback(self): + def trigger(self): print("Mail received: {O}".format(self.subject)) register(MyCallback) @@ -79,7 +79,7 @@ pattern 'Hello ' followed by a word, anywhere in the subject (it uses class MyCallback(Callback): rules = {'subject': [r'Hello (\w)']} - def callback(self): + def trigger(self): print("Mail received for {0}".format(self.matches['subject'][0])) register(MyCallback) @@ -101,7 +101,7 @@ registering: class MyCallback(Callback): - def callback(self): + def trigger(self): print("Mail received for %s!" self.matches['subject'][0]) register(MyCallback, rules={'subject': [r'Hello (\w)']}) @@ -176,7 +176,7 @@ As an example, let's take an email with the subject "Hello Bryan", from class MyCallback(Callback): rules = {'subject': [r'Hello (\w)', 'Hi!'], 'from': ['@doe.com']} - def callback(self): + def trigger(self): print("Mail received for {0}".format(self.matches['subject'][0])) register(MyCallback) diff --git a/mailbot/callback.py b/mailbot/callback.py index 4cfdc05..efa2d34 100644 --- a/mailbot/callback.py +++ b/mailbot/callback.py @@ -53,6 +53,12 @@ class Callback(object): return any(self.matches[item]) def get_email_body(self, message=None): + """Return the message text body. + + Return the first 'text/plain' part of the email.Message that doesn't + have a filename. + + """ if message is None: message = self.message @@ -65,3 +71,8 @@ class Callback(object): if content_type == 'text/plain' and filename is None: # text body of the mail, not an attachment return part.get_payload() + return '' + + def trigger(self): + """Called when a mail matching the registered rules is received.""" + raise NotImplementedError("Must be implemented in a child class.") diff --git a/mailbot/mailbot.py b/mailbot/mailbot.py index 02422c4..fa94a92 100644 --- a/mailbot/mailbot.py +++ b/mailbot/mailbot.py @@ -35,7 +35,7 @@ class MailBot(object): """Check if callback matches rules, and if so, trigger.""" callback = callback_class(message, rules) if callback.check_rules(): - return callback.callback() + return callback.trigger() def process_messages(self): """Process messages: check which callbacks should be triggered.""" diff --git a/mailbot/tests/test_callback.py b/mailbot/tests/test_callback.py index e31536f..4dcf008 100644 --- a/mailbot/tests/test_callback.py +++ b/mailbot/tests/test_callback.py @@ -4,7 +4,7 @@ from email import message_from_file, message_from_string from os import path from re import search -from mock import sentinel, Mock +from mock import Mock from . import MailBotTestCase from .. import Callback @@ -80,10 +80,21 @@ class CallbackTest(MailBotTestCase): self.assertEqual(callback.get_email_body(), None) # not a Message self.assertEqual(callback.get_email_body('foo'), None) # not a Message + # empty email empty_message = message_from_string('') self.assertEqual(callback.get_email_body(empty_message), '') + # badly formed email without a 'text/plain' part + empty_message.set_type('html/plain') + self.assertEqual(callback.get_email_body(empty_message), '') + + # real email email_file = path.join(path.dirname(__file__), 'mails/mail_with_attachment.txt') email = message_from_file(open(email_file, 'r')) self.assertEqual(callback.get_email_body(email), 'Mail content here\n') + + def test_trigger(self): + callback = Callback('foo', 'bar') + + self.assertRaises(NotImplementedError, callback.trigger) diff --git a/mailbot/tests/test_mailbot.py b/mailbot/tests/test_mailbot.py index 0186113..64cca2b 100644 --- a/mailbot/tests/test_mailbot.py +++ b/mailbot/tests/test_mailbot.py @@ -56,7 +56,7 @@ class MailBotTest(MailBotClientTest): def test_process_message_trigger(self): callback = Mock() callback.check_rules.return_value = True - callback.callback.return_value = sentinel.callback_result + callback.trigger.return_value = sentinel.callback_result callback_class = Mock(return_value=callback) res = self.bot.process_message(sentinel.message, callback_class, @@ -65,7 +65,7 @@ class MailBotTest(MailBotClientTest): callback_class.assert_called_once_with(sentinel.message, sentinel.rules) callback.check_rules.assert_called_once_with() - callback.callback.assert_called_once_with() + callback.trigger.assert_called_once_with() self.assertEqual(res, sentinel.callback_result) def test_process_message_no_trigger(self):