Skip to content

Commit

Permalink
bugzilla,wikipedia: take advantage of the new URL callback rule-based…
Browse files Browse the repository at this point in the history
… system
  • Loading branch information
Exirel committed Jul 17, 2020
1 parent 9c19561 commit dd6328e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
39 changes: 19 additions & 20 deletions sopel/modules/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
import requests
import xmltodict

from sopel import plugin, plugins
from sopel.config.types import ListAttribute, StaticSection
from sopel.module import rule


regex = None
LOGGER = logging.getLogger(__name__)


Expand All @@ -42,34 +40,35 @@ def configure(config):


def setup(bot):
global regex
bot.config.define_section('bugzilla', BugzillaSection)

if not bot.config.bugzilla.domains:
return

domains = '|'.join(bot.config.bugzilla.domains)
regex = re.compile((r'https?://(%s)'
r'(/show_bug.cgi\?\S*?)'
r'(id=\d+)')
% domains)
bot.register_url_callback(regex, show_bug)
def _bugzilla_loader(settings):
if not settings.bugzilla.domains:
raise plugins.exceptions.ImproperlyConfigured(
'Bugzilla URL callback requires '
'"bugzilla.domains" to be configured; check your config file.')

domain_pattern = '|'.join(
re.escape(domain)
for domain in settings.bugzilla.domains)

pattern = (
r'https?://(%s)'
r'(/show_bug.cgi\?\S*?)'
r'(id=\d+).*'
) % domain_pattern

def shutdown(bot):
bot.unregister_url_callback(regex, show_bug)
return [re.compile(pattern)]


@rule(r'.*https?://(\S+?)'
r'(/show_bug.cgi\?\S*?)'
r'(id=\d+).*')
@plugin.url_lazy(_bugzilla_loader)
def show_bug(bot, trigger, match=None):
"""Show information about a Bugzilla bug."""
match = match or trigger
domain = match.group(1)
domain = trigger.group(1)
if domain not in bot.config.bugzilla.domains:
return
url = 'https://%s%sctype=xml&%s' % match.groups()
url = 'https://%s%sctype=xml&%s' % trigger.groups()
data = requests.get(url).content
bug = xmltodict.parse(data).get('bugzilla').get('bug')
error = bug.get('@error', None) # error="NotPermitted"
Expand Down
12 changes: 7 additions & 5 deletions sopel/modules/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from requests import get

from sopel.config.types import StaticSection, ValidatedAttribute
from sopel.module import commands, example, NOLIMIT, url
from sopel.module import commands, example, NOLIMIT, output_prefix, url
from sopel.tools.web import quote, unquote

try: # TODO: Remove fallback when dropping py2
Expand Down Expand Up @@ -133,9 +133,9 @@ def say_snippet(bot, trigger, server, query, show_url=True):
snippet = mw_snippet(server, query)
except KeyError:
if show_url:
bot.say("[WIKIPEDIA] Error fetching snippet for \"{}\".".format(page_name))
bot.say("Error fetching snippet for \"{}\".".format(page_name))
return
msg = '[WIKIPEDIA] {} | "{}"'.format(page_name, snippet)
msg = '{} | "{}"'.format(page_name, snippet)
msg_url = msg + ' | https://{}/wiki/{}'.format(server, query)
if msg_url == trigger: # prevents triggering on another instance of Sopel
return
Expand Down Expand Up @@ -166,10 +166,10 @@ def say_section(bot, trigger, server, query, section):

snippet = mw_section(server, query, section)
if not snippet:
bot.say("[WIKIPEDIA] Error fetching section \"{}\" for page \"{}\".".format(section, page_name))
bot.say("Error fetching section \"{}\" for page \"{}\".".format(section, page_name))
return

msg = '[WIKIPEDIA] {} - {} | "{}"'.format(page_name, section.replace('_', ' '), snippet)
msg = '{} - {} | "{}"'.format(page_name, section.replace('_', ' '), snippet)
bot.say(msg)


Expand Down Expand Up @@ -217,6 +217,7 @@ def mw_section(server, query, section):

# Matches a wikipedia page (excluding spaces and #, but not /File: links), with a separate optional field for the section
@url(r'https?:\/\/([a-z]+\.wikipedia\.org)\/wiki\/((?!File\:)[^ #]+)#?([^ ]*)')
@output_prefix('[WIKIPEDIA] ')
def mw_info(bot, trigger, match=None):
"""Retrieves and outputs a snippet of the linked page."""
if match.group(3):
Expand All @@ -230,6 +231,7 @@ def mw_info(bot, trigger, match=None):

@commands('w', 'wiki', 'wik')
@example('.w San Francisco')
@output_prefix('[WIKIPEDIA] ')
def wikipedia(bot, trigger):
lang = bot.config.wikipedia.default_lang

Expand Down
9 changes: 9 additions & 0 deletions sopel/plugins/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ def __init__(self, name):
message = 'Plugin "%s" not registered' % name
self.plugin_name = name
super(PluginNotRegistered, self).__init__(message)


class ImproperlyConfigured(PluginError):
"""Exception raised when a plugin is not properly configured.
This can be used in any place where a plugin requires a specific config,
for example in its ``setup`` function, in any of its rules or commands,
and in the loader function for the :func:`sopel.plugin.url_lazy` decorator.
"""

0 comments on commit dd6328e

Please sign in to comment.