Blackened

This commit is contained in:
jtc42 2020-01-03 00:21:16 +00:00
parent 6be8b0044a
commit ab6fa9f0e5
11 changed files with 104 additions and 80 deletions

View file

@ -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}
return {"properties": d}