Add entry point to generate OpenAPI yaml/json

This is designed to be used as a console entry point so we can generate
the OpenAPI description without starting the server.
This commit is contained in:
Richard 2021-07-06 15:23:42 +01:00
parent 1e21ce51fc
commit 202d942c90

View file

@ -1,4 +1,5 @@
#!/usr/bin/env python
import argparse
import atexit
import logging
import logging.handlers
@ -243,6 +244,27 @@ def ofm_serve():
server: Server = Server(app)
server.run(host="0.0.0.0", port=5000, debug=debug_app, zeroconf=True)
def generate_openapi():
parser = argparse.ArgumentParser("Generate an OpenAPI specification document")
parser.add_argument(
'-o',
dest='output',
default="openapi.yaml",
help=(
'Specify the output filename. If it ends in .json, we output JSON.'
'Use .yml or .yaml for YAML (which is the default'
)
)
args = parser.parse_args()
fname = args.output
if fname.endswith(".json"):
import json
with open(fname, 'w') as fd:
json.dump(labthing.spec.to_dict(), fd)
else:
with open(fname, 'w') as fd:
fd.write(labthing.spec.to_yaml())
# Start the app if the module is run directly
if __name__ == "__main__":