From 202d942c90a4550a54cbb30d593ac4694a9fd7b1 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 6 Jul 2021 15:23:42 +0100 Subject: [PATCH] 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. --- openflexure_microscope/api/app.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index 9bc7f9d9..1739bec0 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -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__":