From 021dc468180eeb20180890eb60eaad4b006aef39 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Wed, 11 Mar 2020 09:58:12 +0100 Subject: [PATCH] collect_api_endpoints.py: separate template and parser and support custom overwrites in the target directory using the format [my_target_api_file.rst.in] --- collect_api_endpoints.in | 10 ++++++++++ collect_api_endpoints.py | 36 +++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 collect_api_endpoints.in diff --git a/collect_api_endpoints.in b/collect_api_endpoints.in new file mode 100644 index 00000000..f60d3f26 --- /dev/null +++ b/collect_api_endpoints.in @@ -0,0 +1,10 @@ +{{ title }} +{{ title_underline }} +{% for controller in controllers %} +.. csv-table:: {{controller.type}} ({{controller.filename}}) + :header: "Method", "Module", "Controller", "Command", "Parameters" + :widths: 4, 15, 15, 30, 40 +{% for endpoint in controller.endpoints %} + "``{{endpoint.method}}``","{{endpoint.module}}","{{endpoint.controller}}","{{endpoint.command}}","{{endpoint.parameters}}" +{%- endfor %} +{% endfor %} diff --git a/collect_api_endpoints.py b/collect_api_endpoints.py index c2d4b88d..a8435c36 100755 --- a/collect_api_endpoints.py +++ b/collect_api_endpoints.py @@ -27,6 +27,8 @@ import os import argparse import re +from jinja2 import Template + EXCLUDE_CONTROLLERS = ['Core/Api/FirmwareController.php'] @@ -103,17 +105,25 @@ if __name__ == '__main__': os.path.dirname(__file__), cmd_args.repo, module_name ) print("update %s" % target_filename) + template_data = { + 'title': "%s" % module_name.title(), + 'title_underline': "".join('~' for x in range(len(module_name))), + 'controllers': [] + } + for controller in all_modules[module_name]: + payload = { + 'type': controller[0]['type'], + 'filename': controller[0]['filename'], + 'endpoints': [] + } + for endpoint in controller: + payload['endpoints'].append(endpoint) + template_data['controllers'].append(payload) + with open(target_filename, 'w') as f_out: - f_out.write("%s\n" % module_name.title()) - f_out.write("".join('~' for x in range(len(module_name)))) - f_out.write("\n\n") - for controller in all_modules[module_name]: - f_out.write(".. csv-table:: %s (%s)\n" % (controller[0]['type'], controller[0]['filename'])) - f_out.write(" :header: \"Method\", \"Module\", \"Controller\", \"Command\", \"Parameters\"\n") - f_out.write(" :widths: 4, 15, 15, 30, 40\n\n") - for endpoint in controller: - f_out.write( - " \"``%(method)s``\",\"%(module)s\",\"%(controller)s\",\"%(command)s\",\"%(parameters)s\"\n" - % endpoint - ) - f_out.write("\n") + if os.path.isfile("%s.in" % target_filename): + template_filename = "%s.in" % target_filename + else: + template_filename = "collect_api_endpoints.in" + template = Template(open(template_filename, "r").read()) + f_out.write(template.render(template_data))