121 lines
4.5 KiB
Python
121 lines
4.5 KiB
Python
import time
|
|
from watchdog.events import FileSystemEventHandler
|
|
from watchdog.observers import Observer
|
|
from watchdog.observers.read_directory_changes import \
|
|
WindowsApiEmitter, DEFAULT_OBSERVER_TIMEOUT, BaseObserver
|
|
|
|
class _Logger_ : pass
|
|
|
|
class WindowsApiEmitterExceptionHandling (WindowsApiEmitter, _Logger_) :
|
|
|
|
Exception = None
|
|
|
|
def run (self, * args, ** kw) :
|
|
try :
|
|
super ().run (* args, ** kw)
|
|
except Exception as err :
|
|
WindowsApiEmitterExceptionHandling.Exception = err
|
|
# end def run
|
|
|
|
# end class WindowsApiEmitterExceptionHandling
|
|
|
|
class WindowsApiObserverExcpetionHandler (Observer, _Logger_) :
|
|
|
|
def __init__ (self, timeout = DEFAULT_OBSERVER_TIMEOUT) :
|
|
BaseObserver.__init__ \
|
|
( self
|
|
, emitter_class = WindowsApiEmitterExceptionHandling
|
|
, timeout = timeout
|
|
)
|
|
# end def __init__
|
|
|
|
@classmethod
|
|
def Start_Directory_Watcher (cls, handler, ** kw) :
|
|
L = cls.L
|
|
retry_wait = kw.pop ("RETRY_SLEEP", 1)
|
|
while True :
|
|
try :
|
|
observer = WindowsApiObserverExcpetionHandler ()
|
|
observer.schedule (handler, handler.directory, ** kw)
|
|
observer.start ()
|
|
L.info ("Directory watching for %s started", handler.directory)
|
|
return observer
|
|
except Exception as e :
|
|
if getattr (e, "errno", None) == 2 :
|
|
L.error ("%s does not exists, retry", handler.directory)
|
|
observer.stop ()
|
|
time.sleep (retry_wait)
|
|
else :
|
|
raise
|
|
# end def Start_Directory_Watcher
|
|
|
|
# end class WindowsApiObserverExcpetionHandler
|
|
|
|
class Directory_Handler (FileSystemEventHandler, _Logger_) :
|
|
|
|
def __init__ (self, directory) :
|
|
super ().__init__ ()
|
|
self.directory = directory
|
|
# end def __init__
|
|
|
|
def on_any_event (self, event):
|
|
self.L.debug ("%s %s", event.event_type, event.src_path)
|
|
if event.event_type == 'deleted' and event.src_path == self.directory :
|
|
WindowsApiEmitterExceptionHandling.Exception = \
|
|
f"Directory has been deleted {self.directory}"
|
|
#if event.event_type == 'created' :
|
|
# try:
|
|
# fileBase = os.path.basename(event.src_path).split('.')[0]
|
|
# filePath = os.path.dirname(event.src_path) (fileBase,filePath))
|
|
# except Exception as e:
|
|
# pass
|
|
|
|
# if event.is_directory:
|
|
# return None
|
|
#
|
|
# elif event.event_type == 'created':
|
|
# # Take any action here when a file is first created.
|
|
# print(datetime.datetime.now())
|
|
# print("Received created event - %s." % event.src_path)
|
|
#
|
|
# elif event.event_type == 'modified':
|
|
# # Taken any action here when a file is modified.
|
|
# print(datetime.datetime.now())
|
|
# print("Received modified event - %s." % event.src_path)
|
|
#
|
|
# else:
|
|
# print(event.event_type,event.src_path)
|
|
# end def on_any_event
|
|
|
|
# end class Directory_Handler
|
|
|
|
if __name__ == "__main__" :
|
|
from App_State import App_State
|
|
import time
|
|
import os
|
|
|
|
cmd = App_State.Add_Logging_Attributes ().parse_args ()
|
|
L = _Logger_.L = App_State.Setup_Logging (cmd)
|
|
L.info ("PID_ %d", os.getpid ())
|
|
def start_watching (path) :
|
|
return WindowsApiObserverExcpetionHandler.Start_Directory_Watcher \
|
|
(Directory_Handler, path)
|
|
# end def start_watching
|
|
|
|
path = r"k:\glueck\watch-me"
|
|
observer = None
|
|
try:
|
|
observer = start_watching (path)
|
|
while True:
|
|
time.sleep (1)
|
|
if WindowsApiEmitterExceptionHandling.Exception :
|
|
App_State.L.exception \
|
|
(str (WindowsApiEmitterExceptionHandling.Exception))
|
|
WindowsApiEmitterExceptionHandling.Exception = None
|
|
if observer :
|
|
observer.stop ()
|
|
observer = start_watching (path)
|
|
finally :
|
|
if observer :
|
|
observer.stop ()
|
|
observer.join () |