helper_msgcoding pylint fixes

This commit is contained in:
lakshyacis 2019-10-08 12:33:48 +05:30
parent f4c7ac5604
commit 05cda087d6
No known key found for this signature in database
GPG Key ID: D2C539C8EC63E9EB

View File

@ -25,19 +25,24 @@ BITMESSAGE_ENCODING_EXTENDED = 3
class MsgEncodeException(Exception): class MsgEncodeException(Exception):
"""Exception during message encoding"""
pass pass
class MsgDecodeException(Exception): class MsgDecodeException(Exception):
"""Exception during message decoding"""
pass pass
class DecompressionSizeException(MsgDecodeException): class DecompressionSizeException(MsgDecodeException):
# pylint: disable=super-init-not-called
"""Decompression resulted in too much data (attack protection)"""
def __init__(self, size): def __init__(self, size):
self.size = size self.size = size
class MsgEncode(object): class MsgEncode(object):
"""Message encoder class"""
def __init__(self, message, encoding=BITMESSAGE_ENCODING_SIMPLE): def __init__(self, message, encoding=BITMESSAGE_ENCODING_SIMPLE):
self.data = None self.data = None
self.encoding = encoding self.encoding = encoding
@ -52,6 +57,7 @@ class MsgEncode(object):
raise MsgEncodeException("Unknown encoding %i" % (encoding)) raise MsgEncodeException("Unknown encoding %i" % (encoding))
def encodeExtended(self, message): def encodeExtended(self, message):
"""Handle extended encoding"""
try: try:
msgObj = messagetypes.message.Message() msgObj = messagetypes.message.Message()
self.data = zlib.compress(msgpack.dumps(msgObj.encode(message)), 9) self.data = zlib.compress(msgpack.dumps(msgObj.encode(message)), 9)
@ -64,15 +70,18 @@ class MsgEncode(object):
self.length = len(self.data) self.length = len(self.data)
def encodeSimple(self, message): def encodeSimple(self, message):
"""Handle simple encoding"""
self.data = 'Subject:%(subject)s\nBody:%(body)s' % message self.data = 'Subject:%(subject)s\nBody:%(body)s' % message
self.length = len(self.data) self.length = len(self.data)
def encodeTrivial(self, message): def encodeTrivial(self, message):
"""Handle trivial encoding"""
self.data = message['body'] self.data = message['body']
self.length = len(self.data) self.length = len(self.data)
class MsgDecode(object): class MsgDecode(object):
"""Message decoder class"""
def __init__(self, encoding, data): def __init__(self, encoding, data):
self.encoding = encoding self.encoding = encoding
if self.encoding == BITMESSAGE_ENCODING_EXTENDED: if self.encoding == BITMESSAGE_ENCODING_EXTENDED:
@ -88,6 +97,7 @@ class MsgDecode(object):
self.subject = _translate("MsgDecode", "Unknown encoding") self.subject = _translate("MsgDecode", "Unknown encoding")
def decodeExtended(self, data): def decodeExtended(self, data):
"""Handle extended encoding"""
dc = zlib.decompressobj() dc = zlib.decompressobj()
tmp = "" tmp = ""
while len(tmp) <= BMConfigParser().safeGetInt("zlib", "maxsize"): while len(tmp) <= BMConfigParser().safeGetInt("zlib", "maxsize"):
@ -131,6 +141,7 @@ class MsgDecode(object):
self.body = msgObj.body self.body = msgObj.body
def decodeSimple(self, data): def decodeSimple(self, data):
"""Handle simple encoding"""
bodyPositionIndex = string.find(data, '\nBody:') bodyPositionIndex = string.find(data, '\nBody:')
if bodyPositionIndex > 1: if bodyPositionIndex > 1:
subject = data[8:bodyPositionIndex] subject = data[8:bodyPositionIndex]