SPB Git forge

spb/websensor

Public
33commits 1branches 0releases
3.4 MBsize
maindefault branch
10 days agolast push
TypeScript 55.4% Python 43.2% SQL 1.2%
1.8 KB · 47 lines python
Raw Blame History
1#!/usr/bin/env python32"""Remove from a registry fragment every sensor that the validator reported as FAIL (and, with3--drop-warn, WARN). Works on the YAML text line by line so the compact flow style is preserved.45Usage: python3 scripts/prune-fragment.py <fragment.yaml> <validator-report.json> [--drop-warn] [--keep-empty]6Sources left without any sensor are removed unless --keep-empty (they may still be useful with7discover: flags — decide case by case).8"""9import json10import re11import sys1213import yaml141516def main() -> None:17    frag, report = sys.argv[1], sys.argv[2]18    drop_warn = "--drop-warn" in sys.argv19    keep_empty = "--keep-empty" in sys.argv20    rep = json.load(open(report))21    bad = {r["url"] for r in rep["results"] if r["status"] == "FAIL" or (drop_warn and r["status"] == "WARN")}22    lines = open(frag).read().split("\n")23    out = []24    removed = 025    for ln in lines:26        m = re.search(r'url:\s*"([^"]+)"', ln) or re.search(r"url:\s*'([^']+)'", ln) or re.search(r"url:\s*(\S+?)[,}]", ln)27        if ln.lstrip().startswith("- {") and m and m.group(1) in bad:28            removed += 129            continue30        out.append(ln)31    text = "\n".join(out)32    # Drop sources with no sensors left (flow-style sensors → check the parsed structure).33    if not keep_empty:34        data = yaml.safe_load(text)35        empty = {s["id"] for s in data["sources"] if not s.get("sensors") and not s.get("discover")}36        if empty:37            blocks = re.split(r"(?m)^(?=  - id: )", text)38            kept = [b for b in blocks if not any(b.startswith(f"  - id: {e}\n") for e in empty)]39            text = "".join(kept)40            print(f"removed {len(empty)} empty sources: {', '.join(sorted(empty))}", file=sys.stderr)41    open(frag, "w").write(text)42    print(f"removed {removed} sensors from {frag}", file=sys.stderr)434445if __name__ == "__main__":46    main()47