ByteStringSplitter can now take kwargs and can be added to other splitters.

pull/157/head
jMyles 2018-02-11 01:03:36 -08:00
parent 505c3a7115
commit ccf3cb1f2d
1 changed files with 24 additions and 8 deletions

View File

@ -47,17 +47,33 @@ class BytestringSplitter(object):
def __len__(self):
return sum(self.get_message_meta(m)[1] for m in self.message_types)
@staticmethod
def get_message_meta(message_type):
if isinstance(message_type, tuple):
message_meta = message_type
else:
try:
message_meta = message_type, message_type._EXPECTED_LENGTH
message_class = message_type[0]
except TypeError:
message_class = message_type
try:
message_length = message_type[1]
except TypeError:
message_length = message_type._EXPECTED_LENGTH
except AttributeError:
raise TypeError("No way to know the expected length. Either pass it as the second member of a tuple or set _EXPECTED_LENGTH on the class you're passing.")
return message_meta
try:
kwargs = message_type[2]
except (IndexError, TypeError):
kwargs = {}
return message_class, message_length, kwargs
def __add__(self, splitter):
return self.__class__(*self.message_types + splitter.message_types)
def __radd__(self, other):
return other + bytes(self)
class RepeatingBytestringSplitter(BytestringSplitter):