Skip to content

Commit

Permalink
Make dependency on python-dateutil optionnal.
Browse files Browse the repository at this point in the history
  • Loading branch information
progval committed Nov 24, 2013
1 parent a0c5e06 commit b8abbd1
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions plugins/Time/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,32 @@
TIME = time # For later use.
from datetime import datetime

from dateutil import parser

import supybot.conf as conf
import supybot.utils as utils
from supybot.commands import *
import supybot.callbacks as callbacks
from supybot.i18n import PluginInternationalization, internationalizeDocstring
_ = PluginInternationalization('Time')

def parse(s):
todo = []
s = s.replace('noon', '12:00')
s = s.replace('midnight', '00:00')
if 'tomorrow' in s:
todo.append(lambda i: i + 86400)
s = s.replace('tomorrow', '')
if 'next week' in s:
todo.append(lambda i: i + 86400*7)
s = s.replace('next week', '')
i = int(time.mktime(parser.parse(s, fuzzy=True).timetuple()))
for f in todo:
i = f(i)
return i

try:
from dateutil import parser
def parse(s):
todo = []
s = s.replace('noon', '12:00')
s = s.replace('midnight', '00:00')
if 'tomorrow' in s:
todo.append(lambda i: i + 86400)
s = s.replace('tomorrow', '')
if 'next week' in s:
todo.append(lambda i: i + 86400*7)
s = s.replace('next week', '')
i = int(time.mktime(parser.parse(s, fuzzy=True).timetuple()))
for f in todo:
i = f(i)
return i
except ImportError:
parse = None

class Time(callbacks.Plugin):
@internationalizeDocstring
Expand Down Expand Up @@ -99,6 +102,9 @@ def at(self, irc, msg, args, s):
<time string> can be any number of natural formats; just try something
and see if it will work.
"""
if not parse:
irc.error(_('This command is not available on this bot, ask the '
'owner to install the python-dateutil library.'), Raise=True)
now = int(time.time())
new = parse(s)
if new != now:
Expand All @@ -113,6 +119,9 @@ def until(self, irc, msg, args, s):
Returns the number of seconds until <time string>.
"""
if not parse:
irc.error(_('This command is not available on this bot, ask the '
'owner to install the python-dateutil library.'), Raise=True)
now = int(time.time())
new = parse(s)
if new != now:
Expand Down

0 comments on commit b8abbd1

Please sign in to comment.