Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions harp/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@
from os import PathLike
from typing import TextIO, Union

from pydantic_yaml import parse_yaml_raw_as
import yaml

from harp.model import Model, Registers


def _convert_keys_to_strings(obj):
"""Recursively converts all dictionary keys to strings.
This is necessary since pydantic deserialization from python objects
seems to expect keys to always be strings."""
if isinstance(obj, dict):
return {str(k): _convert_keys_to_strings(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [_convert_keys_to_strings(i) for i in obj]
return obj


def _read_common_registers() -> Registers:
if __package__ is None:
raise ValueError("__package__ is None: unable to read common registers")

file = resources.files(__package__) / "common.yml"
with file.open("r") as fileIO:
return parse_yaml_raw_as(Registers, fileIO.read())
regs_raw = _convert_keys_to_strings(yaml.safe_load(fileIO.read()))
return Registers.model_validate(regs_raw)


def read_schema(file: Union[str, PathLike, TextIO], include_common_registers: bool = True) -> Model:
Expand All @@ -37,7 +49,8 @@ def read_schema(file: Union[str, PathLike, TextIO], include_common_registers: bo
with open(file) as fileIO:
return read_schema(fileIO)
else:
schema = parse_yaml_raw_as(Model, file.read())
schema_raw = _convert_keys_to_strings(yaml.safe_load(file.read()))
schema = Model.model_validate(schema_raw)
if "WhoAmI" not in schema.registers and include_common_registers:
common = _read_common_registers()
schema.registers = dict(common.registers, **schema.registers)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dynamic = ["version"]
license = "MIT"

dependencies = [
"pydantic-yaml",
"pyyaml",
"pandas"
]

Expand Down
14 changes: 14 additions & 0 deletions tests/data/device.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ bitMasks:
DI1: 0x2
DI2: 0x4
DI3: 0x8
State:
description: Specifies the state of the digital input lines.
bits:
False:
value: 0x0
description: The state is False
True:
value: 0x1
description: The state is True
StateFlowStyle:
description: Specifies the state of the digital input lines.
bits:
False: {value: 0x0, description: The state is False}
True: {value: 0x1, description: The state is True}
groupMasks:
InputMode:
description: Specifies when the device reports the state of digital input lines.
Expand Down