-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.py gui.py
66 lines (54 loc) · 1.74 KB
/
watcher.py gui.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
62
63
64
65
66
import time
import os
import subprocess
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import json
import platform
# Load the configuration data from the config.json file
with open("config.json", "r") as f:
data = json.load(f)
# Paths to watch (Downloads and Desktop folders)
WATCH_PATHS = data["watch_paths"]
class FileEventHandler(FileSystemEventHandler):
def on_created(self, event):
new_file_path = event.src_path
if self.is_valid_file(new_file_path):
print(f'New file created: {new_file_path}')
self.call_main(new_file_path)
def is_valid_file(self, file_path):
if os.path.exists(file_path):
if not file_path.endswith(('.tmp', '.crdownload')):
return True
else:
return False
else:
return True
def call_main(self, file_path):
subprocess.run(["python", "main.py", file_path])
def start_file_watcher(paths):
event_handler = FileEventHandler()
observer = Observer()
for path in paths:
observer.schedule(event_handler, path, recursive=True)
print(f'Watching folder: {path}')
os_name = platform.system()
if os_name == 'Windows':
# Handle Windows-specific functionality
pass
elif os_name == 'Darwin':
# Handle macOS-specific functionality
pass
elif os_name == 'Linux':
# Handle Linux-specific functionality
pass
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
print("Watching specified folders for new files...")
start_file_watcher(WATCH_PATHS)