cdc209536f
- new script python/src/init/init.py: locates the register-python-argcomplete that belongs to the running interpreter (venv bin, NOT the resolve()d symlink target - proven by a live pipx run), appends the managed hook block to ~/.bashrc between # BEGIN/END daedalus init markers, idempotent; --bashrc, --dry-run; Windows notice; zsh warning - per-script spec python/src/init/spec.md (SPEC 6.11/6.12), tests python/tests/test_init_script.py (13; the plain test_init.py name is taken by daedalus/tests) - registry entry, README row + section, pyproject packaging (daedalus.init), CHANGELOG 0.10.0, version pins Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
99 lines
3.6 KiB
Python
99 lines
3.6 KiB
Python
"""Registry of the scripts daedalus can run.
|
|
|
|
Add a script by adding one entry to SCRIPTS. The suffix of ``path`` picks
|
|
the runner (daedalus/runners.py); ``categories`` powers the
|
|
``daedalus <category>`` listing forms (SPEC.md sections 3 and 4).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import dataclasses
|
|
import pathlib
|
|
import re
|
|
|
|
from daedalus import LIBRARY_ROOT
|
|
|
|
NAME_PATTERN = re.compile(r"^[a-z][a-z0-9_-]*$")
|
|
|
|
DEFAULT_CATEGORY = "general"
|
|
|
|
|
|
def _normalize_categories(value: str | list[str] | tuple[str, ...]) -> tuple[str, ...]:
|
|
"""One label or an iterable of labels -> tuple; empty is rejected (SPEC item 4.1.3)."""
|
|
if isinstance(value, str):
|
|
labels: tuple[str, ...] = (value,)
|
|
elif isinstance(value, (list, tuple)):
|
|
labels = tuple(value)
|
|
else:
|
|
raise ValueError(f"categories must be a string or a sequence, not {type(value).__name__}")
|
|
if not labels or any(not label for label in labels):
|
|
raise ValueError("categories must not be empty")
|
|
return labels
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Script:
|
|
"""One registered script: repo-relative path, one-line description, categories."""
|
|
|
|
path: str
|
|
description: str
|
|
categories: str | list[str] | tuple[str, ...] = DEFAULT_CATEGORY
|
|
|
|
def __post_init__(self) -> None:
|
|
"""Store the labels normalized to a tuple (SPEC item 4.1.3)."""
|
|
object.__setattr__(self, "categories", _normalize_categories(self.categories))
|
|
|
|
@property
|
|
def absolute_path(self) -> pathlib.Path:
|
|
"""Absolute path in the checkout or the installed _library."""
|
|
return LIBRARY_ROOT / self.path
|
|
|
|
@property
|
|
def categories_display(self) -> str:
|
|
"""Labels joined with a comma and a space (SPEC item 4.1.5)."""
|
|
return ", ".join(self.categories)
|
|
|
|
|
|
SCRIPTS: dict[str, Script] = {
|
|
"gpgtools": Script(
|
|
path="python/src/gpgtools/gpgtools.py",
|
|
description="YubiKey GPG/SSH helpers: initialize (gnupg readiness check + install), initialize_yukikey (fresh machine, steps 1-4), send_pubkey (resolve key, verify possession, append to a server), store_publickey (install the key from the machine being granted access), configure_ssh (marker-managed per-host ssh block)",
|
|
categories="gpg",
|
|
),
|
|
"hello": Script(
|
|
path="python/src/hello/hello.py",
|
|
description="Print a hello line and the daedalus version",
|
|
categories="test",
|
|
),
|
|
"hello_bash": Script(
|
|
path="bash/hello_bash.sh",
|
|
description="Sample bash script through the shared help() interface",
|
|
categories="test",
|
|
),
|
|
"init": Script(
|
|
path="python/src/init/init.py",
|
|
description="Enable tab completion for daedalus: register argcomplete in bash (appends the managed hook block to ~/.bashrc), idempotent",
|
|
categories="setup",
|
|
),
|
|
}
|
|
"""Every script daedalus can run (SPEC.md section 4)."""
|
|
|
|
|
|
def used_categories() -> set[str]:
|
|
"""The set of every label of every script (SPEC item 4.4.1)."""
|
|
labels: set[str] = set()
|
|
for script in SCRIPTS.values():
|
|
labels.update(script.categories)
|
|
return labels
|
|
|
|
|
|
def is_category_label(token: str) -> bool:
|
|
"""True when token is an in-use category and not a script name (SPEC item 4.4.2)."""
|
|
return token not in SCRIPTS and token in used_categories()
|
|
|
|
|
|
def scripts_matching_categories(labels: list[str] | tuple[str, ...]) -> dict[str, Script]:
|
|
"""Scripts that have every label (AND), sorted by name (SPEC item 4.4.3)."""
|
|
wanted = set(labels)
|
|
return {name: script for name, script in sorted(SCRIPTS.items())
|
|
if wanted <= set(script.categories)}
|