Skip to content

Commit 6ea1653

Browse files
committed
Read common register schema using importlib
1 parent 6d54f45 commit 6ea1653

2 files changed

Lines changed: 6 additions & 10 deletions

File tree

harp/__init__.py

Whitespace-only changes.

harp/schema.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
from os import PathLike
2-
from pathlib import Path
32
from typing import TextIO, Union
43
from harp.model import Model, Registers
54
from pydantic_yaml import parse_yaml_raw_as
5+
from importlib import resources
66

7-
_common_yaml_path = Path(__file__).absolute().parent.joinpath("common.yml")
87

9-
10-
def _read_common_registers(file: Union[str, PathLike, TextIO]) -> Registers:
11-
if isinstance(file, TextIO):
12-
return parse_yaml_raw_as(Registers, file.read())
13-
else:
14-
with open(file) as fileIO:
15-
return _read_common_registers(fileIO)
8+
def _read_common_registers() -> Registers:
9+
file = resources.files(__package__) / "common.yml"
10+
with file.open("rt") as fileIO:
11+
return parse_yaml_raw_as(Registers, fileIO.read())
1612

1713

1814
def read_schema(
@@ -21,7 +17,7 @@ def read_schema(
2117
if isinstance(file, TextIO):
2218
schema = parse_yaml_raw_as(Model, file.read())
2319
if not "WhoAmI" in schema.registers and include_common_registers:
24-
common = _read_common_registers(_common_yaml_path)
20+
common = _read_common_registers()
2521
schema.registers = dict(common.registers, **schema.registers)
2622
if schema.bitMasks is not None and common.bitMasks is not None:
2723
schema.bitMasks = dict(common.bitMasks, **schema.bitMasks)

0 commit comments

Comments
 (0)