Skip to content

Commit

Permalink
feat: fix bugs with parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
plun1331 committed Nov 16, 2024
1 parent c1e9406 commit af2b598
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
8 changes: 6 additions & 2 deletions hytek/hy3/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
__all__ = ("date", "time")


def date(data: str) -> datetime.date:
def date(data: str) -> datetime.date | None:
if not data:
return None
month = int(data[:2])
day = int(data[2:4])
year = int(data[-4:])
return datetime.date(year, month, day)


def time(data: str) -> datetime.time:
def time(data: str) -> datetime.time | None:
if not data:
return None
_time, ampm = data.split(" ")
hours, minutes = _time.split(":")
hours = int(hours)
Expand Down
15 changes: 9 additions & 6 deletions hytek/hy3/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ def to_line(self) -> bytes:


class Hy3Record:
record_type: str
fields: list[Field]
data: dict[str, Any]
raw: bytes
record_type: str = ""
fields: list[Field] = []
data: dict[str, Any] = {}
raw: bytes = b""
children: list[Type[Hy3Record | Hy3RecordGroup]] = []

def __init__(self, raw: bytes = b"", /, **kwargs):
Expand Down Expand Up @@ -204,14 +204,16 @@ def parse_record(cls, line: bytes):
return cls(line)
given_checksum = line[-2:]
if cls.calculate_checksum(line[:-2]) != given_checksum:
print(line, type(line))
raise Exception("Bad checksum")
params = {}
index = 2
for field in cls.fields:
data = line[index:index + field.length]
index += field.length
params[field.name] = field.type(data.decode("utf-8", errors="replace").strip())
try:
params[field.name] = field.type(data.decode("utf-8", errors="replace").strip())
except Exception as e:
raise Exception(f"Failed to parse field {field.name} as {field.type.__name__}: {e}") from e
return cls(line, **params)

@staticmethod
Expand Down Expand Up @@ -297,6 +299,7 @@ class Hy3C3Record(Hy3Record):
fields = []
# here for completeness, contains contact info

# one of the files i have has a C8 record

# Swimmer data
class Hy3D1Record(Hy3Record):
Expand Down

0 comments on commit af2b598

Please sign in to comment.