Removed PersistentDictionary.

This commit is contained in:
Jeremy Fincher 2004-07-29 06:58:42 +00:00
parent 206d8bd527
commit a7dcf7604b
5 changed files with 59 additions and 79 deletions

View File

@ -1,3 +1,6 @@
* Changed Bugzilla to use the registry, rather than a custom
flatfile database format.
* Added the KeepAlive plugin, to send useless keepalive messages * Added the KeepAlive plugin, to send useless keepalive messages
to someone every some period. It's mostly just because we to someone every some period. It's mostly just because we
noticed that MozBot had one, and we couldn't allow ourselves to noticed that MozBot had one, and we couldn't allow ourselves to

View File

@ -46,13 +46,13 @@ import xml.dom.minidom as minidom
from itertools import imap, ifilter from itertools import imap, ifilter
from htmlentitydefs import entitydefs as entities from htmlentitydefs import entitydefs as entities
import supybot.registry as registry
import supybot.conf as conf import supybot.conf as conf
import supybot.utils as utils import supybot.utils as utils
import supybot.plugins as plugins import supybot.plugins as plugins
import supybot.ircutils as ircutils import supybot.ircutils as ircutils
import supybot.privmsgs as privmsgs import supybot.privmsgs as privmsgs
import supybot.registry as registry
import supybot.webutils as webutils import supybot.webutils as webutils
import supybot.callbacks as callbacks import supybot.callbacks as callbacks
import supybot.structures as structures import supybot.structures as structures
@ -68,33 +68,16 @@ priorityKeys = ['p1', 'p2', 'p3', 'p4', 'p5', 'Low', 'Normal', 'High',
severityKeys = ['enhancement', 'trivial', 'minor', 'normal', 'major', severityKeys = ['enhancement', 'trivial', 'minor', 'normal', 'major',
'critical', 'blocker'] 'critical', 'blocker']
dbfilename = os.path.join(conf.supybot.directories.data(), 'Bugzilla.db')
def makeDb(filename):
if os.path.exists(filename):
d = structures.PersistentDictionary(filename)
else:
d = structures.PersistentDictionary(filename)
d['gcc'] = ['http://gcc.gnu.org/bugzilla', 'GCC']
d['rh'] = ['http://bugzilla.redhat.com/bugzilla', 'Red Hat']
d['gnome'] = ['http://bugzilla.gnome.org/bugzilla', 'Gnome']
d['mozilla'] = ['http://bugzilla.mozilla.org', 'Mozilla']
d['ximian'] = ['http://bugzilla.ximian.com/bugzilla', 'Ximian Gnome']
d.flush()
return d
class BugzillaError(Exception): class BugzillaError(Exception):
"""A bugzilla error""" """A bugzilla error"""
pass pass
def configure(advanced): def configure(advanced):
from supybot.questions import output, expect, anything, yn from supybot.questions import output, expect, anything, yn
conf.registerPlugin('Bugzilla', True) conf.registerPlugin('Bugzilla', True)
output("""The Bugzilla plugin has the functionality to watch for URLs output("""The Bugzilla plugin has the functionality to watch for URLs
that match a specific pattern (we call this a snarfer). When that match a specific pattern (we call this a snarfer). When
supybot sees such a URL, he will parse the web page for a supybot sees such a URL, it will parse the web page for
information and reply with the results.""") information and reply with the results.""")
if yn('Do you want this bug snarfer enabled by default?', default=False): if yn('Do you want this bug snarfer enabled by default?', default=False):
conf.supybot.plugins.Bugzilla.bugSnarfer.setValue(True) conf.supybot.plugins.Bugzilla.bugSnarfer.setValue(True)
@ -111,6 +94,24 @@ conf.registerChannelValue(conf.supybot.plugins.Bugzilla, 'replyNoBugzilla',
to use when notifying the user that there is no information about that to use when notifying the user that there is no information about that
bugzilla site.""")) bugzilla site."""))
class Bugzillae(registry.SpaceSeparatedListOfStrings):
List = ircutils.IrcSet
conf.registerGlobalValue(conf.supybot.plugins.Bugzilla, 'bugzillae',
Bugzillae([], """Determines what bugzillas will be added to the bot when it
starts."""))
def registerBugzilla(name, url='', description=''):
conf.supybot.plugins.Bugzilla.bugzillae().add(name)
group = conf.registerGroup(conf.supybot.plugins.Bugzilla.bugzillae, name)
URL = conf.registerGlobalValue(group, 'url', registry.String(url, ''))
DESC = conf.registerGlobalValue(group, 'description',
registry.String(description, ''))
if url:
URL.setValue(url)
if description:
DESC.setValue(description)
class Bugzilla(callbacks.PrivmsgCommandAndRegexp): class Bugzilla(callbacks.PrivmsgCommandAndRegexp):
"""Show a link to a bug report with a brief description""" """Show a link to a bug report with a brief description"""
threaded = True threaded = True
@ -120,7 +121,11 @@ class Bugzilla(callbacks.PrivmsgCommandAndRegexp):
callbacks.PrivmsgCommandAndRegexp.__init__(self) callbacks.PrivmsgCommandAndRegexp.__init__(self)
self.entre = re.compile('&(\S*?);') self.entre = re.compile('&(\S*?);')
# Schema: {name, [url, description]} # Schema: {name, [url, description]}
self.db = makeDb(dbfilename) self.db = ircutils.IrcDict()
for name in self.registryValue('bugzillae'):
registerBugzilla(name)
group = self.registryValue('bugzillae.%s' % name, value=False)
self.db[name] = [group.url(), group.description()]
self.shorthand = utils.abbrev(self.db.keys()) self.shorthand = utils.abbrev(self.db.keys())
def keywords2query(self, keywords): def keywords2query(self, keywords):
@ -139,9 +144,6 @@ class Bugzilla(callbacks.PrivmsgCommandAndRegexp):
query.append('ctype=csv') query.append('ctype=csv')
return query return query
def die(self):
self.db.close()
def add(self, irc, msg, args): def add(self, irc, msg, args):
"""<name> <url> <description> """<name> <url> <description>
@ -155,6 +157,7 @@ class Bugzilla(callbacks.PrivmsgCommandAndRegexp):
if url[-1] == '/': if url[-1] == '/':
url = url[:-1] url = url[:-1]
self.db[name] = [url, description] self.db[name] = [url, description]
registerBugzilla(name, url, description)
self.shorthand = utils.abbrev(self.db.keys()) self.shorthand = utils.abbrev(self.db.keys())
irc.replySuccess() irc.replySuccess()
@ -168,6 +171,7 @@ class Bugzilla(callbacks.PrivmsgCommandAndRegexp):
try: try:
name = self.shorthand[name] name = self.shorthand[name]
del self.db[name] del self.db[name]
self.registryValue('bugzillae').remove(name)
self.shorthand = utils.abbrev(self.db.keys()) self.shorthand = utils.abbrev(self.db.keys())
irc.replySuccess() irc.replySuccess()
except KeyError: except KeyError:

