Skip to content

Commit

Permalink
all linter updates
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed Nov 27, 2024
1 parent 87a2f4d commit e065fed
Show file tree
Hide file tree
Showing 45 changed files with 594 additions and 400 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

# [unreleased]

# [v0.26.0] 2024-11-27

## Changed

- `MetadataPdu` options have to be specified as an optional list of abstract TLVs now.
A new getter method `options_as_tlv` can be used to retrieve a list of concrete TLV objects.

## Fixed

- `CrcError` exception constructor was previously named `__int__` by accident.

# [v0.25.0] 2024-10-29

## Changed
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
name = "spacepackets"
description = "Various CCSDS and ECSS packet implementations"
readme = "README.md"
version = "0.25.0"
version = "0.26.0"
requires-python = ">=3.9"
license = {text = "Apache-2.0"}
authors = [
Expand Down Expand Up @@ -82,7 +82,8 @@ ignore = [
"S101", # Use of assert, should be changed in the future
"ANN204", # Do not use return typing on __init__, __new__ and __call__ methods
"E111", # Recommended to be disabled when using the ruff formatter
"E114" # Recommended to be disabled when using the ruff formatter
"E114", # Recommended to be disabled when using the ruff formatter
"PLR2004" # This lint is a bit too conservative. Not every number needs to be a named constant.
]

[tool.ruff.lint.extend-per-file-ignores]
Expand Down
9 changes: 9 additions & 0 deletions spacepackets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
)
from spacepackets.exceptions import BytesTooShortError

__all__ = [
"BytesTooShortError",
"PacketType",
"SequenceFlags",
"SpHeader",
"SpacePacket",
"SpacePacketHeader",
]

__LIB_LOGGER = logging.getLogger(__name__)


Expand Down
13 changes: 13 additions & 0 deletions spacepackets/ccsds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@
get_total_space_packet_len_from_len_field,
)
from .time import * # noqa: F403 # re-export

__all__ = [
"SPACE_PACKET_HEADER_SIZE",
"AbstractSpacePacket",
"PacketId",
"PacketSeqCtrl",
"PacketType",
"SequenceFlags",
"SpHeader",
"SpacePacket",
"SpacePacketHeader",
"get_total_space_packet_len_from_len_field",
]
56 changes: 30 additions & 26 deletions spacepackets/ccsds/spacepacket.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import enum
import struct
from abc import ABC, abstractmethod
from collections.abc import Sequence
from typing import Deque, Final, List, Optional, Tuple
from typing import TYPE_CHECKING, Final

from spacepackets.exceptions import BytesTooShortError

if TYPE_CHECKING:
from collections import deque
from collections.abc import Sequence

CCSDS_HEADER_LEN: Final[int] = 6
SPACE_PACKET_HEADER_SIZE: Final[int] = CCSDS_HEADER_LEN
SEQ_FLAG_MASK: Final[int] = 0xC000
Expand Down Expand Up @@ -66,7 +69,7 @@ def raw(self) -> int:
return self.seq_flags << 14 | self.seq_count

@classmethod
def empty(cls):
def empty(cls) -> PacketSeqCtrl:
return cls(seq_flags=SequenceFlags.CONTINUATION_SEGMENT, seq_count=0)

def __eq__(self, other: object) -> bool:
Expand All @@ -75,7 +78,7 @@ def __eq__(self, other: object) -> bool:
return False

@classmethod
def from_raw(cls, raw: int):
def from_raw(cls, raw: int) -> PacketSeqCtrl:
return cls(seq_flags=SequenceFlags((raw >> 14) & 0b11), seq_count=raw & ~SEQ_FLAG_MASK)


Expand All @@ -91,7 +94,7 @@ def __init__(self, ptype: PacketType, sec_header_flag: bool, apid: int):
self.apid = apid

@classmethod
def empty(cls):
def empty(cls) -> PacketId:
return cls(ptype=PacketType.TM, sec_header_flag=False, apid=0)

def __repr__(self):
Expand Down Expand Up @@ -166,7 +169,8 @@ def pack(self) -> bytearray:


class SpacePacketHeader(AbstractSpacePacket):
"""This class encapsulates the space packet header. Packet reference: Blue Book CCSDS 133.0-B-2"""
"""This class encapsulates the space packet header. Packet reference: Blue Book
CCSDS 133.0-B-2"""

