pollinotify is a simple extension module that wraps the Linux inotify service, providing a way to watch for specific kinds of file events happening to specified files or in specified directories. Its major selling points are that:
1) It is polled, so instead of having to set up a background thread, or attach a separate event-watching package, the programmer calls it with an optional timeout, so it reports event that have already occurred, or waits for at most the specified time, or until an event occurs; 2) The polling can wait for specific kinds of event.
As the module depends on the inotify service, it is only available on Linux.
A number of constants representing common Linux file system event codes, with friendly names:
Constant Name | Linux Name | Interpretation |
---|---|---|
Access | IN_ACCESS | Object accessed |
Modify | IN_MODIFY | Object modified |
Attributes | IN_ATTRIB | Object attributes modified |
Open | IN_OPEN | File opened |
CloseWrite | IN_CLOSE_WRITE | File closed after contents changed |
CloseOther | IN_CLOSE_NOWRITE | File closed without contents changed |
Close | IN_CLOSE | File closed |
MoveFrom | IN_MOVED_FROM | Object moved from location |
MoveTo | IN_MOVED_TO | Object moved to location |
Move | IN_MOVE | Object moved |
MoveSelf | IN_MOVE_SELF | Object being monitored is deleted |
Create | IN_CREATE | Object created |
Delete | IN_DELETE | Object deleted |
DeleteSelf | IN_DELETE_SELF | Object being monitored is deleted |
Ignored | IN_IGNORED | Ignored |
DirEvent | IN_ISDIR | The monitored object to which the event occurred is a directory |
AllEvents | IN_ALL_EVENTS | Any event |
The constants are set up so they can be or-ed together to create meaningful combinations of event types.
FileEvent represents an event detected by the inotify service. The event information is held in the attributes:
In addition, if e is a FileEvent object then
str(e) = maskAsString(e.mask) len(e) = len(e.decode()) x in e = e.matches(x)
The fundamental class of the module. It connects to the system inotify service and uses it to poll for events in one or more specified file system paths. Polling is based on a timeout, and so can be non-blocking.
It has the following methods:
If w is a Watcher instance then
len(w) = w.nEvents() iter(w) = iter(w.events())
A simple example that polls with a timeout of 1 second and lists those events corresponding to file creation or modification (including touch) in the user's home directory.
import pollinotify n=pollinotify.Watcher() n.addPath('~') while True: got=n.poll(timeout=1000) if got: events=n.events(match=notify.CloseWrite) print 'Got %s events' % len(events) for event in events: print event.path,event.mask,str(event)