View File

@ -33,8 +33,6 @@
Data structures for Python. Data structures for Python.
""" """
## from __future__ import generators
__revision__ = "$Id$" __revision__ = "$Id$"
import supybot.fix as fix import supybot.fix as fix
@ -42,6 +40,7 @@ import supybot.fix as fix
import types import types
import pprint import pprint
import string import string
import threading
from itertools import imap from itertools import imap
class RingBuffer(object): class RingBuffer(object):
@ -153,10 +152,7 @@ class RingBuffer(object):
self.L[idx] = elt self.L[idx] = elt
def __repr__(self): def __repr__(self):
if self.full: return 'RingBuffer(%r, %r)' % (self.maxSize, list(self))
return 'RingBuffer(%r, %r)' % (self.maxSize, list(self))
else:
return 'RingBuffer(%r, %r)' % (self.maxSize, list(self))
def __getstate__(self): def __getstate__(self):
return (self.maxSize, self.full, self.i, self.L) return (self.maxSize, self.full, self.i, self.L)
@ -353,30 +349,29 @@ class TwoWayDictionary(dict):
dict.__delitem__(self, value) dict.__delitem__(self, value)
class PersistentDictionary(dict): class WorkQueue(object):
_trans = string.maketrans('\r\n', ' ') def __init__(self, q=None, n=1):
_locals = locals if q is None:
_globals = globals q = Queue.Queue()
def __init__(self, filename, globals=None, locals=None): self.q = q
mylocals = locals self.numberOfThreads = n
myglobals = globals self.threads = []
if mylocals is None: for _ in xrange(n):
mylocals = self._locals() t = threading.Thread(target=self.target)
if myglobals is None: self.threads.append(t)
myglobals = self._globals() t.start()
self.filename = filename
try: def target(self):
fd = file(filename, 'r') while 1:
s = fd.read() f = self.q.get()
fd.close() if f is None:
s.translate(self._trans) self.q.put(None)
super(self.__class__, self).__init__(eval(s, myglobals, mylocals)) break
except IOError: else:
pass f()
def close(self): def enqueue(self, f):
fd = file(self.filename, 'w') self.q.put(f)
fd.write(pprint.pformat(self))
fd.close()
flush = close
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -35,6 +35,7 @@ if network:
class BugzillaTest(PluginTestCase, PluginDocumentation): class BugzillaTest(PluginTestCase, PluginDocumentation):
plugins = ('Bugzilla',) plugins = ('Bugzilla',)
def testBug(self): def testBug(self):
self.assertNotError('add gcc http://gcc.gnu.org/bugzilla gcc')
self.assertNotError('bug gcc 5') self.assertNotError('bug gcc 5')
def testAddRemove(self): def testAddRemove(self):
@ -44,10 +45,12 @@ if network:
self.assertError('bug xiph 413') self.assertError('bug xiph 413')
def testSearch(self): def testSearch(self):
self.assertNotError('add gcc http://gcc.gnu.org/bugzilla gcc')
self.assertNotError('search gcc alpha') self.assertNotError('search gcc alpha')
self.assertNotError('search --keywords=fixed gcc alpha') self.assertNotError('search --keywords=fixed gcc alpha')
def testConfigBugzillaSnarfer(self): def testConfigBugzillaSnarfer(self):
self.assertNotError('add gcc http://gcc.gnu.org/bugzilla gcc')
conf.supybot.plugins.bugzilla.bugSnarfer.setValue(False) conf.supybot.plugins.bugzilla.bugSnarfer.setValue(False)
self.assertNoResponse( self.assertNoResponse(
'http://gcc.gnu.org/bugzilla/show_bug.cgi?id=5') 'http://gcc.gnu.org/bugzilla/show_bug.cgi?id=5')

View File

@ -576,30 +576,5 @@ class TwoWayDictionaryTestCase(SupyTestCase):
self.failIf('foo' in d) self.failIf('foo' in d)
class PersistentDictionaryTestCase(SupyTestCase):
def test(self):
d = PersistentDictionary('test.dict')
d['foo'] = 'bar'
d[1] = 2
d.close()
d2 = PersistentDictionary('test.dict')
self.failUnless('foo' in d)
self.assertEqual(d['foo'], 'bar')
self.failUnless(1 in d)
self.assertEqual(d[1], 2)
self.failUnless('foo' in d2)
self.assertEqual(d2['foo'], 'bar')
self.failUnless(1 in d2)
self.assertEqual(d2[1], 2)
def testFlush(self):
d = PersistentDictionary('test.dict')
d[1] = 2
d.flush()
d2 = PersistentDictionary('test.dict')
self.failUnless(1 in d2)
self.assertEqual(d2[1], 2)
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: