diff --git a/CHANGELOG.md b/CHANGELOG.md index da55eba03c6e7519673b68a95473304696d0bbfa..7df23e0c1937d92572d27d7e4e811dfbc318b2cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,4 @@ # v2.18.1 + +* added `BinaryParser.get_remaining_bytes` +* added `BinaryParser.get_remaining_bytes_count` diff --git a/satella/__init__.py b/satella/__init__.py index 8a9a487bca75f26b5c278fa53414777afe0f2901..003b957117fc3b97261e503aca3a766106954ad3 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.18.1a1' +__version__ = '2.18.1a2' diff --git a/satella/parsing.py b/satella/parsing.py index 3fb65820dfcc7b81e4ef30470f10e9bdd1078a1f..c8098b7021a0348f71fe87356305b6775689f85e 100644 --- a/satella/parsing.py +++ b/satella/parsing.py @@ -6,7 +6,9 @@ from satella.exceptions import NotEnoughBytes class BinaryParser: """ - A class that allows parsing binary streams easily + A class that allows parsing binary streams easily. + + This supports __len__ to return the amount of bytes remaining. :param b_stream: an object that allows indiced access, and allows subscripts to span ranges, which will return items parseable by struct @@ -15,6 +17,15 @@ class BinaryParser: :ivar offset: offset from which bytes will be readed """ + def __len__(self) -> int: + return self.get_remaining_bytes_count() + + def get_remaining_bytes_count(self) -> int: + """ + Return the amount of bytes remaining. This will not advance the pointer + """ + return self.stream_length - self.pointer + def __init__(self, b_stream: tp.Union[bytes, bytearray], offset: int = 0): self.b_stream = b_stream self.struct_cache = {} @@ -57,6 +68,8 @@ class BinaryParser: This must be a single-character struct! + This will advance the pointer by size of st. + :param st: a single-character struct.Struct or a single character struct specification :return: a value returned from it :raises NotEnoughBytes: not enough bytes remain in the stream! @@ -85,6 +98,8 @@ class BinaryParser: """ Try to obtain as many bytes as this struct requires and return them parsed. + This will advance the pointer by size of st. + :param st: a struct.Struct or a multi character struct specification :return: a tuple of un-parsed values :raises NotEnoughBytes: not enough bytes remain in the stream! @@ -97,3 +112,11 @@ class BinaryParser: return st.unpack(self.b_stream[self.pointer:self.pointer+st_len]) finally: self.pointer += st_len + + def get_remaining_bytes(self) -> tp.Union[bytes, bytearray]: + """ + Return the remaining bytes. + + This will not advance the pointer + """ + return self.b_stream[self.pointer:] diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 20da8c159ea5e71c00ed5237b959aac9123802c4..796f858279a8b76414201dc532b9ed7769226910 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -14,3 +14,5 @@ class TestParsing(unittest.TestCase): self.assertRaises(NotEnoughBytes, lambda: bp.get_struct('>L')) self.assertRaises(NotEnoughBytes, lambda: bp.get_bytes(5)) self.assertRaises(NotEnoughBytes, lambda: BinaryParser(b'', 1)) + self.assertEqual(bp.get_remaining_bytes(), b'\x00') + self.assertEqual(len(bp), 1)