Skip to content

Commit

Permalink
Use caching for HTTP requests in issue node.
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Sep 3, 2020
1 parent 5869974 commit 0c6f03d
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ sphinx-toolbox

.. |docs| image:: https://img.shields.io/readthedocs/sphinx-toolbox/latest?logo=read-the-docs
:target: https://sphinx-toolbox.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status
:alt: Documentation Build Status

.. |docs_check| image:: https://github.com/domdfcoding/sphinx-toolbox/workflows/Docs%20Check/badge.svg
:target: https://github.com/domdfcoding/sphinx-toolbox/actions?query=workflow%3A%22Docs+Check%22
Expand Down
8 changes: 8 additions & 0 deletions doc-source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ sphinx-toolbox

.. end short_desc
.. start shields
.. list-table::
:stub-columns: 1
Expand All @@ -33,6 +34,7 @@ sphinx-toolbox
:alt: Docs Check Status

.. |travis| travis-shield::
:travis-site: com
:alt: Travis Build Status

.. |actions_windows| actions-shield::
Expand All @@ -53,18 +55,22 @@ sphinx-toolbox
:alt: CodeFactor Grade

.. |pypi-version| pypi-shield::
:project: sphinx-toolbox
:version:
:alt: PyPI - Package Version

.. |supported-versions| pypi-shield::
:project: sphinx-toolbox
:py-versions:
:alt: PyPI - Supported Python Versions

.. |supported-implementations| pypi-shield::
:project: sphinx-toolbox
:implementations:
:alt: PyPI - Supported Implementations

.. |wheel| pypi-shield::
:project: sphinx-toolbox
:wheel:
:alt: PyPI - Wheel

Expand All @@ -90,6 +96,8 @@ sphinx-toolbox
.. |pre_commit| pre-commit-shield::
:alt: pre-commit

.. end shields
.. toctree::
:hidden:
Expand Down
6 changes: 3 additions & 3 deletions doc-source/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
git+git://github.com/domdfcoding/sphinx-autodoc-typehints.git@typevar-as-pydata
autodocsumm>=0.2.0
default_values>=0.0.8
domdf_sphinx_theme>=0.1.0
extras_require
extras_require>=0.1.1
seed_intersphinx_mapping>=0.1.1
sphinx-copybutton>=0.2.12
sphinx-notfound-page
sphinx-notfound-page>=0.5
sphinxcontrib-httpdomain>=1.7.0
sphinxemoji>=0.1.6
toctree_plus>=0.0.3
git+git://github.com/domdfcoding/sphinx-autodoc-typehints.git@typevar-as-pydata
18 changes: 17 additions & 1 deletion sphinx_toolbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
from docutils.nodes import document
from docutils.statemachine import StringList
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment
from sphinx.parsers import RSTParser

# this package
from sphinx_toolbox import code, config, confval, installation, issues, rest_example, shields, source
from sphinx_toolbox.cache import cache
from sphinx_toolbox.issues import get_issue_title

__author__: str = "Dominic Davis-Foster"
__copyright__: str = "2020 Dominic Davis-Foster"
Expand All @@ -45,7 +48,18 @@
__version__: str = "0.1.0"
__email__: str = "dominic@davis-foster.co.uk"

__all__ = ["setup"]
__all__ = ["setup", "sphinx_purge_cache"]


def sphinx_purge_cache(app: Sphinx, env: BuildEnvironment, docname):
"""
Clear any cached URLs.
:param app: The sphinx application.
:param env: The sphinx build environment.
"""

cache.clear()


def setup(app: Sphinx) -> Dict[str, Any]:
Expand Down Expand Up @@ -120,4 +134,6 @@ def parse(self, inputstring: Union[str, StringList], document: document) -> None

app.add_source_parser(CustomRSTParser, override=True)

app.connect("env-purge-doc", sphinx_purge_cache)

return {"version": __version__, "parallel_read_safe": True}
38 changes: 38 additions & 0 deletions sphinx_toolbox/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
#
# cache.py
"""
Caching functions.
"""
#
# Copyright (c) 2020 Dominic Davis-Foster <dominic@davis-foster.co.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
#

# stdlib
from datetime import timedelta

# 3rd party
from apeye.rate_limiter import HTTPCache

__all__ = ["cache"]

#: HTTP Cache that caches requests for up to 4 hours.
cache = HTTPCache("sphinx-toolbox", expires_after=timedelta(hours=4))
32 changes: 23 additions & 9 deletions sphinx_toolbox/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,22 @@

# stdlib
import warnings
from typing import Dict, List, Tuple, Union
from typing import Dict, List, Optional, Tuple, Union

# 3rd party
import requests
from apeye.url import URL
from bs4 import BeautifulSoup
from docutils import nodes, utils
from docutils.nodes import Node, system_message
from docutils.nodes import system_message
from docutils.parsers.rst.states import Inliner
from domdf_python_tools.typing import PathLike
from sphinx.util.nodes import split_explicit_title
from sphinx.writers.html import HTMLTranslator

# this package
from sphinx_toolbox.cache import cache
from sphinx_toolbox.utils import make_github_url

__all__ = ["IssueNode", "issue_role", "pull_role", "visit_issue_node", "depart_issue_node"]
__all__ = ["IssueNode", "issue_role", "pull_role", "visit_issue_node", "depart_issue_node", "get_issue_title"]


class IssueNode(nodes.reference):
Expand Down Expand Up @@ -199,12 +198,10 @@ def visit_issue_node(translator: HTMLTranslator, node: IssueNode):
:param node: The node being visited.
"""

r = requests.get(node.issue_url)
issue_title = get_issue_title(node.issue_url)

if r.status_code == 200:
soup = BeautifulSoup(r.content, "html5lib")
if issue_title:
node.has_tooltip = True
issue_title = soup.find_all("span", attrs={"class": "js-issue-title"})[0].contents[0].strip().strip()
translator.body.append(f'<abbr title="{issue_title}">')
translator.visit_reference(node)
else:
Expand All @@ -222,3 +219,20 @@ def depart_issue_node(translator: HTMLTranslator, node: IssueNode):
if node.has_tooltip:
translator.depart_reference(node)
translator.body.append("</abbr>")


def get_issue_title(issue_url: str) -> Optional[str]:
"""
Returns the title of the issue with the given url,
or :py:obj:`None` if the issue isn't found.
:param issue_url:
"""

r = cache.session.get(issue_url)

if r.status_code == 200:
soup = BeautifulSoup(r.content, "html5lib")
return soup.find_all("span", attrs={"class": "js-issue-title"})[0].contents[0].strip().strip()

return None
1 change: 1 addition & 0 deletions tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def test_setup():
EventListener(id=1, handler=installation.installation_node_purger.purge_nodes, priority=500),
EventListener(id=2, handler=rest_example_purger.purge_nodes, priority=500),
EventListener(id=3, handler=installation.extensions_node_purger.purge_nodes, priority=500),
EventListener(id=4, handler=sphinx_toolbox.sphinx_purge_cache, priority=500),
],
}

Expand Down

0 comments on commit 0c6f03d

Please sign in to comment.