Skip to content

Commit

Permalink
[bugzilla, github, reddit, youtube] Remove handlers on shutdown
Browse files Browse the repository at this point in the history
This solves #615. When reloading these modules, a new compiled regex
object was created and inserted into the url handler set. Because it was
a new object, it didn't replace the old one (even though the pattern was
the same). This meant that every time the module was reloaded, another
version of the function was added, meaning the output would be produced
an additional time.
  • Loading branch information
embolalia committed Sep 1, 2014
1 parent 439b09e commit de4abaa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
7 changes: 7 additions & 0 deletions willie/modules/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from willie.module import rule


regex = None

def configure(config):
"""
Expand All @@ -30,6 +32,7 @@ def configure(config):


def setup(bot):
global regex
regexes = []
if not (bot.config.has_option('bugzilla', 'domains')
and bot.config.bugzilla.get_list('domains')):
Expand All @@ -45,6 +48,10 @@ def setup(bot):
bot.memory['url_callbacks'][regex] = show_bug


def shutdown(bot):
del bot.memory['url_callbacks'][regex]


@rule(r'.*https?://(\S+?)'
'(/show_bug.cgi\?\S*?)'
'(id=\d+).*')
Expand Down
8 changes: 7 additions & 1 deletion willie/modules/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'([A-z0-9\-]+/[A-z0-9\-]+)/'
'(?:issues|pull)/'
'([\d]+)')
regex = re.compile(issueURL)

def checkConfig(bot):
if not bot.config.has_option('github', 'oauth_token') or not bot.config.has_option('github', 'repo'):
Expand All @@ -45,12 +46,17 @@ def configure(config):
config.interactive_add('github', 'repo', 'Github repository', 'embolalia/willie')
return chunk


def setup(bot):
regex = re.compile(issueURL)
if not bot.memory.contains('url_callbacks'):
bot.memory['url_callbacks'] = tools.WillieMemory()
bot.memory['url_callbacks'][regex] = issue_info


def shutdown(bot):
del bot.memory['url_callbacks'][regex]


@commands('makeissue', 'makebug')
def issue(bot, trigger):
"""Create a GitHub issue, also known as a bug report. Syntax: .makeissue Title of the bug report"""
Expand Down
9 changes: 7 additions & 2 deletions willie/modules/reddit-info.py → willie/modules/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@
domain = r'https?://(?:www\.|np\.)?reddit\.com'
post_url = '(%s/r/.*?/comments/[\w-]+)' % domain
user_url = '%s/u(ser)?/([\w-]+)' % domain
post_regex = re.compile(post_url)
user_regex = re.compile(user_url)


def setup(bot):
post_regex = re.compile(post_url)
user_regex = re.compile(user_url)
if not bot.memory.contains('url_callbacks'):
bot.memory['url_callbacks'] = tools.WillieMemory()
bot.memory['url_callbacks'][post_regex] = rpost_info
bot.memory['url_callbacks'][user_regex] = redditor_info


def shutdown(bot):
del bot.memory['url_callbacks'][post_regex]
del bot.memory['url_callbacks'][user_regex]


@rule('.*%s.*' % post_url)
def rpost_info(bot, trigger, match=None):
r = praw.Reddit(user_agent='Willie IRC bot - see dft.ba/-williesource for more')
Expand Down
8 changes: 7 additions & 1 deletion willie/modules/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
else:
from html.parser import HTMLParser

regex = re.compile('(youtube.com/watch\S*v=|youtu.be/)([\w-]+)')


def setup(bot):
regex = re.compile('(youtube.com/watch\S*v=|youtu.be/)([\w-]+)')
if not bot.memory.contains('url_callbacks'):
bot.memory['url_callbacks'] = tools.WillieMemory()
bot.memory['url_callbacks'][regex] = ytinfo


def shutdown(bot):
del bot.memory['url_callbacks'][regex]


def ytget(bot, trigger, uri):
bytes = web.get(uri)
result = json.loads(bytes)
Expand Down

0 comments on commit de4abaa

Please sign in to comment.