A whole lot more asserts and an isCapability predicate.

This commit is contained in:
Jeremy Fincher 2004-09-28 07:17:18 +00:00
parent 9ca87988ae
commit 3dec784447

View File

@ -48,6 +48,9 @@ import supybot.ircutils as ircutils
import supybot.registry as registry import supybot.registry as registry
import supybot.unpreserve as unpreserve import supybot.unpreserve as unpreserve
def isCapability(capability):
return len(capability.split(None, 1)) == 1
def fromChannelCapability(capability): def fromChannelCapability(capability):
"""Returns a (channel, capability) tuple from a channel capability.""" """Returns a (channel, capability) tuple from a channel capability."""
assert isChannelCapability(capability) assert isChannelCapability(capability)
@ -57,24 +60,28 @@ def isChannelCapability(capability):
"""Returns True if capability is a channel capability; False otherwise.""" """Returns True if capability is a channel capability; False otherwise."""
if ',' in capability: if ',' in capability:
(channel, capability) = capability.split(',', 1) (channel, capability) = capability.split(',', 1)
return ircutils.isChannel(channel) return ircutils.isChannel(channel) and isCapability(capability)
else: else:
return False return False
def makeChannelCapability(channel, capability): def makeChannelCapability(channel, capability):
"""Makes a channel capability given a channel and a capability.""" """Makes a channel capability given a channel and a capability."""
assert isCapability(capability)
assert ircutils.isChannel(channel)
return '%s,%s' % (channel, capability) return '%s,%s' % (channel, capability)
def isAntiCapability(capability): def isAntiCapability(capability):
"""Returns True if capability is an anticapability; False otherwise.""" """Returns True if capability is an anticapability; False otherwise."""
if isChannelCapability(capability): if isChannelCapability(capability):
(_, capability) = fromChannelCapability(capability) (_, capability) = fromChannelCapability(capability)
return capability and capability[0] == '-' return isCapability(capability) and capability[0] == '-'
def makeAntiCapability(capability): def makeAntiCapability(capability):
"""Returns the anticapability of a given capability.""" """Returns the anticapability of a given capability."""
assert not isAntiCapability(capability), 'makeAntiCapability does not ' \ assert isCapability(capability)
'work on anticapabilities; you probably want invertCapability.' assert not isAntiCapability(capability), \
'makeAntiCapability does not work on anticapabilities. ' \
'You probably want invertCapability.'
if isChannelCapability(capability): if isChannelCapability(capability):
(channel, capability) = fromChannelCapability(capability) (channel, capability) = fromChannelCapability(capability)
return makeChannelCapability(channel, '-' + capability) return makeChannelCapability(channel, '-' + capability)
@ -83,6 +90,7 @@ def makeAntiCapability(capability):
def unAntiCapability(capability): def unAntiCapability(capability):
"""Takes an anticapability and returns the non-anti form.""" """Takes an anticapability and returns the non-anti form."""
assert isCapability(capability)
if not isAntiCapability(capability): if not isAntiCapability(capability):
raise ValueError, '%s is not an anti capability' % capability raise ValueError, '%s is not an anti capability' % capability
if isChannelCapability(capability): if isChannelCapability(capability):
@ -93,11 +101,18 @@ def unAntiCapability(capability):
def invertCapability(capability): def invertCapability(capability):
"""Make a capability into an anticapability and vice versa.""" """Make a capability into an anticapability and vice versa."""
assert isCapability(capability)
if isAntiCapability(capability): if isAntiCapability(capability):
return unAntiCapability(capability) return unAntiCapability(capability)
else: else:
return makeAntiCapability(capability) return makeAntiCapability(capability)
def canonicalCapability(capability):
assert isCapability(capability)
if callable(capability):
capability = capability()
return capability.lower()
def unWildcardHostmask(hostmask): def unWildcardHostmask(hostmask):
return hostmask.translate(string.ascii, '!@*?') return hostmask.translate(string.ascii, '!@*?')
@ -339,14 +354,17 @@ class IrcChannel(object):
def addBan(self, hostmask, expiration=0): def addBan(self, hostmask, expiration=0):
"""Adds a ban to the channel banlist.""" """Adds a ban to the channel banlist."""
assert ircutils.isUserHostmask(hostmask)
self.bans[hostmask] = int(expiration) self.bans[hostmask] = int(expiration)
def removeBan(self, hostmask): def removeBan(self, hostmask):
"""Removes a ban from the channel banlist.""" """Removes a ban from the channel banlist."""
assert ircutils.isUserHostmask(hostmask)
return self.bans.pop(hostmask) return self.bans.pop(hostmask)
def checkBan(self, hostmask): def checkBan(self, hostmask):
"""Checks whether a given hostmask is banned by the channel banlist.""" """Checks whether a given hostmask is banned by the channel banlist."""
assert ircutils.isUserHostmask(hostmask)
now = time.time() now = time.time()
for (pattern, expiration) in self.bans.items(): for (pattern, expiration) in self.bans.items():
if now < expiration or not expiration: if now < expiration or not expiration:
@ -359,18 +377,22 @@ class IrcChannel(object):
def addIgnore(self, hostmask, expiration=0): def addIgnore(self, hostmask, expiration=0):
"""Adds an ignore to the channel ignore list.""" """Adds an ignore to the channel ignore list."""
assert ircutils.isUserHostmask(hostmask)
self.ignores[hostmask] = int(expiration) self.ignores[hostmask] = int(expiration)
def removeIgnore(self, hostmask): def removeIgnore(self, hostmask):
"""Removes an ignore from the channel ignore list.""" """Removes an ignore from the channel ignore list."""
assert ircutils.isUserHostmask(hostmask)
return self.ignores.pop(hostmask) return self.ignores.pop(hostmask)
def addCapability(self, capability): def addCapability(self, capability):
"""Adds a capability to the channel's default capabilities.""" """Adds a capability to the channel's default capabilities."""
assert isCapability(capability)
self.capabilities.add(capability) self.capabilities.add(capability)
def removeCapability(self, capability): def removeCapability(self, capability):
"""Removes a capability from the channel's default capabilities.""" """Removes a capability from the channel's default capabilities."""
assert isCapability(capability)
self.capabilities.remove(capability) self.capabilities.remove(capability)
def setDefaultCapability(self, b): def setDefaultCapability(self, b):
@ -379,6 +401,7 @@ class IrcChannel(object):
def checkCapability(self, capability): def checkCapability(self, capability):
"""Checks whether a certain capability is allowed by the channel.""" """Checks whether a certain capability is allowed by the channel."""
assert isCapability(capability)
if capability in self.capabilities: if capability in self.capabilities:
return self.capabilities.check(capability) return self.capabilities.check(capability)
else: else:
@ -389,6 +412,7 @@ class IrcChannel(object):
def checkIgnored(self, hostmask): def checkIgnored(self, hostmask):
"""Checks whether a given hostmask is to be ignored by the channel.""" """Checks whether a given hostmask is to be ignored by the channel."""
assert ircutils.isUserHostmask(hostmask)
if self.lobotomized: if self.lobotomized:
return True return True
if self.checkBan(hostmask): if self.checkBan(hostmask):