cpdocs API documentation
Project documentation Docs

File: cpdocs/providers/python.py

1# --------------------------------------------------------------------------------------------------2# SPDX-License-Identifier: Apache-2.03# SPDX-FileCopyrightText: 2026 cpdocs developers4# --------------------------------------------------------------------------------------------------5"""Contract provider for a plain ``pyproject.toml`` Python project."""6 7from __future__ import annotations8 9import json10import tomllib11from pathlib import Path12 13from ..contract import CONTRACT_VERSION, read_feature_set14 15 16def _project(source: Path) -> dict[str, object]:17    path = source / "pyproject.toml"18    if not path.is_file():19        raise RuntimeError(f"cpdocs manifest python: {path} does not exist")20    project = tomllib.loads(path.read_text(encoding="utf-8")).get("project", {})21    if not isinstance(project, dict) or not project.get("name"):22        raise RuntimeError(f"cpdocs manifest python: {path} has no [project] name")23    return project24 25 26def write_feature_sets(source: Path, output: Path) -> Path:27    project = _project(source)28    manifest = {29        "cpdocs-feature-sets": CONTRACT_VERSION,30        "generator": "cpdocs manifest python",31        "project": {"name": str(project["name"]), "version": str(project.get("version") or "")},32        "feature-sets": [{"features": []}],33    }34    output.parent.mkdir(parents=True, exist_ok=True)35    output.write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")36    return output37 38 39def write_manifest(40    source: Path,41    feature_set_file: Path,42    output: Path,43    *,44    package: str,45    package_source: str,46) -> Path:47    read_feature_set(feature_set_file)48    root = source / package_source49    if not root.is_dir():50        raise RuntimeError(f"cpdocs manifest python: package source {root} does not exist")51    manifest = {52        "cpdocs-manifest": CONTRACT_VERSION,53        "units": [54            {55                "id": package,56                "language": "python",57                "package": package,58                "api": [{"path": package_source}],59            }60        ],61    }62    output.parent.mkdir(parents=True, exist_ok=True)63    output.write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")64    return output