Skip to content

Commit

Permalink
[rss] Added a private method to display a command's docstring
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
saltire committed Sep 12, 2013
1 parent 11a3acd commit 8d2d8a9
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions willie/modules/rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <command>"""
if not trigger.admin:
Expand Down Expand Up @@ -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> <Feed_Name> <URL> [fg] [bg]")
self._show_doc(bot, 'add')
return

channel = match.group(1)
Expand Down Expand Up @@ -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)
Expand All @@ -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 <enable|disable> [#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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 <command>"""
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 <command>")
bot.reply("Available RSS commands: " + ', '.join(self.actions))
Expand Down

0 comments on commit 8d2d8a9

Please sign in to comment.