From a4701b258c35240e2610b13994551ecfda00b66e Mon Sep 17 00:00:00 2001 From: James Lu Date: Mon, 11 Nov 2019 11:53:30 -0800 Subject: [PATCH] Plugin: accept author name in 'contributors' command --- plugins/Plugin/plugin.py | 33 ++++++++++++++++----------------- plugins/Plugin/test.py | 6 ++++++ src/__init__.py | 7 +++++-- test/test_misc.py | 6 ++++++ 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/plugins/Plugin/plugin.py b/plugins/Plugin/plugin.py index 6fc41c569..d48f0c1c4 100644 --- a/plugins/Plugin/plugin.py +++ b/plugins/Plugin/plugin.py @@ -139,26 +139,19 @@ class Plugin(callbacks.Plugin): @internationalizeDocstring def contributors(self, irc, msg, args, cb, nick): - """ [] + """ [] Replies with a list of people who made contributions to a given plugin. - If is specified, that person's specific contributions will - be listed. Note: The is the part inside of the parentheses - in the people listing. + If is specified, that person's specific contributions will + be listed. You can specify a person's name by their full name or their nick, + which is shown inside brackets if available. """ - def getShortName(authorInfo): - """ - Take an Authors object, and return only the name and nick values - in the format 'First Last (nick)'. - """ - return '%(name)s (%(nick)s)' % authorInfo.__dict__ - def buildContributorsString(longList): """ Take a list of long names and turn it into : shortname[, shortname and shortname]. """ - L = [getShortName(n) for n in longList] + L = [authorInfo.format(short=True) for authorInfo in longList] return format('%L', L) def buildPeopleString(module): @@ -187,12 +180,18 @@ class Plugin(callbacks.Plugin): for the requested plugin. """ contributors = getattr(module, '__contributors__', {}) - # Make a mapping of nicks to author instances - contributorNicks = dict((author.nick.lower(), author) for author in contributors.keys()) + # Make a mapping of nicks and names to author instances + contributorNicks = {} + for contributor in contributors.keys(): + if contributor.nick: + contributorNicks[contributor.nick.lower()] = contributor + if contributor.name: + contributorNicks[contributor.name.lower()] = contributor lnick = nick.lower() author = getattr(module, '__author__', supybot.authors.unknown) - if author != supybot.authors.unknown and lnick == author.nick.lower(): + if author != supybot.authors.unknown and \ + (lnick == (author.name or '').lower() or lnick == (author.nick or '').lower()): # Special case for the plugin author. We remove legacy handling of the case where # someone is listed both as author and contributor, which should never really happen? return _('%s wrote the %s plugin.') % (author, cb.name()) @@ -201,7 +200,7 @@ class Plugin(callbacks.Plugin): authorInfo = contributorNicks[lnick] contributions = contributors[authorInfo] - fullName = getShortName(authorInfo) + fullName = authorInfo.format(short=True) if contributions: return format(_('%s contributed the following to %s: %s'), @@ -217,7 +216,7 @@ class Plugin(callbacks.Plugin): else: nick = ircutils.toLower(nick) irc.reply(buildPersonString(module)) - contributors = wrap(contributors, ['plugin', additional('nick')]) + contributors = wrap(contributors, ['plugin', additional('text')]) Plugin = internationalizeDocstring(Plugin) Class = Plugin diff --git a/plugins/Plugin/test.py b/plugins/Plugin/test.py index 9f9c7f91f..9f2bcc1eb 100644 --- a/plugins/Plugin/test.py +++ b/plugins/Plugin/test.py @@ -54,6 +54,12 @@ class PluginTestCase(PluginTestCase): # Test ability to list contributions # As of 2019-10-19 there is no more distinction between commands and non-commands self.assertRegexp('contributors Plugin skorobeus', 'original contributors command') + self.assertRegexp('contributors Plugin Kevin Murphy', 'original contributors command') + self.assertRegexp('contributors Plugin James Lu', 'refactored contributors command') + + # Test handling of the plugin author, who is usually not listed in __contributors__ + self.assertRegexp('contributors Plugin jemfinch', 'wrote the Plugin plugin') + self.assertRegexp('contributors Plugin Jeremy Fincher', 'wrote the Plugin plugin') # TODO: test handling of a person with multiple contributions to a command diff --git a/src/__init__.py b/src/__init__.py index 240d57a17..2d2b014b8 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -50,7 +50,7 @@ class Author(object): self.nick = nick self.email = email - def __str__(self): + def format(self, short=False): # If only one of these are defined, take the nick as the name name = self.name or self.nick or 'Unknown author' @@ -58,11 +58,14 @@ class Author(object): if self.nick and name != self.nick: # Format as "Name (nick)" if both are given and different s += ' (%s)' % self.nick - if self.email: + if self.email and not short: # Add "Name (nick) " or "Name " if provided s += ' <%s>' % self.email return s + def __str__(self): + return self.format() + class authors(object): # This is basically a bag. jemfinch = Author('Jeremy Fincher', 'jemfinch', 'jemfinch@users.sf.net') jamessan = Author('James McCoy', 'jamessan', 'vega.james@gmail.com') diff --git a/test/test_misc.py b/test/test_misc.py index a1e55604a..426445a5f 100644 --- a/test/test_misc.py +++ b/test/test_misc.py @@ -54,4 +54,10 @@ class MiscTestCase(SupyTestCase): # Only email? self.assertEqual(str(supybot.Author(email='xyzzy@localhost.localdomain')), 'Unknown author ') + def testAuthorExpandShort(self): + self.assertEqual(supybot.authors.progval.format(short=True), + 'Valentin Lorentz (ProgVal)') + self.assertEqual(supybot.authors.jlu.format(short=True), + 'James Lu') + # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: