Coverage for packages/workshop/src/livery/workshop/_identity.py: 100%

Measured by coverage.py 7.16.2 on CPython 3.14 while the suites of tests, packages/workshop, packages/extensions/ty, packages/extensions/ruff, packages/extensions/python, packages/extensions/pytest, packages/extensions/pyrefly, packages/extensions/nanobind, packages/extensions/mypy, packages/extensions/mkdocstrings, packages/extensions/lfs, packages/extensions/docs, packages/extensions/cpp, packages/extensions/coverage, packages/extensions/conan, packages/extensions/cmake, packages/extensions/claude, packages/extensions/clang-tidy, packages/extensions/clang-format, packages/extensions/changelog, packages/extensions/basedpyright ran.

1"""The workspace's identity and its members, as the contracts state them.
2
3The root `workshop.toml`'s `[workspace]` table names the project, its
4description, its namespace, its authors and its copyright year; each
5package's `workshop.toml` names the package, its kind, its description
6and the extras its dev-group entry installs, and discovery gives the
7extensions its set holds. The renders read these facts under
8the names the templates spell (`project_name`, `package_name`, the
9`packages` roster), so a template reads the same data whatever stores
10it.
11
12Reach for [livery.workshop._identity.project_facts][] for the project's
13render data and [livery.workshop._identity.package_facts][] for one
14member's.
15"""
16
17from __future__ import annotations(empty)
18
19from pathlib import Path(empty)
20from typing import Any, cast(empty)
21
22from livery.workshop._contract import load_contract(empty)
23
24
25def _workspace(root: Path) -> dict[str, Any]:(empty)
26 path = root / "workshop.toml"
27 if not path.is_file():
28 return {}
29 return cast("dict[str, Any]", load_contract(path).get("workspace") or {})
30
31
32def is_born(root: Path) -> bool:(empty)
33 """Whether the workspace at *root* has an identity: its contract names it."""
34 return bool(_workspace(root).get("name"))
35
36
37def project_facts(root: Path) -> dict[str, Any]:(empty)
38 """The project's identity and roster, under the names the templates spell.
39
40 A fact the contract leaves out takes the default a birth would have
41 written: the directory's name, a description and an author derived
42 from it, the namespace spelled from the name.
43 """
44 return {**identity(root), "packages": roster(root)}
45
46
47def identity(root: Path) -> dict[str, Any]:(empty)
48 """The project's identity alone, without the roster discovery builds.
49
50 A package's render reads this while a member is still being born,
51 before its contract exists for discovery to find.
52 """
53 workspace = _workspace(root)
54 name = str(workspace.get("name") or root.resolve().name)
55 authors = cast("list[dict[str, str]]", workspace.get("authors") or [])
56 first = authors[0] if authors else {}
57 return {
58 "project_name": name,
59 "project_description": str(
60 workspace.get("description") or f"The {name} monorepo (virtual root)."
61 ),
62 "author_name": str(first.get("name") or f"{name} authors"),
63 "author_email": str(first.get("email") or ""),
64 "copyright_year": str(workspace.get("copyright-year") or ""),
65 "namespace_package": str(
66 workspace.get("namespace")
67 or name.lower().replace("-", "_").replace(" ", "_")
68 ),
69 }
70
71
72def roster(root: Path) -> list[dict[str, Any]]:(empty)
73 """Each member as the project file lists it, in path order.
74
75 Each carries `extensions`, the extensions its set holds, in
76 composition order. A member holding the runtime's language carries
77 `dev`, the dev-group entry with its `dev-extras`; any other carries
78 none and stays out of the uv workspace.
79 """
80 from livery.workshop._composition import extension_set
81 from livery.workshop._packages import discover_packages, holds_runtime_language
82
83 if not (root / "packages").is_dir():
84 return []
85 entries: list[dict[str, Any]] = []
86 for package in discover_packages(root):
87 entry: dict[str, Any] = {
88 "dir": package.member,
89 "name": package.name,
90 "extensions": list(extension_set(package)),
91 }
92 if holds_runtime_language(package):
93 contract = load_contract(package.directory / "workshop.toml")
94 extras = cast("list[str]", contract.get("dev-extras") or [])
95 entry["dev"] = (
96 f"{package.name}[{','.join(extras)}]" if extras else package.name
97 )
98 entries.append(entry)
99 return entries
100
101
102def python_dirs(root: Path) -> list[str]:(empty)
103 """The ``src`` and ``tests`` directories that hold a python file.
104
105 Those of each member holding the runtime's language, as
106 root-relative posix paths in path order. An extension's ``src`` may
107 hold data alone, and a checker such as mypy refuses a named
108 directory with no ``.py`` or ``.pyi`` file in it.
109 """
110 from livery.workshop._packages import discover_packages, holds_runtime_language
111
112 if not (root / "packages").is_dir():
113 return []
114 found: list[str] = []
115 for package in discover_packages(root):
116 if not holds_runtime_language(package):
117 continue
118 for name in ("src", "tests"):
119 directory = package.directory / name
120 if directory.is_dir() and any(
121 next(directory.rglob(f"*{suffix}"), None) for suffix in (".py", ".pyi")
122 ):
123 found.append(f"packages/{package.member}/{name}")
124 return found
125
126
127def default_description(name: str, project: str) -> str:(empty)
128 """The one-line description of the package *name* whose contract names none."""
129 return f"{name}: a package of the {project} workspace."
130
131
132def package_facts(root: Path, directory: Path) -> dict[str, Any]:(empty)
133 """One member's identity, under the names the package templates spell."""
134 project = identity(root)
135 contract = load_contract(directory / "workshop.toml")
136 name = str(contract.get("name") or directory.name)
137 return {
138 "package_name": name,
139 "package_description": str(
140 contract.get("description")
141 or default_description(name, str(project["project_name"]))
142 ),
143 "package_dir": directory.relative_to(root / "packages").as_posix(),
144 "namespace_package": project["namespace_package"],
145 "author_name": project["author_name"],
146 "author_email": project["author_email"],
147 "copyright_year": project["copyright_year"],
148 }