diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index e7bddaa3..4819f740 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -1,10 +1,7 @@ #!/usr/bin/env python -""" -TODO: Implement API route to cleanly shut down server -""" from flask import ( - Flask, render_template) + Flask, render_template, jsonify) from flask.logging import default_handler from serial import SerialException @@ -12,6 +9,7 @@ from serial import SerialException from flask_cors import CORS from openflexure_microscope.api.exceptions import JSONExceptionHandler +from openflexure_microscope.api.utilities import list_routes from openflexure_microscope import Microscope from openflexure_microscope.camera.pi import StreamingCamera @@ -89,15 +87,6 @@ def attach_microscope(): ##### WEBAPP ROUTES ###### -@app.route('/') -def index(): - """ - API demo app - """ - return render_template( - 'index_v1.html' - ) - ##### API ROUTES ###### from openflexure_microscope.api.v1 import blueprints @@ -121,6 +110,22 @@ app.register_blueprint(plugin_blueprint, url_prefix=uri('/plugin', 'v1')) task_blueprint = blueprints.task.construct_blueprint(api_microscope) app.register_blueprint(task_blueprint, url_prefix=uri('/task', 'v1')) +@app.route('/') +def index(): + """ + API demo app + """ + return render_template( + 'index_v1.html' + ) + +@app.route('/routes') +def routes(): + """ + List of all connected API routes + """ + return jsonify(list_routes(app)) + # Automatically clean up microscope at exit def cleanup(): global api_microscope @@ -138,9 +143,7 @@ def cleanup(): api_microscope.close() logging.debug("App teardown complete.") - atexit.register(cleanup) - if __name__ == "__main__": app.run(host='0.0.0.0', port="5000", threaded=True, debug=True, use_reloader=False) \ No newline at end of file diff --git a/openflexure_microscope/api/static/main_v1.js b/openflexure_microscope/api/static/main_v1.js index 71710062..588ba180 100644 --- a/openflexure_microscope/api/static/main_v1.js +++ b/openflexure_microscope/api/static/main_v1.js @@ -379,8 +379,6 @@ addEventListener("keydown", function (e) { // If not currently in an input box if (!(e.target instanceof HTMLInputElement)) { - - // TODO: Remove this if condition? Pointless? // If stage movement keys are pressed if ((leftKeyID in keysDown) || (rightKeyID in keysDown) || (upKeyID in keysDown) || (downKeyID in keysDown) || (pgupKeyID in keysDown) || (pgdnKeyID in keysDown)) { // Calculate movement array diff --git a/openflexure_microscope/api/utilities.py b/openflexure_microscope/api/utilities.py index 3a96ede9..27dbeb7b 100644 --- a/openflexure_microscope/api/utilities.py +++ b/openflexure_microscope/api/utilities.py @@ -1,7 +1,7 @@ import pprint import logging from werkzeug.exceptions import BadRequest - +from flask import url_for class JsonPayload: def __init__(self, request): @@ -69,5 +69,20 @@ def get_bool(get_arg): def list_routes(app): - """Print available functions.""" - pprint.pprint(list(map(lambda x: repr(x), app.url_map.iter_rules()))) + output = {} + for rule in app.url_map.iter_rules(): + + options = {} + for arg in rule.arguments: + options[arg] = "[{0}]".format(arg) + + endpoint = rule.endpoint + methods = list(rule.methods) + url = url_for(rule.endpoint, **options) + line = { + 'endpoint': endpoint, + 'methods': methods + } + output[url] = line + + return output \ No newline at end of file diff --git a/openflexure_microscope/camera/base.py b/openflexure_microscope/camera/base.py index 5dc30f6a..f6cd3e71 100644 --- a/openflexure_microscope/camera/base.py +++ b/openflexure_microscope/camera/base.py @@ -345,7 +345,7 @@ class BaseCamera(object): write_to_file: bool = True, temporary: bool = False, filename: str = None, - folder: str = None, + folder: str = "", fmt: str = 'h264'): """ diff --git a/openflexure_microscope/camera/capture.py b/openflexure_microscope/camera/capture.py index 8b8ec94a..84febb58 100644 --- a/openflexure_microscope/camera/capture.py +++ b/openflexure_microscope/camera/capture.py @@ -25,9 +25,10 @@ def clear_tmp(): """ global TEMP_CAPTURE_PATH - logging.info("Clearing {}...".format(TEMP_CAPTURE_PATH)) - shutil.rmtree(TEMP_CAPTURE_PATH) - logging.debug("Cleared {}.".format(TEMP_CAPTURE_PATH)) + if os.path.isdir(TEMP_CAPTURE_PATH): + logging.info("Clearing {}...".format(TEMP_CAPTURE_PATH)) + shutil.rmtree(TEMP_CAPTURE_PATH) + logging.debug("Cleared {}.".format(TEMP_CAPTURE_PATH)) def pull_usercomment_dict(filepath): @@ -37,6 +38,7 @@ def pull_usercomment_dict(filepath): filepath: Path to the Exif-containing file """ exif_dict = piexif.load(filepath) + if 'Exif' in exif_dict and 37510 in exif_dict['Exif']: return yaml.load(exif_dict['Exif'][37510].decode()) else: @@ -80,9 +82,12 @@ def capture_from_dict(capture_dict): ) # Create a placeholder capture capture.split_file_path(capture.file) + capture.temporary = capture_dict['temporary'] + if capture.format.upper() in EXIF_FORMATS: md_exif = pull_usercomment_dict(capture.file) else: + logging.debug("Unsupported format for EXIF data. Skipping.") md_exif = {} md_database = capture_dict['metadata'] @@ -95,6 +100,9 @@ def capture_from_dict(capture_dict): capture._metadata = extract_with_priority('custom', md_exif, md_database) capture.tags = extract_with_priority('tags', md_exif, md_database) + + capture.initialise_stream() + return capture diff --git a/openflexure_microscope/stage/openflexure.py b/openflexure_microscope/stage/openflexure.py index 6b5c8585..7f7a5472 100644 --- a/openflexure_microscope/stage/openflexure.py +++ b/openflexure_microscope/stage/openflexure.py @@ -5,7 +5,6 @@ from openflexure_microscope.lock import StrictLock import logging -# TODO: Implement lock on movement class Stage(OpenFlexureStage): def __init__(self, *args, **kwargs): """ diff --git a/pyproject.toml b/pyproject.toml index a3240034..cd7e76ab 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api" [tool.poetry] name = "openflexure_microscope" -version = "1.0.0-rc.3" +version = "1.0.0-rc.4" description = "Python module, and Flask-based web API, to run the OpenFlexure Microscope." authors = [