Skip to content

commands

Commands for somesy.

write_somesy_file

write_somesy_file(
    metadata: ProjectMetadata,
    path: Path = Path("somesy.toml"),
    *,
    config: SomesyConfig | None = None,
    overwrite: bool = False
) -> None

Write project metadata to a standalone somesy.toml file.

Source code in src/somesy/commands/init_config.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def write_somesy_file(
    metadata: ProjectMetadata,
    path: Path = Path("somesy.toml"),
    *,
    config: SomesyConfig | None = None,
    overwrite: bool = False,
) -> None:
    """Write project metadata to a standalone ``somesy.toml`` file."""
    if path.exists() and not overwrite:
        raise FileExistsError(f"Output file already exists: {path}")

    content = {
        "project": BaseModel.model_dump(
            metadata, mode="json", by_alias=True, exclude_none=True
        )
    }
    if config is not None:
        content["config"] = config.model_dump(mode="json", by_alias=True)
    with open(path, "w" if overwrite else "x") as file:
        tomlkit.dump(content, file)

set_project_value

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

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

Source code in src/somesy/commands/set_value.py
21
22
23
24
25
26
27
28
29
30
31
32
33
def set_project_value(input_file: Path, field: str, value: str) -> None:
    """Set one scalar project-metadata value in a somesy.toml file."""
    if field not in SETTABLE_FIELDS:
        allowed = ", ".join(sorted(SETTABLE_FIELDS))
        raise ValueError(f"Unknown project field '{field}'. Choose one of: {allowed}.")
    if not SomesyInput.is_somesy_file_path(input_file):
        raise ValueError("The input file must be a somesy.toml file.")

    content = get_input_content(input_file, no_unwrap=True)
    ProjectMetadata.model_validate({**content["project"], field: value})
    content["project"][field] = value
    with open(input_file, "w") as file:
        tomlkit.dump(content, file)