-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild.py
More file actions
174 lines (125 loc) · 4.37 KB
/
build.py
File metadata and controls
174 lines (125 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
Build matrix processing.
"""
from collections.abc import Iterable, Mapping, Sequence
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, TypeVar, cast, overload
import dacite
from .hardware import BoardTarget, BuildItem
from .repo import Repo
from .yaml import YAML
T = TypeVar("T")
@dataclass
class _BuildMatrixWrapper:
include: list[BuildItem] = field(default_factory=list)
class BuildMatrix:
"""Interface to read and edit ZMK's build.yaml file"""
_path: Path
_yaml: YAML
_data: dict[str, Any] | None
@classmethod
def from_repo(cls, repo: Repo) -> "BuildMatrix":
"""Get the build matrix for a repo"""
return cls(repo.build_matrix_path)
def __init__(self, path: Path):
self._path = path
self._yaml = YAML(typ="rt")
self._yaml.indent(mapping=2, sequence=4, offset=2)
try:
self._data = cast("dict[str, Any]", self._yaml.load(self._path))
except FileNotFoundError:
self._data = None
def write(self) -> None:
"""Update the YAML file, creating it if necessary"""
self._yaml.dump(self._data, self._path)
@property
def path(self) -> Path:
"""Path to the matrix's YAML file"""
return self._path
@property
def include(self) -> list[BuildItem]:
"""List of build items in the matrix"""
normalized = _keys_to_python(self._data)
if not normalized:
return []
config = dacite.Config(type_hooks={BoardTarget: BoardTarget.parse})
wrapper = dacite.from_dict(_BuildMatrixWrapper, normalized, config)
return wrapper.include
def has_item(self, item: BuildItem) -> bool:
"""Get whether the matrix has a build item"""
return item in self.include
def append(self, items: BuildItem | Iterable[BuildItem]) -> list[BuildItem]:
"""
Add build items to the matrix.
:return: the items that were added.
"""
items = [items] if isinstance(items, BuildItem) else items
include = self.include
items = [i for i in items if i not in include]
if not items:
return []
if not self._data:
self._data = cast("dict[str, Any]", self._yaml.map())
if "include" not in self._data:
self._data["include"] = self._yaml.seq()
self._data["include"].extend([_to_yaml(i) for i in items])
return items
def remove(self, items: BuildItem | Iterable[BuildItem]) -> list[BuildItem]:
"""
Remove build items from the matrix.
:return: the items that were removed.
"""
if not self._data or "include" not in self._data:
return []
removed = []
items = [items] if isinstance(items, BuildItem) else items
# TODO: there's probably a more efficient way to do this, but this is easy
for item in items:
try:
index = self.include.index(item)
del self._data["include"][index]
removed.append(item)
except ValueError:
pass
return removed
@overload
def _keys_to_python(data: str) -> str: ...
@overload
def _keys_to_python(
data: Sequence[T],
) -> Sequence[T]: ...
@overload
def _keys_to_python(data: Mapping[str, T]) -> Mapping[str, T]: ...
@overload
def _keys_to_python(data: T) -> T: ...
def _keys_to_python(data: Any) -> Any:
"""
Fix any keys with hyphens to underscores so that dacite.from_dict() will
work correctly.
"""
def fix_key(key: str):
return key.replace("-", "_")
match data:
case str():
return data
case Sequence():
return [_keys_to_python(i) for i in data]
case Mapping():
return {fix_key(k): _keys_to_python(v) for k, v in data.items()}
case _:
return data
def _to_yaml(item: BuildItem):
"""
Convert a BuildItem to a dict with keys changed back from underscores to hyphens
and values changed to YAML-compatible types.
"""
def fix_key(key: str):
return key.replace("_", "-")
def fix_value(value: Any):
match value:
case BoardTarget():
return str(value)
case _:
return value
return {fix_key(k): fix_value(v) for k, v in item.__dict__.items() if v is not None}