From 8730cbd36703b5b4db3ecf41c095ccc093b89875 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 9 Sep 2024 19:42:50 +0200 Subject: [PATCH] Entity ID TLV eq custom method and AbstractTlvBase __repr__ --- CHANGELOG.md | 11 +++++++++++ spacepackets/cfdp/tlv/base.py | 7 ++++++- spacepackets/cfdp/tlv/tlv.py | 12 ++++++++++++ tests/cfdp/tlvslvs/test_entity_id.py | 11 +++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b8ada96..0e2d4331 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/). # [unreleased] +# [v0.24.2] 2024-09-09 + +## Fixed + +- Custom `EntityIdTlv` `__eq__` implementation which only compares the numerical value + of the entity ID TLVs + +## Added + +- `AbstractTlvBase` `__repr__` implementation + # [v0.24.1] 2024-04-23 ## Reverted diff --git a/spacepackets/cfdp/tlv/base.py b/spacepackets/cfdp/tlv/base.py index dd229d9e..504cf2d8 100644 --- a/spacepackets/cfdp/tlv/base.py +++ b/spacepackets/cfdp/tlv/base.py @@ -25,7 +25,12 @@ def tlv_type(self) -> TlvType: def value(self) -> bytes: pass - def __eq__(self, other: AbstractTlvBase): + def __repr__(self) -> str: + return f"Tlv(tlv_type={self.tlv_type!r}, value=0x[{self.value.hex(sep=',')}])" + + def __eq__(self, other: object): + if not isinstance(other, AbstractTlvBase): + return False return self.tlv_type == other.tlv_type and self.value == other.value def check_type(self, tlv_type: TlvType): diff --git a/spacepackets/cfdp/tlv/tlv.py b/spacepackets/cfdp/tlv/tlv.py index 9ebd76b1..601d2a20 100644 --- a/spacepackets/cfdp/tlv/tlv.py +++ b/spacepackets/cfdp/tlv/tlv.py @@ -15,6 +15,7 @@ ) from spacepackets.exceptions import BytesTooShortError from spacepackets.cfdp.exceptions import TlvTypeMissmatch +from spacepackets.util import UnsignedByteField def map_enum_status_code_to_int(status_code: FilestoreResponseStatusCode) -> int: @@ -502,6 +503,9 @@ def _set_fields(cls, instance: FileStoreResponseTlv, data: bytes): class EntityIdTlv(AbstractTlvBase): + """This helper class has a :py:meth:`__eq__` implementation which only compares the numerical value + of the entity IDs""" + TLV_TYPE = TlvType.ENTITY_ID def __init__(self, entity_id: bytes): @@ -540,3 +544,11 @@ def from_tlv(cls, cfdp_tlv: CfdpTlv) -> EntityIdTlv: entity_id_tlv = cls.__empty() entity_id_tlv.tlv = cfdp_tlv return entity_id_tlv + + def __eq__(self, other: AbstractTlvBase) -> bool: + """Custom implementation which only compares the numerical value of the entity IDs""" + if not isinstance(other, EntityIdTlv): + return False + own_id = UnsignedByteField.from_bytes(self.value) + other_id = UnsignedByteField.from_bytes(other.value) + return own_id.value == other_id.value diff --git a/tests/cfdp/tlvslvs/test_entity_id.py b/tests/cfdp/tlvslvs/test_entity_id.py index 1d4df1d4..2a4405eb 100644 --- a/tests/cfdp/tlvslvs/test_entity_id.py +++ b/tests/cfdp/tlvslvs/test_entity_id.py @@ -41,3 +41,14 @@ def test_invalid_type(self): entity_id_tlv_tlv.tlv_type = TlvType.FILESTORE_REQUEST with self.assertRaises(TlvTypeMissmatch): EntityIdTlv.from_tlv(cfdp_tlv=entity_id_tlv_tlv) + + def test_custom_eq(self): + self.entity_id_tlv = EntityIdTlv(entity_id=bytes([0x00, 0x01])) + self.entity_id_tlv_same = EntityIdTlv(entity_id=bytes([0x01])) + self.assertEqual(self.entity_id_tlv, self.entity_id_tlv_same) + + def test_repr(self): + repr_str = repr(self.entity_id_tlv) + self.assertEqual( + repr_str, f"Tlv(tlv_type={TlvType.ENTITY_ID!r}, value=0x[00,01,02,03])" + )