dr.botzo/modules/Countdown.py
Brian S. Stephan 2aa369add7 rewrite recursion/alias code for the 500th time.
more of a moving of the code, actually, it now exists in (an overridden)
_handle_event, so that recursions happen against irc events directly,
rather than an already partially interpreted object.

with this change, modules don't need to implement do() nor do we have a
need for the internal_bus, which was doing an additional walk of the
modules after the irc event was already handled and turned into text. now
the core event handler does the recursion scans.

to support this, we bring back the old replypath trick and use it again,
so we know when to send a privmsg reply and when to return text so that
it may be chained in recursion. this feels old hat by now, but if you
haven't been following along, you should really look at the diff.

that's the meat of the change. the rest is updating modules to use
self.reply() and reimplementing (un)register_handlers where appropriate
2011-02-17 01:08:45 -06:00

111 lines
4.2 KiB
Python

"""
Countdown - track and display time until events
Copyright (C) 2010 Brian S. Stephan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from ConfigParser import NoOptionError, NoSectionError
from datetime import datetime
import re
from dateutil.parser import *
from dateutil.relativedelta import *
from dateutil.tz import *
from extlib import irclib
from Module import Module
class Countdown(Module):
"""Class that adds a countdown item to the bot."""
def do(self, connection, event, nick, userhost, what, admin_unlocked):
"""Add/retrieve countdown items."""
match = re.search('^!countdown\s+add\s+(\S+)\s+(.*)$', what)
if match:
item = match.group(1)
target = parse(match.group(2), default=datetime.now().replace(tzinfo=tzlocal()))
if not self.config.has_section(self.__class__.__name__):
self.config.add_section(self.__class__.__name__)
self.config.set(self.__class__.__name__, item, target.astimezone(tzutc()).isoformat())
replystr = 'added countdown item ' + item
return self.reply(connection, event, replystr)
match = re.search('^!countdown\s+remove\s+(\S+)$', what)
if match:
try:
item = match.group(1)
if self.config.remove_option(self.__class__.__name__, item):
replystr = 'removed countdown item ' + item
return self.reply(connection, event, replystr)
except NoSectionError: pass
match = re.search('^!countdown\s+list$', what)
if match:
try:
cdlist = self.config.options(self.__class__.__name__)
self.remove_metaoptions(cdlist)
cdlist.sort()
liststr = ', '.join(cdlist)
return self.reply(connection, event, liststr)
except NoSectionError: pass
match = re.search('^!countdown\s+(\S+)$', what)
if match:
try:
item = match.group(1)
timestr = self.config.get(self.__class__.__name__, item)
time = parse(timestr)
rdelta = relativedelta(time, datetime.now().replace(tzinfo=tzlocal()))
relstr = item + ' will occur in '
if rdelta.years != 0:
relstr += str(rdelta.years) + ' years '
if rdelta.years > 1:
relstr += 's'
relstr += ', '
if rdelta.months != 0:
relstr += str(rdelta.months) + ' month'
if rdelta.months > 1:
relstr += 's'
relstr += ', '
if rdelta.days != 0:
relstr += str(rdelta.days) + ' day'
if rdelta.days > 1:
relstr += 's'
relstr += ', '
if rdelta.hours != 0:
relstr += str(rdelta.hours) + ' hour'
if rdelta.hours > 1:
relstr += 's'
relstr += ', '
if rdelta.minutes != 0:
relstr += str(rdelta.minutes) + ' minute'
if rdelta.minutes > 1:
relstr += 's'
relstr += ', '
if rdelta.seconds != 0:
relstr += str(rdelta.seconds) + ' second'
if rdelta.seconds > 1:
relstr += 's'
relstr += ', '
return self.reply(connection, event, relstr[0:-2])
except NoOptionError: pass
# vi:tabstop=4:expandtab:autoindent
# kate: indent-mode python;indent-width 4;replace-tabs on;