Package: pollinotify
1 Summarypollinotifyis a simple extension module that wraps the Linuxinotifyservice, 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 ispolled, 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 theinotifyservice,it is only available on Linux.2 Module Structure2.1 ConstantsA number of constants representing common Linux file system event codes, with friendly names:Constant NameLinux NameInterpretationAccessIN_ACCESSObject accessedModifyIN_MODIFYObject modifiedAttributesIN_ATTRIBObject attributes modifiedOpenIN_OPENFile openedCloseWriteIN_CLOSE_WRITEFile closed after contents changedCloseOtherIN_CLOSE_NOWRITEFile closed without contents changedCloseIN_CLOSEFile closedMoveFromIN_MOVED_FROMObject moved from locationMoveToIN_MOVED_TOObject moved to locationMoveIN_MOVEObject movedMoveSelfIN_MOVE_SELFObject being monitored is deletedCreateIN_CREATEObject createdDeleteIN_DELETEObject deletedDeleteSelfIN_DELETE_SELFObject being monitored is deletedIgnoredIN_IGNOREDIgnoredDirEventIN_ISDIRThe monitored object to which the event occurred is a directoryAllEventsIN_ALL_EVENTSAny event2.2 Utility functionsmaskAsString(mask)Takes a value equal to one or more of the event constantsor-ed together
and returns a readable string representation, consisting of a space-separated
list of the names of the matching events2.3FileEventclassFileEventrepresents an event detected by theinotifyservice.  The event information is held
in the attributes:self.pathThe path to the file or directory suffering the eventself.maskValue representing the event(s) it suffered, represented as anor-ed collection of event codes, one for
each kind of event that was detectedself.decode()Returns a list of the names of the event types to which the event correspondsself.matches(mask)ReturnsTrueif the argument is the code for one of the event types that the object represents,FalseotherwiseIn addition, ifeis aFileEventobject thenstr(e) = maskAsString(e.mask)
len(e) = len(e.decode())
x in e = e.matches(x)2.4WatcherclassThe fundamental class of the module.  It connects to the systeminotifyservice 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:__init__()No-args constructoraddPath(path,mode=AllEvents)Addspathto the list of directories to be polled for events; polling
will collect only events that match theor-ed event code mask
specified in the optional argumentmode(defaults to all events)poll(timeout=0)Polls for events occurring on the specified paths, returningTrueif any occur,Falseotherwise; times out
aftertimeoutmilliseconds, in which case it returnsFalseevents(match=AllEvents)Returns a list ofFileEventobjects, one for
each event detected during last polling session
that matches the specified optionalor-ed event code masknPaths()Returns the number of paths currently registered with the WatchernEvents()Returns the number of events detected in the last polling sessionIfwis aWatcherinstance thenlen(w)  = w.nEvents()
iter(w) = iter(w.events())3 EXAMPLEA simple example that polls with a timeout of 1 second and lists those events
corresponding to file creation or modification (includingtouch) 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{} events'.format(len(events))
        for event in events:
            print('{} : {} : {}'.format(event.path,event.mask,str(event)))