Files
computer-playbook/cli/administration/deploy/dedicated/modes.py
kevinveenbirkenbachandClaude Opus 5 5137465c6f style: delete every comment the comments-valid policy rejects
3529 invalid comments in 528 files, grandfathered in while the linter
only saw the working-tree diff. Removal ran through the linter's own
collectors, so detection and deletion cannot drift: a comment whose
opener line is blank before it and whose closer line is blank after it
loses all its lines, anything else loses only the comment region, and a
line left holding a bare delimiter goes with it.

Five files keep their comments behind a nocheck marker instead. Four are
upstream configs whose inline docs are upstream's own (mautrix, systemd
journald, nextcloud-oidc-login, MediaWiki). In littlejs the
commented-out entries ARE the deactivation mechanism for an example
tile, so deleting them would delete configuration.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-18 03:22:02 +02:00

127 lines
3.5 KiB
Python

from __future__ import annotations
import re
from pathlib import Path
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
import argparse
MODE_LINE_RE = re.compile(
r"""^\s*(?P<key>[A-Z0-9_]+)\s*:\s*(?P<value>.+?)\s*(?:#\s*(?P<cmt>.*))?\s*$"""
)
def _parse_bool_literal(text: str) -> bool | None:
"""Convert simple true/false/yes/no/on/off into boolean."""
t = text.strip().lower()
if t in ("true", "yes", "on"):
return True
if t in ("false", "no", "off"):
return False
return None
def load_modes_from_yaml(modes_yaml_path: str) -> list[dict[str, Any]]:
"""
Load MODE_* definitions from a YAML-like key/value file.
Expected lines:
MODE_FOO: true # comment
MODE_BAR: false # comment
MODE_BAZ: null # comment
"""
if not Path(modes_yaml_path).exists():
raise FileNotFoundError(f"Modes file not found: {modes_yaml_path}")
modes: list[dict[str, Any]] = []
with Path(modes_yaml_path).open(encoding="utf-8") as fh:
for raw in fh:
line = raw.rstrip()
if not line or line.lstrip().startswith("#"):
continue
m = MODE_LINE_RE.match(line)
if not m:
continue
key = m.group("key")
val = m.group("value").strip()
cmt = (m.group("cmt") or "").strip()
if not key.startswith("MODE_"):
continue
default_bool = _parse_bool_literal(val)
modes.append(
{
"name": key,
"default": default_bool,
"help": cmt or f"Toggle {key}",
}
)
return modes
def add_dynamic_mode_args(
parser: argparse.ArgumentParser, modes_meta: list[dict[str, Any]]
) -> dict[str, dict[str, Any]]:
"""
Add command-line args dynamically based on MODE_* metadata.
Returns a spec mapping MODE_NAME -> info used by build_modes_from_args().
"""
spec: dict[str, dict[str, Any]] = {}
for meta in modes_meta:
name = meta["name"]
default = meta["default"]
desc = meta["help"]
short = name.replace("MODE_", "").lower()
if default is True:
opt = f"--skip-{short}"
dest = f"skip_{short}"
parser.add_argument(opt, action="store_true", dest=dest, help=desc)
spec[name] = {"dest": dest, "default": True, "kind": "bool_true"}
elif default is False:
opt = f"--{short}"
dest = short
parser.add_argument(opt, action="store_true", dest=dest, help=desc)
spec[name] = {"dest": dest, "default": False, "kind": "bool_false"}
else:
opt = f"--{short}"
dest = short
parser.add_argument(opt, choices=["true", "false"], dest=dest, help=desc)
spec[name] = {"dest": dest, "default": None, "kind": "explicit"}
return spec
def build_modes_from_args(
spec: dict[str, dict[str, Any]], args_namespace: argparse.Namespace
) -> dict[str, Any]:
"""Resolve CLI arguments into a MODE_* dictionary."""
modes: dict[str, Any] = {}
for mode_name, info in spec.items():
dest = info["dest"]
kind = info["kind"]
value = getattr(args_namespace, dest, None)
if kind == "bool_true":
modes[mode_name] = not value
elif kind == "bool_false":
modes[mode_name] = bool(value)
elif value is not None:
modes[mode_name] = value == "true"
return modes