-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.py
executable file
·61 lines (47 loc) · 1.64 KB
/
watcher.py
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
#!/bin/python
import time
from libqtile.command.client import InteractiveCommandClient
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
c = InteractiveCommandClient()
class Watcher(Observer):
DIRECTORY_TO_WATCH = "/home/ervin/.config/qtile/"
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(1)
except Exception:
self.observer.stop()
print("RuntimeError")
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
null_ls_condition = event.src_path.split("/")[-1].startswith(".null-ls")
cpython_condition = "cpython" in event.src_path.split("/")[-1]
if event.is_directory:
return None
elif (
event.event_type == "created"
and not null_ls_condition
and not cpython_condition
):
# Take any action here when a file is first created.
print(f"Received created event - {event.src_path}.")
c.reload_config()
elif (
event.event_type == "modified"
and not null_ls_condition
and not cpython_condition
):
# Taken any action here when a file is modified.
print(f"Received modified event - {event.src_path}.")
c.reload_config()
if __name__ == "__main__":
w = Watcher()
w.run()