mattrguerra/meridian-skillsmainPrivate on GitHub, published here in full

_stub.py

Language Python1.4 KB46 lines

skills/client-enrich/scripts/_stub.py

  1. """Frontmatter helpers shared by the client-enrich write-back scripts.
  2. Frontmatter is edited as text, one key at a time, so every line the scripts do
  3. not own stays byte-identical. Not run directly.
  4. """
  5. import re
  6. import sys
  7. def split_frontmatter(text):
  8. m = re.match(r"^---\n(.*?)\n---\n", text, re.DOTALL)
  9. if not m:
  10. sys.exit("ERROR: no YAML frontmatter found at top of stub.")
  11. return m.group(1), text[m.end() :]
  12. def fm_get(fm, key):
  13. m = re.search(rf"(?m)^{re.escape(key)}:[ \t]*(.*)$", fm)
  14. return m.group(1).strip() if m else None
  15. def fm_is_empty(val):
  16. if val is None:
  17. return True
  18. v = val.strip().strip('"').strip("'").strip()
  19. return v == "" or v == "[TBD]"
  20. def fm_set(fm, key, value, force, literal=True):
  21. """Fill `key` only if it is empty or "[TBD]" (any value with force). Never adds a key.
  22. re.sub reads backslashes in the replacement as escapes, so literal=True
  23. doubles them and the value lands exactly as given. patch_research.py passes
  24. literal=False, which keeps its established handling of a backslash.
  25. """
  26. cur = fm_get(fm, key)
  27. if cur is None:
  28. return fm, False
  29. if not force and not fm_is_empty(cur):
  30. return fm, False
  31. if literal:
  32. value = value.replace("\\", "\\\\")
  33. return re.sub(
  34. rf"(?m)^{re.escape(key)}:[ \t]*.*$", f'{key}: "{value}"', fm, count=1
  35. ), True