Removed the immutability checks in IrcMsg.

This commit is contained in:
Jeremy Fincher 2003-08-11 06:57:17 +00:00
parent 241d20ea30
commit eb6fb90801

View File

@ -74,8 +74,7 @@ class IrcMsg(object):
IrcMsg(prefix='', args=(newSource, otherMsg.args[1]), msg=otherMsg) IrcMsg(prefix='', args=(newSource, otherMsg.args[1]), msg=otherMsg)
""" """
__slots__ = ('_args', '_command', '_host', '_nick', __slots__ = ('args', 'command', 'host', 'nick', 'prefix', 'user',
'_prefix', '_user',
'_hash', '_str', '_repr', '_len') '_hash', '_str', '_repr', '_len')
def __init__(self, s='', command='', args=None, prefix='', msg=None): def __init__(self, s='', command='', args=None, prefix='', msg=None):
if not s and not command and not msg: if not s and not command and not msg:
@ -84,94 +83,78 @@ class IrcMsg(object):
self._repr = None self._repr = None
self._hash = None self._hash = None
self._len = None self._len = None
if msg: if msg is not None:
prefix = msg.prefix self.prefix = msg.prefix
command = msg.command self.command = msg.command
args = msg.args self.args = tuple(msg.args)
if command: # Must be using command=, args=, prefix= form. if command: # Must be using command=, args=, prefix= form.
self.prefix = prefix
self.command = command
if args is not None: if args is not None:
#debug.printf(args)
assert filter(ircutils.isValidArgument, args) == args assert filter(ircutils.isValidArgument, args) == args
self.args = tuple(args)
else: else:
args = () self.args = ()
else: # Must be using a string. elif s: # Must be using a string.
if s[0] == ':': if s[0] == ':':
prefix, s = s[1:].split(None, 1) self.prefix, s = s[1:].split(None, 1)
else: else:
prefix = '' self.prefix = ''
if s.find(' :') != -1: if s.find(' :') != -1:
s, last = s.split(' :', 1) s, last = s.split(' :', 1)
args = s.split() self.args = s.split()
args.append(last.rstrip('\r\n')) self.args.append(last.rstrip('\r\n'))
else: else:
args = s.split() self.args = s.split()
command = args.pop(0) self.command = self.args.pop(0)
if ircutils.isUserHostmask(prefix): if ircutils.isUserHostmask(self.prefix):
(nick, user, host) = ircutils.splitHostmask(prefix) (self.nick,self.user,self.host)=ircutils.splitHostmask(self.prefix)
else: else:
(nick, user, host) = ('', '', '') (self.nick, self.user, self.host) = ('', '', '')
self._prefix = prefix self.args = tuple(self.args)
## self._nick = ircutils.nick(nick)
self._nick = nick
self._user = user
self._host = host
self._command = command
## if self.command == 'NICK' and type(args) != tuple:
## args[0] = ircutils.nick(args[0])
self._args = tuple(args)
prefix = property(lambda self: self._prefix)
nick = property(lambda self: self._nick)
user = property(lambda self: self._user)
host = property(lambda self: self._host)
command = property(lambda self: self._command)
args = property(lambda self: self._args)
def __str__(self): def __str__(self):
if self._str is not None: if self._str is not None:
return self._str return self._str
ret = ''
if self.prefix: if self.prefix:
if len(self.args) > 1: if len(self.args) > 1:
ret = ':%s %s %s :%s\r\n' % \ self._str = ':%s %s %s :%s\r\n' % \
(self.prefix, self.command, (self.prefix, self.command,
' '.join(self.args[:-1]), self.args[-1]) ' '.join(self.args[:-1]), self.args[-1])
else: else:
if self.args: if self.args:
ret = ':%s %s :%s\r\n' % \ self._str = ':%s %s :%s\r\n' % \
(self.prefix, self.command, self.args[0]) (self.prefix, self.command, self.args[0])
else: else:
ret = ':%s %s\r\n' % (self.prefix, self.command) self._str = ':%s %s\r\n' % (self.prefix, self.command)
else: else:
if len(self.args) > 1: if len(self.args) > 1:
ret = '%s %s :%s\r\n' % \ self._str = '%s %s :%s\r\n' % \
(self.command, ' '.join(self.args[:-1]), self.args[-1]) (self.command,
' '.join(self.args[:-1]), self.args[-1])
else: else:
if self.args: if self.args:
ret = '%s :%s\r\n' % (self.command, self.args[0]) self._str = '%s :%s\r\n' % (self.command, self.args[0])
else: else:
ret = '%s\r\n' % self.command self._str = '%s\r\n' % self.command
self._str = ret return self._str
return ret
def __len__(self): def __len__(self):
# This might not take into account the length of the prefix, but leaves # This might not take into account the length of the prefix, but leaves
# some room for variation. # some room for variation.
if self._len is not None: if self._len is not None:
return self._len return self._len
ret = 0 self._len = 0
if self.prefix: if self.prefix:
ret += len(self.prefix) self._len += len(self.prefix)
else: else:
ret += 42 # Ironically, the average length of an IRC prefix. self._len += 42 # Ironically, the average length of an IRC prefix.
ret += len(self.command) self._len += len(self.command)
if self.args: if self.args:
for arg in self.args: for arg in self.args:
ret += len(arg) + 1 # Remember the space prior to the arg. self._len += len(arg) + 1 # Remember space prior to the arg.
ret += 2 # For the colon before the prefix and before the last arg. self._len += 2 # For colon before the prefix and before the last arg.
self._len = ret return self._len
return ret
def __eq__(self, other): def __eq__(self, other):
return hash(self) == hash(other) and \ return hash(self) == hash(other) and \