From 6640435520f3d8b149101a2d06b5717d2d5344e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eli=C3=A1n=20Hanisch?= Date: Sun, 15 Jul 2012 21:15:06 -0300 Subject: [PATCH] make @baninfo display time in human readable format, instead of number of seconds. --- Bantracker/plugin.py | 67 ++++++++++++++++++++++++++++++++++++-------- Bantracker/test.py | 4 ++- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/Bantracker/plugin.py b/Bantracker/plugin.py index b451c1d..9b8de36 100644 --- a/Bantracker/plugin.py +++ b/Bantracker/plugin.py @@ -53,6 +53,7 @@ import supybot.ircmsgs as ircmsgs import supybot.conf as conf import supybot.ircdb as ircdb import supybot.schedule as schedule +import supybot.utils as utils from fnmatch import fnmatch import sqlite import pytz @@ -142,6 +143,47 @@ def readTimeDelta(s): return seconds +# utils.gen.timeElapsed is too noisy, what do I care of the seconds and minutes +# if the period is like a month long, or the zero values? +def timeElapsed(elapsed, short=False, resolution=2): + """Given seconds, returns a string with an English description of + the amount of time passed. + """ + + ret = [] + before = False + def Format(s, i): + if i: + if short: + ret.append('%s%s' % (i, s[0])) + else: + ret.append(utils.str.format('%n', (i, s))) + elapsed = int(elapsed) + + # Handle negative times + if elapsed < 0: + before = True + elapsed = -elapsed + + for s, i in (('year', 31536000), ('month', 2592000), ('week', 604800), + ('day', 86400), ('hour', 3600), ('minute', 60)): + count, elapsed = elapsed // i, elapsed % i + Format(s, count) + if len(ret) == resolution: + break + #Format('second', elapsed) # seconds are pointless for now + if not ret: + raise ValueError, 'Time difference not great enough to be noted.' + result = '' + #ret = ret[:resolution] + if short: + result = ' '.join(ret) + else: + result = utils.str.format('%L', ret) + if before: + result += ' ago' + return result + def capab(user, capability): capability = capability.lower() capabilities = list(user.capabilities) @@ -1512,19 +1554,22 @@ class Bantracker(callbacks.Plugin): else: br = None + expires = None if br: - irc.reply("[%s] %s - %s - %s - expires in %s" \ - % (id, type, mask, channel, - (br.ban.when + br.expires) - nowSeconds())) - return + expires = (br.ban.when + br.expires) - nowSeconds() + try: + expires = "expires in %s" % timeElapsed(expires) + except ValueError: + expires = "expires soon" + else: + if type in ('quiet', 'ban'): + if not removal: + expires = "never expires" + else: + expires = "not active" - if type in ('quiet', 'ban'): - if not removal: - irc.reply("[%s] %s - %s - %s - never expires" \ - % (id, type, mask, channel)) - else: - irc.reply("[%s] %s - %s - %s - not active" \ - % (id, type, mask, channel)) + if expires: + irc.reply("[%s] %s - %s - %s - %s" % (id, type, mask, channel, expires)) else: irc.reply("[%s] %s - %s - %s" % (id, type, mask, channel)) diff --git a/Bantracker/test.py b/Bantracker/test.py index 69193e4..bbbeb11 100644 --- a/Bantracker/test.py +++ b/Bantracker/test.py @@ -452,7 +452,9 @@ class BantrackerTestCase(ChannelPluginTestCase): self.feedBan('asd!*@*') self.assertResponse('baninfo 1', '[1] ban - asd!*@* - #test - never expires') self.assertNotError('banremove 1 10') - self.assertResponse('baninfo 1', '[1] ban - asd!*@* - #test - expires in 10.0') + self.assertResponse('baninfo 1', '[1] ban - asd!*@* - #test - expires soon') + self.assertNotError('banremove 1 34502') + self.assertResponse('baninfo 1', '[1] ban - asd!*@* - #test - expires in 9 hours and 35 minutes') self.irc.feedMsg(ircmsgs.unban(self.channel, 'asd!*@*', 'op!user@host.net')) self.assertResponse('baninfo 1', '[1] ban - asd!*@* - #test - not active')