# A class to store data from an individual line received on IRC.
import re
import re
class KwargParseException(Exception):
class KwargParseException(Exception):
@@ -11,8 +9,9 @@ class KwargParseException(Exception):
pass
pass
class Data(object):
class Data(object):
"""Store data from an individual line received on IRC."""
def __init__(self, line):
def __init__(self, line):
"""Store data from an individual line received on IRC."""
self.line = line
self.line = line
self.chan = str()
self.chan = str()
self.nick = str()
self.nick = str()
@@ -21,35 +20,42 @@ class Data(object):
self.msg = str()
self.msg = str()
def parse_args(self):
def parse_args(self):
"""parse command arguments from self.msg into self.command and self.args"""
args = self.msg.strip().split(' ') # strip out extra whitespace and split the message into a list
while '' in args: # remove any empty arguments
args.remove('')
"""Parse command args from self.msg into self.command and self.args."""
args = self.msg.strip().split(" ")
while "" in args:
args.remove("")
self.args = args[1:] # the command arguments
self.is_command = False # whether this is a real command or not
# Isolate command arguments:
self.args = args[1:]
self.is_command = False # is this message a command?
try:
try:
self.command = args[0] # the command itself
self.command = args[0]
except IndexError:
except IndexError:
self.command = None
self.command = None
try:
try:
if self.command.startswith('!') or self.command.startswith('.'):
if self.command.startswith('!') or self.command.startswith('.'):
self.is_command = True
self.is_command = True
self.command = self.command[1:] # strip '!' or '.'
self.command = self.command.lower() # lowercase command name
self.command = self.command[1:] # Strip the '!' or '.'
self.command = self.command.lower()
except AttributeError:
except AttributeError:
pass
pass
def parse_kwargs(self):
def parse_kwargs(self):
"""parse command arguments from self.args, given as !command key1=value1 key2=value2..., into a dict self.kwargs: {'key1': 'value2', 'key2': 'value2'...}"""
"""Parse keyword arguments embedded in self.args.
Parse a command given as "!command key1=value1 key2=value2..." into a
dict, self.kwargs, like {'key1': 'value2', 'key2': 'value2'...}.