diff --git a/labthings/server/decorators.py b/labthings/server/decorators.py index ecb0a298..bc6a8a6a 100644 --- a/labthings/server/decorators.py +++ b/labthings/server/decorators.py @@ -164,13 +164,9 @@ def __init__(self, code, description=None, mimetype=None, **kwargs): } if self.mimetype: - self.response_dict.update({ - "responses": { - self.code: { - "content": {self.mimetype: {}} - } - } - }) + self.response_dict.update( + {"responses": {self.code: {"content": {self.mimetype: {}}}}} + ) def __call__(self, f): # Pass params to call function attribute for external access diff --git a/labthings/server/extensions.py b/labthings/server/extensions.py index 987563a0..93d48093 100644 --- a/labthings/server/extensions.py +++ b/labthings/server/extensions.py @@ -8,11 +8,7 @@ import os import glob -from ..core.utilities import ( - get_docstring, - camel_to_snake, - snake_to_spine, -) +from ..core.utilities import get_docstring, camel_to_snake, snake_to_spine class BaseExtension: diff --git a/labthings/server/labthing.py b/labthings/server/labthing.py index bbbd8cf7..3c82be29 100644 --- a/labthings/server/labthing.py +++ b/labthings/server/labthing.py @@ -219,7 +219,7 @@ def _register_view(self, app, view, *urls, endpoint=None, **kwargs): # 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, view, self.spec)) + # self.spec.path(**view2path(rule, view, self.spec)) # TEST: Getting Flask rule objects flask_rules = app.url_map._rules_by_endpoint.get(endpoint) diff --git a/labthings/server/spec/__init__.py b/labthings/server/spec/__init__.py index 4ddad04f..1f9b4334 100644 --- a/labthings/server/spec/__init__.py +++ b/labthings/server/spec/__init__.py @@ -2,11 +2,7 @@ from apispec import APISpec from apispec.ext.marshmallow import MarshmallowPlugin -from ...core.utilities import ( - get_docstring, - get_summary, - rupdate, -) +from ...core.utilities import get_docstring, get_summary, rupdate from ..fields import Field from marshmallow import Schema as BaseSchema @@ -40,9 +36,7 @@ def rule2path(rule: Rule, view: View, spec: APISpec): # Add URL arguments if rule.arguments: for op in params.get("operations").keys(): - params["operations"][op].update({ - "parameters": rule_to_params(rule) - }) + params["operations"][op].update({"parameters": rule_to_params(rule)}) # Add extra parameters if hasattr(view, "__apispec__"): diff --git a/labthings/server/spec/paths.py b/labthings/server/spec/paths.py index 6c70c2c4..55f49fec 100644 --- a/labthings/server/spec/paths.py +++ b/labthings/server/spec/paths.py @@ -4,47 +4,45 @@ import werkzeug.routing -PATH_RE = re.compile(r'<(?:[^:<>]+:)?([^<>]+)>') +PATH_RE = re.compile(r"<(?:[^:<>]+:)?([^<>]+)>") def rule_to_path(rule): - return PATH_RE.sub(r'{\1}', rule.rule) + return PATH_RE.sub(r"{\1}", rule.rule) # Conversion map of werkzeug rule converters to Javascript schema types CONVERTER_MAPPING = { - werkzeug.routing.UnicodeConverter: ('string', None), - werkzeug.routing.IntegerConverter: ('integer', 'int32'), - werkzeug.routing.FloatConverter: ('number', 'float'), + werkzeug.routing.UnicodeConverter: ("string", None), + werkzeug.routing.IntegerConverter: ("integer", "int32"), + werkzeug.routing.FloatConverter: ("number", "float"), } -DEFAULT_TYPE = ('string', None) +DEFAULT_TYPE = ("string", None) def rule_to_params(rule, overrides=None): - overrides = (overrides or {}) + overrides = overrides or {} result = [ argument_to_param(argument, rule, overrides.get(argument, {})) for argument in rule.arguments ] for key in overrides.keys(): - if overrides[key].get('in') in ('header', 'query'): - overrides[key]['name'] = overrides[key].get('name', key) + if overrides[key].get("in") in ("header", "query"): + overrides[key]["name"] = overrides[key].get("name", key) result.append(overrides[key]) return result def argument_to_param(argument, rule, override=None): - param = { - 'in': 'path', - 'name': argument, - 'required': True, - } - type_, format_ = CONVERTER_MAPPING.get(type(rule._converters[argument]), DEFAULT_TYPE) - param['type'] = type_ + param = {"in": "path", "name": argument, "required": True} + type_, format_ = CONVERTER_MAPPING.get( + type(rule._converters[argument]), DEFAULT_TYPE + ) + param["type"] = type_ if format_ is not None: - param['format'] = format_ + param["format"] = format_ if rule.defaults and argument in rule.defaults: - param['default'] = rule.defaults[argument] + param["default"] = rule.defaults[argument] param.update(override or {}) return param diff --git a/labthings/server/utilities.py b/labthings/server/utilities.py index bc9821f6..404bf630 100644 --- a/labthings/server/utilities.py +++ b/labthings/server/utilities.py @@ -1,7 +1,4 @@ -from ..core.utilities import ( - get_docstring, - get_summary, -) +from ..core.utilities import get_docstring, get_summary from .view import View diff --git a/labthings/server/views/docs/__init__.py b/labthings/server/views/docs/__init__.py index 05ef5248..ef09d3d0 100644 --- a/labthings/server/views/docs/__init__.py +++ b/labthings/server/views/docs/__init__.py @@ -1,4 +1,12 @@ -from flask import abort, url_for, jsonify, render_template, Blueprint, current_app, request +from flask import ( + abort, + url_for, + jsonify, + render_template, + Blueprint, + current_app, + request, +) from labthings.core.utilities import get_docstring @@ -36,7 +44,7 @@ class W3CThingDescriptionView(View): """ def get(self): - base_url = request.host_url.rstrip('/') + base_url = request.host_url.rstrip("/") props = {} for key, prop in current_labthing().properties.items(): @@ -53,20 +61,14 @@ def get(self): hasattr(prop, "post") or hasattr(prop, "put") or hasattr(prop, "delete") ) props[key]["writeOnly"] = not hasattr(prop, "get") - props[key]["links"] = [ - {"href": f"{base_url}{url}"} for url in prop_urls - ] + props[key]["links"] = [{"href": f"{base_url}{url}"} for url in prop_urls] props[key]["uriVariables"] = {} for prop_rule in prop_rules: params = rule_to_params(prop_rule) params_dict = {} for param in params: - params_dict.update({ - param.get("name"): { - "type": param.get("type") - } - }) + params_dict.update({param.get("name"): {"type": param.get("type")}}) props[key]["uriVariables"].update(params_dict) if not props[key]["uriVariables"]: del props[key]["uriVariables"] @@ -102,12 +104,8 @@ def get(self): "labthings_docs", __name__, static_folder="./static", template_folder="./templates" ) -docs_blueprint.add_url_rule( - "/swagger", view_func=APISpecView.as_view("swagger_json") -) +docs_blueprint.add_url_rule("/swagger", view_func=APISpecView.as_view("swagger_json")) docs_blueprint.add_url_rule( "/swagger-ui", view_func=SwaggerUIView.as_view("swagger_ui") ) -docs_blueprint.add_url_rule( - "/td", view_func=W3CThingDescriptionView.as_view("w3c_td") -) +docs_blueprint.add_url_rule("/td", view_func=W3CThingDescriptionView.as_view("w3c_td"))