Added docstrings to all the functions.

This commit is contained in:
Jeremy Fincher 2003-04-20 09:32:52 +00:00
parent bacbf53386
commit 8bdfe2de97

View File

@ -36,10 +36,8 @@ import string
import fnmatch import fnmatch
import operator import operator
import debug
import world
def isUserHostmask(s): def isUserHostmask(s):
"""Returns whether or not the string s is a valid User hostmask."""
p1 = s.find('!') p1 = s.find('!')
p2 = s.find('@') p2 = s.find('@')
if p1 < p2-1 and p1 >= 1 and p2 >= 3 and len(s) > p2+1: if p1 < p2-1 and p1 >= 1 and p2 >= 3 and len(s) > p2+1:
@ -48,52 +46,63 @@ def isUserHostmask(s):
return False return False
def isServerHostmask(s): def isServerHostmask(s):
"""Returns True if s is a valid server hostmask."""
return (not isUserHostmask(s) and s.find('!') == -1 and s.find('@') == -1) return (not isUserHostmask(s) and s.find('!') == -1 and s.find('@') == -1)
def nickFromHostmask(hostmask): def nickFromHostmask(hostmask):
"""Returns the nick from a user hostmask."""
assert isUserHostmask(hostmask) assert isUserHostmask(hostmask)
return nick(hostmask.split('!', 1)[0]) return nick(hostmask.split('!', 1)[0])
def userFromHostmask(hostmask): def userFromHostmask(hostmask):
"""Returns the user from a user hostmask."""
assert isUserHostmask(hostmask) assert isUserHostmask(hostmask)
return hostmask.split('!', 1)[1].split('@', 1)[0] return hostmask.split('!', 1)[1].split('@', 1)[0]
def hostFromHostmask(hostmask): def hostFromHostmask(hostmask):
"""Returns the host from a user hostmask."""
assert isUserHostmask(hostmask) assert isUserHostmask(hostmask)
return hostmask.split('@', 1)[1] return hostmask.split('@', 1)[1]
def splitHostmask(hostmask): def splitHostmask(hostmask):
"""Returns the nick, user, host of a user hostmask."""
assert isUserHostmask(hostmask) assert isUserHostmask(hostmask)
nck, rest = hostmask.split('!', 1) nck, rest = hostmask.split('!', 1)
user, host = rest.split('@', 1) user, host = rest.split('@', 1)
return (nick(nck), user, host) return (nick(nck), user, host)
def joinHostmask(nick, ident, host): def joinHostmask(nick, ident, host):
"""Joins the nick, ident, host into a user hostmask."""
assert nick and ident and host assert nick and ident and host
return '%s!%s@%s' % (nick, ident, host) return '%s!%s@%s' % (nick, ident, host)
_lowertrans = string.maketrans(string.ascii_uppercase + r'\[]~', _lowertrans = string.maketrans(string.ascii_uppercase + r'\[]~',
string.ascii_lowercase + r'|{}^') string.ascii_lowercase + r'|{}^')
def toLower(nick): def toLower(s):
return nick.translate(_lowertrans) """Returns the string s lowered according to IRC case rules."""
return s.translate(_lowertrans)
def nickEqual(nick1, nick2): def nickEqual(nick1, nick2):
"""Returns True if nick1 == nick2 according to IRC case rules."""
return toLower(nick1) == toLower(nick2) return toLower(nick1) == toLower(nick2)
_nickchars = r'-[]\\`^{}' _nickchars = r'-[]\\`^{}'
_nickre = re.compile(r'^[%sA-Za-z][%s0-9A-Za-z]+$' % (re.escape(_nickchars), _nickre = re.compile(r'^[%sA-Za-z][%s0-9A-Za-z]+$' % (re.escape(_nickchars),
re.escape(_nickchars))) re.escape(_nickchars)))
def isNick(s): def isNick(s):
"""Returns True if s is a valid IRC nick."""
if re.match(_nickre, s): if re.match(_nickre, s):
return True return True
else: else:
return False return False
def isChannel(s): def isChannel(s):
"""Returns True if s is a valid IRC channel name."""
return (s and s[0] in '#&+!' and len(s) <= 50 and \ return (s and s[0] in '#&+!' and len(s) <= 50 and \
'\x07' not in s and ',' not in s and ' ' not in s) '\x07' not in s and ',' not in s and ' ' not in s)
def hostmaskPatternEqual(pattern, hostmask): def hostmaskPatternEqual(pattern, hostmask):
"""Returns True if hostmask matches the hostmask pattern pattern."""
return fnmatch.fnmatch(toLower(hostmask), toLower(pattern)) return fnmatch.fnmatch(toLower(hostmask), toLower(pattern))
_ipchars = string.digits + '.' _ipchars = string.digits + '.'
@ -189,16 +198,19 @@ def bold(s):
return "\x02%s\x02" % s return "\x02%s\x02" % s
def isValidArgument(s): def isValidArgument(s):
"""Returns if s is strictly a valid argument for an IRC message."""
return '\r' not in s and '\n' not in s and '\x00' not in s return '\r' not in s and '\n' not in s and '\x00' not in s
notFunky = string.ascii[32:]+'\x02' notFunky = string.ascii[32:]+'\x02'
def safeArgument(s): def safeArgument(s):
"""If s is unsafe for IRC, returns a safe version."""
if isValidArgument(s) and s.translate(string.ascii, notFunky) == '': if isValidArgument(s) and s.translate(string.ascii, notFunky) == '':
return s return s
else: else:
return repr(s) return repr(s)
def replyTo(msg): def replyTo(msg):
"""Returns the appropriate target to send responses to msg."""
if isChannel(msg.args[0]): if isChannel(msg.args[0]):
return msg.args[0] return msg.args[0]
else: else:
@ -236,6 +248,7 @@ class nick(str):
class IrcDict(dict): class IrcDict(dict):
"""Subclass of dict to make key comparison IRC-case insensitive."""
def __contains__(self, s): def __contains__(self, s):
return dict.__contains__(self, toLower(s)) return dict.__contains__(self, toLower(s))
has_key = __contains__ has_key = __contains__