Watchers

Watchers implement methods of determining if a file has changed.

All watchers inherit from the base Watcher class. This allows the derived class to focus on performing the change detection rather than having to implement core functionality.

The Base Class

The Watcher base class is responsible for providing the high level interface to a watcher, the actual functionality is left to the derived class.

The watchers are typically created and managed by an instance of a WatcherManager class.

Warning

This class cannot be instantiated directly, it is an abstract base class. Only derived classes that inherit from this class and implement _get_value() can be instantiated.

class watch_do.watchers.watcher.Watcher(file_name)[source]

This is the base Watcher that all other watchers should inherit from.

A file name is passed in that will be monitored for changes.

Note

The file state is only checked when the has_changed() method is called.

Initialise the Watcher.

Parameters:file_name (str) – The file path that the watcher should detect changes for.
_get_value()[source]

Get the current value of the watched file.

Attention

This method should be overwritten and implemented in child classes.

This method determines the current change value of the file being watched. This could be the file’s hash, the modified time, or some other value that can be used to determine if we should report the file as changed on the next call to has_changed().

Returns:A value representing the current state of the object that this base class can use to determine if the file has changed.
Return type:str
file_name

Get the name and path of the file that this watcher is monitoring.

has_changed()[source]

Determine if the file has changed since the last call to this method.

Warning

The first call to this method will always return False.

Returns:A boolean, indicating if the watched file has changed.
Return type:bool

Built-In Watchers

These are the built-in watchers that were available for use at the time this documentation was built.

Note

The watchers below all inherit from the above Watcher class. This means that all methods and properites detailed above are also available on these classes below even though they aren’t mentioned.

class watch_do.watchers.MD5(file_name)[source]

MD5 hash based change detection.

This class uses MD5 hashes based on the files contents to enable change detection.

Initialise the Watcher.

Parameters:file_name (str) – The file path that the watcher should detect changes for.
class watch_do.watchers.ModificationTime(file_name)[source]

A modification time based watcher.

This class uses the files modification time to enable change detection.

Initialise the Watcher.

Parameters:file_name (str) – The file path that the watcher should detect changes for.