-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_file_watcher.py
More file actions
51 lines (40 loc) · 1.46 KB
/
test_file_watcher.py
File metadata and controls
51 lines (40 loc) · 1.46 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
# Load dll filewatcher.dll
import ctypes
import os
import time
# Get the path of the dll file
dll_path = os.path.join(os.path.dirname(
os.path.abspath(__file__)), 'filewatcher.dll')
# Load the dll file
dll = ctypes.cdll.LoadLibrary(dll_path)
# void (*file_watcher_callback) (const char *file, file_watcher_event_type event, bool is_directory, void *data)
# Define the callback function type
callback_type = ctypes.CFUNCTYPE(
None, ctypes.c_char_p, ctypes.c_int, ctypes.c_bool, ctypes.c_void_p)
# void file_watcher_watch_async (const char* folder_path,
# bool b_recursive,
# file_watcher_callback callback,
# bool* bWatching,
# void* data);
# Define the function
dll.file_watcher_watch_async.argtypes = [
ctypes.c_char_p, ctypes.c_bool, callback_type, ctypes.POINTER(ctypes.c_bool), ctypes.c_void_p]
dll.file_watcher_watch_async.restype = None
def callback(file, event, is_directory, data):
print('Callback: file = {}, event = {}, is_directory = {}, data = {}'.format(
file, event, is_directory, data))
print('Start watching, press Ctrl+C to stop...')
# Call the function
folder_path = b'.'
b_recursive = True
bWatching = ctypes.c_bool()
callback_func = callback_type(callback)
dll.file_watcher_watch_async(folder_path, b_recursive,
callback_func, ctypes.byref(bWatching), None)
# Wait for Ctrl+C
try:
while True:
time.sleep(10)
print('Watching...')
except KeyboardInterrupt:
print('Stop watching...')