Module pyinotify :: Class WatchManager
[hide private]
[frames] | no frames]

Class WatchManager

source code


Provide operations for watching files and directories. Its internal dictionary is used to reference watched items. When used inside threaded code, one must instanciate as many WatchManager instances as there are ThreadedNotifier instances.

Instance Methods [hide private]
 
__init__(self, exclude_filter=<function <lambda> at 0x7f64fff3ede8>)
Initialization: init inotify, init watch manager dictionary.
source code
 
close(self)
Close inotify's file descriptor, this action will also automatically remove (i.e.
source code
int
get_fd(self)
Return assigned inotify's file descriptor.
source code
 
get_watch(self, wd)
Get watch from provided watch descriptor wd.
source code
 
del_watch(self, wd)
Remove watch entry associated to watch descriptor wd.
source code
 
__format_path(self, path)
Format path to its internal (stored in watch manager) representation.
source code
 
__add_watch(self, path, mask, proc_fun, auto_add, exclude_filter)
Add a watch on path, build a Watch object and insert it in the watch manager dictionary.
source code
 
__glob(self, path, do_glob) source code
dict of {str: int}
add_watch(self, path, mask, proc_fun=None, rec=False, auto_add=False, do_glob=False, quiet=True, exclude_filter=None)
Add watch(s) on the provided |path|(s) with associated |mask| flag value and optionally with a processing |proc_fun| function and recursive flag |rec| set to True.
source code
list of int
__get_sub_rec(self, lpath)
Get every wd from self._wmd if its path is under the path of one (at least) of those in lpath.
source code
dict of {int: bool}
update_watch(self, wd, mask=None, proc_fun=None, rec=False, auto_add=False, quiet=True)
Update existing watch descriptors |wd|.
source code
list of type(param)
__format_param(self, param)
Returns: wrap param.
source code
int or None
get_wd(self, path)
Returns the watch descriptor associated to path.
source code
string or None
get_path(self, wd)
Returns the path associated to WD, if WD is unknown it returns None.
source code
string
__walk_rec(self, top, rec)
Yields each subdirectories of top, doesn't follow symlinks.
source code
dict of {int: bool}
rm_watch(self, wd, rec=False, quiet=True)
Removes watch(s).
source code
Same as add_watch().
watch_transient_file(self, filename, mask, proc_class)
Watch a transient file, which will be created and deleted frequently over time (e.g.
source code
 
get_ignore_events(self) source code
 
set_ignore_events(self, nval) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]
dict watches
Get a reference on the internal watch manager dictionary.
  ignore_events

Inherited from object: __class__

Method Details [hide private]

__init__(self, exclude_filter=<function <lambda> at 0x7f64fff3ede8>)
(Constructor)

source code 

Initialization: init inotify, init watch manager dictionary. Raise OSError if initialization fails, raise InotifyBindingNotFoundError if no inotify binding was found (through ctypes or from direct access to syscalls).

Parameters:
  • exclude_filter (callable object) - boolean function, returns True if current path must be excluded from being watched. Convenient for providing a common exclusion filter for every call to add_watch.
Overrides: object.__init__

close(self)

source code 

Close inotify's file descriptor, this action will also automatically remove (i.e. stop watching) all its associated watch descriptors. After a call to this method the WatchManager's instance become useless and cannot be reused, a new instance must then be instanciated. It makes sense to call this method in few situations for instance if several independant WatchManager must be instanciated or if all watches must be removed and no other watches need to be added.

get_fd(self)

source code 

Return assigned inotify's file descriptor.

Returns: int
File descriptor.

get_watch(self, wd)

source code 

Get watch from provided watch descriptor wd.

Parameters:
  • wd (int) - Watch descriptor.

del_watch(self, wd)

source code 

Remove watch entry associated to watch descriptor wd.

Parameters:
  • wd (int) - Watch descriptor.

__add_watch(self, path, mask, proc_fun, auto_add, exclude_filter)

source code 

Add a watch on path, build a Watch object and insert it in the watch manager dictionary. Return the wd value.

add_watch(self, path, mask, proc_fun=None, rec=False, auto_add=False, do_glob=False, quiet=True, exclude_filter=None)

source code 

Add watch(s) on the provided |path|(s) with associated |mask| flag value and optionally with a processing |proc_fun| function and recursive flag |rec| set to True. Ideally |path| components should not be unicode objects. Note that although unicode paths are accepted there are converted to byte strings before a watch is put on that path. The encoding used for converting the unicode object is given by sys.getfilesystemencoding(). If |path| is already watched it is ignored, but if it is called with option rec=True a watch is put on each one of its not-watched subdirectory.

Parameters:
  • path (string or list of strings) - Path to watch, the path can either be a file or a directory. Also accepts a sequence (list) of paths.
  • mask (int) - Bitmask of events.
  • proc_fun (function or ProcessEvent instance or instance of one of its subclasses or callable object.) - Processing object.
  • rec (bool) - Recursively add watches from path on all its subdirectories, set to False by default (doesn't follows symlinks in any case).
  • auto_add (bool) - Automatically add watches on newly created directories in watched parent |path| directory. If |auto_add| is True, IN_CREATE is ored with |mask| when the watch is added.
  • do_glob (bool) - Do globbing on pathname (see standard globbing module for more informations).
  • quiet (bool) - if False raises a WatchManagerError exception on error. See example not_quiet.py.
  • exclude_filter (callable object) - predicate (boolean function), which returns True if the current path must be excluded from being watched. This argument has precedence over exclude_filter passed to the class' constructor.
Returns: dict of {str: int}
dict of paths associated to watch descriptors. A wd value is positive if the watch was added sucessfully, otherwise the value is negative. If the path was invalid or was already watched it is not included into this returned dictionary.

__get_sub_rec(self, lpath)

source code 

Get every wd from self._wmd if its path is under the path of one (at least) of those in lpath. Doesn't follow symlinks.

Parameters:
  • lpath (list of int) - list of watch descriptor
Returns: list of int
list of watch descriptor

update_watch(self, wd, mask=None, proc_fun=None, rec=False, auto_add=False, quiet=True)

source code 

Update existing watch descriptors |wd|. The |mask| value, the processing object |proc_fun|, the recursive param |rec| and the |auto_add| and |quiet| flags can all be updated.

Parameters:
  • wd (int or list of int) - Watch Descriptor to update. Also accepts a list of watch descriptors.
  • mask (int) - Optional new bitmask of events.
  • proc_fun (function or ProcessEvent instance or instance of one of its subclasses or callable object.) - Optional new processing function.
  • rec (bool) - Optionally adds watches recursively on all subdirectories contained into |wd| directory.
  • auto_add (bool) - Automatically adds watches on newly created directories in the watch's path corresponding to |wd|. If |auto_add| is True, IN_CREATE is ored with |mask| when the watch is updated.
  • quiet (bool) - If False raises a WatchManagerError exception on error. See example not_quiet.py
Returns: dict of {int: bool}
dict of watch descriptors associated to booleans values. True if the corresponding wd has been successfully updated, False otherwise.

__format_param(self, param)

source code 
Parameters:
  • param (string or int) - Parameter.
Returns: list of type(param)
wrap param.

get_wd(self, path)

source code 

Returns the watch descriptor associated to path. This method presents a prohibitive cost, always prefer to keep the WD returned by add_watch(). If the path is unknown it returns None.

Parameters:
  • path (str) - Path.
Returns: int or None
WD or None.

get_path(self, wd)

source code 

Returns the path associated to WD, if WD is unknown it returns None.

Parameters:
  • wd (int) - Watch descriptor.
Returns: string or None
Path or None.

__walk_rec(self, top, rec)

source code 

Yields each subdirectories of top, doesn't follow symlinks. If rec is false, only yield top.

Parameters:
  • top (string) - root directory.
  • rec (bool) - recursive flag.
Returns: string
path of one subdirectory.

rm_watch(self, wd, rec=False, quiet=True)

source code 

Removes watch(s).

Parameters:
  • wd (int or list of int.) - Watch Descriptor of the file or directory to unwatch. Also accepts a list of WDs.
  • rec (bool) - Recursively removes watches on every already watched subdirectories and subfiles.
  • quiet (bool) - If False raises a WatchManagerError exception on error. See example not_quiet.py
Returns: dict of {int: bool}
dict of watch descriptors associated to booleans values. True if the corresponding wd has been successfully removed, False otherwise.

watch_transient_file(self, filename, mask, proc_class)

source code 

Watch a transient file, which will be created and deleted frequently over time (e.g. pid file).

Parameters:
  • filename (string) - Filename.
  • mask (int) - Bitmask of events, should contain IN_CREATE and IN_DELETE.
  • proc_class (ProcessEvent's instance or of one of its subclasses.) - ProcessEvent (or of one of its subclass), beware of accepting a ProcessEvent's instance as argument into __init__, see transient_file.py example for more details.
Returns: Same as add_watch().
Same as add_watch().

Attention: Currently under the call to this function it is not possible to correctly watch the events triggered into the same base directory than the directory where is located this watched transient file. For instance it would be wrong to make these two successive calls: wm.watch_transient_file('/var/run/foo.pid', ...) and wm.add_watch('/var/run/', ...)


Property Details [hide private]

watches

Get a reference on the internal watch manager dictionary.

Get Method:
unreachable.watches(self) - Get a reference on the internal watch manager dictionary.
Type:
dict

ignore_events

Get Method:
get_ignore_events(self)
Set Method:
set_ignore_events(self, nval)
Delete Method:
'Make watch manager ignoring new events.'