Skip to content

Commit 90b1b59

Browse files
authored
Merge pull request #92 from us-irs/bytes-api-update
raw byte APIs accept both bytes and bytearray
2 parents e70fb33 + 7fca8af commit 90b1b59

27 files changed

+100
-87
lines changed

spacepackets/ccsds/spacepacket.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def packet_len(self) -> int:
394394
return CCSDS_HEADER_LEN + self.data_len + 1
395395

396396
@classmethod
397-
def unpack(cls, data: bytes) -> SpacePacketHeader:
397+
def unpack(cls, data: bytes | bytearray) -> SpacePacketHeader:
398398
"""Unpack a raw space packet into the space packet header instance.
399399
400400
:raise ValueError: Raw packet length invalid
@@ -445,8 +445,8 @@ class SpacePacket:
445445
def __init__(
446446
self,
447447
sp_header: SpacePacketHeader,
448-
sec_header: bytes | None,
449-
user_data: bytes | None,
448+
sec_header: bytes | bytearray | None,
449+
user_data: bytes | bytearray | None,
450450
):
451451
self.sp_header = sp_header
452452
self.sec_header = sec_header

spacepackets/cfdp/lv.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
class CfdpLv:
10-
def __init__(self, value: bytes):
10+
def __init__(self, value: bytes | bytearray):
1111
"""This class encapsulates CFDP Length-Value (LV) fields.
1212
1313
Raises
@@ -42,7 +42,7 @@ def pack(self) -> bytearray:
4242
return packet
4343

4444
@classmethod
45-
def unpack(cls, raw_bytes: bytes) -> CfdpLv:
45+
def unpack(cls, raw_bytes: bytes | bytearray) -> CfdpLv:
4646
"""Parses LV field at the start of the given bytearray
4747
4848
:raise ValueError: Invalid length found

spacepackets/cfdp/pdu/ack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def __repr__(self):
121121
)
122122

123123
@classmethod
124-
def unpack(cls, data: bytes) -> AckPdu:
124+
def unpack(cls, data: bytes | bytearray) -> AckPdu:
125125
"""Generate an object instance from raw data. Care should be taken to check whether
126126
the raw bytestream really contains an ACK PDU.
127127

spacepackets/cfdp/pdu/eof.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def pack(self) -> bytearray:
122122
return eof_pdu
123123

124124
@classmethod
125-
def unpack(cls, data: bytes) -> EofPdu:
125+
def unpack(cls, data: bytes | bytearray) -> EofPdu:
126126
"""Generate an object instance from raw data. Care should be taken to check whether
127127
the raw bytestream really contains an EOF PDU.
128128
@@ -156,7 +156,9 @@ def unpack(cls, data: bytes) -> EofPdu:
156156
eof_pdu.fault_location = EntityIdTlv.unpack(data=data[current_idx:])
157157
return eof_pdu
158158

159-
def __eq__(self, other: EofPdu):
159+
def __eq__(self, other: object) -> bool:
160+
if not isinstance(other, EofPdu):
161+
return False
160162
return (
161163
self.pdu_file_directive == other.pdu_file_directive
162164
and self.condition_code == other.condition_code

spacepackets/cfdp/pdu/file_data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def pack(self) -> bytearray:
215215
return file_data_pdu
216216

217217
@classmethod
218-
def unpack(cls, data: bytes) -> FileDataPdu:
218+
def unpack(cls, data: bytes | bytearray) -> FileDataPdu:
219219
"""Generate an object instance from raw data. Care should be taken to check whether
220220
the raw bytestream really contains a File Data PDU.
221221
@@ -242,7 +242,7 @@ def unpack(cls, data: bytes) -> FileDataPdu:
242242
metadata = data[current_idx : current_idx + segment_metadata_len]
243243
current_idx += segment_metadata_len
244244
file_data_packet.segment_metadata = SegmentMetadata(
245-
record_cont_state=rec_cont_state, metadata=metadata
245+
record_cont_state=rec_cont_state, metadata=bytes(metadata)
246246
)
247247
if not file_data_packet.pdu_header.large_file_flag_set:
248248
struct_arg_tuple = ("!I", 4)
@@ -259,7 +259,7 @@ def unpack(cls, data: bytes) -> FileDataPdu:
259259
if file_data_packet.pdu_header.crc_flag == CrcFlag.WITH_CRC:
260260
data = data[:-2]
261261
if current_idx < len(data):
262-
file_data_packet._params.file_data = data[current_idx:]
262+
file_data_packet._params.file_data = bytes(data[current_idx:])
263263
return file_data_packet
264264

