python event start from file funcion
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class WatchPendingHandler(FileSystemEventHandler):
''' Run a handler for every file added to the pending dir
This class also handles what I call a bug in the watchdog module
which means that you can get more than one call per real event
in the watched dir tree.
'''
def __init__(self):
super(WatchPendingHandler, self).__init__()
# wip is used to avoid bug in watchdog which means multiple calls
# for one real event.
# For reference: https://github.com/gorakhargosh/watchdog/issues/346
self.wip = []
def on_created(self, event):
path = event.src_path
if event.is_directory:
logging.debug('WatchPendingHandler() New dir created in pending dir: {}'.format(path))
return
if path in self.wip:
logging.debug('WatchPendingHandler() Dup created event for %s', path)
return
self.wip.append(path)
logging.debug('WatchPendingHandler() New file created in pending dir: {}'.format(path))
HandleNewlyCreated(path)
def on_moved(self, event):
logging.debug('WatchPendingHandler() %s has been moved', event.src_path)
with contextlib.suppress(ValueError):
self.wip.remove(event.src_path)
def on_deleted(self, event):
path = event.src_path
logging.debug('WatchPendingHandler() %s has been deleted', path)
with contextlib.suppress(ValueError):
self.wip.remove(path)
observer = Observer()
observer.schedule(WatchPendingHandler(), DIR_PENDING, recursive=True)
observer.start()
logging.info('Watching %s', DIR_PENDING)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()