Loading openflexure_microscope/common/flask_labthings/decorators.py +25 −2 Original line number Diff line number Diff line Loading @@ -3,9 +3,8 @@ from functools import wraps, update_wrapper from flask import make_response from openflexure_microscope.common.labthings_core.utilities import rupdate from openflexure_microscope.common.labthings_core.spec import update_spec from openflexure_microscope.common.labthings_core.decorators import doc, doc_response from .spec import update_spec def unpack(value): Loading Loading @@ -76,3 +75,27 @@ class use_kwargs(use_args): """ kwargs["as_kwargs"] = True use_args.__init__(self, schema, **kwargs) class doc(object): def __init__(self, **kwargs): self.kwargs = kwargs def __call__(self, f): # Pass params to call function attribute for external access update_spec(f, self.kwargs) return f class doc_response(object): def __init__(self, code, description, **kwargs): self.code = code self.description = description self.kwargs = kwargs 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}}} rupdate(f.__apispec__, d) return f openflexure_microscope/common/flask_labthings/fields.py +1 −1 Original line number Diff line number Diff line from openflexure_microscope.common.labthings_core.fields import * from marshmallow.fields import * openflexure_microscope/common/flask_labthings/labthing.py +2 −3 Original line number Diff line number Diff line Loading @@ -2,16 +2,15 @@ from flask import url_for, jsonify from apispec import APISpec from apispec.ext.marshmallow import MarshmallowPlugin from . import EXTENSION_NAME from .extensions import BaseExtension from .utilities import description_from_view from .spec import view2path from .views.extensions import ExtensionList from .views.tasks import TaskList, TaskResource from openflexure_microscope.common.labthings_core.utilities import get_docstring from openflexure_microscope.common.labthings_core.spec import view2path from . import EXTENSION_NAME import logging Loading openflexure_microscope/common/flask_labthings/resource.py +18 −3 Original line number Diff line number Diff line from flask.views import MethodView from openflexure_microscope.common.labthings_core.resource import BaseResource class Resource(MethodView, BaseResource): """Currently identical to MethodView class Resource(MethodView): """ A LabThing Resource class should make use of functions get(), put(), post(), and delete() corresponding to HTTP methods. These functions will allow for automated documentation generation """ methods = ["get", "post", "put", "delete"] endpoint = None def __init__(self, *args, **kwargs): MethodView.__init__(self, *args, **kwargs) def doc(self): docs = {"operations": {}} if hasattr(self, "__apispec__"): docs.update(self.__apispec__) for meth in BaseResource.methods: if hasattr(self, meth) and hasattr(getattr(self, meth), "__apispec__"): docs["operations"][meth] = {} docs["operations"][meth] = getattr(self, meth).__apispec__ return docs openflexure_microscope/common/flask_labthings/schema.py +39 −1 Original line number Diff line number Diff line from openflexure_microscope.common.labthings_core.schema import * # -*- coding: utf-8 -*- import flask import marshmallow MARSHMALLOW_VERSION_INFO = tuple( [int(part) for part in marshmallow.__version__.split(".") if part.isdigit()] ) sentinel = object() class Schema(marshmallow.Schema): """Base serializer with which to define custom serializers. See `marshmallow.Schema` for more details about the `Schema` API. """ def jsonify(self, obj, many=sentinel, *args, **kwargs): """Return a JSON response containing the serialized data. :param obj: Object to serialize. :param bool many: Whether `obj` should be serialized as an instance or as a collection. If unset, defaults to the value of the `many` attribute on this Schema. :param kwargs: Additional keyword arguments passed to `flask.jsonify`. .. versionchanged:: 0.6.0 Takes the same arguments as `marshmallow.Schema.dump`. Additional keyword arguments are passed to `flask.jsonify`. .. versionchanged:: 0.6.3 The `many` argument for this method defaults to the value of the `many` attribute on the Schema. Previously, the `many` argument of this method defaulted to False, regardless of the value of `Schema.many`. """ if many is sentinel: many = self.many if MARSHMALLOW_VERSION_INFO[0] >= 3: data = self.dump(obj, many=many) else: data = self.dump(obj, many=many).data return flask.jsonify(data, *args, **kwargs) Loading
openflexure_microscope/common/flask_labthings/decorators.py +25 −2 Original line number Diff line number Diff line Loading @@ -3,9 +3,8 @@ from functools import wraps, update_wrapper from flask import make_response from openflexure_microscope.common.labthings_core.utilities import rupdate from openflexure_microscope.common.labthings_core.spec import update_spec from openflexure_microscope.common.labthings_core.decorators import doc, doc_response from .spec import update_spec def unpack(value): Loading Loading @@ -76,3 +75,27 @@ class use_kwargs(use_args): """ kwargs["as_kwargs"] = True use_args.__init__(self, schema, **kwargs) class doc(object): def __init__(self, **kwargs): self.kwargs = kwargs def __call__(self, f): # Pass params to call function attribute for external access update_spec(f, self.kwargs) return f class doc_response(object): def __init__(self, code, description, **kwargs): self.code = code self.description = description self.kwargs = kwargs 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}}} rupdate(f.__apispec__, d) return f
openflexure_microscope/common/flask_labthings/fields.py +1 −1 Original line number Diff line number Diff line from openflexure_microscope.common.labthings_core.fields import * from marshmallow.fields import *
openflexure_microscope/common/flask_labthings/labthing.py +2 −3 Original line number Diff line number Diff line Loading @@ -2,16 +2,15 @@ from flask import url_for, jsonify from apispec import APISpec from apispec.ext.marshmallow import MarshmallowPlugin from . import EXTENSION_NAME from .extensions import BaseExtension from .utilities import description_from_view from .spec import view2path from .views.extensions import ExtensionList from .views.tasks import TaskList, TaskResource from openflexure_microscope.common.labthings_core.utilities import get_docstring from openflexure_microscope.common.labthings_core.spec import view2path from . import EXTENSION_NAME import logging Loading
openflexure_microscope/common/flask_labthings/resource.py +18 −3 Original line number Diff line number Diff line from flask.views import MethodView from openflexure_microscope.common.labthings_core.resource import BaseResource class Resource(MethodView, BaseResource): """Currently identical to MethodView class Resource(MethodView): """ A LabThing Resource class should make use of functions get(), put(), post(), and delete() corresponding to HTTP methods. These functions will allow for automated documentation generation """ methods = ["get", "post", "put", "delete"] endpoint = None def __init__(self, *args, **kwargs): MethodView.__init__(self, *args, **kwargs) def doc(self): docs = {"operations": {}} if hasattr(self, "__apispec__"): docs.update(self.__apispec__) for meth in BaseResource.methods: if hasattr(self, meth) and hasattr(getattr(self, meth), "__apispec__"): docs["operations"][meth] = {} docs["operations"][meth] = getattr(self, meth).__apispec__ return docs
openflexure_microscope/common/flask_labthings/schema.py +39 −1 Original line number Diff line number Diff line from openflexure_microscope.common.labthings_core.schema import * # -*- coding: utf-8 -*- import flask import marshmallow MARSHMALLOW_VERSION_INFO = tuple( [int(part) for part in marshmallow.__version__.split(".") if part.isdigit()] ) sentinel = object() class Schema(marshmallow.Schema): """Base serializer with which to define custom serializers. See `marshmallow.Schema` for more details about the `Schema` API. """ def jsonify(self, obj, many=sentinel, *args, **kwargs): """Return a JSON response containing the serialized data. :param obj: Object to serialize. :param bool many: Whether `obj` should be serialized as an instance or as a collection. If unset, defaults to the value of the `many` attribute on this Schema. :param kwargs: Additional keyword arguments passed to `flask.jsonify`. .. versionchanged:: 0.6.0 Takes the same arguments as `marshmallow.Schema.dump`. Additional keyword arguments are passed to `flask.jsonify`. .. versionchanged:: 0.6.3 The `many` argument for this method defaults to the value of the `many` attribute on the Schema. Previously, the `many` argument of this method defaulted to False, regardless of the value of `Schema.many`. """ if many is sentinel: many = self.many if MARSHMALLOW_VERSION_INFO[0] >= 3: data = self.dump(obj, many=many) else: data = self.dump(obj, many=many).data return flask.jsonify(data, *args, **kwargs)