Skip to content

Commit 021dc46

Browse files
committed
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]
1 parent 41186eb commit 021dc46

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

collect_api_endpoints.in

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{{ title }}
2+
{{ title_underline }}
3+
{% for controller in controllers %}
4+
.. csv-table:: {{controller.type}} ({{controller.filename}})
5+
:header: "Method", "Module", "Controller", "Command", "Parameters"
6+
:widths: 4, 15, 15, 30, 40
7+
{% for endpoint in controller.endpoints %}
8+
"``{{endpoint.method}}``","{{endpoint.module}}","{{endpoint.controller}}","{{endpoint.command}}","{{endpoint.parameters}}"
9+
{%- endfor %}
10+
{% endfor %}

collect_api_endpoints.py

+23-13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import os
2828
import argparse
2929
import re
30+
from jinja2 import Template
31+
3032
EXCLUDE_CONTROLLERS = ['Core/Api/FirmwareController.php']
3133

3234

@@ -103,17 +105,25 @@ def parse_api_php(src_filename):
103105
os.path.dirname(__file__), cmd_args.repo, module_name
104106
)
105107
print("update %s" % target_filename)
108+
template_data = {
109+
'title': "%s" % module_name.title(),
110+
'title_underline': "".join('~' for x in range(len(module_name))),
111+
'controllers': []
112+
}
113+
for controller in all_modules[module_name]:
114+
payload = {
115+
'type': controller[0]['type'],
116+
'filename': controller[0]['filename'],
117+
'endpoints': []
118+
}
119+
for endpoint in controller:
120+
payload['endpoints'].append(endpoint)
121+
template_data['controllers'].append(payload)
122+
106123
with open(target_filename, 'w') as f_out:
107-
f_out.write("%s\n" % module_name.title())
108-
f_out.write("".join('~' for x in range(len(module_name))))
109-
f_out.write("\n\n")
110-
for controller in all_modules[module_name]:
111-
f_out.write(".. csv-table:: %s (%s)\n" % (controller[0]['type'], controller[0]['filename']))
112-
f_out.write(" :header: \"Method\", \"Module\", \"Controller\", \"Command\", \"Parameters\"\n")
113-
f_out.write(" :widths: 4, 15, 15, 30, 40\n\n")
114-
for endpoint in controller:
115-
f_out.write(
116-
" \"``%(method)s``\",\"%(module)s\",\"%(controller)s\",\"%(command)s\",\"%(parameters)s\"\n"
117-
% endpoint
118-
)
119-
f_out.write("\n")
124+
if os.path.isfile("%s.in" % target_filename):
125+
template_filename = "%s.in" % target_filename
126+
else:
127+
template_filename = "collect_api_endpoints.in"
128+
template = Template(open(template_filename, "r").read())
129+
f_out.write(template.render(template_data))

0 commit comments

Comments
 (0)