def __init__(
self,
Expand Down Expand Up @@ -320,7 +324,7 @@ def packet_type(self) -> PacketType:
return self.packet_id.ptype

@packet_type.setter
def packet_type(self, packet_type):
def packet_type(self, packet_type: PacketType) -> None:
self.packet_id.ptype = packet_type

@property
Expand All @@ -336,14 +340,14 @@ def sec_header_flag(self) -> bool:
return self._packet_id.sec_header_flag

@sec_header_flag.setter
def sec_header_flag(self, value):
def sec_header_flag(self, value: bool) -> None:
self._packet_id.sec_header_flag = value

@property
def seq_count(self):
def seq_count(self) -> int:
return self._psc.seq_count

def set_data_len_from_packet_len(self, packet_len: int):
def set_data_len_from_packet_len(self, packet_len: int) -> None:
"""Sets the data length field from the given total packet length. The total packet length
must be at least 7 bytes.
Expand All @@ -357,23 +361,23 @@ def set_data_len_from_packet_len(self, packet_len: int):
self.data_len = packet_len - CCSDS_HEADER_LEN - 1

@seq_count.setter
def seq_count(self, seq_cnt):
def seq_count(self, seq_cnt: int) -> None:
self._psc.seq_count = seq_cnt

@property
def seq_flags(self):
def seq_flags(self) -> SequenceFlags:
return self._psc.seq_flags

@seq_flags.setter
def seq_flags(self, value):
def seq_flags(self, value: SequenceFlags) -> None:
self._psc.seq_flags = value

@property
def header_len(self) -> int:
return CCSDS_HEADER_LEN

@apid.setter
def apid(self, apid):
def apid(self, apid: int) -> None:
self.packet_id.apid = apid

@property
Expand Down Expand Up @@ -441,8 +445,8 @@ class SpacePacket:
def __init__(
self,
sp_header: SpacePacketHeader,
sec_header: Optional[bytes],
user_data: Optional[bytes],
sec_header: bytes | None,
user_data: bytes | None,
):
self.sp_header = sp_header
self.sec_header = sec_header
Expand Down Expand Up @@ -475,15 +479,15 @@ def pack(self) -> bytearray:
return packet

@property
def apid(self):
def apid(self) -> int:
return self.sp_header.apid

@property
def seq_count(self):
def seq_count(self) -> int:
return self.sp_header.seq_count

@property
def sec_header_flag(self):
def sec_header_flag(self) -> bool:
return self.sp_header.sec_header_flag

def __eq__(self, other: object):
Expand All @@ -501,7 +505,7 @@ def get_space_packet_id_bytes(
secondary_header_flag: bool,
apid: int,
version: int = 0b000,
) -> Tuple[int, int]:
) -> tuple[int, int]:
"""This function also includes the first three bits reserved for the version.
:param version: Version field of the packet ID. Defined to be 0b000 in the space packet standard
Expand Down Expand Up @@ -542,16 +546,16 @@ def get_apid_from_raw_space_packet(raw_packet: bytes) -> int:
return ((raw_packet[0] & 0x7) << 8) | raw_packet[1]


def get_total_space_packet_len_from_len_field(len_field: int):
def get_total_space_packet_len_from_len_field(len_field: int) -> int:
"""Definition of length field is: C = (Octets in data field - 1).
Therefore, octets in data field in len_field plus one. The total space packet length
is therefore len_field plus one plus the space packet header size (6)"""
return len_field + SPACE_PACKET_HEADER_SIZE + 1


def parse_space_packets(
analysis_queue: Deque[bytearray], packet_ids: Sequence[PacketId]
) -> List[bytearray]:
analysis_queue: deque[bytearray], packet_ids: Sequence[PacketId]
) -> list[bytearray]:
"""Given a deque of bytearrays, parse for space packets. This funtion expects the deque
to be filled on the right side, for example with :py:meth:`collections.deque.append`.
If a split packet with a valid header is detected, this function will re-insert the header into
Expand Down Expand Up @@ -598,10 +602,10 @@ def parse_space_packets(

def __handle_packet_id_match(
concatenated_packets: bytearray,
analysis_queue: Deque[bytearray],
analysis_queue: deque[bytearray],
current_idx: int,
tm_list: List[bytearray],
) -> Tuple[int, int]:
tm_list: list[bytearray],
) -> tuple[int, int]:
total_packet_len = get_total_space_packet_len_from_len_field(
struct.unpack("!H", concatenated_packets[current_idx + 4 : current_idx + 6])[0]
)
Expand Down
8 changes: 8 additions & 0 deletions spacepackets/ccsds/time/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@

from .cds import CdsShortTimestamp
from .common import MS_PER_DAY, SECONDS_PER_DAY, CcsdsTimeCodeId, CcsdsTimeProvider

__all__ = [
"MS_PER_DAY",
"SECONDS_PER_DAY",
"CcsdsTimeCodeId",
"CcsdsTimeProvider",
"CdsShortTimestamp",
]
17 changes: 8 additions & 9 deletions spacepackets/ccsds/time/cds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import math
import struct
import time
from typing import Optional, Tuple

import deprecation

Expand Down Expand Up @@ -59,7 +58,7 @@ def __init__(self, ccsds_days: int, ms_of_day: int, init_dt_unix_stamp: bool = T
-4383
>>> CdsShortTimestamp(0x0102, 0x03040506).pack().hex(sep=',')
'40,01,02,03,04,05,06'
"""
""" # noqa: E501
self.__p_field = bytes([CdsShortTimestamp.CDS_SHORT_ID << 4])
# CCSDS recommends a 1958 Januar 1 epoch, which is different from the Unix epoch
self._ccsds_days = ccsds_days
Expand All @@ -68,11 +67,11 @@ def __init__(self, ccsds_days: int, ms_of_day: int, init_dt_unix_stamp: bool = T
if init_dt_unix_stamp:
self._setup()

def _setup(self):
def _setup(self) -> None:
self._calculate_unix_seconds()
self._calculate_date_time()

def _calculate_unix_seconds(self):
def _calculate_unix_seconds(self) -> None:
unix_days = convert_ccsds_days_to_unix_days(self._ccsds_days)
self._unix_seconds = unix_days * SECONDS_PER_DAY
seconds_of_day = self._ms_of_day / 1000.0
Expand All @@ -81,7 +80,7 @@ def _calculate_unix_seconds(self):
else:
self._unix_seconds += seconds_of_day

def _calculate_date_time(self):
def _calculate_date_time(self) -> None:
if self._unix_seconds < 0:
self._datetime = datetime.datetime(
1970, 1, 1, tzinfo=datetime.timezone.utc
Expand Down Expand Up @@ -122,7 +121,7 @@ def from_unix_days(cls, unix_days: int, ms_of_day: int) -> CdsShortTimestamp:
)

@classmethod
def empty(cls, init_dt_unix_stamp: bool = True):
def empty(cls, init_dt_unix_stamp: bool = True) -> CdsShortTimestamp:
"""Empty instance containing only zero for all fields.
:return:
Expand All @@ -134,7 +133,7 @@ def unpack(cls, data: bytes) -> CdsShortTimestamp:
ccsds_days, ms_of_day = CdsShortTimestamp.unpack_from_raw(data)
return cls(ccsds_days=ccsds_days, ms_of_day=ms_of_day)

