-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path04_sync_and_watch.py
More file actions
212 lines (155 loc) · 6.29 KB
/
04_sync_and_watch.py
File metadata and controls
212 lines (155 loc) · 6.29 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""Example 4: Sync and Watch - Keep docs in sync with code changes.
This example shows how to use the sync and watch features
to automatically update documentation when code changes.
"""
import time
from code2docs.sync.watcher import start_watcher
from code2docs.sync.differ import Differ, ChangeInfo
from code2docs.sync.updater import Updater
from code2docs.config import Code2DocsConfig
# =============================================================================
# DETECTING CHANGES
# =============================================================================
def detect_changes_example(project_path: str) -> ChangeInfo:
"""Detect what files have changed since last documentation generation."""
differ = Differ(project_path)
changes = differ.detect_changes()
print(f"Changes detected:")
print(f" New files: {len(changes.new_files)}")
for f in changes.new_files[:5]: # Show first 5
print(f" - {f}")
print(f" Modified: {len(changes.modified)}")
for f in changes.modified[:5]:
print(f" - {f}")
print(f" Deleted: {len(changes.deleted)}")
for f in changes.deleted[:5]:
print(f" - {f}")
return changes
# =============================================================================
# UPDATING DOCUMENTATION
# =============================================================================
def update_docs_incrementally(project_path: str) -> None:
"""Update only the parts of docs that need changing."""
config = Code2DocsConfig(
project_name="My Project",
output_dir="docs",
sync_markers=True, # Enable sync markers for partial updates
)
differ = Differ(project_path)
changes = differ.detect_changes()
if not changes.has_changes:
print("No changes to document")
return
updater = Updater(config=config)
updater.apply(project_path=project_path, changes=changes)
print("Documentation updated incrementally")
def force_full_regeneration(project_path: str) -> None:
"""Force full regeneration of all documentation."""
config = Code2DocsConfig(
project_name="My Project",
output_dir="docs",
)
# Create empty changes to force full regeneration
from code2docs.sync.differ import ChangeInfo
changes = ChangeInfo(
new_files=[], modified=[], deleted=[],
has_changes=True # Force update
)
updater = Updater(config=config)
updater.apply(project_path=project_path, changes=changes)
print("Full documentation regeneration complete")
# =============================================================================
# FILE WATCHER
# =============================================================================
def watch_and_auto_regenerate(project_path: str, interval: int = 5) -> None:
"""Watch for file changes and auto-regenerate documentation.
Args:
project_path: Path to watch
interval: Check interval in seconds
"""
config = Code2DocsConfig(
project_name="My Project",
output_dir="docs",
)
print(f"Starting watcher on {project_path}")
print(f"Press Ctrl+C to stop")
try:
start_watcher(
project_path=project_path,
config=config,
interval=interval
)
except KeyboardInterrupt:
print("\nWatcher stopped")
# =============================================================================
# CUSTOM WATCHER WITH CALLBACKS
# =============================================================================
def custom_watcher_with_hooks(project_path: str) -> None:
"""Set up a custom watcher with pre/post generation hooks."""
import os
import signal
config = Code2DocsConfig(project_name="My Project")
running = True
def on_change_detected(changes: ChangeInfo) -> None:
"""Called when changes are detected."""
print(f"[{time.strftime('%H:%M:%S')}] Changes detected:")
print(f" New: {len(changes.new_files)}, "
f"Modified: {len(changes.modified)}, "
f"Deleted: {len(changes.deleted)}")
def on_generation_start() -> None:
"""Called before documentation generation."""
print(" -> Starting doc generation...")
def on_generation_complete() -> None:
"""Called after documentation generation."""
print(" -> Documentation updated!")
def signal_handler(signum, frame):
nonlocal running
running = False
print("\nShutting down...")
signal.signal(signal.SIGINT, signal_handler)
differ = Differ(project_path)
updater = Updater(config=config)
print(f"Custom watcher started on {project_path}")
while running:
changes = differ.detect_changes()
if changes.has_changes:
on_change_detected(changes)
on_generation_start()
updater.apply(project_path=project_path, changes=changes)
on_generation_complete()
time.sleep(5) # Check every 5 seconds
# =============================================================================
# GIT INTEGRATION
# =============================================================================
def sync_with_git_changes(project_path: str) -> None:
"""Only regenerate docs for files changed in git."""
import subprocess
# Get list of changed files from git
result = subprocess.run(
["git", "diff", "--name-only", "HEAD"],
capture_output=True,
text=True,
cwd=project_path
)
changed_files = result.stdout.strip().split("\n")
python_files = [f for f in changed_files if f.endswith(".py")]
if not python_files:
print("No Python files changed")
return
print(f"Python files changed: {len(python_files)}")
# Create partial change info
changes = ChangeInfo(
new_files=[],
modified=python_files,
deleted=[],
has_changes=True
)
config = Code2DocsConfig()
updater = Updater(config=config)
updater.apply(project_path=project_path, changes=changes)
if __name__ == "__main__":
# Example usage
# detect_changes_example("./my_project")
# update_docs_incrementally("./my_project")
# watch_and_auto_regenerate("./my_project")
pass