Support functions as file plugins too

It is possible to write plugins which are only a function. At the moment they
are called on each line manually. This allows the function also to be called
on each file once. It works similar to creating the class and calling `run` on
it immediately. The plugin function needs to return a generator.

This is based on the original comment in the `FileChecker.run_ast_checks`
method, but slightly modified as the original comment would've called the
return of the function. But the function could return the reports directly.
This commit is contained in:
Fabian Neundorf 2016-07-24 14:27:05 +00:00
parent 698079f87a
commit 373eb15573
2 changed files with 80 additions and 6 deletions

View file

@ -466,11 +466,13 @@ class FileChecker(object):
for plugin in self.checks.ast_plugins:
checker = self.run_check(plugin, tree=ast)
# NOTE(sigmavirus24): If we want to allow for AST plugins that are
# not classes exclusively, we can do the following:
# retrieve_results = getattr(checker, 'run', lambda: checker)
# Otherwise, we just call run on the checker
for (line_number, offset, text, check) in checker.run():
# If the plugin uses a class, call the run method of it, otherwise
# the call should return something iterable itself
try:
runner = checker.run()
except AttributeError:
runner = checker
for (line_number, offset, text, check) in runner:
self.report(
error_code=None,
line_number=line_number,