From 2ba7bc5c169667c467b85251466f47f56f5b0202 Mon Sep 17 00:00:00 2001 From: Ken Spencer Date: Fri, 27 Oct 2017 13:25:02 -0400 Subject: [PATCH] Time: add 'ddate' (#1310) * Time: add 'ddate', fix contributors, add Time.local as a package_dir in setup.py * Time: add in license information for ddate.py * Time: change 'ddate' command to where it will error out gracefully if 'ddate.base' isn't available * Time: add tests, remove Time.local from package_dir --- plugins/Time/__init__.py | 5 +++-- plugins/Time/local/__init__.py | 0 plugins/Time/plugin.py | 24 +++++++++++++++++++++++- plugins/Time/test.py | 16 +++++++++++++++- 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 plugins/Time/local/__init__.py diff --git a/plugins/Time/__init__.py b/plugins/Time/__init__.py index 3f96f4e4e..6f28080b3 100644 --- a/plugins/Time/__init__.py +++ b/plugins/Time/__init__.py @@ -42,8 +42,9 @@ __author__ = supybot.authors.jemfinch # This is a dictionary mapping supybot.Author instances to lists of # contributions. -__contributors__ = {'tztime': supybot.Author('Valentin Lorentz', 'ProgVal', - 'progval@gmail.com')} +__contributors__ = {supybot.authors.progval: ['tztime'], + supybot.Author('Ken Spencer', 'kspencer', + 'iota@electrocode.net'): ['ddate']} from . import config diff --git a/plugins/Time/local/__init__.py b/plugins/Time/local/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/plugins/Time/plugin.py b/plugins/Time/plugin.py index 2696aaeab..5d9d2f0f6 100644 --- a/plugins/Time/plugin.py +++ b/plugins/Time/plugin.py @@ -27,17 +27,24 @@ # POSSIBILITY OF SUCH DAMAGE. ### +import sys import time TIME = time # For later use. from datetime import datetime import supybot.conf as conf +import supybot.log as log import supybot.utils as utils from supybot.commands import * import supybot.callbacks as callbacks from supybot.i18n import PluginInternationalization, internationalizeDocstring _ = PluginInternationalization('Time') +try: + from ddate.base import DDate as _ddate +except ImportError: + log.info("'ddate' not available, disabling command.") + _ddate = None try: from dateutil import parser @@ -206,7 +213,22 @@ class Time(callbacks.Plugin): irc.reply(datetime.now(timezone).strftime(format)) tztime = wrap(tztime, ['text']) - + def ddate(self, irc, msg, args, year=None, month=None, day=None): + """[ ] + Returns a the Discordian date today, or an optional different date.""" + if _ddate is not None: + if year is not None and month is not None and day is not None: + try: + irc.reply(_ddate(datetime(year=year, month=month, day=day))) + except ValueError as e: + irc.error("%s", e) + else: + irc.reply(_ddate()) + else: + irc.error(_("The 'ddate' module is not installed." + " Use '%s -m pip install --user ddate'" + " See for more information.") % sys.executable) + ddate = wrap(ddate, [optional('positiveint'), optional('positiveint'), optional('positiveint')]) Class = Time # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/plugins/Time/test.py b/plugins/Time/test.py index ff1f4897b..a844e19af 100644 --- a/plugins/Time/test.py +++ b/plugins/Time/test.py @@ -43,6 +43,13 @@ except ImportError: else: has_dateutil = True +try: + import ddate.base +except ImportError: + has_ddate = False +else: + has_ddate = True + try: from unittest import skipIf except ImportError: # Python 2.6 @@ -92,6 +99,13 @@ class TimeTestCase(PluginTestCase): def testNoNestedErrors(self): self.assertNotError('echo [seconds 4m]') + @skipIf(not has_ddate, 'ddate is missing') + def testDDate(self): + self.assertNotError('ddate') + self.assertHelp('ddate 0 0 0') # because nonsense was put in + self.assertHelp('ddate -1 1 1') # because nonsense was put in + self.assertHelp('ddate -1 -1 -1') # because nonsense was put in + # plugin.py:223 would catch these otherwise + self.assertResponse('ddate 1 1 1', 'Sweetmorn, the 1st day of Chaos in the YOLD 1167') # make sure the laws of physics and time aren't out of wack # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: -