Doers

Doers implement interfaces that enable the performing of actions.

All doers inherit from the base Doer class. This allows the derived class to focus on actually executing the action rather than having to implement core functionality.

The Base Class

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

The doers are typically created and managed by an instance of a DoerManager class.

Warning

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

class watch_do.doers.doer.Doer(command)[source]

This is the base Doer that all other doers should inherit from.

A command is passed in that will determine the action that should be performed.

Initialise the Doer.

Parameters:command (str) – The command that details what action should be performed.
static _interpolate_file_name(string, file_name)[source]

Interpolate the file_name into a given string.

The string parameter will be searched for %f and replaced with file_name. Any escaped %f‘s will be unescaped and ignored (i.e. \%f becomes %f).

Parameters:
  • string (str) – The string to interpolate the file_name into.
  • file_name (str) – The file name to insert into the string.
Returns:

The input string with file name interpolated.

Return type:

str

command

Get the command this doer is performing.

run(file_name)[source]

Run the doer against a specific file.

This method runs the command passed into the constructor against a specific file.

Parameters:

file_name (str) – The file name to run this doer against.

Yields:

str

A string containing the output (possibly the partial output)

of the command, both stdout and stderr.

Built-In Doers

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

Note

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

The Shell class provides a method to run shell commands and capture their output.

As an example, the following code would provide a method of getting the output from listing a specific files attributes on the command line.

>>> doer = Shell('ls -lh "%f"')

To actually run and retrieve the output of this command, the run() method should be called.

>>> doer.run('myfile.txt')
class watch_do.doers.shell.Shell(command)[source]

Interface with a shell to allow running standard shell commands.

This doer enables commands to be run in a shell and have the output captured.

Initialise the Doer.

Parameters:command (str) – The command that details what action should be performed.
run(file_name)[source]

Run the command in the shell.

The _interpolate_file_name() is called on the command with file_name as a parameter to ensure this file_name is interpolated if it’s required.

Parameters:

file_name (str) – The file_name that this doer should run against.

Yields:

str

A string containing the output (possibly the partial output)

of the command, both stdout and stderr.