Переглянути джерело

Implement serialization for IRC Data objects.

tags/v0.2
Ben Kurtovic 9 роки тому
джерело
коміт
d5689cafe3
3 змінених файлів з 19 додано та 11 видалено
  1. +16
    -7
      earwigbot/irc/data.py
  2. +3
    -3
      earwigbot/irc/frontend.py
  3. +0
    -1
      earwigbot/irc/watcher.py

+ 16
- 7
earwigbot/irc/data.py Переглянути файл

@@ -27,35 +27,35 @@ __all__ = ["Data"]
class Data(object):
"""Store data from an individual line received on IRC."""

def __init__(self, bot, my_nick, line, msgtype):
self._bot = bot
def __init__(self, my_nick, line, msgtype):
self._my_nick = my_nick.lower()
self._line = line
self._msgtype = msgtype

self._is_private = self._is_command = False
self._msg = self._command = self._trigger = None
self._args = []
self._kwargs = {}

self._parse(msgtype)
self._parse()

def __repr__(self):
"""Return the canonical string representation of the Data."""
res = "Data(bot={0!r}, my_nick={1!r}, line={2!r})"
return res.format(self._bot, self.my_nick, self.line)
res = "Data(my_nick={0!r}, line={1!r})"
return res.format(self.my_nick, self.line)

def __str__(self):
"""Return a nice string representation of the Data."""
return "<Data of {0!r}>".format(" ".join(self.line))

def _parse(self, msgtype):
def _parse(self):
"""Parse a line from IRC into its components as instance attributes."""
sender = re.findall(r":(.*?)!(.*?)@(.*?)\Z", self.line[0])[0]
self._nick, self._ident, self._host = sender
self._reply_nick = self._nick
self._chan = self.line[2]

if msgtype == "PRIVMSG":
if self._msgtype == "PRIVMSG":
if self.chan.lower() == self.my_nick:
# This is a privmsg to us, so set 'chan' as the nick of the
# sender instead of the 'channel', which is ourselves:
@@ -226,3 +226,12 @@ class Data(object):
arguments.
"""
return self._kwargs

def serialize(self):
"""Serialize this object into a tuple and return it."""
return (self._my_nick, self._line, self._msgtype)

@classmethod
def unserialize(cls, data):
"""Return a new Data object built from a serialized tuple."""
return cls(*data)

+ 3
- 3
earwigbot/irc/frontend.py Переглянути файл

@@ -59,15 +59,15 @@ class Frontend(IRCConnection):
def _process_message(self, line):
"""Process a single message from IRC."""
if line[1] == "JOIN":
data = Data(self.bot, self.nick, line, msgtype="JOIN")
data = Data(self.nick, line, msgtype="JOIN")
self.bot.commands.call("join", data)

elif line[1] == "PART":
data = Data(self.bot, self.nick, line, msgtype="PART")
data = Data(self.nick, line, msgtype="PART")
self.bot.commands.call("part", data)

elif line[1] == "PRIVMSG":
data = Data(self.bot, self.nick, line, msgtype="PRIVMSG")
data = Data(self.nick, line, msgtype="PRIVMSG")
if data.is_private:
self.bot.commands.call("msg_private", data)
else:


+ 0
- 1
earwigbot/irc/watcher.py Переглянути файл

@@ -21,7 +21,6 @@
# SOFTWARE.

import imp
import os

from earwigbot.irc import IRCConnection, RC



Завантаження…
Відмінити
Зберегти