Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for newer rstcheck versions #13

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/13-rstcheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "Mark rstcheck 4.x and 5.x as compatible. Support rstcheck 6.x as well (https://github.com/ansible-community/antsibull-docs/pull/13)."
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ antsibull-core = ">= 1.0.0, < 2.0.0"
asyncio-pool = "*"
docutils = "*"
jinja2 = "*"
rstcheck = "^3"
rstcheck = ">= 3.0.0, < 7.0.0"
sphinx = "*"

[tool.poetry.dev-dependencies]
Expand Down
9 changes: 2 additions & 7 deletions src/antsibull_docs/lint_extra_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
import re
import typing as t

import docutils.utils
import rstcheck

from antsibull_core.yaml import load_yaml_file

from .extra_docs import (
Expand All @@ -21,6 +18,7 @@
load_extra_docs_index,
ExtraDocsIndexError,
)
from .rstcheck import check_rst_content


_RST_LABEL_DEFINITION = re.compile(r'''^\.\. _([^:]+):''')
Expand Down Expand Up @@ -53,10 +51,7 @@ def lint_optional_conditions(content: str, path: str, collection_name: str

Return a list of errors.
'''
results = rstcheck.check(
content, filename=path,
report_level=docutils.utils.Reporter.WARNING_LEVEL)
return [(result[0], 0, result[1]) for result in results]
return check_rst_content(content, filename=path)


def lint_collection_extra_docs_files(path_to_collection: str
Expand Down
48 changes: 48 additions & 0 deletions src/antsibull_docs/rstcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# coding: utf-8
# Author: Felix Fontein <[email protected]>
# License: GPLv3+
# Copyright: Ansible Project, 2022
"""Handle rstcheck."""

import os.path
import pathlib
import tempfile
import typing as t

# rstcheck >= 6.0.0 depends on rstcheck-core
try:
import rstcheck_core.checker
import rstcheck_core.config
HAS_RSTCHECK_CORE = True
except ImportError:
HAS_RSTCHECK_CORE = False
import docutils.utils
import rstcheck


def check_rst_content(content: str, filename: t.Optional[str] = None
) -> t.List[t.Tuple[int, int, str]]:
'''
Check the content with rstcheck. Return list of errors and warnings.

The entries in the return list are tuples with line number, column number, and
error/warning message.
'''
if HAS_RSTCHECK_CORE:
filename = os.path.basename(filename or 'file.rst') or 'file.rst'
with tempfile.TemporaryDirectory() as tempdir:
rst_path = os.path.join(tempdir, filename)
with open(rst_path, 'w', encoding='utf-8') as f:
f.write(content)
config = rstcheck_core.config.RstcheckConfig(
report_level=rstcheck_core.config.ReportLevel.WARNING,
)
core_results = rstcheck_core.checker.check_file(pathlib.Path(rst_path), config)
return [(result.line_number, 0, result.message) for result in core_results]
else:
results = rstcheck.check(
content,
filename=filename,
report_level=docutils.utils.Reporter.WARNING_LEVEL,
)
return [(result[0], 0, result[1]) for result in results]
6 changes: 3 additions & 3 deletions stubs/rstcheck.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import docutils.utils

from typing import List, Tuple, Union
from typing import List, Optional, Tuple, Union


def check(source: str,
filename: str = ...,
report_level: docutils.utils.Reporter = ...,
filename: Optional[str] = ...,
report_level: Union[docutils.utils.Reporter, int] = ...,
ignore: Union[dict, None] = ...,
debug: bool = ...) -> List[Tuple[int, str]]: ...
Empty file.
13 changes: 13 additions & 0 deletions stubs/rstcheck_core/checker.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import enum
import pathlib

from typing import List, Optional, Tuple, Union

from . import config, types


def check_file(
source_file: pathlib.Path,
rstcheck_config: config.RstcheckConfig,
overwrite_with_file_config: bool = True,
) -> List[types.LintError]: ...
15 changes: 15 additions & 0 deletions stubs/rstcheck_core/config.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import enum

from typing import List, Optional, Tuple, Union


class ReportLevel(enum.Enum):
INFO = 1
WARNING = 2
ERROR = 3
SEVERE = 4
NONE = 5


class RstcheckConfig:
def __init__(self, report_level: Optional[ReportLevel] = ...): ...
10 changes: 10 additions & 0 deletions stubs/rstcheck_core/types.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import enum
import pathlib

from typing import Literal, Union


class LintError:
source_origin: Union[pathlib.Path, Literal["<string>"], Literal["<stdin>"]]
line_number: int
message: str