2011-01-06 23:25:46 -06:00
|
|
|
"""
|
|
|
|
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/>.
|
|
|
|
"""
|
2010-07-28 23:47:29 -05:00
|
|
|
|
2010-08-01 11:55:14 -05:00
|
|
|
from ConfigParser import NoOptionError, NoSectionError
|
2010-07-29 22:43:57 -05:00
|
|
|
from datetime import datetime
|
2011-01-07 20:37:24 -06:00
|
|
|
import re
|
2010-07-27 20:35:01 -05:00
|
|
|
|
2010-07-29 22:43:57 -05:00
|
|
|
from dateutil.parser import *
|
|
|
|
from dateutil.relativedelta import *
|
|
|
|
from dateutil.tz import *
|
2010-07-30 18:34:10 -05:00
|
|
|
from extlib import irclib
|
2010-07-29 00:18:20 -05:00
|
|
|
|
|
|
|
from Module import Module
|
|
|
|
|
2010-07-27 20:35:01 -05:00
|
|
|
class Countdown(Module):
|
|
|
|
|
2011-01-06 23:25:46 -06:00
|
|
|
"""Class that adds a countdown item to the bot."""
|
|
|
|
|
2011-01-07 23:09:07 -06:00
|
|
|
def do(self, connection, event, nick, userhost, what, admin_unlocked):
|
2011-01-06 23:25:46 -06:00
|
|
|
"""Add/retrieve countdown items."""
|
|
|
|
|
2011-01-07 20:37:24 -06:00
|
|
|
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
|
2011-01-07 23:09:07 -06:00
|
|
|
return replystr
|
2011-01-07 20:37:24 -06:00
|
|
|
|
|
|
|
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
|
2011-01-07 23:09:07 -06:00
|
|
|
return replystr
|
2011-01-07 20:37:24 -06:00
|
|
|
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)
|
2011-01-07 23:09:07 -06:00
|
|
|
return liststr
|
2011-01-07 20:37:24 -06:00
|
|
|
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 += ', '
|
2011-01-07 23:09:07 -06:00
|
|
|
return relstr[0:-2]
|
2011-01-07 20:37:24 -06:00
|
|
|
except NoOptionError: pass
|
2010-07-27 20:35:01 -05:00
|
|
|
|
2010-07-28 23:48:47 -05:00
|
|
|
# vi:tabstop=4:expandtab:autoindent
|
2010-07-28 00:11:58 -05:00
|
|
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|