Added the ASPN Recipe snarfer (RFE #831028) and tests

This commit is contained in:
James Vega 2003-11-02 18:55:08 +00:00
parent 5f55e2b589
commit 4d77ffc92e
2 changed files with 50 additions and 1 deletions

View File

@ -37,11 +37,13 @@ written in) somehow.
import plugins import plugins
import os import os
import re
import imp import imp
import sys import sys
import math import math
import random import random
import string import string
import urllib2
# Stupid printing on import... # Stupid printing on import...
from cStringIO import StringIO from cStringIO import StringIO
@ -51,6 +53,7 @@ sys.stdout = sys.__stdout__
import debug import debug
import utils import utils
import ircutils
import privmsgs import privmsgs
import callbacks import callbacks
@ -82,8 +85,16 @@ example = utils.wrapLines("""
<supybot> jemfinch: list() -> new list list(sequence) -> new list initialized from sequence's items <supybot> 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, '_.') 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): def pydoc(self, irc, msg, args):
"""<python function> """<python function>
@ -155,6 +166,33 @@ class Python(callbacks.Privmsg):
""" """
irc.reply(msg, random.choice(self._these)) irc.reply(msg, random.choice(self._these))
_title = re.compile(r'<b>(Title):</b>&nbsp;(.*)', re.I)
_submit = re.compile(r'<b>(Submitter):</b>&nbsp;(.*)', re.I)
_update = re.compile(r'<b>Last (Updated):</b>&nbsp;(.*)', re.I)
_version = re.compile(r'<b>(Version) no:</b>&nbsp;(.*)', re.I)
_category = re.compile(r'<b>(Category):</b>.*?<a href[^>]+>(.*?)</a>',
re.I | re.S)
_description = re.compile(r'<p><b>(Description):</b></p>.+?<p>(.+?)</p>',
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 Class = Python

View File

@ -54,6 +54,17 @@ class PythonTestCase(PluginTestCase, PluginDocumentation):
def testZen(self): def testZen(self):
self.assertNotError('zen') 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: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: