|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import re |
| 6 | +import sys |
| 7 | +from datetime import datetime, timezone |
| 8 | + |
| 9 | +OUTPUT_DIR = os.environ.get("SHAPE_SYNC_MARKDOWN_DIR", "./pitches") |
| 10 | + |
| 11 | +MANIFEST = { |
| 12 | + "name": "shape-sync-markdowndir-python", |
| 13 | + "version": "1.0.0", |
| 14 | + "description": "Sync briefs to markdown directory (Python)", |
| 15 | + "type": "sync", |
| 16 | + "operations": ["push", "pull", "status", "test"], |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +def slugify(title): |
| 21 | + slug = title.lower() |
| 22 | + slug = re.sub(r"[^a-z0-9]+", "-", slug) |
| 23 | + slug = slug.strip("-") |
| 24 | + return slug |
| 25 | + |
| 26 | + |
| 27 | +def generate_markdown(brief): |
| 28 | + lines = ["---"] |
| 29 | + lines.append(f"id: {brief['id']}") |
| 30 | + lines.append(f"title: \"{brief['title']}\"") |
| 31 | + lines.append(f"status: {brief.get('status', 'proposed')}") |
| 32 | + if brief.get("appetite"): |
| 33 | + lines.append(f"appetite: {brief['appetite']}") |
| 34 | + synced_at = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z") |
| 35 | + lines.append(f"synced_at: {synced_at}") |
| 36 | + lines.append("---") |
| 37 | + lines.append("") |
| 38 | + lines.append(brief.get("body", "")) |
| 39 | + return "\n".join(lines) |
| 40 | + |
| 41 | + |
| 42 | +def parse_frontmatter(content): |
| 43 | + match = re.match(r"^---\n(.+?)\n---\n\n?(.*)", content, re.DOTALL) |
| 44 | + if not match: |
| 45 | + return None |
| 46 | + |
| 47 | + frontmatter_text = match.group(1) |
| 48 | + body = match.group(2) |
| 49 | + |
| 50 | + brief = {"body": body} |
| 51 | + |
| 52 | + for line in frontmatter_text.split("\n"): |
| 53 | + if line.startswith("id:"): |
| 54 | + brief["id"] = line[3:].strip() |
| 55 | + elif line.startswith("title:"): |
| 56 | + title = line[6:].strip() |
| 57 | + title = title.strip('"') |
| 58 | + brief["title"] = title |
| 59 | + elif line.startswith("status:"): |
| 60 | + brief["status"] = line[7:].strip() |
| 61 | + elif line.startswith("appetite:"): |
| 62 | + brief["appetite"] = line[9:].strip() |
| 63 | + |
| 64 | + return brief |
| 65 | + |
| 66 | + |
| 67 | +def handle_test(): |
| 68 | + return {"success": True} |
| 69 | + |
| 70 | + |
| 71 | +def handle_push(params): |
| 72 | + briefs = params.get("briefs", []) |
| 73 | + |
| 74 | + os.makedirs(OUTPUT_DIR, exist_ok=True) |
| 75 | + |
| 76 | + mappings = [] |
| 77 | + for brief in briefs: |
| 78 | + filename = f"{brief['id']}-{slugify(brief['title'])}.md" |
| 79 | + filepath = os.path.join(OUTPUT_DIR, filename) |
| 80 | + |
| 81 | + with open(filepath, "w", encoding="utf-8") as f: |
| 82 | + f.write(generate_markdown(brief)) |
| 83 | + |
| 84 | + mappings.append({ |
| 85 | + "local_id": brief["id"], |
| 86 | + "remote_id": filename, |
| 87 | + "entity_type": "brief", |
| 88 | + }) |
| 89 | + |
| 90 | + return { |
| 91 | + "success": True, |
| 92 | + "data": { |
| 93 | + "pushed": len(briefs), |
| 94 | + "pulled": 0, |
| 95 | + "conflicts": 0, |
| 96 | + "errors": [], |
| 97 | + "mappings": mappings, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | +def handle_pull(): |
| 103 | + if not os.path.isdir(OUTPUT_DIR): |
| 104 | + return { |
| 105 | + "success": True, |
| 106 | + "data": { |
| 107 | + "briefs": [], |
| 108 | + "tasks": [], |
| 109 | + "mappings": [], |
| 110 | + "pushed": 0, |
| 111 | + "pulled": 0, |
| 112 | + "conflicts": 0, |
| 113 | + "errors": [], |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + briefs = [] |
| 118 | + for filename in os.listdir(OUTPUT_DIR): |
| 119 | + if not filename.endswith(".md"): |
| 120 | + continue |
| 121 | + |
| 122 | + filepath = os.path.join(OUTPUT_DIR, filename) |
| 123 | + with open(filepath, encoding="utf-8") as f: |
| 124 | + content = f.read() |
| 125 | + |
| 126 | + brief = parse_frontmatter(content) |
| 127 | + if brief: |
| 128 | + briefs.append(brief) |
| 129 | + |
| 130 | + return { |
| 131 | + "success": True, |
| 132 | + "data": { |
| 133 | + "briefs": briefs, |
| 134 | + "tasks": [], |
| 135 | + "mappings": [], |
| 136 | + "pushed": 0, |
| 137 | + "pulled": len(briefs), |
| 138 | + "conflicts": 0, |
| 139 | + "errors": [], |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | +def handle_status(): |
| 145 | + count = 0 |
| 146 | + if os.path.isdir(OUTPUT_DIR): |
| 147 | + count = len([f for f in os.listdir(OUTPUT_DIR) if f.endswith(".md")]) |
| 148 | + |
| 149 | + return { |
| 150 | + "success": True, |
| 151 | + "data": { |
| 152 | + "mapped_briefs": count, |
| 153 | + "mapped_tasks": 0, |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | +def handle_request(request): |
| 159 | + operation = request.get("operation") |
| 160 | + params = request.get("params", {}) |
| 161 | + |
| 162 | + if operation == "test": |
| 163 | + return handle_test() |
| 164 | + elif operation == "push": |
| 165 | + return handle_push(params) |
| 166 | + elif operation == "pull": |
| 167 | + return handle_pull() |
| 168 | + elif operation == "status": |
| 169 | + return handle_status() |
| 170 | + else: |
| 171 | + return {"success": False, "error": f"Unknown operation: {operation}"} |
| 172 | + |
| 173 | + |
| 174 | +if __name__ == "__main__": |
| 175 | + if "--manifest" in sys.argv: |
| 176 | + print(json.dumps(MANIFEST)) |
| 177 | + else: |
| 178 | + request = json.loads(sys.stdin.readline()) |
| 179 | + response = handle_request(request) |
| 180 | + print(json.dumps(response)) |
0 commit comments