From 1cae9664a764046ea7b1a18aa0f23fd872fa941f Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Thu, 31 Jul 2003 06:20:58 +0000 Subject: [PATCH] Upgraded to 2.3. --- plugins/Alias.py | 9 +- plugins/BadWords.py | 3 +- plugins/ChannelStats.py | 7 +- plugins/Relay.py | 3 +- src/cdb.py | 7 +- src/conf.py | 5 +- src/fix.py | 186 ++++++++++++++++++++++------------------ src/ircdb.py | 23 ++--- src/irclib.py | 19 ++-- src/structures.py | 3 + src/utils.py | 4 +- 11 files changed, 147 insertions(+), 122 deletions(-) diff --git a/plugins/Alias.py b/plugins/Alias.py index 08974100d..4b1fe786d 100644 --- a/plugins/Alias.py +++ b/plugins/Alias.py @@ -31,17 +31,12 @@ """ Allows 'aliases' for other commands. - -Commands include: - alias - unalias - freeze - unfreeze """ from baseplugin import * import re +import sets import conf import debug @@ -109,7 +104,7 @@ def makeNewAlias(name, alias): class Alias(callbacks.Privmsg): def __init__(self): callbacks.Privmsg.__init__(self) - self.frozen = set() + self.frozen = sets.Set() def freeze(self, irc, msg, args): """ diff --git a/plugins/BadWords.py b/plugins/BadWords.py index ffdeac8df..59ac326f5 100644 --- a/plugins/BadWords.py +++ b/plugins/BadWords.py @@ -43,6 +43,7 @@ Commands include: from baseplugin import * import re +import sets import ircdb import ircmsgs @@ -63,7 +64,7 @@ def subber(m): class BadWords(callbacks.Privmsg): def __init__(self): callbacks.Privmsg.__init__(self) - self.badwords = set() + self.badwords = sets.Set() def outFilter(self, irc, msg): if hasattr(self, 'regexp') and msg.command == 'PRIVMSG': diff --git a/plugins/ChannelStats.py b/plugins/ChannelStats.py index 68a541f7f..346962b21 100644 --- a/plugins/ChannelStats.py +++ b/plugins/ChannelStats.py @@ -121,7 +121,7 @@ class ChannelStats(callbacks.Privmsg, ChannelDBHandler): words=words+%s, msgs=msgs+1, actions=actions+%s""", - smileys, frowns, chars, words, isAction) + smileys, frowns, chars, words, int(isAction)) try: name = ircdb.users.getUserName(msg.prefix) except KeyError: @@ -136,7 +136,7 @@ class ChannelStats(callbacks.Privmsg, ChannelDBHandler): %s, %s, 1, %s, 0, 0, 0, 0, 0, 0 )""", name, int(time.time()), msg.args[1], - smileys, frowns, chars, words, isAction) + smileys, frowns, chars, words, int(isAction)) else: cursor.execute("""UPDATE user_stats SET last_seen=%s, last_msg=%s, chars=chars+%s, @@ -145,7 +145,8 @@ class ChannelStats(callbacks.Privmsg, ChannelDBHandler): frowns=frowns+%s WHERE name=%s""", int(time.time()), s, - chars, words, isAction, smileys, frowns, name) + chars, words, int(isAction), + smileys, frowns, name) db.commit() def doJoin(self, irc, msg): diff --git a/plugins/Relay.py b/plugins/Relay.py index 41a9d4092..e37712225 100644 --- a/plugins/Relay.py +++ b/plugins/Relay.py @@ -36,6 +36,7 @@ Handles relaying between networks. from baseplugin import * import re +import sets import time import conf @@ -95,7 +96,7 @@ class Relay(callbacks.Privmsg): self.started = False self.ircstates = {} self.lastmsg = ircmsgs.ping('this is just a fake message') - self.channels = set() + self.channels = sets.Set() self.abbreviations = {} def inFilter(self, irc, msg): diff --git a/src/cdb.py b/src/cdb.py index e2b5064a4..9071ebf63 100644 --- a/src/cdb.py +++ b/src/cdb.py @@ -35,6 +35,7 @@ from fix import * import os import sys +import sets import struct import os.path import cPickle as pickle @@ -273,7 +274,7 @@ class ReaderWriter(IterableMap): self._readJournal() self._openFiles() self.adds = {} - self.removals = set() + self.removals = sets.Set() def _openFiles(self): self.cdb = Reader(self.filename) @@ -294,7 +295,7 @@ class ReaderWriter(IterableMap): self.journal.flush() def _readJournal(self): - removals = set() + removals = sets.Set() adds = {} try: fd = file(self.journalName, 'r') @@ -395,7 +396,7 @@ class ReaderWriter(IterableMap): has_key = __contains__ def iteritems(self): - already = set() + already = sets.Set() for (key, value) in self.cdb.iteritems(): if key in self.removals or key in already: continue diff --git a/src/conf.py b/src/conf.py index 8c7a8853a..4c87a589f 100644 --- a/src/conf.py +++ b/src/conf.py @@ -31,6 +31,7 @@ from fix import * +import sets import os.path ### @@ -79,7 +80,7 @@ allowEval = True # defaultCapabilities: Capabilities allowed to everyone by default. You almost # certainly want to have !owner and !admin in here. ### -defaultCapabilities = set(['!owner', '!admin']) +defaultCapabilities = sets.Set(['!owner', '!admin']) ### # reply%s: Stock replies for various reasons. @@ -177,7 +178,7 @@ detailedTracebacks = True # bot will be found. ### driverModule = 'asyncoreDrivers' -driverModule = 'twistedDrivers' +#driverModule = 'twistedDrivers' ############################### ############################### diff --git a/src/fix.py b/src/fix.py index e6d8c98f4..b11e923ba 100644 --- a/src/fix.py +++ b/src/fix.py @@ -49,109 +49,109 @@ def catch(f, *args, **kwargs): except: return None -class bool(int): - """Just a holdover until 2.3 comes out with its wonderful new bool type.""" - def __new__(cls, val=0): - # This constructor always returns an existing instance - if val: - return True - else: - return False +## class bool(int): +## """2.3 came out.""" +## def __new__(cls, val=0): +## # This constructor always returns an existing instance +## if val: +## return True +## else: +## return False - def __repr__(self): - if self: - return "True" - else: - return "False" +## def __repr__(self): +## if self: +## return "True" +## else: +## return "False" - __str__ = __repr__ +## __str__ = __repr__ - def __and__(self, other): - if isinstance(other, bool): - return bool(int(self) & int(other)) - else: - return int.__and__(self, other) +## def __and__(self, other): +## if isinstance(other, bool): +## return bool(int(self) & int(other)) +## else: +## return int.__and__(self, other) - __rand__ = __and__ +## __rand__ = __and__ - def __or__(self, other): - if isinstance(other, bool): - return bool(int(self) | int(other)) - else: - return int.__or__(self, other) +## def __or__(self, other): +## if isinstance(other, bool): +## return bool(int(self) | int(other)) +## else: +## return int.__or__(self, other) - __ror__ = __or__ +## __ror__ = __or__ - def __xor__(self, other): - if isinstance(other, bool): - return bool(int(self) ^ int(other)) - else: - return int.__xor__(self, other) +## def __xor__(self, other): +## if isinstance(other, bool): +## return bool(int(self) ^ int(other)) +## else: +## return int.__xor__(self, other) - __rxor__ = __xor__ +## __rxor__ = __xor__ -False = int.__new__(bool, 0) -True = int.__new__(bool, 1) +## False = int.__new__(bool, 0) +## True = int.__new__(bool, 1) -class set(object): - """Just a holdover until 2.3 comes out with its wonderful new set type.""" - __slots__ = ('d',) - def __init__(self, seq=()): - self.d = {} - for x in seq: - self.d[x] = None +## class set(object): +## """2.3 came out.""" +## __slots__ = ('d',) +## def __init__(self, seq=()): +## self.d = {} +## for x in seq: +## self.d[x] = None - def __contains__(self, x): - return x in self.d +## def __contains__(self, x): +## return x in self.d - def __iter__(self): - return self.d.iterkeys() +## def __iter__(self): +## return self.d.iterkeys() - def __repr__(self): - return '%s([%s])' % (self.__class__.__name__, - ', '.join(map(repr, self.d.iterkeys()))) +## def __repr__(self): +## return '%s([%s])' % (self.__class__.__name__, +## ', '.join(map(repr, self.d.iterkeys()))) - def __nonzero__(self): - if self.d: - return True - else: - return False - - def __getstate__(self): - return (self.d.keys(),) - - def __setstate__(self, (L,)): - self.d = {} - for x in L: - self.d[x] = None - - def __len__(self): - return len(self.d) - - def add(self, x): - self.d[x] = None - - def remove(self, x): - del self.d[x] - - def discard(self, x): - try: - del self.d[x] - except KeyError: - pass - - def __eq__(self, other): - return self.d == other.d - - def __ne__(self, other): - return not self.d == other.d +## def __nonzero__(self): +## if self.d: +## return True +## else: +## return False ## def __getstate__(self): -## return self.d +## return (self.d.keys(),) -## def __setstate__(self, d): -## self.d = d +## def __setstate__(self, (L,)): +## self.d = {} +## for x in L: +## self.d[x] = None + +## def __len__(self): +## return len(self.d) + +## def add(self, x): +## self.d[x] = None + +## def remove(self, x): +## del self.d[x] + +## def discard(self, x): +## try: +## del self.d[x] +## except KeyError: +## pass + +## def __eq__(self, other): +## return self.d == other.d + +## def __ne__(self, other): +## return not self.d == other.d + +## ## def __getstate__(self): +## ## return self.d + +## ## def __setstate__(self, d): +## ## self.d = d class IterableMap(object): @@ -227,6 +227,22 @@ def window(L, size): for i in xrange(len(L) - (size-1)): yield L[i:i+size] +## def group(seq, groupSize): +## L = [] +## LL = [] +## i = groupSize +## for elt in seq: +## if i > 0: +## LL.append(elt) +## i -= 1 +## else: +## L.append(LL) +## i = groupSize +## LL = [] +## if LL: +## L.append(LL) +## return L + def itersplit(iterable, isSeparator, yieldEmpty=False): acc = [] for element in iterable: diff --git a/src/ircdb.py b/src/ircdb.py index 7c5f818f0..807f3c7f8 100644 --- a/src/ircdb.py +++ b/src/ircdb.py @@ -32,6 +32,7 @@ from fix import * import os +import sets import time import atexit import string @@ -86,37 +87,37 @@ def normalize(s): return s.translate(_normal) -class CapabilitySet(set): +class CapabilitySet(sets.Set): def __init__(self, capabilities=()): - set.__init__(self) + sets.Set.__init__(self) for capability in capabilities: self.add(capability) def add(self, capability): capability = ircutils.toLower(capability) inverted = invertCapability(capability) - if set.__contains__(self, inverted): - set.remove(self, inverted) - set.add(self, capability) + if sets.Set.__contains__(self, inverted): + sets.Set.remove(self, inverted) + sets.Set.add(self, capability) def remove(self, capability): capability = ircutils.toLower(capability) - set.remove(self, capability) + sets.Set.remove(self, capability) def __contains__(self, capability): capability = ircutils.toLower(capability) - if set.__contains__(self, capability): + if sets.Set.__contains__(self, capability): return True - if set.__contains__(self, invertCapability(capability)): + if sets.Set.__contains__(self, invertCapability(capability)): return True else: return False def check(self, capability): capability = ircutils.toLower(capability) - if set.__contains__(self, capability): + if sets.Set.__contains__(self, capability): return True - elif set.__contains__(self, invertCapability(capability)): + elif sets.Set.__contains__(self, invertCapability(capability)): return False else: raise KeyError, capability @@ -319,6 +320,7 @@ class UsersDictionary(object): fd = file(filename, 'r') s = fd.read() fd.close() + Set = sets.Set self.dict = eval(normalize(s)) self.cache = {} # hostmasks to nicks. self.revcache = ircutils.IrcDict() # nicks to hostmasks. @@ -405,6 +407,7 @@ class ChannelsDictionary(object): fd = file(filename, 'r') s = fd.read() fd.close() + Set = sets.Set self.dict = eval(normalize(s)) def getChannel(self, channel): diff --git a/src/irclib.py b/src/irclib.py index a1a5eb750..4c0bed40d 100644 --- a/src/irclib.py +++ b/src/irclib.py @@ -33,6 +33,7 @@ from fix import * from structures import queue, RingBuffer import copy +import sets import time import atexit @@ -110,7 +111,7 @@ class IrcMsgQueue(object): self.highpriority = queue() self.normal = queue() self.lowpriority = queue() - self.msgs = set() + self.msgs = sets.Set() def enqueue(self, msg): if msg in self.msgs: @@ -139,7 +140,7 @@ class IrcMsgQueue(object): return msg def __nonzero__(self): - return (self.highpriority or self.normal or self.lowpriority) + return bool(self.highpriority or self.normal or self.lowpriority) ### @@ -150,10 +151,10 @@ class Channel(object): __slots__ = ('users', 'ops', 'halfops', 'voices', 'topic') def __init__(self): self.topic = '' - self.users = set() - self.ops = set() - self.halfops = set() - self.voices = set() + self.users = sets.Set() + self.ops = sets.Set() + self.halfops = sets.Set() + self.voices = sets.Set() def addUser(self, user): nick = user.lstrip('@%+') @@ -330,9 +331,9 @@ class Irc(object): Handles PING commands already. """ - _nickSetters = set(['001', '002', '003', '004', '250', '251', '252', '254', - '255', '265', '266', '372', '375', '376', '333', '353', - '332', '366']) + _nickSetters = sets.Set(['001', '002', '003', '004', '250', '251', '252', + '254', '255', '265', '266', '372', '375', '376', + '333', '353', '332', '366']) def __init__(self, nick, user='', ident='', password='', callbacks=None): world.ircs.append(self) self.nick = nick diff --git a/src/structures.py b/src/structures.py index 0c469cc2a..dba2c3b27 100644 --- a/src/structures.py +++ b/src/structures.py @@ -184,6 +184,9 @@ class queue(object): def __len__(self): return len(self.front) + len(self.back) + def __nonzero__(self): + return bool(self.back or self.front) + def __contains__(self, elt): return elt in self.front or elt in self.back diff --git a/src/utils.py b/src/utils.py index fcb271f66..67d276793 100755 --- a/src/utils.py +++ b/src/utils.py @@ -198,7 +198,9 @@ def soundex(s, length=4): def dqrepr(s): """Returns a repr() of s guaranteed to be in double quotes.""" - return '"' + repr("'\x00" + s)[6:] + # The wankers-that-be decided not to use double-quotes anymore in 2.3. + # return '"' + repr("'\x00" + s)[6:] + return '"%s"' % s.encode('unicode_escape').replace('"', '\\"') nonEscapedSlashes = re.compile(r'(?