← back to Designer Wallcoverings
onboarding/sangetsu-lilycolor/scripts/sangetsu-spec-fold.py
57 lines
#!/usr/bin/env python3
"""sangetsu-spec-fold.py — fold the headless page-spec scrape back into staging.
Reads staging/sangetsu-page-specs.jsonl (pattern -> width/backing/origin/repeat/
match from the rendered SPECIFICATIONS block) and writes those into the pattern's
spec.* in sangetsu-staging.jsonl — only when currently empty (never overwrites).
Then the caller re-runs sangetsu-build.py + the gate report.
Reversible, $0, staging-only. No dw_unified / Shopify write.
"""
import json, os
HERE = os.path.dirname(os.path.abspath(__file__))
STAGING = os.path.join(os.path.dirname(HERE), "staging")
SPECS = os.path.join(STAGING, "sangetsu-page-specs.jsonl")
STG = os.path.join(STAGING, "sangetsu-staging.jsonl")
specs = {}
if os.path.exists(SPECS):
for l in open(SPECS):
l = l.strip()
if not l:
continue
d = json.loads(l)
specs[d.get("pattern")] = d
rows = [json.loads(l) for l in open(STG) if l.strip()]
w_before = sum(1 for p in rows if str((p.get("spec") or {}).get("width") or "").strip())
w_added = back_added = orig_added = 0
for p in rows:
s = specs.get(p.get("pattern"))
if not s:
continue
sp = p.setdefault("spec", {})
if s.get("width") and not str(sp.get("width") or "").strip():
sp["width"] = s["width"]; sp["_width_src"] = "page-spec"; w_added += 1
if s.get("backing") and not str(sp.get("backing") or "").strip():
sp["backing"] = s["backing"]; back_added += 1
if s.get("origin") and not str(sp.get("origin") or "").strip():
sp["origin"] = s["origin"]; orig_added += 1
for k in ("repeat", "match"):
if s.get(k) and not str(sp.get(k) or "").strip():
sp[k] = s[k]
tmp = STG + ".tmp"
with open(tmp, "w") as f:
for p in rows:
f.write(json.dumps(p, ensure_ascii=False) + "\n")
os.replace(tmp, STG)
w_after = sum(1 for p in rows if str((p.get("spec") or {}).get("width") or "").strip())
print(json.dumps({
"page_specs_loaded": len(specs),
"pattern_width_before": w_before, "pattern_width_after": w_after,
"width_added": w_added, "backing_added": back_added, "origin_added": orig_added,
}, indent=2))