python event start from file funcion

Solutions on MaxInterview for python event start from file funcion by the best coders in the world

showing results for - "python event start from file funcion"
Roberto
17 Nov 2020
1from watchdog.observers import Observer
2from watchdog.events import FileSystemEventHandler
3
4class WatchPendingHandler(FileSystemEventHandler):
5    ''' Run a handler for every file added to the pending dir
6
7    This class also handles what I call a bug in the watchdog module
8    which means that you can get more than one call per real event
9    in the watched dir tree.
10    '''
11
12    def __init__(self):
13        super(WatchPendingHandler, self).__init__()
14        # wip is used to avoid bug in watchdog which means multiple calls
15        # for one real event.
16        # For reference: https://github.com/gorakhargosh/watchdog/issues/346
17        self.wip = []
18
19    def on_created(self, event):
20        path = event.src_path
21        if event.is_directory:
22            logging.debug('WatchPendingHandler() New dir created in pending dir: {}'.format(path))
23            return
24        if path in self.wip:
25            logging.debug('WatchPendingHandler() Dup created event for %s', path)
26            return
27        self.wip.append(path)
28        logging.debug('WatchPendingHandler() New file created in pending dir: {}'.format(path))
29        HandleNewlyCreated(path)
30
31    def on_moved(self, event):
32        logging.debug('WatchPendingHandler() %s has been moved', event.src_path)
33        with contextlib.suppress(ValueError):
34            self.wip.remove(event.src_path)
35
36    def on_deleted(self, event):
37        path = event.src_path
38        logging.debug('WatchPendingHandler() %s has been deleted', path)
39        with contextlib.suppress(ValueError):
40            self.wip.remove(path)
41
42observer = Observer()
43observer.schedule(WatchPendingHandler(), DIR_PENDING, recursive=True)
44observer.start()
45logging.info('Watching %s', DIR_PENDING)
46try:
47    while True:
48        time.sleep(1)
49except KeyboardInterrupt:
50    observer.stop()
51observer.join()
52
similar questions
queries leading to this page
python event start from file funcion