Skip to content

Commit

Permalink
Ignore warnings in injected toctree (#7)
Browse files Browse the repository at this point in the history
* Ignore warnings in injected toctree

* Update delta.py

* Update delta.py

* Update delta.py

* Update delta.py
  • Loading branch information
TheTripleV authored Oct 10, 2021
1 parent 55119f2 commit 1ce6549
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions sphinxext/delta.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
import os
from functools import wraps
from typing import Any, Dict

from docutils import nodes
from sphinx import addnodes
from sphinx.application import Sphinx
from sphinx.application import logger
from sphinx.directives.other import TocTree


def NoWarnings(func):
@wraps(func)
def wrapped(self, *args, **kwargs):
stream = self.state.document.reporter.stream
self.state.document.reporter.stream = None
ret = func(self, *args, **kwargs)
self.state.document.reporter.stream = stream
ret = list(filter(lambda node: not isinstance(node, nodes.system_message), ret))
return ret

return wrapped


class NoWarningsToctree(TocTree):
@NoWarnings
def run(self):
return super().run()

@NoWarnings
def parse_content(self, toctree: addnodes.toctree):
return super().parse_content(toctree)


def on_rtd() -> bool:
Expand All @@ -22,40 +49,46 @@ def inject_changed_files(html_context: Dict[str, str], app: Sphinx) -> None:
res = requests.get(
f"https://api.github.com/repos/{html_context['github_user']}/{html_context['github_repo']}/pulls/{html_context['current_version']}/files"
)

if res.status_code != requests.codes.ok:
return

changes_rst = "".join(
[
".. toctree::\n",
"\n",
".. nowarningstoctree::\n",
" :maxdepth: 1\n",
" :caption: PR CHANGED FILES\n",
"\n",
]
)

if app.config.delta_inject_location is None:
inject_location = "index.rst"
else:
inject_location = app.config.delta_inject_location

for file_context in res.json():
status: str = file_context["status"]
filename: str = file_context["filename"]

if app.config.delta_doc_path is None:
logger.error("Required option delta_doc_path is not set!")
if status == "deleted":
if status == "removed":
continue
if not filename.startswith(app.config.delta_doc_path):
continue
if not filename.endswith(".rst"):
continue

changes_rst += f" {os.path.relpath(filename, app.config.delta_doc_path)}\n"
rel_path = os.path.relpath(filename, app.config.delta_doc_path)
if rel_path == inject_location:
continue
changes_rst += f" {rel_path}\n"

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

if app.config.delta_inject_location is None:
inject_location = "index.rst"
else:
inject_location = app.config.delta_inject_location

inject_location = os.path.join(app.srcdir, inject_location)
with open(inject_location, "a") as f:
f.write(changes_rst)

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

return {
"parallel_read_safe": True,
Expand Down

0 comments on commit 1ce6549

Please sign in to comment.