265265
@property

spacepackets/cfdp/pdu/file_directive.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def __init__(
133133
)
134134
self._directive_type = directive_code
135135

136-
def verify_length_and_checksum(self, data: bytes) -> None:
136+
def verify_length_and_checksum(self, data: bytes | bytearray) -> None:
137137
self.pdu_header.verify_length_and_checksum(data)
138138

139139
@property
@@ -172,7 +172,7 @@ def pack(self) -> bytearray:
172172
return data
173173

174174
@classmethod
175-
def unpack(cls, raw_packet: bytes) -> FileDirectivePduBase:
175+
def unpack(cls, raw_packet: bytes | bytearray) -> FileDirectivePduBase:
176176
"""Unpack a raw bytearray into the File Directive PDU object representation.
177177
178178
:param raw_packet: Unpack PDU file directive base
@@ -195,7 +195,7 @@ def _verify_file_len(self, file_size: int) -> None:
195195
if self.pdu_header.file_flag == LargeFileFlag.NORMAL and file_size > pow(2, 32):
196196
raise ValueError(f"File size {file_size} larger than 32 bit field")
197197

198-
def parse_fss_field(self, raw_packet: bytes, current_idx: int) -> tuple[int, int]:
198+
def parse_fss_field(self, raw_packet: bytes | bytearray, current_idx: int) -> tuple[int, int]:
199199
"""Parse the FSS field, which has different size depending on the large file flag being
200200
set or not. Returns the current index incremented and the parsed file size.
201201

spacepackets/cfdp/pdu/finished.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def pack(self) -> bytearray:
205205
return packet
206206

207207
@classmethod
208-
def unpack(cls, data: bytes) -> FinishedPdu:
208+
def unpack(cls, data: bytes | bytearray) -> FinishedPdu:
209209
"""Generate an object instance from raw data. Care should be taken to check whether
210210
the raw bytestream really contains a Finished PDU.
211211
@@ -241,7 +241,7 @@ def unpack(cls, data: bytes) -> FinishedPdu:
241241
finished_pdu._unpack_tlvs(rest_of_packet=data[current_idx:end_of_optional_tlvs_idx])
242242
return finished_pdu
243243

244-
def _unpack_tlvs(self, rest_of_packet: bytes) -> int:
244+
def _unpack_tlvs(self, rest_of_packet: bytes | bytearray) -> int:
245245
current_idx = 0
246246
fs_responses_list = []
247247
fault_loc = None
@@ -266,7 +266,7 @@ def _unpack_tlvs(self, rest_of_packet: bytes) -> int:
266266
self.fault_location = fault_loc
267267
return current_idx
268268

269-
def __eq__(self, other: object):
269+
def __eq__(self, other: object) -> bool:
270270
if not isinstance(other, FinishedPdu):
271271
return False
272272
return self._params == other._params and self.pdu_file_directive == other.pdu_file_directive

spacepackets/cfdp/pdu/header.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ def packet_len(self) -> int:
120120
def large_file_flag_set(self) -> bool:
121121
return self.file_flag == LargeFileFlag.LARGE
122122

123-
def __eq__(self, other: AbstractPduBase):
123+
def __eq__(self, other: object) -> bool:
124+
if not isinstance(other, AbstractPduBase):
125+
return False
124126
return (
125127
self.pdu_type == other.pdu_type
126128
and self.file_flag == other.file_flag
@@ -131,7 +133,7 @@ def __eq__(self, other: AbstractPduBase):
131133
)
132134

133135
@staticmethod
134-
def header_len_from_raw(data: bytes) -> int:
136+
def header_len_from_raw(data: bytes | bytearray) -> int:
135137
entity_id_len = ((data[3] >> 4) & 0b111) + 1
136138
seq_num_len = (data[3] & 0b111) + 1
137139
return AbstractPduBase.FIXED_LENGTH + 2 * entity_id_len + seq_num_len
@@ -306,7 +308,7 @@ def __empty(cls) -> PduHeader:
306308
)
307309

308310
@classmethod
309-
def unpack(cls, data: bytes) -> PduHeader:
311+
def unpack(cls, data: bytes | bytearray) -> PduHeader:
310312
"""Unpack a raw bytearray into the PDU header object representation.
311313
312314
:param data:
@@ -352,7 +354,7 @@ def unpack(cls, data: bytes) -> PduHeader:
352354
pdu_header.set_entity_ids(source_entity_id=source_entity_id, dest_entity_id=dest_entity_id)
353355
return pdu_header
354356

