Skip to content

Commit

Permalink
feat: add ams configuration generation at build time
Browse files Browse the repository at this point in the history
This commit modifies the configuration in sphinx to generate ams
configuration at build and inject it as well.
  • Loading branch information
jat-canonical committed Sep 9, 2024
1 parent f0e36d5 commit 3819416
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
4 changes: 2 additions & 2 deletions build_requirements.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

sys.path.append('./')
from custom_conf import *
sys.path.append("./")
from custom_conf import custom_extensions, custom_required_modules, redirects

# The file contains helper functions and the mechanism to build the
# .sphinx/requirements.txt file that is needed to set up the virtual
Expand Down
2 changes: 2 additions & 0 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@
if 'github_issues' in html_context and html_context['github_issues'] and not disable_feedback_button:
html_js_files.append('github_issue_links.js')
html_js_files.extend(custom_html_js_files)

generate_ams_configuration()
17 changes: 15 additions & 2 deletions custom_conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import datetime

from scripts.ams_configuration import get_swagger_from_url, parse_swagger

# Custom configuration for the Sphinx documentation builder.
# All configuration specific to your project should be done in this file.
#
Expand Down Expand Up @@ -210,7 +212,18 @@
## Add any configuration that is not covered by the common conf.py file.

# Define a :center: role that can be used to center the content of table cells.
rst_prolog = '''
rst_prolog = """
.. role:: center
:class: align-center
'''
"""


## Generate dynamic configuration using scripts
# Inject AMS configuration valuues and Node configuration values from the swagger
# specification hosted on Github.
def generate_ams_configuration():
with open("scripts/requirements.txt", "r") as f:
for req in f.readlines():
custom_required_modules.append(req)
ams_configuration_file = "reference/ams-configuration.md"
parse_swagger(get_swagger_from_url(), ams_configuration_file)
File renamed without changes.
18 changes: 13 additions & 5 deletions scripts/parse.py → scripts/ams_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import argparse
import json
from typing import Dict

import requests
from jinja2 import Environment
Expand Down Expand Up @@ -29,22 +30,29 @@ def main() -> int:
"--output",
dest="output_file",
help="Destination of the rendered configuration file",
default="ams-configuration.md",
default="reference/ams-configuration.md",
)
args = parser.parse_args()
if args.swagger_path:
with open(args.swagger_path, mode="r") as f:
swagger = json.load(f)
else:
response = requests.get(SWAGGER_URL)
swagger = response.json()
swagger = get_swagger_from_url()
parse_swagger(swagger, args.output_file)


def get_swagger_from_url() -> Dict:
response = requests.get(SWAGGER_URL)
return response.json()


def parse_swagger(swagger, output_file):
configs = _parse_config_schema(swagger)
nodes = _parse_node_schema(swagger)
env = Environment(loader=FileSystemLoader("."))
templ = env.get_template("template.md.j2")
templ = env.get_template("scripts/template.md.j2")
text = templ.render(configs=configs, nodes=nodes)
with open(args.output_file, mode="w+") as op:
with open(output_file, mode="w+") as op:
op.write(text)


Expand Down

0 comments on commit 3819416

Please sign in to comment.