Skip to content

Commit

Permalink
plugins: register and unregister URL Callbacks using new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Exirel committed Mar 20, 2019
1 parent 0c005e4 commit 2c81f72
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
12 changes: 2 additions & 10 deletions sopel/modules/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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+?)'
Expand Down
8 changes: 3 additions & 5 deletions sopel/modules/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from requests import get

from sopel import module, tools
from sopel import module

try:
from ujson import loads
Expand All @@ -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

Expand Down
12 changes: 5 additions & 7 deletions sopel/modules/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions sopel/modules/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 2c81f72

Please sign in to comment.