summaryrefslogtreecommitdiff
path: root/utils.py
diff options
context:
space:
mode:
authorTorsten Marek <tmarek@google.com>2013-06-19 16:29:18 +0200
committerTorsten Marek <tmarek@google.com>2013-06-19 16:29:18 +0200
commitf6bddc94f23ef417ae1577b6416c412a82aff9c7 (patch)
tree14a4b526b3ac568efe4f18ac058ba1e4a7f827fa /utils.py
parentc5e3e1ff04b2d8f3d2525d24b426ef1c7e73a21a (diff)
downloadpylint-f6bddc94f23ef417ae1577b6416c412a82aff9c7.tar.gz
Turn reporters into proper plugins rather than classes that are loaded explicitly.
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py31
1 files changed, 30 insertions, 1 deletions
diff --git a/utils.py b/utils.py
index 2dac831..a13faff 100644
--- a/utils.py
+++ b/utils.py
@@ -21,11 +21,12 @@ import re
import sys
import tokenize
from warnings import warn
+import os
from os.path import dirname, basename, splitext, exists, isdir, join, normpath
from logilab.common.interface import implements
from logilab.common.modutils import modpath_from_file, get_module_files, \
- file_from_modpath
+ file_from_modpath, load_module_from_file
from logilab.common.textutils import normalize_text
from logilab.common.configuration import rest_format_section
from logilab.common.ureports import Section
@@ -42,6 +43,7 @@ class EmptyReport(Exception):
"""raised when a report is empty and so should not be displayed"""
+
MSG_TYPES = {
'I' : 'info',
'C' : 'convention',
@@ -653,3 +655,30 @@ class PyLintASTWalker(object):
self.walk(child)
for cb in self.leave_events.get(cid, ()):
cb(astroid)
+
+
+PY_EXTS = ('.py', '.pyc', '.pyo', '.pyw', '.so', '.dll')
+
+def register_plugins(linter, directory):
+ """load all module and package in the given directory, looking for a
+ 'register' function in each one, used to register pylint checkers
+ """
+ imported = {}
+ for filename in os.listdir(directory):
+ basename, extension = os.path.splitext(filename)
+ if basename in imported or basename == '__pycache__':
+ continue
+ if extension in PY_EXTS and basename != '__init__' or (
+ not extension and os.isdir(os.path.join(directory, basename))):
+ try:
+ module = load_module_from_file(os.path.join(directory, filename))
+ except ValueError:
+ # empty module name (usually emacs auto-save files)
+ continue
+ except ImportError, exc:
+ import sys
+ print >> sys.stderr, "Problem importing module %s: %s" % (filename, exc)
+ else:
+ if hasattr(module, 'register'):
+ module.register(linter)
+ imported[basename] = 1