Manages the file system observer thread. It can watch multiple directories, assigning a separate `RepositoryEventHandler` to each one.
| 314 | |
| 315 | |
| 316 | class CodeWatcher: |
| 317 | """ |
| 318 | Manages the file system observer thread. It can watch multiple directories, |
| 319 | assigning a separate `RepositoryEventHandler` to each one. |
| 320 | """ |
| 321 | def __init__(self, graph_builder: "GraphBuilder", job_manager= "JobManager"): |
| 322 | self.graph_builder = graph_builder |
| 323 | self.observer = Observer() |
| 324 | self.watched_paths = set() # Keep track of paths already being watched. |
| 325 | self.watches = {} # Store watch objects to allow unscheduling |
| 326 | |
| 327 | def watch_directory(self, path: str, perform_initial_scan: bool = True, cgcignore_path: str = None): |
| 328 | """Schedules a directory to be watched for changes.""" |
| 329 | path_obj = Path(path).resolve() |
| 330 | path_str = str(path_obj) |
| 331 | |
| 332 | if path_str in self.watched_paths: |
| 333 | info_logger(f"Path already being watched: {path_str}") |
| 334 | return {"message": f"Path already being watched: {path_str}"} |
| 335 | |
| 336 | # Create a new, dedicated event handler for this specific repository path. |
| 337 | event_handler = RepositoryEventHandler( |
| 338 | self.graph_builder, |
| 339 | path_obj, |
| 340 | perform_initial_scan=perform_initial_scan, |
| 341 | cgcignore_path=cgcignore_path, |
| 342 | ) |
| 343 | |
| 344 | watch = self.observer.schedule(event_handler, path_str, recursive=True) |
| 345 | self.watches[path_str] = watch |
| 346 | self.watched_paths.add(path_str) |
| 347 | info_logger(f"Started watching for code changes in: {path_str}") |
| 348 | |
| 349 | return {"message": f"Started watching {path_str}."} |
| 350 | def unwatch_directory(self, path: str): |
| 351 | """Stops watching a directory for changes.""" |
| 352 | path_obj = Path(path).resolve() |
| 353 | path_str = str(path_obj) |
| 354 | |
| 355 | if path_str not in self.watched_paths: |
| 356 | warning_logger(f"Attempted to unwatch a path that is not being watched: {path_str}") |
| 357 | return {"error": f"Path not currently being watched: {path_str}"} |
| 358 | |
| 359 | watch = self.watches.pop(path_str, None) |
| 360 | if watch: |
| 361 | self.observer.unschedule(watch) |
| 362 | |
| 363 | self.watched_paths.discard(path_str) |
| 364 | info_logger(f"Stopped watching for code changes in: {path_str}") |
| 365 | return {"message": f"Stopped watching {path_str}."} |
| 366 | |
| 367 | def list_watched_paths(self) -> list: |
| 368 | """Returns a list of all currently watched directory paths.""" |
| 369 | return list(self.watched_paths) |
| 370 | |
| 371 | def start(self): |
| 372 | """Starts the observer thread.""" |
| 373 | if not self.observer.is_alive(): |
no outgoing calls
no test coverage detected