register callbacks
This commit is contained in:
26
mailbot/__init__.py
Normal file
26
mailbot/__init__.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
pkg_resources = __import__('pkg_resources')
|
||||
distribution = pkg_resources.get_distribution('mailbot')
|
||||
|
||||
__version__ = distribution.version
|
||||
|
||||
|
||||
from .callback import Callback # noqa
|
||||
from .exceptions import RegisterException # noqa
|
||||
from .mailbot import MailBot # noqa
|
||||
|
||||
|
||||
CALLBACKS_MAP = {}
|
||||
|
||||
|
||||
def register(callback_class, rules=None):
|
||||
"""Register a callback class, optionnally with rules."""
|
||||
if callback_class in CALLBACKS_MAP:
|
||||
raise RegisterException('%s is already registered' % callback_class)
|
||||
|
||||
apply_rules = getattr(callback_class, 'rules', {})
|
||||
if rules:
|
||||
apply_rules.update(rules)
|
||||
CALLBACKS_MAP[callback_class] = apply_rules
|
||||
return apply_rules
|
||||
5
mailbot/callback.py
Normal file
5
mailbot/callback.py
Normal file
@@ -0,0 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
class Callback(object):
|
||||
"""Base class for callbacks."""
|
||||
5
mailbot/exceptions.py
Normal file
5
mailbot/exceptions.py
Normal file
@@ -0,0 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
class RegisterException(Exception):
|
||||
"""Exception raised on a registration error."""
|
||||
5
mailbot/mailbot.py
Normal file
5
mailbot/mailbot.py
Normal file
@@ -0,0 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
class MailBot(object):
|
||||
"""MailBot mail class, where the magic is happening."""
|
||||
15
mailbot/tests/__init__.py
Normal file
15
mailbot/tests/__init__.py
Normal file
@@ -0,0 +1,15 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from unittest2 import TestCase
|
||||
|
||||
from .. import CALLBACKS_MAP
|
||||
|
||||
|
||||
class MailBotTestCase(TestCase):
|
||||
"""TestCase that restores the CALLBACKS_MAP after each test run."""
|
||||
|
||||
def setUp(self):
|
||||
self.callbacks_map_save = CALLBACKS_MAP.copy()
|
||||
|
||||
def tearDown(self):
|
||||
CALLBACKS_MAP = self.callbacks_map_save # noqa
|
||||
43
mailbot/tests/test_mailbot.py
Normal file
43
mailbot/tests/test_mailbot.py
Normal file
@@ -0,0 +1,43 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from . import MailBotTestCase
|
||||
from .. import register, CALLBACKS_MAP, RegisterException
|
||||
|
||||
|
||||
class RegisterTest(MailBotTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(RegisterTest, self).setUp()
|
||||
|
||||
class EmptyCallback(object):
|
||||
pass
|
||||
|
||||
class WithRulesCallback(object):
|
||||
rules = {'foo': 'bar', 'baz': 'bat'}
|
||||
|
||||
self.empty_callback = EmptyCallback
|
||||
self.with_rules_callback = WithRulesCallback
|
||||
|
||||
def test_register(self):
|
||||
before = len(CALLBACKS_MAP)
|
||||
register(self.empty_callback)
|
||||
self.assertEqual(len(CALLBACKS_MAP), before + 1)
|
||||
|
||||
def test_register_existing(self):
|
||||
register(self.empty_callback)
|
||||
self.assertRaises(RegisterException, register, self.empty_callback)
|
||||
self.assertTrue(register(self.with_rules_callback))
|
||||
|
||||
def test_register_without_rules_callback_with_rules(self):
|
||||
register(self.with_rules_callback)
|
||||
self.assertEqual(CALLBACKS_MAP[self.with_rules_callback],
|
||||
self.with_rules_callback.rules)
|
||||
|
||||
def test_register_with_rules_callback_without_rules(self):
|
||||
register(self.empty_callback, {'one': 'two'})
|
||||
self.assertEqual(CALLBACKS_MAP[self.empty_callback], {'one': 'two'})
|
||||
|
||||
def test_register_with_rules_callback_with_rules(self):
|
||||
register(self.with_rules_callback, {'baz': 'wow'})
|
||||
self.assertEqual(CALLBACKS_MAP[self.with_rules_callback],
|
||||
{'foo': 'bar', 'baz': 'wow'})
|
||||
Reference in New Issue
Block a user