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

Class WatchManager

source code


Provide operations for watching files and directories. Integrated dictionary is used to reference watched items.

Instance Methods [hide private]
 
__init__(self, exclude_filter=<function <lambda> at 0x2acf4efb4d70>)
Initialization: init inotify, init watch manager dictionary.
source code
 
__add_watch(self, path, mask, proc_fun, auto_add)
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 given path(s) with the specified mask and optionnally with a processing function and recursive flag.
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(s).
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 None is returned.
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
See 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

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

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

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

source code 

Initialization: init inotify, init watch manager dictionary. Raise OSError if initialization fails.

Parameters:
  • exclude_filter (bool) - 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__

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

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 given path(s) with the specified mask and optionnally with a processing function and recursive flag.

Parameters:
  • path (string or list of string) - 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).
  • auto_add (bool) - Automatically add watches on newly created directories in the watch's path.
  • do_glob (bool) - Do globbing on pathname.
  • quiet (bool) - if True raise an WatchManagerError exception on error. See example not_quiet.py
  • exclude_filter (bool) - boolean function, returns True if current path must be excluded from being watched. Has precedence on exclude_filter defined into __init__.
Returns: dict of {str: int}
dict of paths associated to watch descriptors. A wd value is positive if the watch has been sucessfully added, otherwise the value is negative. If the path is invalid it will be not included into this dict.

__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(s). Both the mask and the processing object can be modified.

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) - Recursively update watches on every already watched subdirectories and subfiles.
  • auto_add (bool) - Automatically add watches on newly created directories in the watch's path.
  • quiet (bool) - if True raise an 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 has an prohibitive cost, always prefer to keep the WD. If path is unknown None is returned.

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 None is returned.

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 True raise an 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: See add_watch().
See add_watch().

Attention: Under the call to this function it will be impossible 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 actually be wrong to make these two successive calls: wm.watch_transient_file('/var/run/foo.pid', ...) and wm.add_watch('/var/run/', ...)