Skip to content

Commit

Permalink
Fixed code style
Browse files Browse the repository at this point in the history
  • Loading branch information
jtc42 committed Jan 17, 2020
1 parent 11bc600 commit dd7da5f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 59 deletions.
10 changes: 3 additions & 7 deletions labthings/server/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions labthings/server/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion labthings/server/labthing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 2 additions & 8 deletions labthings/server/spec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__"):
Expand Down
34 changes: 16 additions & 18 deletions labthings/server/spec/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 1 addition & 4 deletions labthings/server/utilities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from ..core.utilities import (
get_docstring,
get_summary,
)
from ..core.utilities import get_docstring, get_summary

from .view import View

Expand Down
30 changes: 14 additions & 16 deletions labthings/server/views/docs/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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():
Expand All @@ -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"]
Expand Down Expand Up @@ -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"))

0 comments on commit dd7da5f

Please sign in to comment.