Repository navigation
Expand file tree
/
Copy pathwatcher.py
More file actions
52 lines (43 loc) · 1.48 KB
/
Copy pathwatcher.py
File metadata and controls
52 lines (43 loc) · 1.48 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
from logging import getLogger
from pathlib import Path
from typing import Any, Callable
from gitignore_parser import parse_gitignore
from watchdog.events import FileSystemEvent
logger = getLogger("taskiq.worker")
class FileWatcher: # pragma: no cover
"""Filewatcher that watches for filesystem changes."""
def __init__(
self,
callback: Callable[..., None],
use_gitignore: bool = True,
**callback_kwargs: Any,
) -> None:
self.callback = callback
self.gitignore = None
gpath = Path("./.gitignore")
if use_gitignore and gpath.exists():
self.gitignore = parse_gitignore(gpath)
self.callback_kwargs = callback_kwargs
def dispatch(self, event: FileSystemEvent) -> None:
"""
React to event.
This function checks wether we need to
react to event and calls callback if we do.
:param event: incoming fs event.
"""
if event.is_directory:
return
if event.event_type in {"opened", "closed"}:
return
if ".git" in event.src_path:
return
try:
if self.gitignore and self.gitignore(event.src_path):
return
except Exception as exc:
logger.info(
f"Cannot check path `{event.src_path}` in gitignore. Cause: {exc}",
)
return
logger.debug(f"File changed. Event: {event}")
self.callback(**self.callback_kwargs)