#!/usr/bin/env python3
"""Repair p2t_e2e.py: rebuild the writer-prep block cleanly."""
p = '/opt/struktur/obsidian-graphiti-import-staging-p2i/p2t_e2e.py'
lines = open(p, encoding='utf-8').read().split('\n')
# find the writer section and cut everything from the broken _f.write line
# to the line before 'kc = kconn()' that follows it
start = None
end = None
for i, l in enumerate(lines):
if "_f.write(" in l and "SOL-005" in l:
start = i
if start is not None and l.strip() == "kc = kconn()":
end = i
break
assert start is not None and end is not None, (start, end)
good = [
"os.makedirs('/tmp/Solutions', exist_ok=True)",
"with open('/tmp/Solutions/SOL-005-content-briefing-modul.md', 'w') as _f:",
" _f.write('# SOL-005 Content Briefing Modul')",
"os.chdir('/tmp')",
]
lines[start:end] = good
src = '\n'.join(lines)
import py_compile
open(p, 'w', encoding='utf-8').write(src)
py_compile.compile(p, doraise=True)
print('writer block rebuilt + SYNTAX_OK')