Make dependency on python-dateutil optionnal.

This commit is contained in:
Valentin Lorentz 2013-11-24 14:47:26 +00:00
parent a0c5e06445
commit b8abbd1e36

View File

@ -31,8 +31,6 @@ import time
TIME = time # For later use. TIME = time # For later use.
from datetime import datetime from datetime import datetime
from dateutil import parser
import supybot.conf as conf import supybot.conf as conf
import supybot.utils as utils import supybot.utils as utils
from supybot.commands import * from supybot.commands import *
@ -40,20 +38,25 @@ import supybot.callbacks as callbacks
from supybot.i18n import PluginInternationalization, internationalizeDocstring from supybot.i18n import PluginInternationalization, internationalizeDocstring
_ = PluginInternationalization('Time') _ = PluginInternationalization('Time')
def parse(s):
todo = [] try:
s = s.replace('noon', '12:00') from dateutil import parser
s = s.replace('midnight', '00:00') def parse(s):
if 'tomorrow' in s: todo = []
todo.append(lambda i: i + 86400) s = s.replace('noon', '12:00')
s = s.replace('tomorrow', '') s = s.replace('midnight', '00:00')
if 'next week' in s: if 'tomorrow' in s:
todo.append(lambda i: i + 86400*7) todo.append(lambda i: i + 86400)
s = s.replace('next week', '') s = s.replace('tomorrow', '')
i = int(time.mktime(parser.parse(s, fuzzy=True).timetuple())) if 'next week' in s:
for f in todo: todo.append(lambda i: i + 86400*7)
i = f(i) s = s.replace('next week', '')
return i 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): class Time(callbacks.Plugin):
@internationalizeDocstring @internationalizeDocstring
@ -99,6 +102,9 @@ class Time(callbacks.Plugin):
<time string> can be any number of natural formats; just try something <time string> can be any number of natural formats; just try something
and see if it will work. 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()) now = int(time.time())
new = parse(s) new = parse(s)
if new != now: if new != now:
@ -113,6 +119,9 @@ class Time(callbacks.Plugin):
Returns the number of seconds until <time string>. 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()) now = int(time.time())
new = parse(s) new = parse(s)
if new != now: if new != now: