obviously this means all of the modules changed to accomodate. this is one of many steps to reduce the number of times we pass connections and servers and other such info around, when it's mostly unnecessary because modules have a reference to DrBotIRC
116 lines
4.5 KiB
Python
116 lines
4.5 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 Module import Module
|
|
|
|
class Countdown(Module):
|
|
|
|
"""Track when events will happen."""
|
|
|
|
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:
|
|
try:
|
|
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.irc.reply(event, replystr)
|
|
except ValueError as e:
|
|
self.log.error("could not parse countdown item")
|
|
self.log.exception(e)
|
|
return self.irc.reply(event,
|
|
"could not parse countdown item: {0:s}".format(str(e)))
|
|
|
|
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.irc.reply(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.irc.reply(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.irc.reply(event, relstr[0:-2])
|
|
except NoOptionError: pass
|
|
|
|
# vi:tabstop=4:expandtab:autoindent
|
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|