general.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # general.py/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro).
  2. # This copyright was auto-generated on Mon Apr 21 22:24:00 UTC 2025
  3. """Monolithic Parser implementations"""
  4. from typing import Any
  5. from construct import BitsInteger, BitStruct, Flag, Int8ub, Int32ub, Padding
  6. from open_gopro.domain.parser_interface import BytesParserBuilder
  7. from open_gopro.models import ScheduledCapture
  8. from open_gopro.parsers.bytes import ConstructDataclassByteParserBuilder
  9. ScheduledCaptureParser = ConstructDataclassByteParserBuilder(
  10. construct=BitStruct(
  11. Padding(19),
  12. "hour" / BitsInteger(5),
  13. "minute" / BitsInteger(6),
  14. "is_24_hour" / Flag,
  15. "is_enabled" / Flag,
  16. ),
  17. data_class=ScheduledCapture,
  18. int_builder=Int32ub,
  19. )
  20. class IntByteParserBuilder(BytesParserBuilder[int]):
  21. """Built / parse integers to / from bytes
  22. Args:
  23. length (int): length of byte array to store integer
  24. Raises:
  25. ValueError: _description_
  26. """
  27. def __init__(self, length: int) -> None:
  28. match length:
  29. case 1:
  30. self._container = Int8ub
  31. case 4:
  32. self._container = Int32ub
  33. case _:
  34. raise ValueError(f"Length {length} is not handled")
  35. def parse(self, data: bytes) -> int: # noqa: D102
  36. return self._container.parse(data)
  37. def build(self, obj: Any) -> bytes: # noqa: D102
  38. match obj:
  39. case int():
  40. return self._container.build(obj)
  41. case str():
  42. return self._container.build(int(obj))
  43. case _:
  44. raise TypeError(f"Can not build bytes from object of type {type(obj)}")