From f7a3504a93d2717c91e6b09dc6b88d09b48c4157 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Thu, 2 Dec 2004 05:33:29 +0000 Subject: [PATCH] Added getUserName. --- plugins/News.py | 15 +++++---------- plugins/Poll.py | 10 +++------- plugins/Quotes.py | 7 +------ plugins/__init__.py | 14 ++++++++++---- 4 files changed, 19 insertions(+), 27 deletions(-) diff --git a/plugins/News.py b/plugins/News.py index dadbbc85a..ef2aae597 100644 --- a/plugins/News.py +++ b/plugins/News.py @@ -61,21 +61,16 @@ class DbiNewsDB(plugins.DbiChannelDB): def __str__(self): format = conf.supybot.humanTimestampFormat() - try: - user = ircdb.users.getUser(int(self.by)).name - except ValueError: - user = self.by - except KeyError: - user = 'a user that is no longer registered' - if int(self.expires) == 0: + user = plugins.getUserName(self.by) + if self.expires == 0: s = '%s (Subject: "%s", added by %s on %s)' % \ (self.text, self.subject, self.by, - time.strftime(format, time.localtime(int(self.at)))) + time.strftime(format, time.localtime(self.at))) else: s = '%s (Subject: "%s", added by %s on %s, expires at %s)' s = s % (self.text, self.subject, user, - time.strftime(format, time.localtime(int(self.at))), - time.strftime(format, time.localtime(int(self.expires)))) + time.strftime(format, time.localtime(self.at)), + time.strftime(format, time.localtime(self.expires))) return s def __init__(self, filename): diff --git a/plugins/Poll.py b/plugins/Poll.py index 449c51314..5389e1610 100644 --- a/plugins/Poll.py +++ b/plugins/Poll.py @@ -71,12 +71,9 @@ class PollRecord(dbi.Record): ] def __str__(self): format = conf.supybot.humanTimestampFormat() - try: - user = ircdb.users.getUser(int(self.by)).name - except KeyError: - user = 'a user that is no longer registered' + user = plugins.getUserName(self.by) if self.options: - options = 'Options: %s' % '; '.join(map(str, self.options)) + options = 'Options: %s' % '; '.join(self.options) else: options = 'The poll has no options, yet' if self.status: @@ -84,8 +81,7 @@ class PollRecord(dbi.Record): else: status = 'closed' return 'Poll #%s: %s started by %s. %s. Poll is %s.' % \ - (self.id, utils.quoted(self.question), user, - options, status) + (self.id, utils.quoted(self.question), user, options, status) class SqlitePollDB(object): def __init__(self, filename): diff --git a/plugins/Quotes.py b/plugins/Quotes.py index 87569204e..12ec6b9ee 100644 --- a/plugins/Quotes.py +++ b/plugins/Quotes.py @@ -63,12 +63,7 @@ class QuoteRecord(dbi.Record): ] def __str__(self): format = conf.supybot.humanTimestampFormat() - try: - user = ircdb.users.getUser(int(self.by)).name - except ValueError: - user = self.by - except KeyError: - user = 'a user that is no longer registered' + user = plugins.getUserName(self.by) return 'Quote %s added by %s at %s.' % \ (utils.quoted(self.text), user, time.strftime(format, time.localtime(float(self.at)))) diff --git a/plugins/__init__.py b/plugins/__init__.py index 99eb3987b..36a55c831 100644 --- a/plugins/__init__.py +++ b/plugins/__init__.py @@ -355,6 +355,15 @@ class ChannelUserDB(ChannelUserDictionary): raise NotImplementedError +def getUserName(id): + if isinstance(id, int): + try: + return ircdb.users.getUser(id).name + except KeyError: + return 'a user that is no longer registered' + else: + return id + class ChannelIdDatabasePlugin(callbacks.Privmsg): class DB(DbiChannelDB): class DB(dbi.DB): @@ -470,10 +479,7 @@ class ChannelIdDatabasePlugin(callbacks.Privmsg): additional(rest('glob'))]) def showRecord(self, record): - try: - name = ircdb.users.getUser(record.by).name - except KeyError: - name = 'a user that is no longer registered' + name = getUserName(record.by) at = time.localtime(record.at) timeS = time.strftime(conf.supybot.humanTimestampFormat(), at) return '%s #%s: %s (added by %s at %s)' % \