OpenVoiceOS / OpenVoiceOS/ovos-utils

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

Open
#108 1 comment 0 reactions 2 assignees View on GitHub

@NeonDaniel is already working on this.

Since Mar 16, 2023.

enhancement
Dominant language
Python
Stars
6
Forks
13
Avg merge
1d 8m
Merged PRs (30d)
8

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.