-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent_manager.py
More file actions
361 lines (286 loc) · 11.9 KB
/
content_manager.py
File metadata and controls
361 lines (286 loc) · 11.9 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
"""
Content Manager for HelpfulBatBot
Manages fetching and updating content from multiple git repositories.
Supports automatic updates, caching, and flexible include/exclude patterns.
"""
import os
import yaml
import subprocess
from pathlib import Path
from datetime import datetime, timedelta
from typing import List, Dict, Tuple, Optional
import logging
import json
logger = logging.getLogger(__name__)
class ContentSource:
"""Represents a single content source (e.g., a git repository)"""
def __init__(self, config: Dict):
self.name = config["name"]
self.type = config["type"]
self.url = config["url"]
self.branch = config.get("branch", "main")
self.local_path = Path(config["local_path"])
self.update_frequency = config.get("update_frequency", "daily")
self.include_paths = config.get("include_paths", [])
self.exclude_paths = config.get("exclude_paths", [])
self.priority = config.get("priority", 1.0)
self.source_label = config.get("source_label", self.name)
self.last_update = None
self._load_update_timestamp()
def _load_update_timestamp(self):
"""Load last update timestamp from disk"""
update_marker = self.local_path / ".last_update"
if update_marker.exists():
try:
timestamp = float(update_marker.read_text())
self.last_update = datetime.fromtimestamp(timestamp)
except (ValueError, OSError):
pass
def _save_update_timestamp(self):
"""Save update timestamp to disk"""
update_marker = self.local_path / ".last_update"
try:
update_marker.write_text(str(datetime.now().timestamp()))
except OSError as e:
logger.warning(f"Could not save update timestamp: {e}")
def needs_update(self) -> bool:
"""Check if content needs to be updated based on frequency"""
if not self.local_path.exists():
return True
if self.last_update is None:
return True
if self.update_frequency == "on_startup":
return False # Only update once per run
now = datetime.now()
if self.update_frequency == "hourly":
return (now - self.last_update) > timedelta(hours=1)
elif self.update_frequency == "daily":
return (now - self.last_update) > timedelta(days=1)
elif self.update_frequency == "never":
return False
return False
def clone_or_pull(self) -> bool:
"""Clone or pull the git repository"""
logger.info(f"Updating content source: {self.name}")
try:
if not self.local_path.exists():
# Clone repository
logger.info(f"Cloning {self.url} to {self.local_path}")
self.local_path.parent.mkdir(parents=True, exist_ok=True)
result = subprocess.run([
"git", "clone",
"--depth", "1", # Shallow clone for faster download
"--single-branch",
"--branch", self.branch,
self.url,
str(self.local_path)
], check=True, capture_output=True, text=True)
logger.info(f"✅ Cloned {self.name}")
else:
# Pull latest changes
logger.info(f"Pulling latest from {self.name}")
result = subprocess.run([
"git", "-C", str(self.local_path),
"pull", "--ff-only", "origin", self.branch
], check=True, capture_output=True, text=True)
if "Already up to date" in result.stdout:
logger.info(f"✅ {self.name} already up to date")
else:
logger.info(f"✅ Updated {self.name}")
# Mark update time
self.last_update = datetime.now()
self._save_update_timestamp()
return True
except subprocess.CalledProcessError as e:
logger.error(f"Failed to update {self.name}: {e}")
if e.stderr:
logger.error(f"stderr: {e.stderr}")
return False
except Exception as e:
logger.error(f"Unexpected error updating {self.name}: {e}")
return False
def get_files(self) -> List[Path]:
"""Get all files matching include/exclude patterns"""
from pathlib import PurePosixPath
if not self.local_path.exists():
logger.warning(f"Content path does not exist: {self.local_path}")
return []
files = []
# Find all files matching include patterns
for pattern in self.include_paths:
# Use glob to match patterns
for file_path in self.local_path.glob(pattern):
if file_path.is_file():
# Get relative path for exclude checking
try:
rel_path = file_path.relative_to(self.local_path)
rel_posix = PurePosixPath(rel_path)
# Check against exclude patterns
excluded = False
for exclude_pattern in self.exclude_paths:
if rel_posix.match(exclude_pattern):
excluded = True
break
if not excluded:
files.append(file_path)
except ValueError:
# Path is not relative to local_path
pass
logger.info(f"Found {len(files)} files in {self.name}")
return files
def to_dict(self) -> Dict:
"""Serialize to dictionary"""
return {
"name": self.name,
"url": self.url,
"branch": self.branch,
"file_count": len(self.get_files()) if self.local_path.exists() else 0,
"last_update": self.last_update.isoformat() if self.last_update else None,
"priority": self.priority
}
class ContentManager:
"""Manages all content sources for HelpfulBatBot"""
def __init__(self, config_path: str = "content_sources.yaml"):
self.config_path = Path(config_path)
self.sources: List[ContentSource] = []
# Load configuration
if self.config_path.exists():
self._load_config()
else:
logger.warning(f"Config file not found: {config_path}")
logger.warning("Content manager initialized with no sources")
def _load_config(self):
"""Load content sources from YAML configuration"""
try:
with open(self.config_path) as f:
config = yaml.safe_load(f)
if not config or "content_sources" not in config:
logger.warning("No content sources defined in config")
return
for source_config in config["content_sources"]:
try:
source = ContentSource(source_config)
self.sources.append(source)
logger.info(f"Loaded content source: {source.name}")
except Exception as e:
logger.error(f"Failed to load source {source_config.get('name')}: {e}")
except Exception as e:
logger.error(f"Failed to load config from {self.config_path}: {e}")
def update_all(self, force: bool = False) -> bool:
"""
Update all content sources that need updating.
Args:
force: Force update even if not needed by frequency
Returns:
True if all updates succeeded, False if any failed
"""
if not self.sources:
logger.warning("No content sources configured")
return False
success = True
updated_count = 0
for source in self.sources:
if force or source.needs_update():
if source.clone_or_pull():
updated_count += 1
else:
success = False
if updated_count > 0:
logger.info(f"Updated {updated_count}/{len(self.sources)} content sources")
else:
logger.info("All content sources up to date")
return success
def get_all_files(self) -> List[Tuple[Path, str, float, str]]:
"""
Get all files from all sources.
Returns:
List of (file_path, source_name, priority, source_label) tuples
"""
all_files = []
for source in self.sources:
for file_path in source.get_files():
all_files.append((
file_path,
source.name,
source.priority,
source.source_label
))
logger.info(f"Total files from all sources: {len(all_files)}")
return all_files
def get_stats(self) -> Dict:
"""Get statistics about content sources"""
return {
"source_count": len(self.sources),
"sources": [source.to_dict() for source in self.sources],
"total_files": len(self.get_all_files())
}
def extract_notebook_text(notebook_path: Path) -> str:
"""
Extract text content from Jupyter notebook (.ipynb) file.
Args:
notebook_path: Path to .ipynb file
Returns:
Extracted text content
"""
try:
with open(notebook_path, 'r', encoding='utf-8') as f:
notebook = json.load(f)
text_parts = []
text_parts.append(f"# Jupyter Notebook: {notebook_path.name}\n")
for i, cell in enumerate(notebook.get('cells', []), 1):
cell_type = cell.get('cell_type')
source = cell.get('source', [])
# Convert source to string
if isinstance(source, list):
content = ''.join(source)
else:
content = source
if cell_type == 'markdown':
text_parts.append(f"## Cell {i} (Markdown)\n{content}\n")
elif cell_type == 'code':
text_parts.append(f"## Cell {i} (Code)\n```python\n{content}\n```\n")
return '\n\n'.join(text_parts)
except Exception as e:
logger.error(f"Failed to extract notebook text from {notebook_path}: {e}")
return ""
def load_files_from_sources(config_path: str = "content_sources.yaml") -> List[Tuple[str, str, Dict]]:
"""
Load all files from configured content sources.
Args:
config_path: Path to content_sources.yaml
Returns:
List of (file_path_str, content, metadata) tuples
"""
content_manager = ContentManager(config_path)
# Update content if needed
logger.info("Checking for content updates...")
content_manager.update_all()
# Get all files
files = content_manager.get_all_files()
logger.info(f"Loading {len(files)} files for indexing")
documents = []
for file_path, source_name, priority, source_label in files:
try:
# Read file content
if file_path.suffix.lower() == '.ipynb':
content = extract_notebook_text(file_path)
else:
content = file_path.read_text(encoding="utf-8", errors="ignore")
# Skip empty files
if not content.strip():
logger.warning(f"Skipping empty file: {file_path}")
continue
# Create metadata
metadata = {
"file": file_path.name,
"full_path": str(file_path),
"source": source_name,
"source_label": source_label,
"priority": priority,
"last_modified": file_path.stat().st_mtime
}
documents.append((str(file_path), content, metadata))
except Exception as e:
logger.error(f"Failed to load file {file_path}: {e}")
logger.info(f"Successfully loaded {len(documents)} documents")
return documents