Split messages class into multiple files

This commit is contained in:
nemunaire 2015-04-06 09:50:13 +02:00 committed by nemunaire
commit c8d495d508
15 changed files with 280 additions and 198 deletions

View file

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 Mercier Pierre-Olivier
#
@ -16,157 +14,28 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime, timezone
class AbstractMessage:
"""This class represents an abstract message"""
def __init__(self, server, date=None, to=None, to_response=None, frm=None):
"""Initialize an abstract message
Arguments:
server -- the servir identifier
date -- time of the message reception, default: now
to -- list of recipients
to_response -- if channel(s) where send the response differ
frm -- the sender
"""
self.server = server
self.date = datetime.now(timezone.utc) if date is None else date
self.to = to if to is not None else list()
self._to_response = (to_response if (to_response is None or
isinstance(to_response, list))
else [ to_response ])
self.frm = frm # None allowed when it designate this bot
self.frm_owner = False # Filled later, in consumer
@property
def to_response(self):
if self._to_response is not None:
return self._to_response
else:
return self.to
@property
def receivers(self):
# TODO: this is for legacy modules
return self.to_response
@property
def channel(self):
# TODO: this is for legacy modules
return self.to_response[0]
@property
def nick(self):
# TODO: this is for legacy modules
return self.frm
def accept(self, visitor):
visitor.visit(self)
def export_args(self, without=list()):
if not isinstance(without, list):
without = [ without ]
ret = {
"server": self.server,
"date": self.date,
"to": self.to,
"to_response": self._to_response,
"frm": self.frm
}
for w in without:
if w in ret:
del ret[w]
return ret
class TextMessage(AbstractMessage):
"""This class represent a simple message send to someone"""
def __init__(self, message, *args, **kargs):
"""Initialize a message with no particular specificity
Argument:
message -- the parsed message
"""
AbstractMessage.__init__(self, *args, **kargs)
self.message = message
def __str__(self):
return self.message
@property
def text(self):
# TODO: this is for legacy modules
return self.message
class DirectAsk(TextMessage):
"""This class represents a message to this bot"""
def __init__(self, designated, *args, **kargs):
"""Initialize a message to a specific person
Argument:
designated -- the user designated by the message
"""
TextMessage.__init__(self, *args, **kargs)
self.designated = designated
def respond(self, message):
return DirectAsk(self.frm,
message,
server=self.server,
to=self.to_response)
class Command(AbstractMessage):
"""This class represents a specialized TextMessage"""
def __init__(self, cmd, args=None, *nargs, **kargs):
AbstractMessage.__init__(self, *nargs, **kargs)
self.cmd = cmd
self.args = args if args is not None else list()
def __str__(self):
return self.cmd + " @" + ",@".join(self.args)
@property
def cmds(self):
# TODO: this is for legacy modules
return [self.cmd] + self.args
class OwnerCommand(Command):
"""This class represents a special command incomming from the owner"""
pass
from nemubot.message.abstract import Abstract
from nemubot.message.text import Text
from nemubot.message.directask import DirectAsk
from nemubot.message.command import Command
from nemubot.message.command import OwnerCommand
def reload():
global Abstract, Text, DirectAsk, Command, OwnerCommand
import imp
import nemubot.message.abstract
imp.reload(nemubot.message.abstract)
Abstract = nemubot.message.abstract.Abstract
imp.reload(nemubot.message.text)
Text = nemubot.message.text.Text
imp.reload(nemubot.message.directask)
DirectAsk = nemubot.message.directask.DirectAsk
imp.reload(nemubot.message.command)
Command = nemubot.message.command.Command
OwnerCommand = nemubot.message.command.OwnerCommand
import nemubot.message.visitor
imp.reload(nemubot.message.visitor)

View file

@ -0,0 +1,90 @@
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime, timezone
class Abstract:
"""This class represents an abstract message"""
def __init__(self, server, date=None, to=None, to_response=None, frm=None):
"""Initialize an abstract message
Arguments:
server -- the servir identifier
date -- time of the message reception, default: now
to -- list of recipients
to_response -- if channel(s) where send the response differ
frm -- the sender
"""
self.server = server
self.date = datetime.now(timezone.utc) if date is None else date
self.to = to if to is not None else list()
self._to_response = (to_response if (to_response is None or
isinstance(to_response, list))
else [ to_response ])
self.frm = frm # None allowed when it designate this bot
self.frm_owner = False # Filled later, in consumer
@property
def to_response(self):
if self._to_response is not None:
return self._to_response
else:
return self.to
@property
def receivers(self):
# TODO: this is for legacy modules
return self.to_response
@property
def channel(self):
# TODO: this is for legacy modules
return self.to_response[0]
@property
def nick(self):
# TODO: this is for legacy modules
return self.frm
def accept(self, visitor):
visitor.visit(self)
def export_args(self, without=list()):
if not isinstance(without, list):
without = [ without ]
ret = {
"server": self.server,
"date": self.date,
"to": self.to,
"to_response": self._to_response,
"frm": self.frm
}
for w in without:
if w in ret:
del ret[w]
return ret

View file

@ -0,0 +1,43 @@
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from nemubot.message.abstract import Abstract
class Command(Abstract):
"""This class represents a specialized TextMessage"""
def __init__(self, cmd, args=None, *nargs, **kargs):
Abstract.__init__(self, *nargs, **kargs)
self.cmd = cmd
self.args = args if args is not None else list()
def __str__(self):
return self.cmd + " @" + ",@".join(self.args)
@property
def cmds(self):
# TODO: this is for legacy modules
return [self.cmd] + self.args
class OwnerCommand(Command):
"""This class represents a special command incomming from the owner"""
pass

View file

@ -0,0 +1,39 @@
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from nemubot.message.text import Text
class DirectAsk(Text):
"""This class represents a message to this bot"""
def __init__(self, designated, *args, **kargs):
"""Initialize a message to a specific person
Argument:
designated -- the user designated by the message
"""
Text.__init__(self, *args, **kargs)
self.designated = designated
def respond(self, message):
return DirectAsk(self.frm,
message,
server=self.server,
to=self.to_response)

View file

@ -16,7 +16,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from nemubot.message import TextMessage
from nemubot.message import Text
from nemubot.message.visitor import AbstractVisitor
@ -26,7 +26,7 @@ class IRC(AbstractVisitor):
self.pp = ""
def visit_TextMessage(self, msg):
def visit_Text(self, msg):
self.pp += "PRIVMSG %s :" % ",".join(msg.to)
if isinstance(msg.message, str):
self.pp += msg.message
@ -40,31 +40,31 @@ class IRC(AbstractVisitor):
# Avoid nick starting message when discussing on user channel
if len(others) != len(msg.to):
res = TextMessage(msg.message,
server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm)
res = Text(msg.message,
server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm)
res.accept(self)
if len(others):
res = TextMessage("%s: %s" % (msg.designated, msg.message),
server=msg.server, date=msg.date,
to=others, frm=msg.frm)
res = Text("%s: %s" % (msg.designated, msg.message),
server=msg.server, date=msg.date,
to=others, frm=msg.frm)
res.accept(self)
def visit_Command(self, msg):
res = TextMessage("!%s%s%s" % (msg.cmd,
" " if len(msg.args) else "",
" ".join(msg.args)),
server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm)
res = Text("!%s%s%s" % (msg.cmd,
" " if len(msg.args) else "",
" ".join(msg.args)),
server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm)
res.accept(self)
def visit_OwnerCommand(self, msg):
res = TextMessage("`%s%s%s" % (msg.cmd,
" " if len(msg.args) else "",
" ".join(msg.args)),
server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm)
res = Text("`%s%s%s" % (msg.cmd,
" " if len(msg.args) else "",
" ".join(msg.args)),
server=msg.server, date=msg.date,
to=msg.to, frm=msg.frm)
res.accept(self)

41
nemubot/message/text.py Normal file
View file

@ -0,0 +1,41 @@
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from nemubot.message.abstract import Abstract
class Text(Abstract):
"""This class represent a simple message send to someone"""
def __init__(self, message, *args, **kargs):
"""Initialize a message with no particular specificity
Argument:
message -- the parsed message
"""
Abstract.__init__(self, *args, **kargs)
self.message = message
def __str__(self):
return self.message
@property
def text(self):
# TODO: this is for legacy modules
return self.message