Skip to content

How to Write a Plugin

James Tauber edited this page Nov 12, 2019 · 1 revision

Firstly import Plugin from text_validator.base:

from text_validator.base import Plugin

Then write a subclass of Plugin that implements one or more of the following methods:

  • validate_line(self, filename, line_num, line) — called for every line
  • validate_first_line(self, filename, line_num, line) — called for just the first line
  • validate_last_line(self, filename, line_num, line) — called for just the last line

The filename and line_num parameters contain the filename and line number.

The line parameter contains the line to validate. Note that this is passed in as bytes so any string-based validation will need to decode it.

Any configuration parameters for the plugin from the TOML file are available on self.config. For example:

if self.config.get("CHECK_TABS"):
    ...

To report an error on the line, call self.error_callback(filename, line_num, offset, message).

Finally, set a module-level variable plugin to the Plugin subclass you just defined. Your plugin is then referenced by the module name only.

In other words if your plugin is MyValidator in module foo.my_validator, then foo/my_validator.py should define class MyValidator(Plugin) and end with the line:

plugin = MyValidator

In the TOML file, it would be referred to as ["foo.my_validator"]

You can’t perform that action at this time.