| 319 | |
| 320 | |
| 321 | class CodeWatcher: |
| 322 | def __init__( |
| 323 | self, |
| 324 | graph_builder: "GraphBuilder", |
| 325 | job_manager="JobManager", |
| 326 | use_polling: typing.Optional[bool] = None, |
| 327 | ): |
| 328 | self.graph_builder = graph_builder |
| 329 | observer_cls = PollingObserver if should_use_polling_observer(use_polling) else Observer |
| 330 | self.observer = observer_cls() |
| 331 | |
| 332 | self.watched_paths = set() |
| 333 | self.watches = {} |
| 334 | self.handlers = {} |
| 335 | |
| 336 | def watch_directory( |
| 337 | self, |
| 338 | path: str, |
| 339 | perform_initial_scan: bool = True, |
| 340 | cgcignore_path: str = None, |
| 341 | sync_on_start: bool = False, |
| 342 | ): |
| 343 | path_obj = Path(path).resolve() |
| 344 | path_str = str(path_obj) |
| 345 | |
| 346 | if path_str in self.watched_paths: |
| 347 | return {"message": "Already watching"} |
| 348 | |
| 349 | handler = RepositoryEventHandler( |
| 350 | self.graph_builder, |
| 351 | path_obj, |
| 352 | perform_initial_scan=perform_initial_scan, |
| 353 | sync_on_start=sync_on_start, |
| 354 | cgcignore_path=cgcignore_path, |
| 355 | ) |
| 356 | |
| 357 | watch = self.observer.schedule(handler, path_str, recursive=True) |
| 358 | |
| 359 | self.watches[path_str] = watch |
| 360 | self.handlers[path_str] = handler |
| 361 | self.watched_paths.add(path_str) |
| 362 | |
| 363 | return {"message": f"Watching {path_str}"} |
| 364 | |
| 365 | def unwatch_directory(self, path: str): |
| 366 | path_str = str(Path(path).resolve()) |
| 367 | |
| 368 | handler = self.handlers.pop(path_str, None) |
| 369 | if handler: |
| 370 | handler.cancel_timers() |
| 371 | |
| 372 | watch = self.watches.pop(path_str, None) |
| 373 | if watch: |
| 374 | self.observer.unschedule(watch) |
| 375 | |
| 376 | self.watched_paths.discard(path_str) |
| 377 | |
| 378 | return {"message": f"Stopped watching {path_str}"} |
no outgoing calls
no test coverage detected