diff --git a/bot/classes/data.py b/bot/classes/data.py index 1075fc3..e35a3d4 100644 --- a/bot/classes/data.py +++ b/bot/classes/data.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -# A class to store data from an individual line received on IRC. - import re class KwargParseException(Exception): @@ -11,8 +9,9 @@ class KwargParseException(Exception): pass class Data(object): + """Store data from an individual line received on IRC.""" + def __init__(self, line): - """Store data from an individual line received on IRC.""" self.line = line self.chan = str() self.nick = str() @@ -21,35 +20,42 @@ class Data(object): self.msg = str() 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: - self.command = args[0] # the command itself + self.command = args[0] except IndexError: self.command = None try: if self.command.startswith('!') or self.command.startswith('.'): 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: pass 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'...}. + """ self.kwargs = {} for arg in self.args[2:]: try: key, value = re.findall("^(.*?)\=(.*?)$", arg)[0] except IndexError: raise KwargParseException(arg) - if not key or not value: + if key and value: + self.kwargs[key] = value + else: raise KwargParseException(arg) - self.kwargs[key] = value