diff options
Diffstat (limited to 'bot')
-rw-r--r-- | bot/ircmeeting/agenda.py | 56 | ||||
-rw-r--r-- | bot/ircmeeting/meeting.py | 21 | ||||
-rw-r--r-- | bot/tests/run_test.py | 75 |
3 files changed, 144 insertions, 8 deletions
diff --git a/bot/ircmeeting/agenda.py b/bot/ircmeeting/agenda.py index af03c3a..8b9650c 100644 --- a/bot/ircmeeting/agenda.py +++ b/bot/ircmeeting/agenda.py @@ -1,7 +1,15 @@ import json +import threading import urllib import re +class MessageSender: + def __init__(self, irc, message): + self.irc = irc + self.message = message + def send_message(self): + self.irc.reply(self.message) + class Agenda(object): # Messages @@ -18,6 +26,10 @@ class Agenda(object): not_a_number_msg = "Your choice was not recognized as a number. Please retry." out_of_range_msg = "Your choice was out of range!" vote_confirm_msg = "You voted for #{} - {}" + timelimit_added_msg = 'Added "{}" reminder in {}:{}' + timelimit_list_msg = 'Set reminders: "{}"' + timelimit_removed_msg = 'Reminder "{}" removed' + timelimit_missing_msg = 'No such reminder "{}"' # Internal _voters = [] @@ -28,6 +40,7 @@ class Agenda(object): def __init__(self, conf): self.conf = conf + self.reminders = {} def get_agenda_item(self): if not self.conf.manage_agenda: @@ -37,24 +50,36 @@ class Agenda(object): else: return self.empty_agenda_msg - def next_agenda_item(self): + def _swich_agenda_item_to(self, new_item, irc): + self._current_item = new_item + for reminder in self.reminders.values(): + reminder.cancel() + self.reminders = {} + for line in self._agenda[self._current_item][2].split('\n'): + match = re.match( '([0-9]+):([0-9]+) (.*)', line) + if match: + self.add_timelimit(int(match.group(1)), int(match.group(2)), + match.group(3), irc) + self._agenda[self._current_item][2] = '' + + def next_agenda_item(self, irc): if not self.conf.manage_agenda: return('') if self._vote_open: return self.voting_open_so_item_not_changed_msg else: if (self._current_item + 1) < len(self._agenda): - self._current_item += 1 + self._swich_agenda_item_to(self._current_item + 1, irc) return(self.get_agenda_item()) - def prev_agenda_item(self): + def prev_agenda_item(self, irc): if not self.conf.manage_agenda: return('') if self._vote_open: return self.voting_open_so_item_not_changed_msg else: if self._current_item > 0: - self._current_item -= 1 + self._swich_agenda_item_to(self._current_item - 1, irc) return(self.get_agenda_item()) def start_vote(self): @@ -169,6 +194,29 @@ class Agenda(object): option = self._agenda[self._current_item][1].pop(opt) return str.format(self.removed_option_msg, str(opt), option) + def add_timelimit(self, minutes, seconds, message, irc): + sender = MessageSender(irc, message) + reminder = (threading.Timer(60*minutes + seconds, sender.send_message)) + self.reminders[message] = reminder + reminder.start() + result = str.format(self.timelimit_added_msg, message, minutes, seconds) + return(result) + + def list_timielimits(self): + keys = self.reminders.keys() + keys_str = '", "'.join(keys) + result = str.format(self.timelimit_list_msg, keys_str) + return(result) + + def remove_timelimit(self, message): + if message in self.reminders: + timer = self.reminders.pop(message) + timer.cancel() + result = str.format(self.timelimit_removed_msg, message) + else: + result = str.format(self.timelimit_missing_msg, message) + return(result) + def post_result(self): if not self.conf.manage_agenda: return('') diff --git a/bot/ircmeeting/meeting.py b/bot/ircmeeting/meeting.py index c01176a..a86c782 100644 --- a/bot/ircmeeting/meeting.py +++ b/bot/ircmeeting/meeting.py @@ -33,6 +33,7 @@ import time import os import re import stat +import threading import writers import items @@ -301,7 +302,6 @@ else: # Subclass Config and LocalConfig, new type overrides Config. Config = type('Config', (LocalConfig, Config), {}) - class MeetingCommands(object): # Command Definitions # generic parameters to these functions: @@ -323,10 +323,25 @@ class MeetingCommands(object): self.reply(self.config.agenda.get_agenda_item()) def do_nextitem(self, nick, time_, line, **kwargs): - self.reply(self.config.agenda.next_agenda_item()) + self.reply(self.config.agenda.next_agenda_item(self)) def do_previtem(self, nick, time_, line, **kwargs): - self.reply(self.config.agenda.prev_agenda_item()) + self.reply(self.config.agenda.prev_agenda_item(self)) + + def do_timelimit(self, nick, time_, line, **kwargs): + reply = 'Usage "#timelimit add <minutes>:<seconds> <message>" or ' +\ + '"#timelimit list" or "#timelimit remove <message>"' + match = re.match( ' *?add ([0-9]+):([0-9]+) (.*)', line) + if match: + reply = self.config.agenda.add_timelimit(int(match.group(1)), + int(match.group(2)), match.group(3), self) + elif re.match( ' *?list', line): + reply = self.config.agenda.list_timielimits() + else: + match = re.match( ' *?remove (.*)', line) + if(match): + reply = self.config.agenda.remove_timelimit(match.group(1)) + self.reply(reply) def do_changeitem(self, nick, time_, line, **kwargs): self.reply(self.config.agenda.change_agenda_item(line)) diff --git a/bot/tests/run_test.py b/bot/tests/run_test.py index 1358d47..bd116ff 100644 --- a/bot/tests/run_test.py +++ b/bot/tests/run_test.py @@ -6,6 +6,8 @@ import re import shutil import sys import tempfile +import time +import threading import unittest os.environ['MEETBOT_RUNNING_TESTS'] = '1' @@ -342,7 +344,7 @@ class MeetBotTest(unittest.TestCase): def get_simple_agenda_test(self): test = test_meeting.TestMeeting() test.set_voters(['x', 'z']) - test.set_agenda([['first item', ['opt1', 'opt2']], ['second item', []], ['third item', []]]) + test.set_agenda([['first item', ['opt1', 'opt2'], ''], ['second item', [], ''], ['third item', [], '']]) test.M.config.manage_agenda = False test.answer_should_match("20:13:50 <x> #startmeeting", @@ -446,6 +448,77 @@ class MeetBotTest(unittest.TestCase): test.answer_should_match('20:13:50 <x> #vote 0', 'You voted for #0 - opt1') test.answer_should_match('20:13:50 <z> #vote 0', 'You voted for #0 - opt1. Voting closed.') + def test_agenda_time_limit_adding(self): + test = self.get_simple_agenda_test() + test.answer_should_match('20:13:50 <x> #timelimit', 'Usage "#timelimit ' +\ + 'add <minutes>:<seconds> <message>" or "' +\ + '#timelimit list" or "#timelimit remove ' +\ + '<message>"') + test.answer_should_match('20:13:50 <x> #timelimit add 0:1 some other message', + 'Added "some other message" reminder in 0:1') + test.answer_should_match('20:13:50 <x> #timelimit add 1:0 some message', + 'Added "some message" reminder in 1:0') + time.sleep(2) + last_message = test.log[-1] + assert(last_message == 'some other message') + reminders = test.M.config.agenda.reminders + assert(len(reminders) == 2) + for reminder in reminders.values(): + assert(reminder.__class__ == threading._Timer) + + test.process('20:13:50 <x> #nextitem') + + def test_agenda_time_limit_removing_when_changing_item(self): + test = self.get_simple_agenda_test() + + test.process('20:13:50 <x> #timelimit add 0:1 message') + assert(len(test.M.config.agenda.reminders) == 1) + test.process('20:13:50 <x> #nextitem') + assert(len(test.M.config.agenda.reminders) == 0) + test.process('20:13:50 <x> #timelimit add 0:1 message') + assert(len(test.M.config.agenda.reminders) == 1) + test.process('20:13:50 <x> #previtem') + assert(len(test.M.config.agenda.reminders) == 0) + + def test_agenda_time_limit_manual_removing(self): + test = self.get_simple_agenda_test() + + test.process('20:13:50 <x> #timelimit add 0:1 message') + test.process('20:13:50 <x> #timelimit add 0:1 other message') + keys = test.M.config.agenda.reminders.keys() + keys.sort() + assert(keys == ['message', 'other message']) + + test.answer_should_match('20:13:50 <x> #timelimit remove other message', 'Reminder "other message" removed') + keys = test.M.config.agenda.reminders.keys() + assert(keys == ['message']) + + def test_agenda_time_limit_listing(self): + test = self.get_simple_agenda_test() + test.process('20:13:50 <x> #timelimit add 0:1 message') + test.process('20:13:50 <x> #timelimit add 0:1 other message') + test.process('20:13:50 <x> #timelimit add 0:1 yet another message') + keys = test.M.config.agenda.reminders.keys() + test.answer_should_match('20:13:50 <x> #timelimit list', + 'Set reminders: "' + '", "'.join(keys) + '"') + + def test_preset_agenda_time_limits(self): + test = self.get_simple_agenda_test() + test.M.config.agenda._agenda[0][2] = '1:0 message' + test.M.config.agenda._agenda[1][2] = '1:0 another message\n0:10 some other message' + + test.process('20:13:50 <x> #nextitem') + keys = test.M.config.agenda.reminders.keys() + keys.sort() + assert(keys == ['another message', 'some other message']) + + test.process('20:13:50 <x> #previtem') + keys = test.M.config.agenda.reminders.keys() + keys.sort() + assert(keys == ['message']) + + test.process('20:13:50 <x> #nextitem') + if __name__ == '__main__': os.chdir(os.path.join(os.path.dirname(__file__), '.')) |