From d5689cafe391e8fcb4aaa190fd63b2a7ed12f884 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 4 Oct 2015 18:44:20 -0500 Subject: [PATCH] Implement serialization for IRC Data objects. --- earwigbot/irc/data.py | 23 ++++++++++++++++------- earwigbot/irc/frontend.py | 6 +++--- earwigbot/irc/watcher.py | 1 - 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/earwigbot/irc/data.py b/earwigbot/irc/data.py index 25fb7e5..aaff822 100644 --- a/earwigbot/irc/data.py +++ b/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 "".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) diff --git a/earwigbot/irc/frontend.py b/earwigbot/irc/frontend.py index 27fadb6..e92366f 100644 --- a/earwigbot/irc/frontend.py +++ b/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: diff --git a/earwigbot/irc/watcher.py b/earwigbot/irc/watcher.py index 82b0091..bca8360 100644 --- a/earwigbot/irc/watcher.py +++ b/earwigbot/irc/watcher.py @@ -21,7 +21,6 @@ # SOFTWARE. import imp -import os from earwigbot.irc import IRCConnection, RC