Skip to content

Commit 968c43e

Browse files
committed
Retrieve JSON data from an external source
Allow the user to define a custom URL to retrieve the JSON data (instead of hitting Read the Docs API) needed to add the banner into the page. This is useful when there is a git tag that doesn't want to be updated, but now the warning message is out of date. So, by defining a custom URL (potentially a JSON under the git repository served by github, for example) this file is retrieved by the documentation and displayed the custom message needed at certain date. The JSON file can be updated independently from the documentation/tag/etc.
1 parent 0447ab4 commit 968c43e

File tree

5 files changed

+82
-14
lines changed

5 files changed

+82
-14
lines changed

versionwarning/_static/js/versionwarning.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

versionwarning/_static/js/versionwarning.src.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ function checkVersion(config) {
8080
});
8181
}
8282

83+
function handleBanner(config) {
84+
console.debug("handleBanner");
85+
if (config.banner.custom) {
86+
injectCustomWarningBanner(config);
87+
}
88+
else {
89+
checkVersion(config);
90+
}
91+
}
92+
8393
function init() {
8494
console.debug("init");
8595
$.ajax({
@@ -90,11 +100,29 @@ function init() {
90100
if (banner) {
91101
console.debug("There is already a banner added. No checking versions.")
92102
}
93-
else if (config.banner.custom) {
94-
injectCustomWarningBanner(config);
103+
else if (config.meta.json_url && !config.meta.retrieve_data_from_api) {
104+
console.debug("Retrieving dynamic data from 'json_url'.")
105+
$.ajax({
106+
dataType: "json",
107+
url: config.meta.json_url,
108+
success: function(dynamic_config) {
109+
if (config.version.slug in dynamic_config) {
110+
handleBanner(dynamic_config[config.version.slug]);
111+
}
112+
else if (config.meta.only_override_banner) {
113+
handleBanner(config);
114+
}
115+
else {
116+
log.error("JSON data from " + config.meta.json_url + " does not have a warning banner for " + config.version.slug);
117+
}
118+
},
119+
error: function() {
120+
console.error("Error loading dynamic config object");
121+
},
122+
});
95123
}
96124
else {
97-
checkVersion(config);
125+
handleBanner(config);
98126
}
99127
},
100128
error: function() {

versionwarning/exceptions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
class ConfigError(Exception):
5+
"""
6+
Incompatible config exception.
7+
8+
This exception is risen when the extension detects configs that are
9+
incompatible or that depends each other.
10+
"""
11+
12+
NO_JSON_URL = (
13+
'No URL defined to retrieve JSON data. '
14+
'The URL is mandatory when "versionwarning_retrieve_data_from_api" '
15+
'is True'
16+
)

versionwarning/extension.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ def setup(app):
2929
app.add_config_value('versionwarning_project_slug', os.environ.get('READTHEDOCS_PROJECT', None), 'html')
3030
app.add_config_value('versionwarning_project_version', os.environ.get('READTHEDOCS_VERSION', None), 'html')
3131

32+
33+
app.add_config_value('versionwarning_retrieve_data_from_api', True, 'html')
34+
app.add_config_value('versionwarning_json_url', None, 'html')
35+
app.add_config_value('versionwarning_only_override_banner', True, 'html')
36+
3237
if sphinx.version_info >= (1, 8):
3338
# ``config-initied`` requires Sphinx >= 1.8
3439
app.connect('config-inited', generate_versionwarning_data_json)

versionwarning/signals.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import json
44
import os
55

6+
from versionwarning.exceptions import ConfigError
7+
68

79
STATIC_PATH = os.path.join(os.path.dirname(__file__), '_static')
810
JSON_DATA_FILENAME = 'versionwarning-data.json'
@@ -27,6 +29,9 @@ def generate_versionwarning_data_json(app, config=None, **kwargs):
2729
if config is None:
2830
config = app.config
2931

32+
if not config.versionwarning_retrieve_data_from_api and not config.versionwarning_json_url:
33+
raise ConfigError(ConfigError.NO_JSON_URL)
34+
3035
if config.versionwarning_project_version in config.versionwarning_messages:
3136
custom = True
3237
message = config.versionwarning_messages.get(config.versionwarning_project_version)
@@ -43,30 +48,44 @@ def generate_versionwarning_data_json(app, config=None, **kwargs):
4348
admonition_type=config.versionwarning_admonition_type,
4449
)
4550

46-
data = json.dumps({
47-
'meta': {
48-
'api_url': config.versionwarning_api_url,
49-
},
51+
data = {
5052
'banner': {
51-
'html': banner_html,
52-
'id_div': config.versionwarning_banner_id_div,
53-
'body_selector': config.versionwarning_body_selector,
54-
'custom': custom,
53+
config.versionwarning_project_version: {
54+
'html': banner_html,
55+
'id_div': config.versionwarning_banner_id_div,
56+
'body_selector': config.versionwarning_body_selector,
57+
'custom': custom,
58+
},
5559
},
5660
'project': {
5761
'slug': config.versionwarning_project_slug,
5862
},
5963
'version': {
6064
'slug': config.versionwarning_project_version,
6165
},
62-
}, indent=4)
66+
}
67+
68+
if config.versionwarning_retrieve_data_from_api:
69+
url_key = 'api_url'
70+
url = config.versionwarning_api_url
71+
else:
72+
url_key = 'json_url'
73+
url = config.versionwarning_json_url
74+
75+
data.update({
76+
'meta': {
77+
url_key: url,
78+
'retrieve_data_from_api': config.versionwarning_retrieve_data_from_api,
79+
'only_override_banner': config.versionwarning_only_override_banner,
80+
},
81+
})
6382

6483
data_path = os.path.join(STATIC_PATH, 'data')
6584
if not os.path.exists(data_path):
6685
os.mkdir(data_path)
6786

6887
with open(os.path.join(data_path, JSON_DATA_FILENAME), 'w') as f:
69-
f.write(data)
88+
f.write(json.dumps(data, indent=4))
7089

7190
# Add the path where ``versionwarning-data.json`` file and
7291
# ``versionwarning.js`` are saved

0 commit comments

Comments
 (0)