Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Make checkers extensible via entry point
Browse files Browse the repository at this point in the history
This commit adds an entry point called pydocstyle_styles
which allows checks to be loaded dynamically.
  • Loading branch information
samj1912 committed Apr 22, 2019
1 parent e5ec1a8 commit 4d4c544
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@
'console_scripts': [
'pydocstyle = pydocstyle.cli:main',
],
'pydocstyle_styles': [
'pydocstyle.base = pydocstyle.checkers.style.base',
'pydocstyle.numpy = pydocstyle.checkers.style.numpy',
'pydocstyle.other = pydocstyle.checkers.style.other',
]
},
)
5 changes: 1 addition & 4 deletions src/pydocstyle/checkers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from pydocstyle.checkers.hooks import get_checkers
# These imports below are required for registering the
# checkers so that `get_checkers` captures them.
from pydocstyle.checkers.style import numpy, base, other

__all__ = ('get_checkers', 'numpy', 'base', 'other')
__all__ = ('get_checkers',)
23 changes: 23 additions & 0 deletions src/pydocstyle/checkers/hooks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import pkg_resources

from typing import Set

from pydocstyle.utils import log

__registered_checkers = []
__loaded_styles = set() # type: Set[str]

ENTRY_POINT_NAME = 'pydocstyle_styles'


def check_for(kind, terminal=False):
def decorator(f):
Expand All @@ -9,5 +19,18 @@ def decorator(f):
return decorator


def _load_styles():
for entry_point in pkg_resources.iter_entry_points(ENTRY_POINT_NAME):
if entry_point.name not in __loaded_styles:
try:
entry_point.load()
except Exception as error:
log.exception("Unable to load plugin %s.\nError occurred: %s",
entry_point.name, error)
else:
__loaded_styles.add(entry_point.name)


def get_checkers():
_load_styles()
return iter(__registered_checkers)

0 comments on commit 4d4c544

Please sign in to comment.