Skip to content

Commit

Permalink
FIX: check the json switcher file (#917)
Browse files Browse the repository at this point in the history
* fix: check the json switcher file

* read the request as text

* read local file as well as https

* catch every exception

* check at update_config stage

* add output to debug the github build

* point the tests to the _static folder

* add the url parameter to the tests

* remove legacy print

* raise error if json_url is not set

* raise error if the file cannot be read
  • Loading branch information
12rambau authored Oct 4, 2022
1 parent f4ef638 commit ecc01a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
34 changes: 31 additions & 3 deletions src/pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings
from pathlib import Path
from functools import lru_cache
import json
from urllib.parse import urlparse

import jinja2
Expand All @@ -19,6 +20,7 @@
from sphinx.util import logging
from pygments.formatters import HtmlFormatter
from pygments.styles import get_all_styles
import requests

from .bootstrap_html_translator import BootstrapHTML5Translator

Expand Down Expand Up @@ -55,6 +57,33 @@ def update_config(app, env):
" Set version URLs in JSON directly."
)

# check the validity of the theme swithcer file
if isinstance(theme_options.get("switcher"), dict):
theme_switcher = theme_options.get("switcher")

# raise an error if one of these compulsory keys is missing
json_url = theme_switcher["json_url"]
theme_switcher["version_match"]

# try to read the json file. If it's a url we use request,
# else we simply read the local file from the source directory
# it will raise an error if the file does not exist
if urlparse(json_url).scheme in ["http", "https"]:
content = requests.get(json_url).text
else:
content = Path(env.srcdir, json_url).read_text()

# check that the json file is not illformed
# it will throw an error if there is a an issue
switcher_content = json.loads(content)
missing_url = any(["url" not in e for e in switcher_content])
missing_version = any(["version" not in e for e in switcher_content])
if missing_url or missing_version:
raise AttributeError(
f'The version switcher "{json_url}" file is malformed'
' at least one of the items is missing the "url" or "version" key'
)

# Add an analytics ID to the site if provided
analytics = theme_options.get("analytics", {})
# deprecated options for Google Analytics
Expand Down Expand Up @@ -189,9 +218,8 @@ def update_templates(app, pagename, templatename, context, doctree):
app.add_js_file(None, body=f"DOCUMENTATION_OPTIONS.pagename = '{pagename}';")
if isinstance(context.get("theme_switcher"), dict):
theme_switcher = context["theme_switcher"]
if theme_switcher.get("json_url"):
json_url = theme_switcher["json_url"]
version_match = theme_switcher["version_match"]
json_url = theme_switcher["json_url"]
version_match = theme_switcher["version_match"]

# Add variables to our JavaScript for re-use in our main JS script
js = f"""
Expand Down
9 changes: 6 additions & 3 deletions tests/sites/base/switcher.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
[
{
"name": "v0.7.1 (stable)",
"version": "0.7.1"
"version": "0.7.1",
"url": "https://pydata-sphinx-theme.readthedocs.io/en/v0.7.1/"
},
{
"version": "0.7.0"
"version": "0.7.0",
"url": "https://pydata-sphinx-theme.readthedocs.io/en/v0.7.0/"
},
{
"version": "0.6.3"
"version": "0.6.3",
"url": "https://pydata-sphinx-theme.readthedocs.io/en/v0.6.3/"
}
]

0 comments on commit ecc01a0

Please sign in to comment.