OpenVoiceOS / ovos-utils

Apache License 2.0
3 stars 8 forks source link

rework of `FileEventHandler` to handle content change below given path #108

Open emphasize opened 1 year ago

emphasize commented 1 year ago

i'd like to widen the scope of #54

FileWatcher class could be (re)used to monitor filechanges below a path with a slight addition.

Proposal:

class FileEventHandler(FileSystemEventHandler):
    def __init__(self, file_path, callback, ignore_creation=False):
        super().__init__()
        self._callback = callback
        self._file_path = file_path
        self._debounce = 1
        self._last_update = 0
        if ignore_creation:
            self._events = ('modified')
        else:
            self._events = ('created', 'deleted', 'modified')                         # add: also monitor deletions

    def on_any_event(self, event):
        if event.is_directory:
            return
        elif event.event_type in self._events:
            if isdir(self._file_path) or event.src_path == self._file_path:           # add: check if isdir
                if time.time() - self._last_update >= self._debounce:
                    self._callback(event.src_path)
                    self._last_update = time.time()

With this we can monitor either specifically a file if a file is passed or a directory (+subdirectories) otherwise

emphasize commented 1 year ago

Specifically with skill-ovos-filebrowser in mind, to tigger an event (re)creating a database of useable content (+metadata) This should make OCP integration easier/quicker.