소스 검색

Cleaned up Data class a bit.

tags/v0.1^2
Ben Kurtovic 12 년 전
부모
커밋
bffa9b6739
1개의 변경된 파일21개의 추가작업 그리고 15개의 파일을 삭제
  1. +21
    -15
      bot/classes/data.py

+ 21
- 15
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

불러오는 중...
취소
저장