attributes.py
python
| 1 | """muse attributes — display the ``.museattributes`` merge-strategy rules. |
| 2 | |
| 3 | Reads and pretty-prints the ``.museattributes`` file from the current |
| 4 | repository, showing each rule's path pattern, dimension, and strategy. |
| 5 | |
| 6 | Usage:: |
| 7 | |
| 8 | muse attributes # tabular display |
| 9 | muse attributes --json # JSON array of rule objects |
| 10 | """ |
| 11 | from __future__ import annotations |
| 12 | |
| 13 | import json |
| 14 | |
| 15 | import typer |
| 16 | |
| 17 | from muse.core.attributes import load_attributes |
| 18 | from muse.core.repo import require_repo |
| 19 | |
| 20 | app = typer.Typer() |
| 21 | |
| 22 | |
| 23 | @app.callback(invoke_without_command=True) |
| 24 | def attributes( |
| 25 | ctx: typer.Context, |
| 26 | output_json: bool = typer.Option(False, "--json", help="Output rules as JSON."), |
| 27 | ) -> None: |
| 28 | """Display the ``.museattributes`` merge-strategy rules.""" |
| 29 | root = require_repo() |
| 30 | rules = load_attributes(root) |
| 31 | |
| 32 | if output_json: |
| 33 | payload = [ |
| 34 | { |
| 35 | "path_pattern": r.path_pattern, |
| 36 | "dimension": r.dimension, |
| 37 | "strategy": r.strategy, |
| 38 | "source_line": r.source_line, |
| 39 | } |
| 40 | for r in rules |
| 41 | ] |
| 42 | typer.echo(json.dumps(payload, indent=2)) |
| 43 | return |
| 44 | |
| 45 | if not rules: |
| 46 | typer.echo("No .museattributes file found (or file is empty).") |
| 47 | typer.echo( |
| 48 | "Create one at the repository root to declare per-path merge strategies." |
| 49 | ) |
| 50 | return |
| 51 | |
| 52 | # Compute column widths for aligned output. |
| 53 | pat_w = max(len(r.path_pattern) for r in rules) |
| 54 | dim_w = max(len(r.dimension) for r in rules) |
| 55 | |
| 56 | typer.echo(f"{'Path pattern':<{pat_w}} {'Dimension':<{dim_w}} Strategy") |
| 57 | typer.echo(f"{'-' * pat_w} {'-' * dim_w} --------") |
| 58 | for rule in rules: |
| 59 | typer.echo( |
| 60 | f"{rule.path_pattern:<{pat_w}} {rule.dimension:<{dim_w}} {rule.strategy}" |
| 61 | ) |