-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87055e5
commit 16fc017
Showing
18 changed files
with
254 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from EosLib.format.formats import telemetry_data, position | ||
from EosLib.format.formats import telemetry_data, position, empty_format | ||
|
||
|
||
from EosLib.format.definitions import Type as Type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,27 @@ | ||
from EosLib.format import csv_format | ||
|
||
|
||
class DecodeFactory: | ||
def __init__(self): | ||
self._decoders = {} | ||
self._csv_decoders = {} | ||
|
||
def register_decoder(self, format_class): | ||
self._decoders[format_class.get_format_type()] = format_class.decode | ||
if issubclass(format_class, csv_format.CsvFormat): | ||
self._csv_decoders[format_class.get_format_type()] = format_class.decode_from_csv | ||
|
||
def decode(self, data_format, data: bytes): | ||
return self._decoders[data_format](data) | ||
decoder = self._decoders.get(data_format) | ||
if decoder is None: | ||
raise TypeError("No decoder found for this type") | ||
return decoder(data) | ||
|
||
def decode_from_csv(self, data_format, data: str): | ||
decoder = self._csv_decoders.get(data_format) | ||
if decoder is None: | ||
raise TypeError("No decoder found for this type") | ||
return decoder(data) | ||
|
||
|
||
decode_factory = DecodeFactory() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ class Type(IntEnum): | |
COMMAND = 5 | ||
RESPONSE = 6 | ||
TELEMETRY_DATA = 7 | ||
EMPTY = 8 | ||
ERROR = 255 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import struct | ||
|
||
from typing_extensions import Self | ||
|
||
from EosLib.format.base_format import BaseFormat | ||
|
||
from EosLib.format.definitions import Type | ||
|
||
|
||
class EmptyFormat(BaseFormat): | ||
|
||
def __init__(self, num_bytes=0): | ||
self.num_bytes = num_bytes | ||
|
||
def __eq__(self, other): | ||
return self.num_bytes == other.num_bytes | ||
|
||
@staticmethod | ||
def get_format_type() -> Type: | ||
return Type.EMPTY | ||
|
||
def encode(self) -> bytes: | ||
return struct.pack(self.get_format_string()) | ||
|
||
@classmethod | ||
def decode(cls, data: bytes) -> Self: | ||
return EmptyFormat(len(data)) | ||
|
||
def get_format_string(self) -> str: | ||
return self.num_bytes * "x" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from abc import ABC | ||
|
||
from format.formats.format_test import CheckFormat | ||
|
||
from EosLib.format.decode_factory import decode_factory | ||
|
||
|
||
class CheckCsvFormat(CheckFormat, ABC): | ||
def test_encode_decode_csv(self): | ||
base_format = self.get_good_format() | ||
base_format_csv = base_format.encode_to_csv() | ||
new_position = decode_factory.decode_from_csv(self.get_good_format().get_format_type(), base_format_csv) | ||
|
||
assert base_format == new_position |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import abc | ||
import copy | ||
import datetime | ||
|
||
import EosLib.format.csv_format | ||
from EosLib.format.decode_factory import decode_factory | ||
|
||
|
||
# Name can't contain the word Test, hence Check | ||
class CheckFormat(abc.ABC): | ||
@abc.abstractmethod | ||
def get_format_from_list(self, format_list: []): | ||
raise NotImplementedError | ||
|
||
@abc.abstractmethod | ||
def get_good_format_list(self): | ||
raise NotImplementedError | ||
|
||
def get_good_format(self): | ||
return self.get_format_from_list(self.get_good_format_list()) | ||
|
||
def test_is_eq(self): | ||
data_1 = self.get_good_format() | ||
data_2 = self.get_good_format() | ||
|
||
assert data_1 == data_2 | ||
|
||
def test_not_eq(self): | ||
test_passed = True | ||
|
||
data_1 = self.get_good_format() | ||
|
||
for i in range(len(self.get_good_format_list())): | ||
new_data_list = copy.deepcopy(self.get_good_format_list()) | ||
if isinstance(new_data_list[i], (int, float)): | ||
new_data_list[i] += 1 | ||
elif isinstance(new_data_list[i], datetime.datetime): | ||
new_data_list[i] += datetime.timedelta(1) | ||
data_2 = self.get_format_from_list(new_data_list) | ||
|
||
if data_1 == data_2: | ||
test_passed = False | ||
|
||
assert test_passed | ||
|
||
def test_encode_decode_bytes(self): | ||
base_format = self.get_good_format() | ||
base_position_bytes = base_format.encode() | ||
new_format = decode_factory.decode(self.get_good_format().get_format_type(), base_position_bytes) | ||
assert base_format == new_format | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from EosLib.format.formats.empty_format import EmptyFormat | ||
|
||
from tests.format.formats.format_test import CheckFormat | ||
|
||
|
||
class TestEmptyFormat(CheckFormat): | ||
def get_format_from_list(self, format_list: []): | ||
return EmptyFormat() | ||
|
||
def get_good_format_list(self): | ||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import datetime | ||
|
||
from EosLib.format.formats.position import Position | ||
from EosLib.format.formats.position import FlightState | ||
|
||
from format.formats.csv_format_test import CheckCsvFormat | ||
|
||
good_data_list = [datetime.datetime.now(), | ||
33.7756, | ||
84.3963, | ||
974.5, | ||
70.2, | ||
5, | ||
FlightState.DESCENT] | ||
|
||
|
||
def get_position_from_list(position_list: [float]): | ||
return Position(position_list[0], | ||
position_list[1], | ||
position_list[2], | ||
position_list[3], | ||
position_list[4], | ||
position_list[5], | ||
position_list[6]) | ||
|
||
|
||
def get_good_position(): | ||
return get_position_from_list(good_data_list) | ||
|
||
|
||
def test_get_validity_valid(): | ||
good_position = get_good_position() | ||
assert good_position.get_validity() | ||
|
||
|
||
def test_get_validity_bad_satellites(): | ||
bad_satellites_position = get_good_position() | ||
bad_satellites_position.number_of_satellites = 3 | ||
assert not bad_satellites_position.get_validity() | ||
|
||
|
||
class TestPosition(CheckCsvFormat): | ||
def get_format_from_list(self, format_list: []): | ||
return get_position_from_list(format_list) | ||
|
||
def get_good_format_list(self): | ||
return good_data_list |
Oops, something went wrong.