62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
import re
|
|
from .type_lengths import TYPE_LENGTHS
|
|
|
|
class HydroField:
|
|
def __init__(
|
|
self,
|
|
name,
|
|
type,
|
|
print_format,
|
|
xml,
|
|
description="",
|
|
enum="",
|
|
display="",
|
|
units="",
|
|
):
|
|
self.name = name
|
|
self.name_upper = name.upper()
|
|
|
|
self.description = description
|
|
self.array_length = 0
|
|
self.enum = enum
|
|
self.display = display
|
|
self.units = units
|
|
self.print_format = print_format
|
|
self.wire_offset = 0
|
|
self._parse_type(type)
|
|
|
|
def _parse_type(self, type):
|
|
if self._type_is_array(type):
|
|
self._parse_array(type)
|
|
else:
|
|
self._parse_non_array(type)
|
|
|
|
def _type_is_array(self, field_type):
|
|
return bool("[" in field_type or "]" in field_type)
|
|
|
|
def _parse_array(self, field_type):
|
|
m = re.search(r"([a-zA-Z0-9]+?)\[(0-9+)\]", field_type)
|
|
if not m:
|
|
raise Exception(f"Could not parse type: '{field_type}'")
|
|
field_type = m.group(1)
|
|
length = m.group(2)
|
|
if field_type in TYPE_LENGTHS:
|
|
self.type = field_type
|
|
elif f"{field_type}_t" in TYPE_LENGTHS:
|
|
self.type = field_type + "_t"
|
|
else:
|
|
raise Exception(f"Unknown type: '{field_type}'")
|
|
self.type_length = TYPE_LENGTHS[self.type]
|
|
self.array_length = length
|
|
self.wire_length = self.array_length * self.type_length
|
|
|
|
def _parse_non_array(self, field_type):
|
|
if field_type in TYPE_LENGTHS:
|
|
self.type = field_type
|
|
elif f"{field_type}_t" in TYPE_LENGTHS:
|
|
self.type = field_type + "_t"
|
|
else:
|
|
raise Exception(f"Unknown type: '{field_type}'")
|
|
self.type_length = TYPE_LENGTHS[self.type]
|
|
self.array_length = 0
|
|
self.wire_length = self.type_length
|