Coverage for src/somesy/commands/set_value.py: 89%
28 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-11-03 13:13 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-11-03 13:13 +0000
1"""Update scalar project metadata in a somesy.toml file."""
3from pathlib import Path
5import tomlkit
6from pydantic import ValidationError
8from somesy.core.core import get_input_content
9from somesy.core.models import ProjectMetadata, SomesyInput
11SETTABLE_FIELDS = {
12 "name",
13 "description",
14 "abstract",
15 "version",
16 "license",
17 "homepage",
18 "repository",
19 "documentation",
20}
23def set_project_value(input_file: Path, field: str, value: str) -> None:
24 """Set one scalar project-metadata value in a somesy.toml file."""
25 if field not in SETTABLE_FIELDS:
26 allowed = ", ".join(sorted(SETTABLE_FIELDS))
27 raise ValueError(f"Unknown project field '{field}'. Choose one of: {allowed}.")
28 if not SomesyInput.is_somesy_file_path(input_file):
29 raise ValueError("The input file must be a somesy.toml file.")
31 content = get_input_content(input_file, no_unwrap=True)
32 try:
33 ProjectMetadata.model_validate({**content["project"], field: value})
34 except ValidationError as error:
35 first = error.errors()[0]
36 if first["type"] in ("enum", "literal_error"):
37 reason = f"{first['input']!r} is not an allowed value"
38 if field == "license":
39 reason = f"{first['input']!r} is not a known SPDX license identifier"
40 else:
41 reason = first["msg"]
42 if len(reason) > 200:
43 reason = reason[:197] + "..."
44 raise ValueError(f"Invalid value for '{field}': {reason}") from None
45 content["project"][field] = value
46 with open(input_file, "w", encoding="utf-8") as file:
47 tomlkit.dump(content, file)