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

Commit

Permalink
Merge pull request #19 from marvinpinto/help-ping-plugins
Browse files Browse the repository at this point in the history
Allow each plugin to set their Help strings directly
  • Loading branch information
marvinpinto committed Sep 20, 2015
2 parents ac3e640 + ed5a967 commit 80135ed
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 12 deletions.
4 changes: 4 additions & 0 deletions charlesbot/base_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def set_running(self, running):
def queue_message(self, message):
yield from self._q.put(message)

@abstractmethod
def get_help_message(self):
pass

@abstractmethod
def process_message(self, message):
pass
3 changes: 3 additions & 0 deletions charlesbot/plugins/broadcast_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def seed_initial_data(self): # pragma: no cover
loop.create_task(self.seed_channel_membership())
loop.create_task(self.seed_group_membership())

def get_help_message(self): # pragma: no cover
return "!wall <msg> - Broadcast a message to all channels I'm a part of" # NOQA

def log_room_membership(self):
self.log.info("Currently in: %s"
% ", ".join(sorted(self.room_membership.values())))
Expand Down
18 changes: 12 additions & 6 deletions charlesbot/plugins/help_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ class Help(BasePlugin):

def __init__(self):
super().__init__("Help!")
self.initialize_help_message_list()

def initialize_help_message_list(self):
self.help_msg_list = []
self.help_msg_list.append("!help - This help message")
self.help_msg_list.sort()

def add_help_message(self, message):
if message:
self.help_msg_list.append(str(message))
self.help_msg_list.sort()

def get_help_message(self):
msg = [
"!help - This help message",
"!wall <msg> - Broadcast a message to all channels I'm a part of",
"!oncall - Find out who's on-call right now",
]
help_msg = "\n".join(msg)
help_msg = "\n".join(self.help_msg_list)
return "```\n%s\n```" % help_msg

@asyncio.coroutine
Expand Down
3 changes: 3 additions & 0 deletions charlesbot/plugins/jira/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def load_config(self): # pragma: no cover
config_dict = configuration.get()
self.base_url = config_dict['jira']['base_url']

def get_help_message(self): # pragma: no cover
return ""

@asyncio.coroutine
def process_message(self, message):
if not type(message) is SlackMessage:
Expand Down
3 changes: 3 additions & 0 deletions charlesbot/plugins/pagerduty/pagerduty.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def load_config(self): # pragma: no cover
self.token = config_dict['pagerduty']['token']
self.subdomain = config_dict['pagerduty']['subdomain']

def get_help_message(self): # pragma: no cover
return "!oncall - Find out who's on-call right now"

@asyncio.coroutine
def process_message(self, message):
if not type(message) is SlackMessage:
Expand Down
11 changes: 11 additions & 0 deletions charlesbot/robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def initialize_plugins(self):
return_list.append(return_obj)
return return_list

def initialize_static_plugins(self):
self.initialize_help_plugin()

def initialize_help_plugin(self):
from charlesbot.plugins.help_plugin import Help
h = Help()
for plugin in self.plugin_list:
h.add_help_message(plugin.get_help_message())
self.plugin_list.append(h)

@asyncio.coroutine
def queue_message(self, message, plugin):
if message:
Expand All @@ -71,6 +81,7 @@ def start(self): # pragma: no cover
self.slack = SlackConnection()
loop = asyncio.get_event_loop()
self.plugin_list = self.initialize_plugins()
self.initialize_static_plugins()
loop.create_task(self.produce())
loop.add_signal_handler(
signal.SIGINT,
Expand Down
29 changes: 23 additions & 6 deletions tests/plugins/help_plugin/test_help_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ class TestHelpPlugin(asynctest.TestCase):

def setUp(self):
self.slack_client = MagicMock()
self.initialize_help_plugin()

def tearDown(self):
self.initialize_help_plugin()

def initialize_help_plugin(self):
self.hp = Help()
self.hp.send_help_message = MagicMock()

Expand Down Expand Up @@ -72,3 +66,26 @@ def test_two_msgs_two_good(self):
yield from self.hp.process_message(msg)
expected = [call('C1'), call('C2')]
self.hp.send_help_message.assert_has_calls(expected, any_order=True)

@asynctest.ignore_loop
def test_help_msg_falsy(self):
list_entry = "!help - This help message"
self.hp.add_help_message("")
self.assertEqual(len(self.hp.help_msg_list), 1)
self.assertTrue(list_entry in self.hp.help_msg_list)
self.hp.add_help_message([])
self.assertEqual(len(self.hp.help_msg_list), 1)
self.assertTrue(list_entry in self.hp.help_msg_list)
self.hp.add_help_message(0)
self.assertEqual(len(self.hp.help_msg_list), 1)
self.assertTrue(list_entry in self.hp.help_msg_list)

@asynctest.ignore_loop
def test_add_help_msg(self):
list_entry = "!help - This help message"
self.assertEqual(len(self.hp.help_msg_list), 1)
self.assertTrue(list_entry in self.hp.help_msg_list)
self.hp.add_help_message("hi there!")
self.assertEqual(len(self.hp.help_msg_list), 2)
self.assertTrue(list_entry in self.hp.help_msg_list)
self.assertTrue("hi there!" in self.hp.help_msg_list)
3 changes: 3 additions & 0 deletions tests/test_base_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def __init__(myself):
def process_message(myself, message):
myself.call_counter += 1

def get_help_message(myself):
return "This is my help message"

def setUp(self):
self.dc = TestBasePlugin.DummyPlugin()
self.dc.set_running(False)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_robot_help_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import asynctest
from asynctest.mock import patch
from asynctest.mock import MagicMock


class TestRobotHelpMessages(asynctest.TestCase):

def setUp(self):
patcher1 = patch('charlesbot.robot.Robot.initialize_robot')
self.addCleanup(patcher1.stop)
self.mock_initialize_robot = patcher1.start()

from charlesbot.robot import Robot
self.robot = Robot()

@asynctest.ignore_loop
def test_empty_plugin_list(self):
self.robot.plugin_list = []
self.robot.initialize_static_plugins()
self.assertEqual(len(self.robot.plugin_list), 1)

@asynctest.ignore_loop
def test_plugin_list_one_entry(self):
plugin1 = MagicMock()
self.robot.plugin_list = [plugin1]
self.robot.initialize_static_plugins()
self.assertEqual(len(self.robot.plugin_list), 2)

@asynctest.ignore_loop
def test_plugin_list_two_entries(self):
plugin1 = MagicMock()
plugin2 = MagicMock()
self.robot.plugin_list = [plugin1, plugin2]
self.robot.initialize_static_plugins()
self.assertEqual(len(self.robot.plugin_list), 3)

0 comments on commit 80135ed

Please sign in to comment.