output.py
python
sha256:a78e7e5a8740e03315f325d19edeb3aa1b306b3337d04abbaa9a9e0f3bbeb7a1
docs: MuseHub-first before ISR #74 — staging solidify NEXT
Human
10 hours ago
| 1 | """CLI output formatting and reporting.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import json |
| 6 | import sys |
| 7 | from dataclasses import dataclass, field |
| 8 | from typing import Any |
| 9 | |
| 10 | |
| 11 | @dataclass |
| 12 | class OutputContext: |
| 13 | """Stdout/stderr discipline for human and JSON modes.""" |
| 14 | |
| 15 | json_mode: bool = False |
| 16 | quiet: bool = False |
| 17 | verbose: bool = False |
| 18 | no_color: bool = False |
| 19 | |
| 20 | def __post_init__(self) -> None: |
| 21 | if not self.no_color and not sys.stdout.isatty(): |
| 22 | self.no_color = True |
| 23 | |
| 24 | def emit(self, message: str) -> None: |
| 25 | """Print a human message to stdout unless quiet/json.""" |
| 26 | if self.json_mode or self.quiet: |
| 27 | return |
| 28 | print(message) |
| 29 | |
| 30 | def emit_json(self, payload: dict[str, Any]) -> None: |
| 31 | """Print exactly one JSON object to stdout.""" |
| 32 | print(json.dumps(payload, indent=2, sort_keys=True)) |
| 33 | |
| 34 | def warn(self, message: str) -> None: |
| 35 | """Print a warning to stderr.""" |
| 36 | print(message, file=sys.stderr) |
| 37 | |
| 38 | def error(self, message: str) -> None: |
| 39 | """Print an error to stderr.""" |
| 40 | print(message, file=sys.stderr) |
| 41 | |
| 42 | def verbose_msg(self, message: str) -> None: |
| 43 | """Print diagnostic detail to stderr when verbose.""" |
| 44 | if self.verbose: |
| 45 | print(message, file=sys.stderr) |
| 46 | |
| 47 | |
| 48 | @dataclass |
| 49 | class CommandReport: |
| 50 | """Mutable report payload shared across command phases.""" |
| 51 | |
| 52 | data: dict[str, Any] = field(default_factory=dict) |
| 53 | warnings: list[str] = field(default_factory=list) |
| 54 | errors: list[str] = field(default_factory=list) |
| 55 | |
| 56 | def add_warning(self, message: str) -> None: |
| 57 | self.warnings.append(message) |
| 58 | |
| 59 | def to_payload(self) -> dict[str, Any]: |
| 60 | payload = dict(self.data) |
| 61 | payload["warnings"] = list(self.warnings) |
| 62 | return payload |
File History
2 commits
sha256:a78e7e5a8740e03315f325d19edeb3aa1b306b3337d04abbaa9a9e0f3bbeb7a1
docs: MuseHub-first before ISR #74 — staging solidify NEXT
Human
10 hours ago
sha256:4671b7f787ddbe63ced31c895b688c77ab495653b65a730b423329f26b3c1439
feat: K1-P1 complete — agent provenance, build-verification…
Sonnet 4.6
patch
52 days ago