-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwatch-docs.py
More file actions
125 lines (103 loc) · 3.7 KB
/
watch-docs.py
File metadata and controls
125 lines (103 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# AUTO-GENERATED FILE - DO NOT EDIT
# This file was automatically generated by the XDK build tool.
# Any manual changes will be overwritten on the next generation.
#!/usr/bin/env python3
"""
AUTO-GENERATED FILE - DO NOT EDIT
This file was automatically generated by the XDK build tool.
Any manual changes will be overwritten on the next generation.
Watch for changes and regenerate documentation automatically.
"""
import os
import sys
import time
import subprocess
from pathlib import Path
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
print("🚀 Starting X API SDK Documentation Watch Server...")
class DocsHandler(FileSystemEventHandler):
"""Handle file system events for documentation regeneration."""
def __init__(self):
self.last_regeneration = 0
self.debounce_seconds = 2
def on_modified(self, event):
if event.is_directory:
return
# Only watch Python files
if not event.src_path.endswith(".py"):
return
# Skip if in docs or build directories
if "docs" in event.src_path or "__pycache__" in event.src_path:
return
# Debounce rapid changes
current_time = time.time()
if current_time - self.last_regeneration < self.debounce_seconds:
return
self.last_regeneration = current_time
print(f"📝 File changed: {event.src_path}")
regenerate_docs()
def regenerate_docs():
"""Regenerate documentation."""
print("🔄 Regenerating documentation...")
try:
result = subprocess.run(
[sys.executable, "scripts/generate-docs-simple.py"],
check=True,
capture_output=True,
text=True,
)
print(result.stdout)
print("✅ Documentation regenerated successfully")
except subprocess.CalledProcessError as e:
print(f"❌ Failed to regenerate documentation: {e}")
print(f"stderr: {e.stderr}")
def start_server():
"""Start a simple HTTP server for docs."""
try:
import http.server
import socketserver
import threading
PORT = 8080
Handler = http.server.SimpleHTTPRequestHandler
def run_server():
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"📚 Starting documentation server on http://localhost:{PORT}")
httpd.serve_forever()
server_thread = threading.Thread(target=run_server, daemon=True)
server_thread.start()
return server_thread
except Exception as e:
print(f"⚠️ Could not start HTTP server: {e}")
return None
def run_server():
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"📚 Starting documentation server on http://localhost:{PORT}")
httpd.serve_forever()
server_thread = threading.Thread(target=run_server, daemon=True)
server_thread.start()
return server_thread
except Exception as e:
print(f"⚠️ Could not start HTTP server: {e}")
return None
# Initial documentation generation
regenerate_docs()
server_thread = start_server()
# Watch for changes
event_handler = DocsHandler()
observer = Observer()
# Watch xdk directory
xdk_path = Path("xdk")
if xdk_path.exists():
observer.schedule(event_handler, str(xdk_path), recursive=True)
observer.start()
print("👀 Watching for changes... Press Ctrl+C to stop")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\n🛑 Shutting down documentation server...")
observer.stop()
observer.join()
else:
print("⚠️ xdk directory not found")