|
1 | 1 | from os import PathLike |
2 | | -from pathlib import Path |
3 | 2 | from typing import TextIO, Union |
4 | 3 | from harp.model import Model, Registers |
5 | 4 | from pydantic_yaml import parse_yaml_raw_as |
| 5 | +from importlib import resources |
6 | 6 |
|
7 | | -_common_yaml_path = Path(__file__).absolute().parent.joinpath("common.yml") |
8 | 7 |
|
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()) |
16 | 12 |
|
17 | 13 |
|
18 | 14 | def read_schema( |
19 | 15 | file: Union[str, PathLike, TextIO], include_common_registers: bool = True |
20 | 16 | ) -> Model: |
21 | | - if isinstance(file, TextIO): |
| 17 | + if isinstance(file, (str, PathLike)): |
| 18 | + with open(file) as fileIO: |
| 19 | + return read_schema(fileIO) |
| 20 | + else: |
22 | 21 | schema = parse_yaml_raw_as(Model, file.read()) |
23 | 22 | if not "WhoAmI" in schema.registers and include_common_registers: |
24 | | - common = _read_common_registers(_common_yaml_path) |
| 23 | + common = _read_common_registers() |
25 | 24 | schema.registers = dict(common.registers, **schema.registers) |
26 | 25 | if schema.bitMasks is not None and common.bitMasks is not None: |
27 | 26 | schema.bitMasks = dict(common.bitMasks, **schema.bitMasks) |
28 | 27 | if schema.groupMasks is not None and common.groupMasks is not None: |
29 | 28 | schema.groupMasks = dict(common.groupMasks, **schema.groupMasks) |
30 | 29 | return schema |
31 | | - else: |
32 | | - with open(file) as fileIO: |
33 | | - return read_schema(fileIO, include_common_registers) |
0 commit comments