make @baninfo display time in human readable format, instead of number of seconds.
This commit is contained in:
parent
401e5ae5d4
commit
6640435520
|
@ -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 <elapsed> 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))
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue