Feat: ignore directives

This commit is contained in:
Bertrand Benjamin 2022-09-04 10:33:39 +02:00
parent b32ba93c6e
commit c9abbdde5d
3 changed files with 43 additions and 9 deletions

View File

@ -1,9 +1,10 @@
import logging
import re
import sys
import time
import click
import restructuredtext_lint
from docutils.parsers.rst.directives import register_directive
from git import Repo
formatter = logging.Formatter("%(name)s :: %(levelname)s :: %(message)s")
@ -39,12 +40,20 @@ def select_by_extension(files, ext="rst"):
# Rst linter
def rst_lint(filename):
def rst_lint(filename, ignore_directives=["big_button"]):
with open(filename, "r") as f:
errors = restructuredtext_lint.lint(f.read())
filtered_errors = []
for e in errors:
logger.warning(f"{filename} \n{e.full_message}\n")
return errors
if "directive" in e.message and any(
[i in e.message for i in ignore_directives]
):
pass
else:
logger.warning(f"{filename} \n{e.full_message}\n")
filtered_errors.append(e)
return filtered_errors
# Rst parameters normalize
@ -115,7 +124,16 @@ NORMALIZERS_NEW = {
}
def main(argv: list[str] = None) -> int:
@click.command()
@click.argument("commited_files", nargs=-1)
@click.option(
"--ignore_directive",
multiple=True,
default=[],
help="List of ignored directives for restructuredtext_lint",
)
def main(commited_files: list, ignore_directive: list) -> int:
r = Repo()
diff = get_commited_files(r)
@ -124,11 +142,11 @@ def main(argv: list[str] = None) -> int:
# New files
for f in select_by_extension(diff["A"], "rst"):
errors += rst_lint(f)
errors += rst_lint(f, ignore_directives=ignore_directive)
modified += normalize_file(f, NORMALIZERS_NEW)
# Modified files
for f in select_by_extension(diff["M"], "rst"):
errors += rst_lint(f)
errors += rst_lint(f, ignore_directives=ignore_directive)
modified += normalize_file(f, NORMALIZERS_MODIFIED)
return int(len(errors) > 0)

19
poetry.lock generated
View File

@ -28,11 +28,22 @@ category = "dev"
optional = false
python-versions = ">=3.6.1"
[[package]]
name = "click"
version = "8.1.3"
description = "Composable command line interface toolkit"
category = "main"
optional = false
python-versions = ">=3.7"
[package.dependencies]
colorama = {version = "*", markers = "platform_system == \"Windows\""}
[[package]]
name = "colorama"
version = "0.4.5"
description = "Cross-platform colored terminal text."
category = "dev"
category = "main"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
@ -267,7 +278,7 @@ python-versions = "*"
[metadata]
lock-version = "1.1"
python-versions = "^3.10"
content-hash = "f42f1e61719183591304472b3ed90dfbd50fb6ff8ecbd502c7c23c0cd85ce935"
content-hash = "53a66536a5ce62f85d18096d9d1d6586fd4438774cc7f2136391de830081e51c"
[metadata.files]
atomicwrites = [
@ -281,6 +292,10 @@ cfgv = [
{file = "cfgv-3.3.1-py2.py3-none-any.whl", hash = "sha256:c6a0883f3917a037485059700b9e75da2464e6c27051014ad85ba6aaa5884426"},
{file = "cfgv-3.3.1.tar.gz", hash = "sha256:f5a830efb9ce7a445376bb66ec94c638a9787422f96264c98edc6bdeed8ab736"},
]
click = [
{file = "click-8.1.3-py3-none-any.whl", hash = "sha256:bb4d8133cb15a609f44e8213d9b391b0809795062913b383c62be0ee95b1db48"},
{file = "click-8.1.3.tar.gz", hash = "sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e"},
]
colorama = [
{file = "colorama-0.4.5-py2.py3-none-any.whl", hash = "sha256:854bf444933e37f5824ae7bfc1e98d5bce2ebe4160d46b5edf346a89358e99da"},
{file = "colorama-0.4.5.tar.gz", hash = "sha256:e6c6b4334fc50988a639d9b98aa429a0b57da6e17b9a44f0451f930b6967b7a4"},

View File

@ -11,6 +11,7 @@ clean-rst = "clean_rst.main:main"
python = "^3.10"
restructuredtext-lint = "^1.4.0"
GitPython = "^3.1.27"
click = "^8.1.3"
[tool.poetry.dev-dependencies]
pytest = "^5.2"