Refactored adding views, actions, props and components
This commit is contained in:
parent
7715c19305
commit
a4196b6765
10 changed files with 62 additions and 66 deletions
|
|
@ -77,16 +77,20 @@ def ThingAction(viewcls):
|
|||
update_spec(viewcls, {"_groups": ["actions"]})
|
||||
return viewcls
|
||||
|
||||
|
||||
thing_action = ThingAction
|
||||
|
||||
|
||||
def ThingProperty(viewcls):
|
||||
# Pass params to call function attribute for external access
|
||||
update_spec(viewcls, {"tags": ["properties"]})
|
||||
update_spec(viewcls, {"_groups": ["properties"]})
|
||||
return viewcls
|
||||
|
||||
|
||||
thing_property = ThingProperty
|
||||
|
||||
|
||||
class use_args(object):
|
||||
def __init__(self, schema, **kwargs):
|
||||
"""
|
||||
|
|
@ -121,8 +125,10 @@ class Doc(object):
|
|||
update_spec(f, self.kwargs)
|
||||
return f
|
||||
|
||||
|
||||
doc = Doc
|
||||
|
||||
|
||||
class Tag(object):
|
||||
def __init__(self, tags):
|
||||
if isinstance(tags, str):
|
||||
|
|
@ -137,8 +143,10 @@ class Tag(object):
|
|||
update_spec(f, {"tags": self.tags})
|
||||
return f
|
||||
|
||||
|
||||
tag = Tag
|
||||
|
||||
|
||||
class doc_response(object):
|
||||
def __init__(self, code, description=None, **kwargs):
|
||||
self.code = code
|
||||
|
|
|
|||
|
|
@ -28,11 +28,11 @@ class LabThing(object):
|
|||
):
|
||||
self.app = app
|
||||
|
||||
self.devices = {}
|
||||
self.components = {}
|
||||
|
||||
self.extensions = {}
|
||||
|
||||
self.resources = []
|
||||
self.views = []
|
||||
self.properties = {}
|
||||
self.actions = {}
|
||||
|
||||
|
|
@ -93,8 +93,8 @@ class LabThing(object):
|
|||
app.extensions[EXTENSION_NAME] = self
|
||||
|
||||
# Add resources, if registered before tying to a Flask app
|
||||
if len(self.resources) > 0:
|
||||
for resource, urls, endpoint, kwargs in self.resources:
|
||||
if len(self.views) > 0:
|
||||
for resource, urls, endpoint, kwargs in self.views:
|
||||
self._register_view(app, resource, *urls, endpoint=endpoint, **kwargs)
|
||||
|
||||
# Create base routes
|
||||
|
|
@ -110,19 +110,15 @@ class LabThing(object):
|
|||
self.app.register_blueprint(docs_blueprint, url_prefix=self.url_prefix)
|
||||
|
||||
# Add extension overview
|
||||
self.add_resource(
|
||||
ExtensionList, "/extensions", endpoint=EXTENSION_LIST_ENDPOINT
|
||||
)
|
||||
self.register_property(ExtensionList)
|
||||
self.add_view(ExtensionList, "/extensions", endpoint=EXTENSION_LIST_ENDPOINT)
|
||||
# Add task routes
|
||||
self.add_resource(TaskList, "/tasks", endpoint=TASK_LIST_ENDPOINT)
|
||||
self.register_property(TaskList)
|
||||
self.add_resource(TaskResource, "/tasks/<id>", endpoint=TASK_ENDPOINT)
|
||||
self.add_view(TaskList, "/tasks", endpoint=TASK_LIST_ENDPOINT)
|
||||
self.add_view(TaskResource, "/tasks/<id>", endpoint=TASK_ENDPOINT)
|
||||
|
||||
### Device stuff
|
||||
|
||||
def register_device(self, device_object, device_name: str):
|
||||
self.devices[device_name] = device_object
|
||||
def add_component(self, device_object, device_name: str):
|
||||
self.components[device_name] = device_object
|
||||
|
||||
### Extension stuff
|
||||
|
||||
|
|
@ -134,18 +130,12 @@ class LabThing(object):
|
|||
|
||||
for extension_view_id, extension_view in extension_object.views.items():
|
||||
# Add route to the extensions blueprint
|
||||
self.add_resource(
|
||||
self.add_view(
|
||||
extension_view["view"],
|
||||
"/extensions" + extension_view["rule"],
|
||||
**extension_view["kwargs"],
|
||||
)
|
||||
|
||||
for prop in extension_object.properties:
|
||||
self.register_property(prop)
|
||||
|
||||
for action in extension_object.actions:
|
||||
self.register_action(action)
|
||||
|
||||
### Resource stuff
|
||||
|
||||
def _complete_url(self, url_part, registration_prefix):
|
||||
|
|
@ -158,16 +148,8 @@ class LabThing(object):
|
|||
parts = [registration_prefix, self.url_prefix, url_part]
|
||||
return "".join([part for part in parts if part])
|
||||
|
||||
def register_property(self, resource):
|
||||
logging.warning("register_property is deprecated. Use the @ltproperty class decorator instead.")
|
||||
pass
|
||||
|
||||
def register_action(self, resource):
|
||||
logging.warning("register_action is deprecated. Use the @ltaction class decorator instead.")
|
||||
pass
|
||||
|
||||
def add_resource(self, resource, *urls, endpoint=None, **kwargs):
|
||||
"""Adds a resource to the api.
|
||||
def add_view(self, resource, *urls, endpoint=None, **kwargs):
|
||||
"""Adds a view to the api.
|
||||
:param resource: the class name of your resource
|
||||
:type resource: :class:`Type[Resource]`
|
||||
:param urls: one or more url routes to match for the resource, standard
|
||||
|
|
@ -197,11 +179,11 @@ 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))
|
||||
self.views.append((resource, urls, endpoint, kwargs))
|
||||
|
||||
def resource(self, *urls, **kwargs):
|
||||
def view(self, *urls, **kwargs):
|
||||
def decorator(cls):
|
||||
self.add_resource(cls, *urls, **kwargs)
|
||||
self.add_view(cls, *urls, **kwargs)
|
||||
return cls
|
||||
|
||||
return decorator
|
||||
|
|
|
|||
|
|
@ -20,10 +20,12 @@ def update_spec(obj, spec):
|
|||
rupdate(obj.__apispec__, spec)
|
||||
return obj.__apispec__
|
||||
|
||||
|
||||
def get_spec(obj):
|
||||
obj.__apispec__ = obj.__dict__.get("__apispec__", {})
|
||||
return obj.__apispec__
|
||||
|
||||
|
||||
def view2path(rule: str, view: Resource, spec: APISpec):
|
||||
params = {
|
||||
"path": rule, # TODO: Validate this slightly (leading / etc)
|
||||
|
|
|
|||
|
|
@ -1,17 +1,13 @@
|
|||
"""
|
||||
Top-level representation of attached and enabled Extensions
|
||||
"""
|
||||
|
||||
from openflexure_microscope.common.flask_labthings.utilities import get_docstring
|
||||
from openflexure_microscope.common.flask_labthings.schema import Schema
|
||||
from openflexure_microscope.common.flask_labthings import fields
|
||||
|
||||
from ..resource import Resource
|
||||
from ..find import registered_extensions
|
||||
from ..schema import ExtensionSchema
|
||||
from ..decorators import marshal_with
|
||||
from ..decorators import marshal_with, ThingProperty
|
||||
|
||||
|
||||
@ThingProperty
|
||||
class ExtensionList(Resource):
|
||||
"""
|
||||
List and basic documentation for all enabled Extensions
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
from flask import abort, url_for
|
||||
|
||||
from ..decorators import marshal_with
|
||||
from ..decorators import marshal_with, ThingProperty
|
||||
from ..resource import Resource
|
||||
from ..schema import TaskSchema
|
||||
|
||||
from openflexure_microscope.common.labthings_core import tasks
|
||||
|
||||
|
||||
@ThingProperty
|
||||
class TaskList(Resource):
|
||||
"""
|
||||
List and basic documentation for all session tasks
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue