Messages now implements Serializable

This commit is contained in:
nemunaire 2016-04-04 10:21:09 +02:00
parent e0af09f3c5
commit e3150a6061
3 changed files with 40 additions and 7 deletions

View File

@ -120,6 +120,7 @@ class XML(Abstract):
from nemubot.tools.xmlparser import XMLParser from nemubot.tools.xmlparser import XMLParser
from nemubot.datastore.nodes import basic as basicNodes from nemubot.datastore.nodes import basic as basicNodes
from nemubot.datastore.nodes import python as pythonNodes from nemubot.datastore.nodes import python as pythonNodes
from nemubot.message.command import Command
d = { d = {
basicNodes.ListNode.serializetag: basicNodes.ListNode, basicNodes.ListNode.serializetag: basicNodes.ListNode,
@ -127,6 +128,7 @@ class XML(Abstract):
pythonNodes.IntNode.serializetag: pythonNodes.IntNode, pythonNodes.IntNode.serializetag: pythonNodes.IntNode,
pythonNodes.FloatNode.serializetag: pythonNodes.FloatNode, pythonNodes.FloatNode.serializetag: pythonNodes.FloatNode,
pythonNodes.StringNode.serializetag: pythonNodes.StringNode, pythonNodes.StringNode.serializetag: pythonNodes.StringNode,
Command.serializetag: Command,
} }
d.update(extendsTags) d.update(extendsTags)

View File

@ -16,11 +16,16 @@
from datetime import datetime, timezone from datetime import datetime, timezone
from nemubot.datastore.nodes import Serializable
class Abstract:
class Abstract(Serializable):
"""This class represents an abstract message""" """This class represents an abstract message"""
serializetag = "nemubotAMessage"
def __init__(self, server=None, date=None, to=None, to_response=None, frm=None, frm_owner=False): def __init__(self, server=None, date=None, to=None, to_response=None, frm=None, frm_owner=False):
"""Initialize an abstract message """Initialize an abstract message
@ -87,3 +92,8 @@ class Abstract:
del ret[w] del ret[w]
return ret return ret
def serialize(self):
from nemubot.datastore.nodes import ParsingNode
return ParsingNode(tag=Abstract.serializetag, **self.export_args())

View File

@ -1,5 +1,5 @@
# Nemubot is a smart and modulable IM bot. # Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 Mercier Pierre-Olivier # Copyright (C) 2012-2016 Mercier Pierre-Olivier
# #
# This program is free software: you can redistribute it and/or modify # 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 # it under the terms of the GNU Affero General Public License as published by
@ -21,6 +21,9 @@ class Command(Abstract):
"""This class represents a specialized TextMessage""" """This class represents a specialized TextMessage"""
serializetag = "nemubotCommand"
def __init__(self, cmd, args=None, kwargs=None, *nargs, **kargs): def __init__(self, cmd, args=None, kwargs=None, *nargs, **kargs):
super().__init__(*nargs, **kargs) super().__init__(*nargs, **kargs)
@ -28,17 +31,35 @@ class Command(Abstract):
self.args = args if args is not None else list() self.args = args if args is not None else list()
self.kwargs = kwargs if kwargs is not None else dict() self.kwargs = kwargs if kwargs is not None else dict()
def __str__(self):
def __repr__(self):
return self.cmd + " @" + ",@".join(self.args) return self.cmd + " @" + ",@".join(self.args)
@property
def cmds(self): def addChild(self, name, child):
# TODO: this is for legacy modules if name == "list":
return [self.cmd] + self.args self.args = child
elif name == "dict":
self.kwargs = child
else:
return False
return True
def serialize(self):
from nemubot.datastore.nodes import ParsingNode
node = ParsingNode(tag=Command.serializetag, cmd=self.cmd)
if len(self.args):
node.children.append(ParsingNode.serialize_node(self.args))
if len(self.kwargs):
node.children.append(ParsingNode.serialize_node(self.kwargs))
return node
class OwnerCommand(Command): class OwnerCommand(Command):
"""This class represents a special command incomming from the owner""" """This class represents a special command incomming from the owner"""
serializetag = "nemubotOCommand"
pass pass