-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhancement: add lint rules plugin support
Add plugin support using setuptools (pkg_resources) plugin mechanism to YAML Lint to allow users to add their own custom lint rule plugins. Signed-off-by: Satoru SATOH <satoru.satoh@gmail.com>
- Loading branch information
Showing
3 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2020 Satoru SATOH | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
Plugin module utilizing setuptools (pkg_resources) to allow users to add their | ||
own custom lint rules. | ||
""" | ||
try: | ||
from pkg_resources import iter_entry_points | ||
except ImportError: | ||
def iter_entry_points(group): | ||
"""Dummy function for pkg_resources.iter_entry_points.""" | ||
yield | ||
|
||
|
||
PACKAGE_GROUP = "yamllint.plugins" | ||
|
||
|
||
def validate_rule_module(rule_mod): | ||
"""Test if given rule module is valid. | ||
""" | ||
return getattr(rule_mod, "ID", False | ||
) and callable(getattr(rule_mod, "check", False)) | ||
|
||
|
||
def load_plugin_rules_itr(group=PACKAGE_GROUP): | ||
"""Load custom lint rule plugins.""" | ||
for entry in iter_entry_points(group): | ||
try: | ||
rules_mod = entry.load() | ||
yield rules_mod.RULES_MAP | ||
except AttributeError: | ||
pass # TBD | ||
|
||
|
||
def update_rules_map(rules_map): | ||
"""Update and add plugins rules to given rules' map.""" | ||
for plugin_rules_map in load_plugin_rules_itr(): | ||
for rule_id, rule_mod in plugin_rules_map.items(): | ||
if not validate_rule_module(rule_mod): | ||
continue | ||
|
||
if rule_id not in rules_map: | ||
rules_map[rule_id] = rule_mod | ||
|
||
return rules_map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters