Skip to content

Commit 1ce6549

Browse files
authored
Ignore warnings in injected toctree (#7)
* Ignore warnings in injected toctree * Update delta.py * Update delta.py * Update delta.py * Update delta.py
1 parent 55119f2 commit 1ce6549

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

sphinxext/delta.py

+42-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
11
import os
2+
from functools import wraps
23
from typing import Any, Dict
34

5+
from docutils import nodes
6+
from sphinx import addnodes
47
from sphinx.application import Sphinx
58
from sphinx.application import logger
9+
from sphinx.directives.other import TocTree
10+
11+
12+
def NoWarnings(func):
13+
@wraps(func)
14+
def wrapped(self, *args, **kwargs):
15+
stream = self.state.document.reporter.stream
16+
self.state.document.reporter.stream = None
17+
ret = func(self, *args, **kwargs)
18+
self.state.document.reporter.stream = stream
19+
ret = list(filter(lambda node: not isinstance(node, nodes.system_message), ret))
20+
return ret
21+
22+
return wrapped
23+
24+
25+
class NoWarningsToctree(TocTree):
26+
@NoWarnings
27+
def run(self):
28+
return super().run()
29+
30+
@NoWarnings
31+
def parse_content(self, toctree: addnodes.toctree):
32+
return super().parse_content(toctree)
633

734

835
def on_rtd() -> bool:
@@ -22,40 +49,46 @@ def inject_changed_files(html_context: Dict[str, str], app: Sphinx) -> None:
2249
res = requests.get(
2350
f"https://api.github.com/repos/{html_context['github_user']}/{html_context['github_repo']}/pulls/{html_context['current_version']}/files"
2451
)
52+
2553
if res.status_code != requests.codes.ok:
2654
return
2755

2856
changes_rst = "".join(
2957
[
30-
".. toctree::\n",
58+
"\n",
59+
".. nowarningstoctree::\n",
3160
" :maxdepth: 1\n",
3261
" :caption: PR CHANGED FILES\n",
3362
"\n",
3463
]
3564
)
3665

66+
if app.config.delta_inject_location is None:
67+
inject_location = "index.rst"
68+
else:
69+
inject_location = app.config.delta_inject_location
70+
3771
for file_context in res.json():
3872
status: str = file_context["status"]
3973
filename: str = file_context["filename"]
4074

4175
if app.config.delta_doc_path is None:
4276
logger.error("Required option delta_doc_path is not set!")
43-
if status == "deleted":
77+
if status == "removed":
4478
continue
4579
if not filename.startswith(app.config.delta_doc_path):
4680
continue
4781
if not filename.endswith(".rst"):
4882
continue
4983

50-
changes_rst += f" {os.path.relpath(filename, app.config.delta_doc_path)}\n"
84+
rel_path = os.path.relpath(filename, app.config.delta_doc_path)
85+
if rel_path == inject_location:
86+
continue
87+
changes_rst += f" {rel_path}\n"
5188

5289
changes_rst += "\n\n.. todolist::\n"
5390

54-
if app.config.delta_inject_location is None:
55-
inject_location = "index.rst"
56-
else:
57-
inject_location = app.config.delta_inject_location
58-
91+
inject_location = os.path.join(app.srcdir, inject_location)
5992
with open(inject_location, "a") as f:
6093
f.write(changes_rst)
6194

@@ -69,6 +102,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
69102
app.connect("config-inited", config_inited)
70103
app.add_config_value("delta_doc_path", None, str)
71104
app.add_config_value("delta_inject_location", None, str)
105+
app.add_directive("nowarningstoctree", NoWarningsToctree)
72106

73107
return {
74108
"parallel_read_safe": True,

0 commit comments

Comments
 (0)