Skip to content
Open
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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fail_fast: false
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.15.2
rev: v0.15.5
hooks:
- id: ruff-check
- id: ruff-format
Expand All @@ -18,7 +18,7 @@ repos:
- id: check-added-large-files
- id: check-shebang-scripts-are-executable
- repo: https://github.com/tombi-toml/tombi-pre-commit
rev: v0.7.32
rev: v0.9.4
hooks:
- id: tombi-format
- id: tombi-lint
5 changes: 2 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
"source.organizeImports.ruff": "explicit"
}
},
"[markdown][yaml]": {
Expand All @@ -21,8 +21,7 @@
"**/templates/common/**": "mako"
},
"pylint.enabled": false,
"ruff.enabled": true,
"ruff.enable": true,
"python.analysis.importFormat": "relative",
"python.analysis.typeCheckingMode": "standard",
"python-envs.pythonProjects": []
}
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ build-backend = "setuptools.build_meta"
[tool.pyright]
venvPath = "."
venv = ".venv"
exclude = [
".venv",
"build",
"**/.*",
"**/node_modules",
"**/__pycache__",
]

[tool.ruff.lint]
select = [
Expand Down Expand Up @@ -107,3 +114,6 @@ packages = ["zmk"]

[tool.setuptools_scm]
version_file = "zmk/_version.py"

[tool.tombi.files]
exclude = ["uv.lock"]
50 changes: 15 additions & 35 deletions zmk/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,19 @@
"""

from collections.abc import Iterable, Mapping, Sequence
from dataclasses import asdict, dataclass, field
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 BuildItem:
"""An item in the build matrix"""

board: str
shield: str | None = None
snippet: str | None = None
cmake_args: str | None = None
artifact_name: str | None = None

def __rich__(self) -> str:
parts = []
parts.append(self.board)

if self.shield:
parts.append(self.shield)

if self.snippet:
parts.append(f"[dim]snippet: {self.snippet}[/dim]")

if self.artifact_name:
parts.append(f"[dim]artifact-name: {self.artifact_name}[/dim]")

if self.cmake_args:
parts.append(f"[dim]cmake-args: {self.cmake_args}[/dim]")

return "[dim], [/dim]".join(parts)


@dataclass
class _BuildMatrixWrapper:
include: list[BuildItem] = field(default_factory=list)
Expand Down Expand Up @@ -71,7 +43,7 @@ def __init__(self, path: Path):
self._data = None

def write(self) -> None:
"""Updated the YAML file, creating it if necessary"""
"""Update the YAML file, creating it if necessary"""
self._yaml.dump(self._data, self._path)

@property
Expand All @@ -86,7 +58,8 @@ def include(self) -> list[BuildItem]:
if not normalized:
return []

wrapper = dacite.from_dict(_BuildMatrixWrapper, normalized)
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:
Expand Down Expand Up @@ -183,12 +156,19 @@ def fix_key(key: str):

def _to_yaml(item: BuildItem):
"""
Convert a BuildItem to a dict with keys changed back from underscores to hyphens.
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("_", "-")

data = asdict(item)
def fix_value(value: Any):
match value:
case BoardTarget():
return str(value)

case _:
return value

return {fix_key(k): v for k, v in data.items() if v is not None}
return {fix_key(k): fix_value(v) for k, v in item.__dict__.items() if v is not None}
Loading