Added substitution and fixed ' returns the same thing all the time' bug.

This commit is contained in:
Jeremy Fincher 2003-11-05 04:57:13 +00:00
parent 57e0e75c20
commit c803edc689
2 changed files with 31 additions and 21 deletions

View File

@ -31,6 +31,7 @@
import fix import fix
import gc
import os import os
import sys import sys
import sets import sets
@ -100,6 +101,7 @@ class ChannelDBHandler(object):
except AttributeError: # In case it doesn't have a close method. except AttributeError: # In case it doesn't have a close method.
pass pass
del db del db
gc.collect()
class PeriodicFileDownloader(object): class PeriodicFileDownloader(object):
@ -309,31 +311,32 @@ class Toggleable(object):
irc.error(msg, '%r isn\'t a valid name to toggle. ' irc.error(msg, '%r isn\'t a valid name to toggle. '
'Valid names are %s' % (name, self._toggleNames())) 'Valid names are %s' % (name, self._toggleNames()))
_randomnickRe = re.compile ("\$randomnick", re.I) _randomnickRe = re.compile(r'\$randomnick', re.I)
_randomdateRe = re.compile ("\$randomdate", re.I) _randomdateRe = re.compile(r'\$randomdate', re.I)
_randomintRe = re.compile ("\$randomint", re.I) _randomintRe = re.compile(r'\$randomint', re.I)
_whoRe = re.compile ("\$who", re.I) _channelRe = re.compile(r'\$channel', re.I)
_botnickRe = re.compile("\$botnick", re.I) _whoRe = re.compile(r'\$who', re.I)
_todayRe = re.compile("\$today", re.I) _botnickRe = re.compile(r'\$botnick', re.I)
_nowRe = re.compile("\$now", re.I) _todayRe = re.compile(r'\$today', re.I)
def randInt(m): _nowRe = re.compile(r'\$now', re.I)
return str(random.randint(-1000, 1000))
def randDate(m):
t = pow(2,30)*random.random()+time.time()/4.0
return time.ctime(t)
def standardSubstitute(irc, msg, text): def standardSubstitute(irc, msg, text):
"""Do the standard set of substitutions on text, and return it""" """Do the standard set of substitutions on text, and return it"""
if ircutils.isChannel(msg.args[0]): if ircutils.isChannel(msg.args[0]):
channel = msg.args[0] channel = msg.args[0]
else: else:
channel = None channel = 'somewhere'
if channel: def randInt(m):
user = random.choice(list(irc.state.channels[channel].users)) return str(random.randint(-1000, 1000))
text = _randomnickRe.sub(user, text) def randDate(m):
t = pow(2,30)*random.random()+time.time()/4.0
return time.ctime(t)
def randNick(m):
if channel != 'somewhere':
return random.choice(list(irc.state.channels[channel].users))
else: else:
text = _randomnickRe.sub('anyone', text) return 'someone'
text = _channelRe.sub(channel, text)
text = _randomnickRe.sub(randNick, text)
text = _randomdateRe.sub(randDate, text) text = _randomdateRe.sub(randDate, text)
text = _randomintRe.sub(randInt, text) text = _randomintRe.sub(randInt, text)
text = _whoRe.sub(msg.nick, text) text = _whoRe.sub(msg.nick, text)

View File

@ -93,7 +93,7 @@ class ToggleDictionaryTestCase(unittest.TestCase):
class holder: class holder:
users = sets.Set(['foo', 'bar', 'baz']) users = sets.Set(map(str, range(1000)))
class FunctionsTestCase(unittest.TestCase): class FunctionsTestCase(unittest.TestCase):
class irc: class irc:
@ -124,6 +124,13 @@ class FunctionsTestCase(unittest.TestCase):
self.assert_(plugins.standardSubstitute(self.irc, msg, '$now')) self.assert_(plugins.standardSubstitute(self.irc, msg, '$now'))
n = plugins.standardSubstitute(self.irc, msg, '$randomnick') n = plugins.standardSubstitute(self.irc, msg, '$randomnick')
self.failUnless(n in self.irc.state.channels['#foo'].users) self.failUnless(n in self.irc.state.channels['#foo'].users)
n = plugins.standardSubstitute(self.irc, msg, '$randomnick '*100)
L = n.split()
self.failIf(all(L[0].__eq__, L), 'all $randomnicks were the same')
c = plugins.standardSubstitute(self.irc, msg, '$channel')
self.assertEqual(c, msg.args[0])