Skip to content

Commit

Permalink
Replace version placeholders in built schema
Browse files Browse the repository at this point in the history
  • Loading branch information
duncandewhurst committed Jun 11, 2024
1 parent 783b81d commit 7365a75
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ _build
.python-version
.vscode
tests/__pycache__
_readthedocs
3 changes: 2 additions & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ build:
# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/conf.py
builder: dirhtml

# If using Sphinx, optionally build your docs in additional formats such as PDF
# formats:
Expand All @@ -26,4 +27,4 @@ sphinx:
# Optionally declare the Python requirements required to build your docs
python:
install:
- requirements: requirements.txt
- requirements: requirements.txt
10 changes: 5 additions & 5 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
SPHINXOPTS =
SPHINXBUILD = sphinx-build
PAPER =
BUILDDIR = _build
BUILDDIR = _readthedocs

# Internal variables.
PAPEROPT_a4 = -D latex_paper_size=a4
Expand Down Expand Up @@ -56,9 +56,9 @@ html:

.PHONY: dirhtml
dirhtml:
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."

.PHONY: singlehtml
singlehtml:
Expand Down Expand Up @@ -226,6 +226,6 @@ dummy:

.PHONY: autobuild
autobuild:
sphinx-autobuild -nW -q -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
sphinx-autobuild --re-ignore='docs/_readthedocs/*' -nW -q -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo
@echo "Auto-build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
@echo "Auto-build finished. The HTML pages are in $(BUILDDIR)/html."
72 changes: 70 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))
import json
import os
import shutil

# -- General configuration ------------------------------------------------

Expand Down Expand Up @@ -98,7 +101,7 @@
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store','_static/docson']
exclude_patterns = ['_build', '_readthedocs', 'Thumbs.db', '.DS_Store','_static/docson']

# The reST default role (used for this markup: `text`) to use for all
# documents.
Expand Down Expand Up @@ -369,4 +372,69 @@

linkcheck_ignore = [
'https://linux.die.net/man/3/libuuid', # 403 Client Error: Forbidden for url
]
]

# Substitute branch name placeholders in schema.

def create_directory(path):
output_dir = os.path.dirname(path)
os.makedirs(output_dir, exist_ok=True)

def replace_substring_in_json(file_path, search_substring, replace_string, output_path=None):
# Read the JSON file
with open(file_path, 'r') as file:
data = json.load(file)

# Recursively search and replace the substring in the JSON data
_replace_substring_in_json(data, search_substring, replace_string)

# Set output path
if output_path is None:
output_path = file_path

# Create the directory if it does not exist
create_directory(output_path)

# Write the modified JSON data to the output file
with open(output_path, 'w') as file:
json.dump(data, file, indent=4)

def _replace_substring_in_json(data, search_substring, replace_string):
if isinstance(data, dict):
for key, value in data.items():
if isinstance(value, str):
data[key] = value.replace(search_substring, replace_string)
else:
_replace_substring_in_json(value, search_substring, replace_string)
elif isinstance(data, list):
for i, item in enumerate(data):
if isinstance(item, str):
data[i] = item.replace(search_substring, replace_string)
else:
_replace_substring_in_json(item, search_substring, replace_string)


def setup(app):
# Connect handlers to events
app.connect('config-inited', config_inited)
app.connect('env-before-read-docs', env_before_read_docs)
app.connect('build-finished', build_finished)


def config_inited(app, config):
shutil.copytree('../schema', '../.temp', dirs_exist_ok=True)

rtd_version = os.getenv('READTHEDOCS_VERSION')

# Replace {{version}} placeholders
if rtd_version is not None:
replace_substring_in_json('../.temp/network-schema.json', '{{version}}', rtd_version)


def env_before_read_docs(app, env, docnames):
create_directory('_readthedocs/html/')
shutil.copyfile('../.temp/network-schema.json', '_readthedocs/html/network-schema.json')


def build_finished(app, exception):
shutil.rmtree('../.temp')

0 comments on commit 7365a75

Please sign in to comment.