Skip to content

Commit

Permalink
Merge pull request #20 from quentinlampin/packet_descriptor_hashable
Browse files Browse the repository at this point in the history
Packet descriptor hashable
  • Loading branch information
quentinlampin authored Oct 13, 2023
2 parents 122fe50 + 667d58a commit 0175171
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion microschc/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.16.0'
__version__ = '0.16.1'
4 changes: 2 additions & 2 deletions microschc/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def __init__(self, name: str, parsers: List[HeaderParser]) -> None:
self.parsers = parsers

def parse(self, buffer: Buffer) -> PacketDescriptor:
raw: Buffer = buffer.copy()
header_descriptors: List[HeaderDescriptor] = []

packet_length: int = buffer.length
for parser in self.parsers:
header_descriptor = parser.parse(buffer=buffer)
header_descriptors.append(header_descriptor)
Expand All @@ -50,7 +50,7 @@ def parse(self, buffer: Buffer) -> PacketDescriptor:
direction=DirectionIndicator.DOWN, # default value
fields=packet_fields,
payload=buffer,
length=packet_length
raw=raw,
)

return packet_descriptor
Expand Down
42 changes: 41 additions & 1 deletion microschc/rfc8724.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ def __json__(self) -> dict:

def __repr__(self) -> str:
return f"[{self.id}|{self.value}]"

def __eq__(self, other: object) -> bool:
if not isinstance(other, FieldDescriptor):
return False

if self.id != other.id:
return False

if self.value != other.value:
return False

if self.position != other.position:
return False

return True

def json(self, indent=None, separators=None):
return json.dumps(self.__json__(), indent=indent, separators=separators)
Expand Down Expand Up @@ -141,18 +156,43 @@ class PacketDescriptor:
direction: DirectionIndicator
fields: List[FieldDescriptor]
payload: Buffer
raw: Buffer
length: int

def __init__(self, direction: DirectionIndicator, fields: List[FieldDescriptor], payload: Buffer, raw: Buffer = None):
self.direction = direction
self.fields = fields
self.payload = payload
if raw is None:
self.raw = Buffer(content=b'', length=0)
for field in fields:
self.raw += field.value
self.raw += payload
else:
self.raw = raw
self.length = self.raw.length

def __repr__(self):
fields_str: str = ','.join([str(field) for field in self.fields])
repr: str = f"[{self.direction}|{fields_str}]"
return repr

def __eq__(self, other: object) -> bool:
if not isinstance(other, PacketDescriptor):
return False
if len(self.fields) != len(other.fields):
return False
return self.raw == other.raw

def __hash__(self) -> int:
return self.raw.__hash__()

def __json__(self) -> dict:
jsonisable: dict = {
'direction': self.direction,
'fields': [ field.__json__() for field in self.fields],
'payload': self.payload.__json__(),
'raw': self.raw.__json__(),
'length': self.length
}
return jsonisable
Expand All @@ -165,7 +205,7 @@ def __from_json_object__(json_object):
direction=json_object['direction'],
fields=[FieldDescriptor.__from_json_object__(fd_json) for fd_json in json_object['fields']],
payload=Buffer.__from_json_object__(json_object['payload']),
length=json_object['length']
raw=Buffer.__from_json_object__(json_object['raw']),
)

def from_json(json_str: str):
Expand Down
7 changes: 3 additions & 4 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,23 @@ def test_packet_descriptor_to_json():
FieldDescriptor(id='fd2', value=Buffer(content=b'\x00\x02', length=16), position=0)
],
payload=Buffer(content=b'\x00\x01\x02', length=24),
length=100
)
json_str = packet_descriptor.json()
assert json_str == '{"direction": "Up", "fields": [{"id": "fd1", "value": {"content": "0001", "length": 16, "padding": "left"}, "position": 0}, {"id": "fd2", "value": {"content": "0002", "length": 16, "padding": "left"}, "position": 0}], "payload": {"content": "000102", "length": 24, "padding": "left"}, "length": 100}'
assert json_str == '{"direction": "Up", "fields": [{"id": "fd1", "value": {"content": "0001", "length": 16, "padding": "left"}, "position": 0}, {"id": "fd2", "value": {"content": "0002", "length": 16, "padding": "left"}, "position": 0}], "payload": {"content": "000102", "length": 24, "padding": "left"}, "raw": {"content": "00010002000102", "length": 56, "padding": "right"}, "length": 56}'

def test_packet_descriptor_from_json():
"""
test JSON deserialization of PacketDescriptor objects
"""
json_str = '{"direction": "Up", "fields": [{"id": "fd1", "value": {"content": "0001", "length": 16, "padding": "left"}, "position": 0}, {"id": "fd2", "value": {"content": "0002", "length": 16, "padding": "left"}, "position": 0}], "payload": {"content": "000102", "length": 24, "padding": "left"}, "length": 100}'
json_str = '{"direction": "Up", "fields": [{"id": "fd1", "value": {"content": "0001", "length": 16, "padding": "left"}, "position": 0}, {"id": "fd2", "value": {"content": "0002", "length": 16, "padding": "left"}, "position": 0}], "payload": {"content": "000102", "length": 24, "padding": "left"}, "raw": {"content": "00010002000102", "length": 56, "padding": "right"}, "length": 56}'
packet_descriptor: PacketDescriptor = PacketDescriptor.from_json(json_str=json_str)
assert packet_descriptor.direction == DirectionIndicator.UP
assert packet_descriptor.fields == [
FieldDescriptor(id='fd1', value=Buffer(content=b'\x00\x01', length=16), position=0),
FieldDescriptor(id='fd2', value=Buffer(content=b'\x00\x02', length=16), position=0)
]
assert packet_descriptor.payload == Buffer(content=b'\x00\x01\x02', length=24)
assert packet_descriptor.length == 100
assert packet_descriptor.length == 56

def test_rule_field_descriptor_to_json():
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_microschc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert __version__ == '0.16.0'
assert __version__ == '0.16.1'

0 comments on commit 0175171

Please sign in to comment.