-
Notifications
You must be signed in to change notification settings - Fork 0
34 add setters validators #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e01f4d5
71fe0e9
5d89846
8d662dd
73662e3
8dd7c2d
8a6c2f1
baec0fc
70f1d30
c5f1c32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,55 @@ 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) -> 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 | ||
|
|
||
| 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 | ||
|
|
||
| return True | ||
|
|
||
| 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: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use the body validator
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want me to make a function or is there already a function for this? If so I can't find it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth making one, there's good logic in validate_packet. Also this function's logic is wrong (only sets if length 0) and it doesn't return something even though the docstring specifies that it should |
||
| self.body = new_body | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| @staticmethod | ||
| def check_body(body: bytes) -> bool: | ||
| """ 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 or not isinstance(body, bytes): | ||
| 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. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is very incorrect