mattrguerra/meridian-outreach-vaultmainPrivate on GitHub, published here in full

config.py

Language Python1.9 KB58 lines

07-Automation/partner_pipeline/config.py

  1. """partner-thresholds.md config and the arming guard."""
  2. from __future__ import annotations
  3. import re
  4. import sys
  5. import yaml
  6. from .paths import CONFIG_MD
  7. # Any value of this literal in the active mode means the pipeline is not armed.
  8. SENTINEL = "TBD"
  9. # The two classify outcomes that are not a partner tier.
  10. NON_PARTNER = ("Not a partner client", "UNCLASSIFIED: thresholds not set")
  11. def load_config() -> dict:
  12. """Extract and parse the first ```yaml fenced block from partner-thresholds.md."""
  13. if not CONFIG_MD.exists():
  14. sys.exit(f"Config not found: {CONFIG_MD}")
  15. text = CONFIG_MD.read_text(encoding="utf-8")
  16. m = re.search(r"```yaml\s*\n(.*?)\n```", text, re.DOTALL)
  17. if not m:
  18. sys.exit("No ```yaml block found in partner-thresholds.md")
  19. return yaml.safe_load(m.group(1)) or {}
  20. def _has_sentinel(node) -> bool:
  21. if isinstance(node, str):
  22. return node.strip() == SENTINEL
  23. if isinstance(node, dict):
  24. return any(_has_sentinel(v) for v in node.values())
  25. if isinstance(node, list):
  26. return any(_has_sentinel(v) for v in node)
  27. return False
  28. def is_armed(cfg: dict) -> tuple[bool, list[str]]:
  29. """A run is armed only when the active mode has no TBDs and min_orders is set."""
  30. reasons = []
  31. mode = cfg.get("mode", "ltv_bands")
  32. active = cfg.get(mode)
  33. if _has_sentinel(active):
  34. reasons.append(f"'{mode}' block still has TBD placeholder(s)")
  35. if cfg.get("min_orders_to_qualify") == SENTINEL:
  36. reasons.append("min_orders_to_qualify is TBD")
  37. return (len(reasons) == 0, reasons)
  38. def guard_or_exit():
  39. """The write stages refuse to run until the thresholds are set."""
  40. cfg = load_config()
  41. armed, reasons = is_armed(cfg)
  42. if not armed:
  43. sys.exit("REFUSED: thresholds not set (" + "; ".join(reasons) +
  44. "). Fill partner-thresholds.md before write stages.")
  45. return cfg