From 4d77ffc92ef04f32ca6acc182d2f00dd36349ae4 Mon Sep 17 00:00:00 2001 From: James Vega Date: Sun, 2 Nov 2003 18:55:08 +0000 Subject: [PATCH] Added the ASPN Recipe snarfer (RFE #831028) and tests --- plugins/Python.py | 40 +++++++++++++++++++++++++++++++++++++++- test/test_Python.py | 11 +++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/plugins/Python.py b/plugins/Python.py index 521af80f0..cdec3f303 100644 --- a/plugins/Python.py +++ b/plugins/Python.py @@ -37,11 +37,13 @@ written in) somehow. import plugins import os +import re import imp import sys import math import random import string +import urllib2 # Stupid printing on import... from cStringIO import StringIO @@ -51,6 +53,7 @@ sys.stdout = sys.__stdout__ import debug import utils +import ircutils import privmsgs import callbacks @@ -82,8 +85,16 @@ example = utils.wrapLines(""" jemfinch: list() -> new list list(sequence) -> new list initialized from sequence's items """) -class Python(callbacks.Privmsg): +class Python(callbacks.PrivmsgCommandAndRegexp, plugins.Toggleable): modulechars = '%s%s%s' % (string.ascii_letters, string.digits, '_.') + threaded = True + regexps = ['aspnRecipes'] + toggles = plugins.ToggleDictionary({'ASPN' : True}) + + def __init__(self): + callbacks.PrivmsgCommandAndRegexp.__init__(self) + plugins.Toggleable.__init__(self) + def pydoc(self, irc, msg, args): """ @@ -155,6 +166,33 @@ class Python(callbacks.Privmsg): """ irc.reply(msg, random.choice(self._these)) + _title = re.compile(r'(Title): (.*)', re.I) + _submit = re.compile(r'(Submitter): (.*)', re.I) + _update = re.compile(r'Last (Updated): (.*)', re.I) + _version = re.compile(r'(Version) no: (.*)', re.I) + _category = re.compile(r'(Category):.*?]+>(.*?)', + re.I | re.S) + _description = re.compile(r'

(Description):

.+?

(.+?)

', + re.I | re.S) + _searches = (_title, _submit, _update, _version, _category, _description) + _bold = lambda self, g: (ircutils.bold(g[0]),) + g[1:] + def aspnRecipes(self, irc, msg, match): + r"http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/\d+" + if not self.toggles.get('ASPN', channel=msg.args[0]): + return + url = match.group(0) + fd = urllib2.urlopen(url) + s = fd.read() + fd.close() + resp = [] + for r in self._searches: + m = r.search(s) + if m: + resp.append('%s: %s' % self._bold(m.groups())) + if resp: + #debug.printf('; '.join(resp)) + irc.reply(msg, '; '.join(resp), prefixName = False) + Class = Python diff --git a/test/test_Python.py b/test/test_Python.py index f9822a448..c1e0fd432 100644 --- a/test/test_Python.py +++ b/test/test_Python.py @@ -54,6 +54,17 @@ class PythonTestCase(PluginTestCase, PluginDocumentation): def testZen(self): self.assertNotError('zen') + + def testAspnRecipes(self): + self.assertRegexp('http://aspn.activestate.com/ASPN/Cookbook/Python/'\ + 'Recipe/230113', 'Implementation of sets using sorted lists') + + def testToggle(self): + self.assertHelp('python toggle') + self.assertRegexp('python toggle aspn', '\(aspn: (?:Off|On)\)') + self.assertRegexp('python toggle aspn off', '\(aspn: Off\)') + self.assertRegexp('python toggle aspn off', '\(aspn: Off\)') + self.assertRegexp('python toggle aspn on', '\(aspn: On\)') # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: