From cbca0b1f94b75c49ee0159c3f2c603bdfe3902c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <pmaslanka@smok.co> Date: Thu, 14 Oct 2021 19:54:10 +0200 Subject: [PATCH] * added `BinaryParser.get_remaining_bytes` * added `BinaryParser.get_remaining_bytes_count` --- CHANGELOG.md | 3 +++ satella/__init__.py | 2 +- satella/parsing.py | 25 ++++++++++++++++++++++++- tests/test_parsing.py | 2 ++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da55eba0..7df23e0c 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 8a9a487b..003b9571 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 3fb65820..c8098b70 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 20da8c15..796f8582 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) -- GitLab