[TASK] Make parsing work, single to doublequotes.

This commit is contained in:
Jan Philipp Timme 2015-10-24 22:27:08 +02:00
parent 9b04262445
commit bd825fe160
2 changed files with 21 additions and 15 deletions

4
bot.py
View File

@ -83,8 +83,8 @@ class IrcProtocol(ManagedProtocol):
self.process_data() self.process_data()
def process_data(self): def process_data(self):
while b'\r\n' in self._buffer: while b"\r\n" in self._buffer:
line, self._buffer = self._buffer.split(b'\r\n', 1) line, self._buffer = self._buffer.split(b"\r\n", 1)
line = self.decode(line.strip()) line = self.decode(line.strip())
irc_line = irc.IrcLine.from_string(line) irc_line = irc.IrcLine.from_string(line)
print(self.encode(str(irc_line))) print(self.encode(str(irc_line)))

32
irc.py
View File

@ -1,20 +1,22 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
def parse(line): def parse(line):
if line[0:1] == ':': prefix = ""
prefix, line = line.split(None, 1) if line[0:1] == ":":
prefix = prefix[1:] prefix = ":"
subject, line = line.split(None, 1)
subject = subject[1:]
else: else:
prefix = "" subject = ""
if ' :' in line: if " :" in line:
tmp_str, trailing = line.split(' :', 1) tmp_str, trailing = line.split(" :", 1)
tmp_args = tmp_str.split() tmp_args = tmp_str.split()
else: else:
trailing = "" trailing = ""
tmp_args = line.split() tmp_args = line.split()
command, *middle = tmp_args command, *middle = tmp_args
params = middle[:] params = middle[:]
return prefix, command, params, trailing return prefix, subject, command, params, trailing
class IrcLine(object): class IrcLine(object):
@ -22,6 +24,7 @@ class IrcLine(object):
""" """
def __init__(self): def __init__(self):
self.prefix = "" self.prefix = ""
self.subject = ""
self.command = "" self.command = ""
self.params = "" self.params = ""
self.trailing = "" self.trailing = ""
@ -32,15 +35,16 @@ class IrcLine(object):
data = parse(string) data = parse(string)
print(data) print(data)
instance.prefix = data[0] instance.prefix = data[0]
instance.command = data[1] instance.subject = data[1]
instance.params = data[2] instance.command = data[2]
instance.trailing = data[3] instance.params = data[3]
instance.trailing = data[4]
return instance return instance
def __repr__(self): def __repr__(self):
e = [] e = []
if self.prefix: if self.subject:
e.append(self.prefix) e.append(self.subject)
if self.command: if self.command:
e.append(self.command) e.append(self.command)
if self.params: if self.params:
@ -48,6 +52,8 @@ class IrcLine(object):
if self.trailing: if self.trailing:
e.append(":{}".format(self.trailing)) e.append(":{}".format(self.trailing))
result = " ".join(e) result = " ".join(e)
if self.prefix:
result = "".join([self.prefix, result])
return result return result
@classmethod @classmethod
@ -58,7 +64,7 @@ class IrcLine(object):
instance.trailing = msg instance.trailing = msg
return instance return instance
if __name__ == '__main__': if __name__ == "__main__":
l = IrcLine.from_string(":JPT|NC!~AS@euirc-6f528752.pools.arcor-ip.net JOIN :#euirc") l = IrcLine.from_string(":JPT|NC!~AS@euirc-6f528752.pools.arcor-ip.net JOIN :#euirc")
print(str(l)) print(str(l))
print() print()