Skip to content

Commit

Permalink
Check calculated value from binary.
Browse files Browse the repository at this point in the history
  • Loading branch information
HeMan committed Nov 8, 2020
1 parent f08649f commit cf6dc8b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions binmap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ def enumfield(
return dataclasses.field(default=default, metadata={"enum": enumclass})


"""
Field generator function for calculated fields
:param Callable function: function that calculates the field.
:return: dataclass field
"""
def calculatedfield(function: Callable) -> dataclasses.Field:
return dataclasses.field(default=0, metadata={"function": function})

Expand Down Expand Up @@ -285,5 +291,8 @@ def frombytes(self, value: bytes):
elif "autolength" in self.__datafieldsmap[name].metadata:
if arg != getattr(self, name):
raise ValueError("Length doesn't match")
elif "function" in self.__datafieldsmap[name].metadata:
if arg != self.__datafieldsmap[name].metadata["function"](self):
raise ValueError("Wrong calculated value")
else:
setattr(self, name, arg)
12 changes: 11 additions & 1 deletion tests/test_binmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ class CalculatedField(binmap.BinmapDataclass):
temp: types.signedchar = 0
hum: types.unsignedchar = 0

def chk(self):
def chk(self) -> types.unsignedchar:
return (self.temp + self.hum) & 0xFF

checksum: types.unsignedchar = binmap.calculatedfield(chk)
Expand All @@ -610,3 +610,13 @@ def test_calculated_field(self):

assert cf.checksum == 239
assert bytes(cf) == b"\xe5\x0a\xef"

def test_calculated_field_binary(self):
cf = CalculatedField(b"\xe2\x12\xf4")
assert cf.temp == -30
assert cf.hum == 18
assert cf.checksum == 244

with pytest.raises(ValueError) as excinfo:
CalculatedField(b"\xe4\x18\x00")
assert "Wrong calculated value" in str(excinfo)

0 comments on commit cf6dc8b

Please sign in to comment.