diff --git a/ChangeLog b/ChangeLog index 9c2eca0a9..402f812fe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -47,6 +47,8 @@ * Fixed bug in Gameknot.gkstats when retrieving the stats for users who haven't yet played a game. + + * Added a numUsers() method to ircdb.UsersDB (ircdb.users). 2003-11-18 Jeremy Fincher diff --git a/src/ircdb.py b/src/ircdb.py index 2c8b34073..5301b5320 100644 --- a/src/ircdb.py +++ b/src/ircdb.py @@ -37,7 +37,7 @@ import os import sets import time import string -from itertools import imap +from itertools import imap, ilen, ifilter import log import conf @@ -458,6 +458,9 @@ class UsersDB(object): except KeyError: return False + def numUsers(self): + return ilen(ifilter(None, self.users))-1 # -1 is for the bot user. + def setUser(self, id, user): """Sets a user (given its id) to the IrcUser given it.""" assert isinstance(id, int), 'setUser takes an integer userId.' diff --git a/test/test_ircdb.py b/test/test_ircdb.py index de1908d89..1634785fa 100644 --- a/test/test_ircdb.py +++ b/test/test_ircdb.py @@ -311,6 +311,27 @@ class UsersDBTestCase(unittest.TestCase): pass self.users = ircdb.UsersDB(self.filename) + def testNumUsers(self): + self.assertEqual(self.users.numUsers(), 0) + (id, u) = self.users.newUser() + hostmask = 'foo!bar@baz' + banmask = ircutils.banmask(hostmask) + u.addHostmask(banmask) + u.name = 'foo' + self.users.setUser(id, u) + self.assertEqual(self.users.numUsers(), 1) + (id, u) = self.users.newUser() + hostmask = 'biff!fladksfj@blakjdsf' + banmask = ircutils.banmask(hostmask) + u.addHostmask(banmask) + u.name = 'biff' + self.users.setUser(id, u) + self.assertEqual(self.users.numUsers(), 2) + self.users.delUser(2) + self.assertEqual(self.users.numUsers(), 1) + self.users.delUser(1) + self.assertEqual(self.users.numUsers(), 0) + def testGetSetDelUser(self): self.assertRaises(KeyError, self.users.getUser, 'foo') self.assertRaises(KeyError, self.users.getUser, 'foo!bar@baz')