From 3a4a6236f4d15a26f70069ad5667d6dd59be99ef Mon Sep 17 00:00:00 2001 From: Jatin Arora Date: Mon, 9 Sep 2024 16:53:31 +0530 Subject: [PATCH] feat: add ams configuration generation at build time This commit modifies the configuration in sphinx to generate ams configuration at build and inject it as well. --- custom_conf.py | 16 ++++++++++++++-- scripts/{parse.py => ams_configuration.py} | 18 +++++++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) rename scripts/{parse.py => ams_configuration.py} (87%) diff --git a/custom_conf.py b/custom_conf.py index e900d6fa..373b95fd 100644 --- a/custom_conf.py +++ b/custom_conf.py @@ -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. # @@ -210,7 +212,17 @@ ## 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. +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) diff --git a/scripts/parse.py b/scripts/ams_configuration.py similarity index 87% rename from scripts/parse.py rename to scripts/ams_configuration.py index 0fffd6b4..49a1ec3d 100755 --- a/scripts/parse.py +++ b/scripts/ams_configuration.py @@ -2,6 +2,7 @@ import argparse import json +from typing import Dict import requests from jinja2 import Environment @@ -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)