This repository has been archived on 2025-02-24. You can view files and clone it, but cannot push or open issues or pull requests.

38 lines
1.2 KiB
Python
Raw Normal View History

import logging
from messagetypes import MsgBase
logger = logging.getLogger('default')
class Message(MsgBase):
2019-09-30 18:42:36 +05:30
"""Encapsulate a message"""
# pylint: disable=attribute-defined-outside-init
def decode(self, data):
2019-09-30 18:42:36 +05:30
"""Decode a message"""
# UTF-8 and variable type validator
2019-09-30 18:42:36 +05:30
if isinstance(data["subject"], str):
self.subject = unicode(data["subject"], 'utf-8', 'replace')
else:
self.subject = unicode(str(data["subject"]), 'utf-8', 'replace')
2019-09-30 18:42:36 +05:30
if isinstance(data["body"], str):
self.body = unicode(data["body"], 'utf-8', 'replace')
else:
self.body = unicode(str(data["body"]), 'utf-8', 'replace')
def encode(self, data):
2019-09-30 18:42:36 +05:30
"""Encode a message"""
super(Message, self).__init__()
try:
self.data["subject"] = data["subject"]
self.data["body"] = data["body"]
except KeyError as e:
logger.error("Missing key %s", e)
return self.data
def process(self):
2019-09-30 18:42:36 +05:30
"""Process a message"""
logger.debug("Subject: %i bytes", len(self.subject))
logger.debug("Body: %i bytes", len(self.body))