From 2c81f72471416054f72767e79bde1e0f1b6bf33b Mon Sep 17 00:00:00 2001 From: Florian Strzelecki Date: Wed, 20 Mar 2019 00:16:34 +0100 Subject: [PATCH] plugins: register and unregister URL Callbacks using new interface --- sopel/modules/bugzilla.py | 12 ++---------- sopel/modules/instagram.py | 8 +++----- sopel/modules/reddit.py | 12 +++++------- sopel/modules/wikipedia.py | 10 +++++----- 4 files changed, 15 insertions(+), 27 deletions(-) diff --git a/sopel/modules/bugzilla.py b/sopel/modules/bugzilla.py index 77e1c12534..42e9e66545 100644 --- a/sopel/modules/bugzilla.py +++ b/sopel/modules/bugzilla.py @@ -11,7 +11,6 @@ import xmltodict -from sopel import tools from sopel.config.types import StaticSection, ListAttribute from sopel.logger import get_logger from sopel.module import rule @@ -46,24 +45,17 @@ def setup(bot): if not bot.config.bugzilla.domains: return - if not bot.memory.contains('url_callbacks'): - bot.memory['url_callbacks'] = tools.SopelMemory() domains = '|'.join(bot.config.bugzilla.domains) regex = re.compile((r'https?://(%s)' r'(/show_bug.cgi\?\S*?)' r'(id=\d+)') % domains) - bot.memory['url_callbacks'][regex] = show_bug + bot.register_url_callback(regex, show_bug) def shutdown(bot): - try: - del bot.memory['url_callbacks'][regex] - except KeyError: - # bot.config.bugzilla.domains was probably just empty on startup - # everything's daijoubu - pass + bot.unregister_url_callback(regex) @rule(r'.*https?://(\S+?)' diff --git a/sopel/modules/instagram.py b/sopel/modules/instagram.py index 7826bfd417..a525115f9f 100644 --- a/sopel/modules/instagram.py +++ b/sopel/modules/instagram.py @@ -13,7 +13,7 @@ from requests import get -from sopel import module, tools +from sopel import module try: from ujson import loads @@ -26,13 +26,11 @@ def setup(bot): - if not bot.memory.contains('url_callbacks'): - bot.memory['url_callbacks'] = tools.SopelMemory() - bot.memory['url_callbacks'][instagram_pattern] = instaparse + bot.register_url_callback(instagram_pattern, instaparse) def shutdown(bot): - del bot.memory['url_callbacks'][instagram_pattern] + bot.unregister_url_callback(instagram_pattern) # TODO: Parse Instagram profile page diff --git a/sopel/modules/reddit.py b/sopel/modules/reddit.py index 5c74a7101a..e6ab1d9fa0 100644 --- a/sopel/modules/reddit.py +++ b/sopel/modules/reddit.py @@ -5,7 +5,7 @@ from sopel.module import commands, rule, example, require_chanmsg, NOLIMIT, OP from sopel.formatting import bold, color, colors from sopel.web import USER_AGENT -from sopel.tools import SopelMemory, time +from sopel.tools import time import datetime as dt import praw import re @@ -34,15 +34,13 @@ def setup(bot): - if not bot.memory.contains('url_callbacks'): - bot.memory['url_callbacks'] = SopelMemory() - bot.memory['url_callbacks'][post_regex] = rpost_info - bot.memory['url_callbacks'][user_regex] = redditor_info + bot.register_url_callback(post_regex, rpost_info) + bot.register_url_callback(user_regex, redditor_info) def shutdown(bot): - del bot.memory['url_callbacks'][post_regex] - del bot.memory['url_callbacks'][user_regex] + bot.unregister_url_callback(post_regex) + bot.unregister_url_callback(user_regex) @rule('.*%s.*' % post_url) diff --git a/sopel/modules/wikipedia.py b/sopel/modules/wikipedia.py index 493af46c40..ae0e4f53b0 100644 --- a/sopel/modules/wikipedia.py +++ b/sopel/modules/wikipedia.py @@ -3,7 +3,6 @@ # Licensed under the Eiffel Forum License 2. from __future__ import unicode_literals, absolute_import, print_function, division -from sopel import tools from sopel.config.types import StaticSection, ValidatedAttribute from sopel.module import NOLIMIT, commands, example, rule from requests import get @@ -19,6 +18,7 @@ from urllib.parse import quote, unquote REDIRECT = re.compile(r'^REDIRECT (.*)') +WIKIPEDIA_REGEX = re.compile('([a-z]+).(wikipedia.org/wiki/)([^ ]+)') class WikipediaSection(StaticSection): @@ -30,11 +30,11 @@ class WikipediaSection(StaticSection): def setup(bot): bot.config.define_section('wikipedia', WikipediaSection) + bot.register_url_callback(WIKIPEDIA_REGEX, mw_info) - regex = re.compile('([a-z]+).(wikipedia.org/wiki/)([^ ]+)') - if not bot.memory.contains('url_callbacks'): - bot.memory['url_callbacks'] = tools.SopelMemory() - bot.memory['url_callbacks'][regex] = mw_info + +def shutdown(bot): + bot.unregister_url_callback(WIKIPEDIA_REGEX) def configure(config):