From ab6fa9f0e52e20f2e08ebd2c1ede22b590bd4648 Mon Sep 17 00:00:00 2001 From: jtc42 Date: Fri, 3 Jan 2020 00:21:16 +0000 Subject: [PATCH] Blackened --- openflexure_microscope/api/app.py | 8 +- .../api/v2/views/actions/camera.py | 8 +- .../api/v2/views/captures.py | 4 +- .../common/flask_labthings/decorators.py | 14 +-- .../common/flask_labthings/labthing.py | 34 +++--- .../common/flask_labthings/resource.py | 2 +- .../common/flask_labthings/spec.py | 102 ++++++++++-------- .../common/flask_labthings/utilities.py | 7 +- .../common/flask_labthings/views/plugins.py | 1 + .../common/flask_labthings/views/tasks.py | 1 + .../common/labthings_core/utilities.py | 3 +- 11 files changed, 104 insertions(+), 80 deletions(-) diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index 5ecacda3..4d486dd1 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -169,7 +169,7 @@ atexit.register(cleanup) if __name__ == "__main__": app.run(host="0.0.0.0", port="5000", threaded=True, debug=True, use_reloader=False) - #from pprint import pprint - #pprint(labthing.spec.to_dict()) - #with open('spec.yaml', 'w') as f: - # f.write(labthing.spec.to_yaml()) \ No newline at end of file + # from pprint import pprint + # pprint(labthing.spec.to_dict()) + # with open('spec.yaml', 'w') as f: + # f.write(labthing.spec.to_yaml()) diff --git a/openflexure_microscope/api/v2/views/actions/camera.py b/openflexure_microscope/api/v2/views/actions/camera.py index 7adec1e0..7ae5635c 100644 --- a/openflexure_microscope/api/v2/views/actions/camera.py +++ b/openflexure_microscope/api/v2/views/actions/camera.py @@ -1,7 +1,12 @@ from openflexure_microscope.api.utilities import get_bool, JsonResponse from openflexure_microscope.common.flask_labthings.resource import Resource from openflexure_microscope.common.flask_labthings.find import find_device -from openflexure_microscope.common.flask_labthings.decorators import use_args, marshal_with, doc, doc_response +from openflexure_microscope.common.flask_labthings.decorators import ( + use_args, + marshal_with, + doc, + doc_response, +) from openflexure_microscope.common.flask_labthings import fields from openflexure_microscope.utilities import filter_dict @@ -10,6 +15,7 @@ from openflexure_microscope.api.v2.views.captures import capture_schema import logging from flask import jsonify, request, abort, url_for, redirect, send_file + @doc(tags=["actions"]) class CaptureAPI(Resource): """ diff --git a/openflexure_microscope/api/v2/views/captures.py b/openflexure_microscope/api/v2/views/captures.py index 618419b6..bfb31e87 100644 --- a/openflexure_microscope/api/v2/views/captures.py +++ b/openflexure_microscope/api/v2/views/captures.py @@ -18,7 +18,9 @@ from marshmallow import pre_dump class CaptureSchema(Schema): id = fields.String() - file = fields.String(data_key="path", description="Path of file on microscope device") + file = fields.String( + data_key="path", description="Path of file on microscope device" + ) exists = fields.Bool(data_key="available") filename = fields.String() metadata = fields.Dict() diff --git a/openflexure_microscope/common/flask_labthings/decorators.py b/openflexure_microscope/common/flask_labthings/decorators.py index 40e0eb3e..dbab2caa 100644 --- a/openflexure_microscope/common/flask_labthings/decorators.py +++ b/openflexure_microscope/common/flask_labthings/decorators.py @@ -5,6 +5,7 @@ from flask import make_response from .utilities import rupdate from .spec import update_spec + def unpack(value): """Return a three tuple of data, code, and headers""" if not isinstance(value, tuple): @@ -93,14 +94,7 @@ class doc_response(object): def __call__(self, f): # Pass params to call function attribute for external access - f.__apispec__ = f.__dict__.get('__apispec__', {}) - d = { - "responses": { - self.code: { - "description": self.description, - **self.kwargs - } - } - } + f.__apispec__ = f.__dict__.get("__apispec__", {}) + d = {"responses": {self.code: {"description": self.description, **self.kwargs}}} rupdate(f.__apispec__, d) - return f \ No newline at end of file + return f diff --git a/openflexure_microscope/common/flask_labthings/labthing.py b/openflexure_microscope/common/flask_labthings/labthing.py index ea895b4b..f98fb4a8 100644 --- a/openflexure_microscope/common/flask_labthings/labthing.py +++ b/openflexure_microscope/common/flask_labthings/labthing.py @@ -17,6 +17,7 @@ from . import EXTENSION_NAME import logging + class LabThing(object): def __init__( self, @@ -60,32 +61,31 @@ class LabThing(object): self.init_app(app) @property - def description(self, ): + def description(self,): return self._description - + @description.setter def description(self, description: str): self._description = description self.spec.description = description - + @property - def title(self, ): + def title(self,): return self._title - + @title.setter def title(self, title: str): self._title = title self.spec.title = title - + @property - def version(self, ): + def version(self,): return str(self._version) - + @version.setter def version(self, version: str): self._version = version self.spec.version = version - ### Flask stuff @@ -117,7 +117,9 @@ class LabThing(object): # Add thing description self.app.add_url_rule(self._complete_url("/td", ""), "td", self.td) # Add swagger spec - self.app.add_url_rule(self._complete_url("/swagger", ""), "swagger", self.swagger) + self.app.add_url_rule( + self._complete_url("/swagger", ""), "swagger", self.swagger + ) # Add plugin overview self.add_resource(PluginListResource, "/plugins") @@ -212,7 +214,7 @@ class LabThing(object): if self.app is not None: self._register_view(self.app, resource, *urls, endpoint=endpoint, **kwargs) - + self.resources.append((resource, urls, endpoint, kwargs)) def resource(self, *urls, **kwargs): @@ -261,9 +263,7 @@ class LabThing(object): # Add the url to the application or blueprint app.add_url_rule(rule, view_func=resource_func, **kwargs) # Add the resource to our API spec - self.spec.path( - **view2path(rule, resource, self.spec) - ) + self.spec.path(**view2path(rule, resource, self.spec)) ### Utilities @@ -334,8 +334,8 @@ class LabThing(object): "tasks": { "href": self.url_for(TaskList, _external=True), **description_from_view(TaskList), - } - } + }, + }, } return jsonify(rr) @@ -344,4 +344,4 @@ class LabThing(object): """ OpenAPI v3 documentation """ - return jsonify(self.spec.to_dict()) \ No newline at end of file + return jsonify(self.spec.to_dict()) diff --git a/openflexure_microscope/common/flask_labthings/resource.py b/openflexure_microscope/common/flask_labthings/resource.py index 5b675654..fd2575e2 100644 --- a/openflexure_microscope/common/flask_labthings/resource.py +++ b/openflexure_microscope/common/flask_labthings/resource.py @@ -4,6 +4,7 @@ from flask.views import MethodView class Resource(MethodView): """Currently identical to MethodView """ + endpoint = None methods = ["get", "post", "put", "delete"] @@ -15,7 +16,6 @@ class Resource(MethodView): if hasattr(self, "__apispec__"): docs.update(self.__apispec__) - for meth in Resource.methods: if hasattr(self, meth) and hasattr(getattr(self, meth), "__apispec__"): docs["operations"][meth] = {} diff --git a/openflexure_microscope/common/flask_labthings/spec.py b/openflexure_microscope/common/flask_labthings/spec.py index 3bbf0633..dd53defa 100644 --- a/openflexure_microscope/common/flask_labthings/spec.py +++ b/openflexure_microscope/common/flask_labthings/spec.py @@ -1,27 +1,31 @@ - from .resource import Resource from .utilities import rupdate from apispec import APISpec from apispec.ext.marshmallow import MarshmallowPlugin -from openflexure_microscope.common.labthings_core.utilities import get_docstring, get_summary +from openflexure_microscope.common.labthings_core.utilities import ( + get_docstring, + get_summary, +) from .fields import Field from marshmallow import Schema as BaseSchema from collections import Mapping + def update_spec(obj, spec): - obj.__apispec__ = obj.__dict__.get('__apispec__', {}) - rupdate(obj.__apispec__, spec) - return obj.__apispec__ + obj.__apispec__ = obj.__dict__.get("__apispec__", {}) + rupdate(obj.__apispec__, spec) + return obj.__apispec__ + def view2path(rule: str, view: Resource, spec: APISpec): params = { "path": rule, # TODO: Validate this slightly (leading / etc) "operations": view2operations(view, spec), "description": get_docstring(view), - "summary": get_summary(view) + "summary": get_summary(view), } if hasattr(view, "__apispec__"): @@ -30,6 +34,7 @@ def view2path(rule: str, view: Resource, spec: APISpec): return params + def view2operations(view: Resource, spec: APISpec, populate_default: bool = True): ops = {} for method in Resource.methods: @@ -39,58 +44,67 @@ def view2operations(view: Resource, spec: APISpec, populate_default: bool = True ops[method] = { "responses": { 200: { - "description": get_summary(getattr(view, method)) or "Success" + "description": get_summary(getattr(view, method)) + or "Success" }, - 404: { - "description": "Resource not found" - } - }, + 404: {"description": "Resource not found"}, + } } else: ops[method] = {} - - rupdate(ops[method], { - "description": get_docstring(getattr(view, method)), - "summary": get_summary(getattr(view, method)) - }) - + + rupdate( + ops[method], + { + "description": get_docstring(getattr(view, method)), + "summary": get_summary(getattr(view, method)), + }, + ) + if hasattr(getattr(view, method), "__apispec__"): - rupdate(ops[method], doc2operation(getattr(view, method).__apispec__, spec)) - + rupdate( + ops[method], doc2operation(getattr(view, method).__apispec__, spec) + ) + return ops def doc2operation(apispec: dict, spec: APISpec): op = {} if "_params" in apispec: - rupdate(op, { - "requestBody": { - "content": { - "application/json": { - "schema": convert_schema(apispec.get("_params"), spec) - } - } - } - }) - - if "_schema" in apispec: - rupdate(op, - { - "responses": { - 200: { + rupdate( + op, + { + "requestBody": { "content": { "application/json": { - "schema": convert_schema(apispec.get("_schema"), spec) + "schema": convert_schema(apispec.get("_params"), spec) } - }, + } } - } - }) + }, + ) + + if "_schema" in apispec: + rupdate( + op, + { + "responses": { + 200: { + "content": { + "application/json": { + "schema": convert_schema(apispec.get("_schema"), spec) + } + } + } + } + }, + ) for key, val in apispec.items(): if not key in ["_params", "_schema"]: op[key] = val - + return op @@ -100,12 +114,14 @@ def convert_schema(schema, spec: APISpec): elif isinstance(schema, Mapping): return map2properties(schema, spec) else: - raise TypeError("Unsupported schema type. Ensure schema is a Schema class, or dictionary of Field objects") + raise TypeError( + "Unsupported schema type. Ensure schema is a Schema class, or dictionary of Field objects" + ) + def map2properties(schema, spec: APISpec): marshmallow_plugin = next( - plugin for plugin in spec.plugins - if isinstance(plugin, MarshmallowPlugin) + plugin for plugin in spec.plugins if isinstance(plugin, MarshmallowPlugin) ) converter = marshmallow_plugin.converter @@ -118,4 +134,4 @@ def map2properties(schema, spec: APISpec): else: d[k] = v - return {"properties": d} \ No newline at end of file + return {"properties": d} diff --git a/openflexure_microscope/common/flask_labthings/utilities.py b/openflexure_microscope/common/flask_labthings/utilities.py index 9fb110cf..4eff13ef 100644 --- a/openflexure_microscope/common/flask_labthings/utilities.py +++ b/openflexure_microscope/common/flask_labthings/utilities.py @@ -1,4 +1,7 @@ -from openflexure_microscope.common.labthings_core.utilities import get_docstring, get_summary +from openflexure_microscope.common.labthings_core.utilities import ( + get_docstring, + get_summary, +) from .schema import Schema, marshmallow, MARSHMALLOW_VERSION_INFO import collections.abc @@ -25,4 +28,4 @@ def rupdate(d, u): d[k] = rupdate(d.get(k, {}), v) else: d[k] = v - return d \ No newline at end of file + return d diff --git a/openflexure_microscope/common/flask_labthings/views/plugins.py b/openflexure_microscope/common/flask_labthings/views/plugins.py index c4e90c8c..df4d53b3 100644 --- a/openflexure_microscope/common/flask_labthings/views/plugins.py +++ b/openflexure_microscope/common/flask_labthings/views/plugins.py @@ -48,6 +48,7 @@ class PluginListResource(Resource): """ List and basic documentation for all enabled plugins """ + @marshal_with(PluginSchema(many=True)) def get(self): """ diff --git a/openflexure_microscope/common/flask_labthings/views/tasks.py b/openflexure_microscope/common/flask_labthings/views/tasks.py index 4b1450cf..8c00ec07 100644 --- a/openflexure_microscope/common/flask_labthings/views/tasks.py +++ b/openflexure_microscope/common/flask_labthings/views/tasks.py @@ -39,6 +39,7 @@ class TaskList(Resource): """ List and basic documentation for all session tasks """ + @marshal_with(TaskSchema(many=True)) def get(self): return tasks.tasks() diff --git a/openflexure_microscope/common/labthings_core/utilities.py b/openflexure_microscope/common/labthings_core/utilities.py index a4e880d3..f2795517 100644 --- a/openflexure_microscope/common/labthings_core/utilities.py +++ b/openflexure_microscope/common/labthings_core/utilities.py @@ -5,5 +5,6 @@ def get_docstring(obj): else: return "" + def get_summary(obj): - return get_docstring(obj).partition("\n")[0].strip() \ No newline at end of file + return get_docstring(obj).partition("\n")[0].strip()