From 0907d013cdd3df80c838a3ce6321a307acd42309 Mon Sep 17 00:00:00 2001 From: jtc42 Date: Fri, 15 Nov 2019 16:51:48 +0000 Subject: [PATCH] Tidied and improved JSON representations --- .../api/v2/blueprints/actions/__init__.py | 2 -- .../api/v2/blueprints/actions/system.py | 8 ++------ openflexure_microscope/api/v2/blueprints/captures.py | 12 ++++-------- openflexure_microscope/api/v2/blueprints/tasks.py | 4 ++-- .../plugins/default/autofocus/api.py | 1 + openflexure_microscope/task.py | 2 +- 6 files changed, 10 insertions(+), 19 deletions(-) diff --git a/openflexure_microscope/api/v2/blueprints/actions/__init__.py b/openflexure_microscope/api/v2/blueprints/actions/__init__.py index 6900e796..c2270552 100644 --- a/openflexure_microscope/api/v2/blueprints/actions/__init__.py +++ b/openflexure_microscope/api/v2/blueprints/actions/__init__.py @@ -45,7 +45,6 @@ _actions = { def enabled_actions(): global _actions - return {k: v for k, v in _actions.items() if v["conditions"]} @@ -53,7 +52,6 @@ def actions_representation(): global _actions actions = {} - for name, action in enabled_actions().items(): d = { "links": {"self": url_for(f".{name}")}, diff --git a/openflexure_microscope/api/v2/blueprints/actions/system.py b/openflexure_microscope/api/v2/blueprints/actions/system.py index 6e3d9b0d..86878026 100644 --- a/openflexure_microscope/api/v2/blueprints/actions/system.py +++ b/openflexure_microscope/api/v2/blueprints/actions/system.py @@ -1,12 +1,8 @@ from openflexure_microscope.api.views import MicroscopeView -from flask import jsonify - import subprocess -import logging -# TODO: Make robust against different host OS class ShutdownAPI(MicroscopeView): def post(self): """ @@ -17,7 +13,7 @@ class ShutdownAPI(MicroscopeView): """ subprocess.Popen(['shutdown', '-h', 'now']) - return '', 202 + return "{}", 201 class RebootAPI(MicroscopeView): @@ -30,4 +26,4 @@ class RebootAPI(MicroscopeView): """ subprocess.Popen(['sudo', 'shutdown', '-r', 'now']) - return '', 202 + return "{}", 201 diff --git a/openflexure_microscope/api/v2/blueprints/captures.py b/openflexure_microscope/api/v2/blueprints/captures.py index 0ffc3dff..f9bae501 100644 --- a/openflexure_microscope/api/v2/blueprints/captures.py +++ b/openflexure_microscope/api/v2/blueprints/captures.py @@ -61,9 +61,7 @@ class ListAPI(MicroscopeView): :status 200: capture found :status 404: no capture found with that id """ - include_unavailable = get_bool(request.args.get("include_unavailable")) - - representation = captures_representation(self.microscope.camera.images, include_unavailable=include_unavailable) + representation = captures_representation(self.microscope.camera.images) return jsonify(representation) @@ -132,11 +130,9 @@ class CaptureAPI(MicroscopeView): """ - all_captures = captures_representation(self.microscope.camera.images, include_unavailable=True) - - if capture_id in all_captures: - representation = all_captures[capture_id] - else: + try: + representation = captures_representation(self.microscope.camera.images)[capture_id] + except KeyError: return abort(404) # 404 Not Found return jsonify(representation) diff --git a/openflexure_microscope/api/v2/blueprints/tasks.py b/openflexure_microscope/api/v2/blueprints/tasks.py index d2a5c440..327dfe02 100644 --- a/openflexure_microscope/api/v2/blueprints/tasks.py +++ b/openflexure_microscope/api/v2/blueprints/tasks.py @@ -121,7 +121,7 @@ class TaskAPI(MethodView): """ try: - task = tasks.tasks()[task_id] + task = tasks_representation()[task_id] except KeyError: return abort(404) # 404 Not Found @@ -140,7 +140,7 @@ class TaskAPI(MethodView): """ try: - task = tasks.tasks()[task_id] + task = tasks_representation()[task_id] except KeyError: return abort(404) # 404 Not Found diff --git a/openflexure_microscope/plugins/default/autofocus/api.py b/openflexure_microscope/plugins/default/autofocus/api.py index 100f5f13..5f646921 100644 --- a/openflexure_microscope/plugins/default/autofocus/api.py +++ b/openflexure_microscope/plugins/default/autofocus/api.py @@ -34,6 +34,7 @@ class AutofocusAPI(MicroscopeViewPlugin): else: abort(503, 'No stage connected. Unable to autofocus.') + class FastAutofocusAPI(MicroscopeViewPlugin): def post(self): payload = JsonResponse(request) diff --git a/openflexure_microscope/task.py b/openflexure_microscope/task.py index e9dd7e3d..c195e3fc 100644 --- a/openflexure_microscope/task.py +++ b/openflexure_microscope/task.py @@ -7,7 +7,7 @@ from openflexure_microscope.common.tasks import ( remove_task, ) - +# DEPRECATED class TaskOrchestrator: """ DEPRECATED: Class responsible for spawning threaded tasks, and storing their returns.