-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchdog-publisher.py
executable file
·70 lines (56 loc) · 2.06 KB
/
watchdog-publisher.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
67
68
69
#!/usr/bin/python3
# -*-coding:Utf-8 -*
import configparser
import sys
import os.path
import datetime
from os import listdir
from os.path import isfile, join
import pika
import time
import yaml
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
with open("config.yml", 'r') as ymlfile:
cfg = yaml.load(ymlfile)
class Watcher:
DIRECTORY_TO_WATCH = cfg['rbmq']['path']
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(5)
except:
self.observer.stop()
print("Error")
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
return None
elif event.event_type == 'created':
# Take any action here when a file is first created.
print("Received created event " + event.src_path)
print(event.src_path)
print(type(event.src_path))
if event.src_path.endswith('part'):
print("[x] We have a new part file")
else:
credential_params = pika.PlainCredentials(cfg['rbmq']['user'],cfg['rbmq']['passwd'])
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost',credentials=credential_params))
channel = connection.channel()
channel.queue_declare(queue=cfg['rbmq']['queue']) # Declare a queue
channel.basic_publish(exchange='', routing_key=cfg['rbmq']['queue'], body=event.src_path)
print("[x] We have a new file")
connection.close()
elif event.event_type == 'modified':
# Taken any action here when a file is modified.
print("Received modified event " + event.src_path)
if __name__ == '__main__':
w = Watcher()
w.run()