Coverage for src/somesy/commands/set_value.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-09-04 11:35 +0000

1"""Update scalar project metadata in a somesy.toml file.""" 

2 

3from pathlib import Path 

4 

5import tomlkit 

6 

7from somesy.core.core import get_input_content 

8from somesy.core.models import ProjectMetadata, SomesyInput 

9 

10SETTABLE_FIELDS = { 

11 "name", 

12 "description", 

13 "version", 

14 "license", 

15 "homepage", 

16 "repository", 

17 "documentation", 

18} 

19 

20 

21def set_project_value(input_file: Path, field: str, value: str) -> None: 

22 """Set one scalar project-metadata value in a somesy.toml file.""" 

23 if field not in SETTABLE_FIELDS: 

24 allowed = ", ".join(sorted(SETTABLE_FIELDS)) 

25 raise ValueError(f"Unknown project field '{field}'. Choose one of: {allowed}.") 

26 if not SomesyInput.is_somesy_file_path(input_file): 

27 raise ValueError("The input file must be a somesy.toml file.") 

28 

29 content = get_input_content(input_file, no_unwrap=True) 

30 ProjectMetadata.model_validate({**content["project"], field: value}) 

31 content["project"][field] = value 

32 with open(input_file, "w") as file: 

33 tomlkit.dump(content, file)