Skip to content

Commit 6d79596

Browse files
authored
Add support for newer rstcheck versions (#13)
* Make newer rstcheck versions as compatible. * Support rstcheck_core via rstcheck 6.x.
1 parent a8ec75b commit 6d79596

File tree

9 files changed

+94
-11
lines changed

9 files changed

+94
-11
lines changed

changelogs/fragments/13-rstcheck.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- "Mark rstcheck 4.x and 5.x as compatible. Support rstcheck 6.x as well (https://github.com/ansible-community/antsibull-docs/pull/13)."

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ antsibull-core = ">= 1.0.0, < 2.0.0"
3535
asyncio-pool = "*"
3636
docutils = "*"
3737
jinja2 = "*"
38-
rstcheck = "^3"
38+
rstcheck = ">= 3.0.0, < 7.0.0"
3939
sphinx = "*"
4040

4141
[tool.poetry.dev-dependencies]

src/antsibull_docs/lint_extra_docs.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
import re
1111
import typing as t
1212

13-
import docutils.utils
14-
import rstcheck
15-
1613
from antsibull_core.yaml import load_yaml_file
1714

1815
from .extra_docs import (
@@ -21,6 +18,7 @@
2118
load_extra_docs_index,
2219
ExtraDocsIndexError,
2320
)
21+
from .rstcheck import check_rst_content
2422

2523

2624
_RST_LABEL_DEFINITION = re.compile(r'''^\.\. _([^:]+):''')
@@ -53,10 +51,7 @@ def lint_optional_conditions(content: str, path: str, collection_name: str
5351
5452
Return a list of errors.
5553
'''
56-
results = rstcheck.check(
57-
content, filename=path,
58-
report_level=docutils.utils.Reporter.WARNING_LEVEL)
59-
return [(result[0], 0, result[1]) for result in results]
54+
return check_rst_content(content, filename=path)
6055

6156

6257
def lint_collection_extra_docs_files(path_to_collection: str

src/antsibull_docs/rstcheck.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# coding: utf-8
2+
# Author: Felix Fontein <[email protected]>
3+
# License: GPLv3+
4+
# Copyright: Ansible Project, 2022
5+
"""Handle rstcheck."""
6+
7+
import os.path
8+
import pathlib
9+
import tempfile
10+
import typing as t
11+
12+
# rstcheck >= 6.0.0 depends on rstcheck-core
13+
try:
14+
import rstcheck_core.checker
15+
import rstcheck_core.config
16+
HAS_RSTCHECK_CORE = True
17+
except ImportError:
18+
HAS_RSTCHECK_CORE = False
19+
import docutils.utils
20+
import rstcheck
21+
22+
23+
def check_rst_content(content: str, filename: t.Optional[str] = None
24+
) -> t.List[t.Tuple[int, int, str]]:
25+
'''
26+
Check the content with rstcheck. Return list of errors and warnings.
27+
28+
The entries in the return list are tuples with line number, column number, and
29+
error/warning message.
30+
'''
31+
if HAS_RSTCHECK_CORE:
32+
filename = os.path.basename(filename or 'file.rst') or 'file.rst'
33+
with tempfile.TemporaryDirectory() as tempdir:
34+
rst_path = os.path.join(tempdir, filename)
35+
with open(rst_path, 'w', encoding='utf-8') as f:
36+
f.write(content)
37+
config = rstcheck_core.config.RstcheckConfig(
38+
report_level=rstcheck_core.config.ReportLevel.WARNING,
39+
)
40+
core_results = rstcheck_core.checker.check_file(pathlib.Path(rst_path), config)
41+
return [(result.line_number, 0, result.message) for result in core_results]
42+
else:
43+
results = rstcheck.check(
44+
content,
45+
filename=filename,
46+
report_level=docutils.utils.Reporter.WARNING_LEVEL,
47+
)
48+
return [(result[0], 0, result[1]) for result in results]

stubs/rstcheck.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import docutils.utils
22

3-
from typing import List, Tuple, Union
3+
from typing import List, Optional, Tuple, Union
44

55

66
def check(source: str,
7-
filename: str = ...,
8-
report_level: docutils.utils.Reporter = ...,
7+
filename: Optional[str] = ...,
8+
report_level: Union[docutils.utils.Reporter, int] = ...,
99
ignore: Union[dict, None] = ...,
1010
debug: bool = ...) -> List[Tuple[int, str]]: ...

stubs/rstcheck_core/__init__.pyi

Whitespace-only changes.

stubs/rstcheck_core/checker.pyi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import enum
2+
import pathlib
3+
4+
from typing import List, Optional, Tuple, Union
5+
6+
from . import config, types
7+
8+
9+
def check_file(
10+
source_file: pathlib.Path,
11+
rstcheck_config: config.RstcheckConfig,
12+
overwrite_with_file_config: bool = True,
13+
) -> List[types.LintError]: ...

stubs/rstcheck_core/config.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import enum
2+
3+
from typing import List, Optional, Tuple, Union
4+
5+
6+
class ReportLevel(enum.Enum):
7+
INFO = 1
8+
WARNING = 2
9+
ERROR = 3
10+
SEVERE = 4
11+
NONE = 5
12+
13+
14+
class RstcheckConfig:
15+
def __init__(self, report_level: Optional[ReportLevel] = ...): ...

stubs/rstcheck_core/types.pyi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import enum
2+
import pathlib
3+
4+
from typing import Literal, Union
5+
6+
7+
class LintError:
8+
source_origin: Union[pathlib.Path, Literal["<string>"], Literal["<stdin>"]]
9+
line_number: int
10+
message: str

0 commit comments

Comments
 (0)