2019-09-20 13:19:04 +02:00
|
|
|
"""
|
|
|
|
src/messagetypes/message.py
|
|
|
|
=================================
|
|
|
|
"""
|
2016-11-12 17:20:45 +01:00
|
|
|
from debug import logger
|
|
|
|
from messagetypes import MsgBase
|
2019-09-20 13:19:04 +02:00
|
|
|
# pylint: disable=attribute-defined-outside-init
|
2016-11-12 17:20:45 +01:00
|
|
|
|
2016-11-15 17:10:33 +01:00
|
|
|
|
2016-11-12 17:20:45 +01:00
|
|
|
class Message(MsgBase):
|
2019-09-20 13:19:04 +02:00
|
|
|
"""Base method, helps to decode, encode and process the message"""
|
|
|
|
def __init__(self): # pylint: disable=super-init-not-called
|
2016-11-12 17:20:45 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
def decode(self, data):
|
2019-09-20 13:19:04 +02:00
|
|
|
"""Method used for decoding the message"""
|
2016-11-15 17:10:33 +01:00
|
|
|
# UTF-8 and variable type validator
|
2019-09-20 13:19:04 +02:00
|
|
|
# pylint: disable=unidiomatic-typecheck
|
2016-11-15 17:10:33 +01:00
|
|
|
if type(data["subject"]) is str:
|
|
|
|
self.subject = unicode(data["subject"], 'utf-8', 'replace')
|
|
|
|
else:
|
|
|
|
self.subject = unicode(str(data["subject"]), 'utf-8', 'replace')
|
|
|
|
if type(data["body"]) is str:
|
|
|
|
self.body = unicode(data["body"], 'utf-8', 'replace')
|
|
|
|
else:
|
|
|
|
self.body = unicode(str(data["body"]), 'utf-8', 'replace')
|
2016-11-03 22:41:36 +01:00
|
|
|
|
2016-11-12 17:20:45 +01:00
|
|
|
def encode(self, data):
|
2019-09-20 13:19:04 +02:00
|
|
|
"""Method used for encoding the message"""
|
|
|
|
# pylint: disable=no-member
|
2016-11-12 17:20:45 +01:00
|
|
|
super(Message, self).encode()
|
|
|
|
try:
|
|
|
|
self.data["subject"] = data["subject"]
|
|
|
|
self.data["body"] = data["body"]
|
|
|
|
except KeyError as e:
|
2019-07-05 10:45:12 +02:00
|
|
|
logger.error("Missing key %s", e)
|
2016-11-12 17:20:45 +01:00
|
|
|
return self.data
|
|
|
|
|
2016-11-03 22:41:36 +01:00
|
|
|
def process(self):
|
2019-09-20 13:19:04 +02:00
|
|
|
"""Method used for process the message"""
|
2016-11-12 17:20:45 +01:00
|
|
|
logger.debug("Subject: %i bytes", len(self.subject))
|
|
|
|
logger.debug("Body: %i bytes", len(self.body))
|