inotify_simple
inotify_simple copied to clipboard
RFE: Add option to watch only a type of extension or a regex matching
When there's an active directory in which we only care about a few files, the events can grow a lot, where most of them might not even be related to the files/extensions we care about.
Example directory structure:
folder/
1.txt
2.txt
3.chatty
4.chatty
5.chatty
6.chatty
7.chatty
8.chatty
In the above if we say only cared for events on *.txt, but the files on *.chatty are constantly updated, it'll lead to a lot of events, if we could however be able to do this:
wd = inotify.add_watch('folder/*.txt', watch_flags)
then it'd be a bit more optimal and helpful.
I understand there's a workaround where i put *.txt files in a separate folder and then watch them. But not in every case can we control directory structure so was hoping if this is possible.
The equivalent inotify-tools flag would be --include
You could do something like:
wds = [inotify.add_watch(name) for name in glob.glob('folder/*.txt')]
to watch the files you are interested in instead of the parent directory. I suppose you would need to retain a mapping of watch descriptors to filenames, since the events won't include the filenames, so maybe more like:
names_by_wd = {inotify.add_watch(name): name for name in glob.glob('folder/*.txt')}
That won't give events for any files ending in .txt
that are created after the watches are added, for that you would indeed need to watch the parent directory. But you could watch it only for file creation events and not file modified events to avoid all the spam on the files you're not interested in.
Since the glob module is available in the stdlib and any functionality I would add would simply be wrapping something like the above, I think users of inotify_simple
should probably use the glob
library directly rather than it being used by inotify_simple
.