From 8d2d8a9d740cd966f92606bdac8647a89526f3fb Mon Sep 17 00:00:00 2001 From: saltire sable Date: Thu, 12 Sep 2013 10:28:11 -0400 Subject: [PATCH] [rss] Added a private method to display a command's docstring Eliminates a bit of redundancy. Also corrected a few errors in the docstrings, which should be less frequent now that we're not repeating them anywhere. --- willie/modules/rss.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/willie/modules/rss.py b/willie/modules/rss.py index 8f8e726b40..3ab6722558 100644 --- a/willie/modules/rss.py +++ b/willie/modules/rss.py @@ -17,6 +17,7 @@ from willie.module import commands, interval from willie.config import ConfigurationError + socket.setdefaulttimeout(10) INTERVAL = 60 * 5 # seconds between checking for new updates @@ -136,6 +137,13 @@ def __init__(self, bot): self.actions = sorted(method[5:] for method in dir(self) if method[:5] == '_rss_') + def _show_doc(self, bot, command): + """Given an RSS command, say the docstring for the corresponding method.""" + for line in getattr(self, '_rss_' + command).__doc__.split('\n'): + if line: + bot.reply(line.strip()) + + def manage_rss(self, bot, trigger): """Manage RSS feeds. Usage: .rss """ if not trigger.admin: @@ -185,7 +193,7 @@ def _rss_add(self, bot, trigger, c): ''' match = re.match(pattern, trigger.group(), re.IGNORECASE | re.VERBOSE) if match is None: - bot.reply("Add a feed to a channel, or modify an existing one. Usage: .rss add <#channel> [fg] [bg]") + self._show_doc(bot, 'add') return channel = match.group(1) @@ -222,8 +230,9 @@ def _rss_del(self, bot, trigger, c): (?:\s+("[^"]+"|[\w-]+))? # name (optional) """ match = re.match(pattern, trigger.group(), re.IGNORECASE | re.VERBOSE) + # at least one of channel and feed name is required if match is None or (not match.group(1) and not match.group(2)): - bot.reply("Remove one or all feeds from one or all channels. Usage: .rss del [#channel] [Feed_Name]") + self._show_doc(bot, 'del') return channel = match.group(1) @@ -250,24 +259,25 @@ def _rss_enable(self, bot, trigger, c): def _rss_disable(self, bot, trigger, c): - """Disable a feed or feeds. Usage: .rss enable [#channel] [Feed_Name]""" + """Disable a feed or feeds. Usage: .rss disable [#channel] [Feed_Name]""" return self._toggle(bot, trigger, c) def _toggle(self, bot, trigger, c): """Enable or disable a feed or feeds. Usage: .rss [#channel] [Feed_Name]""" + command = trigger.group(3) + pattern = r""" ^\.rss\s+(enable|disable) # command (?:\s+([&#+!][^\s,]+))? # channel (optional) (?:\s+("[^"]+"|[\w-]+))? # name (optional) """ match = re.match(pattern, trigger.group(), re.IGNORECASE | re.VERBOSE) + # at least one of channel and feed name is required if match is None or (not match.group(2) and not match.group(3)): - bot.reply("{0} a feed or feeds. Usage: .rss toggle [#channel] [Feed_Name]".format( - trigger.group(3).capitalize())) + self._show_doc(bot, command) return - command = trigger.group(3) enabled = 1 if command == 'enable' else 0 channel = match.group(2) feed_name = match.group(3).strip('"') if match.group(3) else None @@ -296,7 +306,7 @@ def _rss_list(self, bot, trigger, c): """ match = re.match(pattern, trigger.group(), re.IGNORECASE | re.VERBOSE) if match is None: - bot.reply("Remove one or all feeds from one or all channels. Usage: .rss del [#channel] [Feed_Name]") + self._show_doc(bot, 'list') return channel = match.group(1) @@ -337,11 +347,9 @@ def _rss_fetch(self, bot, trigger, c): def _rss_help(self, bot, trigger, c): """Get help on any of the RSS feed commands. Usage: .rss help """ - if trigger.group(4) in self.actions: - # print the docstring from the given method - for line in getattr(self, '_rss_' + trigger.group(4)).__doc__.split('\n'): - if line: - bot.reply(line.strip()) + command = trigger.group(4) + if command in self.actions: + self._show_doc(bot, command) else: bot.reply("For help on a command, type: .rss help ") bot.reply("Available RSS commands: " + ', '.join(self.actions))