def read_from_raw(self, data: bytes):
def read_from_raw(self, data: bytes) -> CdsShortTimestamp:
"""Updates the instance from a given raw CDS short timestamp
:param data:
Expand All @@ -144,7 +143,7 @@ def read_from_raw(self, data: bytes):
self._setup()

@staticmethod
def unpack_from_raw(data: bytes) -> Tuple[int, int]:
def unpack_from_raw(data: bytes) -> tuple[int, int]:
if len(data) < CdsShortTimestamp.TIMESTAMP_SIZE:
raise BytesTooShortError(CdsShortTimestamp.TIMESTAMP_SIZE, len(data))
p_field = data[0]
Expand Down Expand Up @@ -241,7 +240,7 @@ def from_date_time(cls, dt: datetime.datetime) -> CdsShortTimestamp:
return cls.from_datetime(dt)

@staticmethod
def ms_of_today(seconds_since_epoch: Optional[float] = None):
def ms_of_today(seconds_since_epoch: float | None = None) -> int:
if seconds_since_epoch is None:
seconds_since_epoch = time.time()
fraction_ms = seconds_since_epoch - math.floor(seconds_since_epoch)
Expand Down
2 changes: 1 addition & 1 deletion spacepackets/ccsds/time/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def pack(self) -> bytes:
pass

@abstractmethod
def read_from_raw(self, timestamp: bytes):
def read_from_raw(self, timestamp: bytes) -> CcsdsTimeProvider:
pass

@abstractmethod
Expand Down
37 changes: 37 additions & 0 deletions spacepackets/cfdp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,40 @@
TlvHolder,
TlvType,
)

__all__ = [
"NULL_CHECKSUM_U32",
"CfdpLv",
"CfdpTlv",
"ChecksumType",
"ConditionCode",
"CrcFlag",
"DeliveryCode",
"Direction",
"DirectiveType",
"EntityIdTlv",
"FaultHandlerCode",
"FaultHandlerOverrideTlv",
"FileStatus",
"FileStoreRequestTlv",
"FileStoreResponseTlv",
"FilestoreActionCode",
"FilestoreResponseStatusCode",
"FinishedParams",
"FlowLabelTlv",
"GenericPduPacket",
"InvalidCrc",
"LargeFileFlag",
"MessageToUserTlv",
"PduConfig",
"PduFactory",
"PduHolder",
"PduType",
"SegmentMetadataFlag",
"SegmentationControl",
"TlvHolder",
"TlvType",
"TlvTypeMissmatch",
"TransactionId",
"TransmissionMode",
]
Loading

0 comments on commit e065fed

Please sign in to comment.