355-
def verify_length_and_checksum(self, data: bytes) -> int:
357+
def verify_length_and_checksum(self, data: bytes | bytearray) -> int:
356358
if len(data) < self.packet_len:
357359
raise BytesTooShortError(self.packet_len, len(data))
358360
if (

spacepackets/cfdp/pdu/helper.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ class PduFactory:
126126
"""Helper class to generate PDUs and retrieve PDU information from a raw bytestream"""
127127

128128
@staticmethod
129-
def from_raw(data: bytes) -> GenericPduPacket | None: # noqa: PLR0911
129+
def from_raw(data: bytes | bytearray) -> GenericPduPacket | None: # noqa: PLR0911
130+
if len(data) == 0:
131+
return None
130132
if not PduFactory.is_file_directive(data):
131133
return FileDataPdu.unpack(data)
132134
directive = PduFactory.pdu_directive_type(data)
@@ -147,19 +149,19 @@ def from_raw(data: bytes) -> GenericPduPacket | None: # noqa: PLR0911
147149
return None
148150

149151
@staticmethod
150-
def from_raw_to_holder(data: bytes) -> PduHolder:
152+
def from_raw_to_holder(data: bytes | bytearray) -> PduHolder:
151153
return PduHolder(PduFactory.from_raw(data))
152154

153155
@staticmethod
154-
def pdu_type(data: bytes) -> PduType:
156+
def pdu_type(data: bytes | bytearray) -> PduType:
155157
return PduType((data[0] >> 4) & 0x01)
156158

157159
@staticmethod
158-
def is_file_directive(data: bytes) -> bool:
160+
def is_file_directive(data: bytes | bytearray) -> bool:
159161
return PduFactory.pdu_type(data) == PduType.FILE_DIRECTIVE
160162

161163
@staticmethod
162-
def pdu_directive_type(data: bytes) -> DirectiveType | None:
164+
def pdu_directive_type(data: bytes | bytearray) -> DirectiveType | None:
163165
"""Retrieve the PDU directive type from a raw bytestream.
164166
165167
:raises ValueError: Invalid directive type.

spacepackets/cfdp/pdu/keep_alive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def pack(self) -> bytearray:
7575
return keep_alive_packet
7676

7777
@classmethod
78-
def unpack(cls, data: bytes) -> KeepAlivePdu:
78+
def unpack(cls, data: bytes | bytearray) -> KeepAlivePdu:
7979
"""Generate an object instance from raw data. Care should be taken to check whether
8080
the raw bytestream really contains a Keep Alive PDU.
8181

0 commit comments

Comments
 (0)