From e01f4d5110e7b59711daaee33b542fab184d9960 Mon Sep 17 00:00:00 2001 From: David-Rey Date: Tue, 7 Feb 2023 17:37:19 -0500 Subject: [PATCH 1/8] added setters and validators to packet class --- EosLib/packet/packet.py | 66 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/EosLib/packet/packet.py b/EosLib/packet/packet.py index 33cfc04..a860280 100644 --- a/EosLib/packet/packet.py +++ b/EosLib/packet/packet.py @@ -22,6 +22,7 @@ def __init__(self, body: bytes, data_header: DataHeader, transmit_header: Transm self.body = body # type: bytes self.data_header = data_header # type: DataHeader self.transmit_header = transmit_header # type: TransmitHeader + self.validate_packet() # checks if packet is valid def __eq__(self, other): """ Compares two packets for value equality @@ -129,6 +130,71 @@ def encode_to_string(self): data_header=self.data_header.encode_to_string(), body=self.body.decode()) + def set_data_header(self, new_data_header: DataHeader): + """ Setter that sets new data header + + :return: boolean True set is successful + """ + if new_data_header is not None: + if new_data_header.validate_data_header(): + self.data_header = new_data_header + + return True + + def set_transmit_header(self, new_transmit_header: TransmitHeader): + """ Setter that sets new transmit header + + :return: boolean True set is successful + """ + if new_transmit_header is not None: + if new_transmit_header.validate_transmit_header(): + self.transmit_header = new_transmit_header + + return True + + def set_body(self, new_body: bytes): + """ Setter that sets new body + + :return: boolean True set is successful + """ + if new_body is not None and len(new_body) == 0: + self.body = new_body + + @staticmethod + def check_data_header(data_header: DataHeader): + """ Takes a packet data header and checks to see if it is valid + + :return: boolean True if valid + """ + if data_header is None: + raise PacketFormatError("All packets must have a data header") + else: + data_header.validate_data_header() + + return True + + @staticmethod + def check_transmit_header(transmit_header: TransmitHeader): + """ Takes a packet transmit header and checks to see if it is valid + + :return: boolean True if valid + """ + if transmit_header is not None: + transmit_header.validate_transmit_header() + + return True + + @staticmethod + def check_body(body: bytes): + """ Takes a packet body and checks to see if it is valid + + :return: boolean True if valid + """ + if body is None or len(body) == 0: + raise PacketFormatError("All packets must have a body") + + return True + @staticmethod def decode(packet_bytes: bytes): """Takes a bytes object and decodes it into a Packet object. From 71fe0e9607f1237a335895c8158a8bac8584e0f8 Mon Sep 17 00:00:00 2001 From: David-Rey Date: Tue, 7 Feb 2023 19:21:14 -0500 Subject: [PATCH 2/8] added check to see if value is part of enum --- EosLib/packet/data_header.py | 15 ++++++++------- EosLib/packet/definitions.py | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/EosLib/packet/data_header.py b/EosLib/packet/data_header.py index a8b7afa..6870762 100644 --- a/EosLib/packet/data_header.py +++ b/EosLib/packet/data_header.py @@ -8,7 +8,6 @@ class DataHeader: - data_header_struct_format_string = "!" \ "B" \ "B" \ @@ -50,17 +49,19 @@ def validate_data_header(self): :return: True if valid """ - if not isinstance(self.sender, int) or not 0 <= self.sender <= 255 or self.sender == \ - definitions.Device.NO_DEVICE: - raise DataHeaderFormatError("Invalid Sender") + # if not isinstance(self.sender, int) or not 0 <= self.sender <= 255 or self.sender == \ + # definitions.Device.NO_DEVICE: + if not isinstance(self.sender, int) or self.sender == definitions.Device.NO_DEVICE: + if not self.sender in definitions.Device: + raise DataHeaderFormatError("Invalid Sender") - if not isinstance(self.data_type, int) or not 0 <= self.data_type <= 255: + if not isinstance(self.data_type, int) or not self.data_type in definitions.Type: raise DataHeaderFormatError("Invalid Type") - if not isinstance(self.priority, int) or not 0 <= self.priority <= 255: + if not isinstance(self.priority, int) or not self.priority in definitions.Priority: raise DataHeaderFormatError("Invalid Priority") - if not isinstance(self.destination, int) or not 0 <= self.destination <= 255: + if not isinstance(self.destination, int) or not self.destination in definitions.Type: raise DataHeaderFormatError("Invalid Destination") if not isinstance(self.generate_time, datetime): diff --git a/EosLib/packet/definitions.py b/EosLib/packet/definitions.py index 6d1c255..0c01488 100644 --- a/EosLib/packet/definitions.py +++ b/EosLib/packet/definitions.py @@ -1,8 +1,18 @@ -from enum import IntEnum, unique +from enum import IntEnum, unique, EnumMeta + + +class EnumMetaClass(EnumMeta): + def __contains__(cls, item): + try: + cls(item) + except ValueError: + return False + else: + return True @unique -class Type(IntEnum): +class Type(IntEnum, metaclass=EnumMetaClass): NO_TYPE = 0 TELEMETRY = 1 WARNING = 2 @@ -12,7 +22,7 @@ class Type(IntEnum): @unique -class Priority(IntEnum): +class Priority(IntEnum, metaclass=EnumMetaClass): NO_TRANSMIT = 0 URGENT = 11 TELEMETRY = 9 @@ -23,7 +33,7 @@ class Priority(IntEnum): @unique -class Device(IntEnum): +class Device(IntEnum, metaclass=EnumMetaClass): NO_DEVICE = 0 PRESSURE = 1 PARTICULATES = 2 From 5d8984647e444561947bc9ce59c55e5866b1c3bc Mon Sep 17 00:00:00 2001 From: David-Rey Date: Sat, 11 Feb 2023 18:23:06 -0500 Subject: [PATCH 3/8] fixed changes as per pull review (ignore last commit. Forgot to do git --add :/ ) --- EosLib/packet/data_header.py | 11 +++++------ EosLib/packet/definitions.py | 18 ++++++++++++++---- EosLib/packet/packet.py | 7 +++---- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/EosLib/packet/data_header.py b/EosLib/packet/data_header.py index a8b7afa..0351dc7 100644 --- a/EosLib/packet/data_header.py +++ b/EosLib/packet/data_header.py @@ -8,7 +8,6 @@ class DataHeader: - data_header_struct_format_string = "!" \ "B" \ "B" \ @@ -50,17 +49,17 @@ def validate_data_header(self): :return: True if valid """ - if not isinstance(self.sender, int) or not 0 <= self.sender <= 255 or self.sender == \ - definitions.Device.NO_DEVICE: + if not isinstance(self.sender, int) or self.sender == definitions.Device.NO_DEVICE or\ + not self.sender in definitions.Device: raise DataHeaderFormatError("Invalid Sender") - if not isinstance(self.data_type, int) or not 0 <= self.data_type <= 255: + if not isinstance(self.data_type, int) or not self.data_type in definitions.Type: raise DataHeaderFormatError("Invalid Type") - if not isinstance(self.priority, int) or not 0 <= self.priority <= 255: + if not isinstance(self.priority, int) or not self.priority in definitions.Priority: raise DataHeaderFormatError("Invalid Priority") - if not isinstance(self.destination, int) or not 0 <= self.destination <= 255: + if not isinstance(self.destination, int) or not self.destination in definitions.Device: raise DataHeaderFormatError("Invalid Destination") if not isinstance(self.generate_time, datetime): diff --git a/EosLib/packet/definitions.py b/EosLib/packet/definitions.py index 6d1c255..0c01488 100644 --- a/EosLib/packet/definitions.py +++ b/EosLib/packet/definitions.py @@ -1,8 +1,18 @@ -from enum import IntEnum, unique +from enum import IntEnum, unique, EnumMeta + + +class EnumMetaClass(EnumMeta): + def __contains__(cls, item): + try: + cls(item) + except ValueError: + return False + else: + return True @unique -class Type(IntEnum): +class Type(IntEnum, metaclass=EnumMetaClass): NO_TYPE = 0 TELEMETRY = 1 WARNING = 2 @@ -12,7 +22,7 @@ class Type(IntEnum): @unique -class Priority(IntEnum): +class Priority(IntEnum, metaclass=EnumMetaClass): NO_TRANSMIT = 0 URGENT = 11 TELEMETRY = 9 @@ -23,7 +33,7 @@ class Priority(IntEnum): @unique -class Device(IntEnum): +class Device(IntEnum, metaclass=EnumMetaClass): NO_DEVICE = 0 PRESSURE = 1 PARTICULATES = 2 diff --git a/EosLib/packet/packet.py b/EosLib/packet/packet.py index a860280..92d5d39 100644 --- a/EosLib/packet/packet.py +++ b/EosLib/packet/packet.py @@ -146,9 +146,8 @@ def set_transmit_header(self, new_transmit_header: TransmitHeader): :return: boolean True set is successful """ - if new_transmit_header is not None: - if new_transmit_header.validate_transmit_header(): - self.transmit_header = new_transmit_header + if new_transmit_header.validate_transmit_header(): + self.transmit_header = new_transmit_header return True @@ -190,7 +189,7 @@ def check_body(body: bytes): :return: boolean True if valid """ - if body is None or len(body) == 0: + if body is None or len(body) == 0 or not isinstance(body, bytes): raise PacketFormatError("All packets must have a body") return True From 73662e3fb2fdcdd2f2f97442af1900597cea3f42 Mon Sep 17 00:00:00 2001 From: David-Rey <71460537+David-Rey@users.noreply.github.com> Date: Mon, 13 Feb 2023 00:10:06 -0500 Subject: [PATCH 4/8] added a few tests. Still not done with pull request --- EosLib/packet/packet.py | 15 +++++++++------ tests/packet/test_packet.py | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/EosLib/packet/packet.py b/EosLib/packet/packet.py index 92d5d39..f2f5459 100644 --- a/EosLib/packet/packet.py +++ b/EosLib/packet/packet.py @@ -130,7 +130,7 @@ def encode_to_string(self): data_header=self.data_header.encode_to_string(), body=self.body.decode()) - def set_data_header(self, new_data_header: DataHeader): + def set_data_header(self, new_data_header: DataHeader) -> bool: """ Setter that sets new data header :return: boolean True set is successful @@ -141,7 +141,7 @@ def set_data_header(self, new_data_header: DataHeader): return True - def set_transmit_header(self, new_transmit_header: TransmitHeader): + def set_transmit_header(self, new_transmit_header: TransmitHeader) -> bool: """ Setter that sets new transmit header :return: boolean True set is successful @@ -151,16 +151,19 @@ def set_transmit_header(self, new_transmit_header: TransmitHeader): return True - def set_body(self, new_body: bytes): + def set_body(self, new_body: bytes) -> bool: """ Setter that sets new body :return: boolean True set is successful """ if new_body is not None and len(new_body) == 0: self.body = new_body + return True + + return False @staticmethod - def check_data_header(data_header: DataHeader): + def check_data_header(data_header: DataHeader) -> bool: """ Takes a packet data header and checks to see if it is valid :return: boolean True if valid @@ -173,7 +176,7 @@ def check_data_header(data_header: DataHeader): return True @staticmethod - def check_transmit_header(transmit_header: TransmitHeader): + def check_transmit_header(transmit_header: TransmitHeader) -> bool: """ Takes a packet transmit header and checks to see if it is valid :return: boolean True if valid @@ -184,7 +187,7 @@ def check_transmit_header(transmit_header: TransmitHeader): return True @staticmethod - def check_body(body: bytes): + def check_body(body: bytes) -> bool: """ Takes a packet body and checks to see if it is valid :return: boolean True if valid diff --git a/tests/packet/test_packet.py b/tests/packet/test_packet.py index ffb3cc7..46aa6e5 100644 --- a/tests/packet/test_packet.py +++ b/tests/packet/test_packet.py @@ -277,3 +277,28 @@ def test_packet_print_no_body(): "No body" assert expected_string == test_packet.__str__() + + +def test_set_data_header(): + test_packet = get_valid_packet() + data_header = DataHeader(definitions.Device.GPS, + definitions.Type.TELEMETRY, + definitions.Priority.TELEMETRY, + definitions.Device.GPS, + datetime.now()) + + test_packet.set_data_header(data_header) + + +def test_set_transmit_header(): + test_packet = get_valid_packet() + transmit_header = TransmitHeader(0, datetime.now()) + + test_packet.set_transmit_header(transmit_header) + +def test_set_body(): + test_packet = get_valid_packet() + body = bytes("temp", 'utf-8') + + test_packet.set_body(body) + From 8dd7c2d147002136d9bdc19e584c1b81c550a986 Mon Sep 17 00:00:00 2001 From: David-Rey Date: Thu, 16 Feb 2023 20:08:06 -0500 Subject: [PATCH 5/8] fixed changes from pull request --- EosLib/packet/data_header.py | 14 ++++++++++++++ EosLib/packet/packet.py | 24 ------------------------ EosLib/packet/transmit_header.py | 12 ++++++++++++ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/EosLib/packet/data_header.py b/EosLib/packet/data_header.py index 9bf0999..8d81108 100644 --- a/EosLib/packet/data_header.py +++ b/EosLib/packet/data_header.py @@ -112,3 +112,17 @@ def decode(header_bytes: bytes): decoded_header = DataHeader(unpacked[1], unpacked[2], unpacked[3], unpacked[4], datetime.fromtimestamp(unpacked[5])) return decoded_header + + +def check_data_header(data_header: DataHeader) -> bool: + """ Takes a packet data header and checks to see if it is valid + + :return: boolean True if valid + """ + if data_header is None: + raise PacketFormatError("All packets must have a data header") + else: + data_header.validate_data_header() + + return True + diff --git a/EosLib/packet/packet.py b/EosLib/packet/packet.py index f2f5459..0ff71fa 100644 --- a/EosLib/packet/packet.py +++ b/EosLib/packet/packet.py @@ -162,30 +162,6 @@ def set_body(self, new_body: bytes) -> bool: return False - @staticmethod - def check_data_header(data_header: DataHeader) -> bool: - """ Takes a packet data header and checks to see if it is valid - - :return: boolean True if valid - """ - if data_header is None: - raise PacketFormatError("All packets must have a data header") - else: - data_header.validate_data_header() - - return True - - @staticmethod - def check_transmit_header(transmit_header: TransmitHeader) -> bool: - """ Takes a packet transmit header and checks to see if it is valid - - :return: boolean True if valid - """ - if transmit_header is not None: - transmit_header.validate_transmit_header() - - return True - @staticmethod def check_body(body: bytes) -> bool: """ Takes a packet body and checks to see if it is valid diff --git a/EosLib/packet/transmit_header.py b/EosLib/packet/transmit_header.py index f875516..52b3769 100644 --- a/EosLib/packet/transmit_header.py +++ b/EosLib/packet/transmit_header.py @@ -82,3 +82,15 @@ def decode(header_bytes: bytes): unpacked = struct.unpack(TransmitHeader.transmit_header_struct_format_string, header_bytes) decoded_header = TransmitHeader(unpacked[1], datetime.fromtimestamp(unpacked[2])) return decoded_header + + +def check_transmit_header(transmit_header: TransmitHeader) -> bool: + """ Takes a packet transmit header and checks to see if it is valid + + :return: boolean True if valid + """ + if transmit_header is not None: + transmit_header.validate_transmit_header() + + return True + From 8a6c2f1fd05b637cad1d61b3bb4736199965c7a6 Mon Sep 17 00:00:00 2001 From: David-Rey Date: Tue, 21 Feb 2023 16:14:40 -0500 Subject: [PATCH 6/8] Fixed changes as per pull request --- EosLib/packet/packet.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/EosLib/packet/packet.py b/EosLib/packet/packet.py index 0ff71fa..2dba203 100644 --- a/EosLib/packet/packet.py +++ b/EosLib/packet/packet.py @@ -134,18 +134,24 @@ def set_data_header(self, new_data_header: DataHeader) -> bool: """ Setter that sets new data header :return: boolean True set is successful + :raises: PacketFormatError if data header is none """ if new_data_header is not None: if new_data_header.validate_data_header(): self.data_header = new_data_header + return True - return True + raise PacketFormatError("data header can not be none") def set_transmit_header(self, new_transmit_header: TransmitHeader) -> bool: """ Setter that sets new transmit header :return: boolean True set is successful """ + if new_transmit_header is None: + self.transmit_header = new_transmit_header + return True + if new_transmit_header.validate_transmit_header(): self.transmit_header = new_transmit_header From baec0fc3070df487e3d6cb52961c51acf4fa7c0b Mon Sep 17 00:00:00 2001 From: David-Rey Date: Sun, 26 Feb 2023 14:33:56 -0500 Subject: [PATCH 7/8] made changes as per pull request and added tests --- EosLib/packet/data_header.py | 12 +++++------- EosLib/packet/packet.py | 2 +- EosLib/packet/transmit_header.py | 1 - tests/packet/test_packet.py | 19 ++++++++++++++++++- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/EosLib/packet/data_header.py b/EosLib/packet/data_header.py index 8d81108..a0d7ab9 100644 --- a/EosLib/packet/data_header.py +++ b/EosLib/packet/data_header.py @@ -50,18 +50,17 @@ def validate_data_header(self): :return: True if valid """ - if not isinstance(self.sender, int) or self.sender == definitions.Device.NO_DEVICE or\ - not self.sender in definitions.Device: + if not isinstance(self.sender, int) or self.sender == definitions.Device.NO_DEVICE or \ + not self.sender in definitions.Device: raise DataHeaderFormatError("Invalid Sender") - if not isinstance(self.data_type, int) or not self.data_type in definitions.Type: + if not isinstance(self.data_type, int) or self.data_type not in definitions.Type: raise DataHeaderFormatError("Invalid Type") - if not isinstance(self.priority, int) or not self.priority in definitions.Priority: + if not isinstance(self.priority, int) or self.priority not in definitions.Priority: raise DataHeaderFormatError("Invalid Priority") - - if not isinstance(self.destination, int) or not self.destination in definitions.Device: + if not isinstance(self.destination, int) or self.destination not in definitions.Device: raise DataHeaderFormatError("Invalid Destination") if not isinstance(self.generate_time, datetime): @@ -125,4 +124,3 @@ def check_data_header(data_header: DataHeader) -> bool: data_header.validate_data_header() return True - diff --git a/EosLib/packet/packet.py b/EosLib/packet/packet.py index 2dba203..ae80e7e 100644 --- a/EosLib/packet/packet.py +++ b/EosLib/packet/packet.py @@ -22,7 +22,7 @@ def __init__(self, body: bytes, data_header: DataHeader, transmit_header: Transm self.body = body # type: bytes self.data_header = data_header # type: DataHeader self.transmit_header = transmit_header # type: TransmitHeader - self.validate_packet() # checks if packet is valid + self.validate_packet() # checks if packet is valid def __eq__(self, other): """ Compares two packets for value equality diff --git a/EosLib/packet/transmit_header.py b/EosLib/packet/transmit_header.py index 52b3769..321e0ca 100644 --- a/EosLib/packet/transmit_header.py +++ b/EosLib/packet/transmit_header.py @@ -93,4 +93,3 @@ def check_transmit_header(transmit_header: TransmitHeader) -> bool: transmit_header.validate_transmit_header() return True - diff --git a/tests/packet/test_packet.py b/tests/packet/test_packet.py index 46aa6e5..add7527 100644 --- a/tests/packet/test_packet.py +++ b/tests/packet/test_packet.py @@ -6,7 +6,8 @@ from datetime import datetime from EosLib.packet.packet import TransmitHeader, DataHeader, Packet, PacketFormatError from EosLib.packet.exceptions import DataHeaderFormatError, TransmitHeaderFormatError - +from EosLib.packet.data_header import check_data_header +from EosLib.packet.transmit_header import check_transmit_header def get_valid_packet(): transmit_header = TransmitHeader(0, datetime.now()) @@ -296,9 +297,25 @@ def test_set_transmit_header(): test_packet.set_transmit_header(transmit_header) + def test_set_body(): test_packet = get_valid_packet() body = bytes("temp", 'utf-8') test_packet.set_body(body) + +def test_check_data_header(): + test_packet = get_valid_packet() + check_data_header(test_packet.data_header) + + +def test_check_transmit_header(): + test_packet = get_valid_packet() + check_transmit_header(test_packet.transmit_header) + + +def test_check_body(): + test_packet = get_valid_packet() + Packet.check_body(test_packet.body) + From c5f1c327ea90da04b54b622517762b267310bba3 Mon Sep 17 00:00:00 2001 From: David-Rey Date: Sun, 2 Apr 2023 14:55:31 -0400 Subject: [PATCH 8/8] fixed white space --- tests/packet/test_packet.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/packet/test_packet.py b/tests/packet/test_packet.py index fec2dc9..b6f2dae 100644 --- a/tests/packet/test_packet.py +++ b/tests/packet/test_packet.py @@ -335,5 +335,3 @@ def test_check_transmit_header(): def test_check_body(): test_packet = get_valid_packet() Packet.check_body(test_packet.body) - -