diff --git a/src/labthings/default_views/actions.py b/src/labthings/default_views/actions.py index fd6e27d4..09ebc1cf 100644 --- a/src/labthings/default_views/actions.py +++ b/src/labthings/default_views/actions.py @@ -3,7 +3,7 @@ from ..view import View from ..view.marshalling import marshal_with from ..schema import ActionSchema -from ..find import current_labthing +from ..find import current_thing class ActionQueue(View): @@ -12,7 +12,7 @@ class ActionQueue(View): """ def get(self): - return ActionSchema(many=True).dump(current_labthing().actions.greenlets) + return ActionSchema(many=True).dump(current_thing.actions.greenlets) class ActionView(View): @@ -29,7 +29,7 @@ def get(self, task_id): Includes progress and intermediate data. """ - task_dict = current_labthing().actions.to_dict() + task_dict = current_thing.actions.to_dict() if task_id not in task_dict: return abort(404) # 404 Not Found @@ -44,7 +44,7 @@ def delete(self, task_id): If the task is finished, deletes its entry. """ - task_dict = current_labthing().actions.to_dict() + task_dict = current_thing.actions.to_dict() if task_id not in task_dict: return abort(404) # 404 Not Found diff --git a/src/labthings/default_views/docs/__init__.py b/src/labthings/default_views/docs/__init__.py index 4703595a..b879f4dc 100644 --- a/src/labthings/default_views/docs/__init__.py +++ b/src/labthings/default_views/docs/__init__.py @@ -1,7 +1,7 @@ from flask import render_template, Blueprint, make_response from ...view import View -from ...find import current_labthing +from ...find import current_thing class APISpecView(View): @@ -9,7 +9,7 @@ class APISpecView(View): def get(self): """OpenAPI v3 documentation""" - return current_labthing().spec.to_dict() + return current_thing.spec.to_dict() class SwaggerUIView(View): diff --git a/src/labthings/default_views/root.py b/src/labthings/default_views/root.py index 1e563142..8ec888a1 100644 --- a/src/labthings/default_views/root.py +++ b/src/labthings/default_views/root.py @@ -1,4 +1,4 @@ -from ..find import current_labthing +from ..find import current_thing from ..view import View @@ -6,4 +6,4 @@ class RootView(View): """W3C Thing Description""" def get(self): - return current_labthing().thing_description.to_dict() + return current_thing.thing_description.to_dict() diff --git a/src/labthings/default_views/sockets.py b/src/labthings/default_views/sockets.py index c28f1aa4..b2aa2f3b 100644 --- a/src/labthings/default_views/sockets.py +++ b/src/labthings/default_views/sockets.py @@ -1,5 +1,5 @@ from ..sockets import SocketSubscriber -from ..find import current_labthing +from ..find import current_thing import gevent import logging @@ -11,7 +11,7 @@ def socket_handler(ws): # Create a socket subscriber wssub = SocketSubscriber(ws) - current_labthing().subscribers.add(wssub) + current_thing.subscribers.add(wssub) logging.info(f"Added subscriber {wssub}") # Start the socket connection handler loop while not ws.closed: @@ -23,7 +23,7 @@ def socket_handler(ws): ws.send(response) gevent.sleep(0.1) # Remove the subscriber once the loop returns - current_labthing().subscribers.remove(wssub) + current_thing.subscribers.remove(wssub) logging.info(f"Removed subscriber {wssub}") diff --git a/src/labthings/default_views/tasks.py b/src/labthings/default_views/tasks.py index ff4a5d46..d4f52d6f 100644 --- a/src/labthings/default_views/tasks.py +++ b/src/labthings/default_views/tasks.py @@ -4,7 +4,7 @@ from ..view import View from ..view.marshalling import marshal_with from ..schema import TaskSchema -from ..find import current_labthing +from ..find import current_thing class TaskList(View): @@ -18,7 +18,7 @@ def get(self): logging.warning( "TaskList is deprecated and will be removed in a future version. Use the Actions list instead." ) - return TaskSchema(many=True).dump(current_labthing().actions.greenlets) + return TaskSchema(many=True).dump(current_thing.actions.greenlets) class TaskView(View): @@ -40,7 +40,7 @@ def get(self, task_id): logging.warning( "TaskView is deprecated and will be removed in a future version. Use the Action view instead." ) - task_dict = current_labthing().actions.to_dict() + task_dict = current_thing.actions.to_dict() if task_id not in task_dict: return abort(404) # 404 Not Found @@ -58,7 +58,7 @@ def delete(self, task_id): logging.warning( "TaskView is deprecated and will be removed in a future version. Use the Action view instead." ) - task_dict = current_labthing().actions.to_dict() + task_dict = current_thing.actions.to_dict() if task_id not in task_dict: return abort(404) # 404 Not Found diff --git a/src/labthings/logging.py b/src/labthings/logging.py index a81c8da5..8f36551e 100644 --- a/src/labthings/logging.py +++ b/src/labthings/logging.py @@ -1,4 +1,4 @@ -from .find import current_labthing +from .find import current_thing from logging import StreamHandler @@ -11,8 +11,8 @@ def emit(self, record): log_event = self.rest_format_record(record) # Broadcast to subscribers - if current_labthing(): - current_labthing().emit("logging", log_event) + if current_thing: + current_thing.emit("logging", log_event) def rest_format_record(self, record): return {"message": str(record.msg), "level": record.levelname.lower()} diff --git a/src/labthings/td.py b/src/labthings/td.py index 3b700e69..c01962be 100644 --- a/src/labthings/td.py +++ b/src/labthings/td.py @@ -4,7 +4,7 @@ from .event import Event from .json.schemas import schema_to_json from .json.paths import rule_to_params, rule_to_path -from .find import current_labthing +from .find import current_thing from .utilities import get_docstring, snake_to_camel @@ -114,7 +114,7 @@ def links(self): td_links.append( { "rel": link_description.get("rel"), - "href": current_labthing().url_for( + "href": current_thing.url_for( link_description.get("view"), **link_description.get("params"), _external=True, @@ -139,11 +139,11 @@ def to_dict(self): "https://www.w3.org/2019/wot/td/v1", "https://iot.mozilla.org/schemas/", ], - "@type": current_labthing().types, + "@type": current_thing.types, "id": url_for("root", _external=True), "base": request.host_url, - "title": current_labthing().title, - "description": current_labthing().description, + "title": current_thing.title, + "description": current_thing.description, "properties": self.properties, "actions": self.actions, # "events": self.events, # TODO: Enable once properly populated diff --git a/src/labthings/view/__init__.py b/src/labthings/view/__init__.py index 77ef2dbf..401fe217 100644 --- a/src/labthings/view/__init__.py +++ b/src/labthings/view/__init__.py @@ -8,7 +8,7 @@ from ..utilities import unpack, get_docstring, get_summary, merge from ..representations import DEFAULT_REPRESENTATIONS -from ..find import current_labthing, current_thing +from ..find import current_thing from ..event import PropertyStatusEvent from ..schema import Schema, ActionSchema, build_action_schema from ..tasks import Pool diff --git a/tests/test_default_views.py b/tests/test_default_views.py index 1eeae893..c470c29f 100644 --- a/tests/test_default_views.py +++ b/tests/test_default_views.py @@ -1,4 +1,4 @@ -from labthings.find import current_labthing +from labthings.find import current_thing import gevent @@ -22,7 +22,7 @@ def test_actions_list(thing_client): def task_func(): pass - task_obj = current_labthing().actions.spawn(task_func) + task_obj = current_thing.actions.spawn(task_func) with thing_client as c: response = c.get("/actions").json @@ -34,7 +34,7 @@ def test_action_representation(thing_client): def task_func(): pass - task_obj = current_labthing().actions.spawn(task_func) + task_obj = current_thing.actions.spawn(task_func) task_id = str(task_obj.id) with thing_client as c: @@ -52,12 +52,12 @@ def task_func(): while True: gevent.sleep(0) - task_obj = current_labthing().actions.spawn(task_func) + task_obj = current_thing.actions.spawn(task_func) task_id = str(task_obj.id) # Wait for task to start task_obj.started_event.wait() - assert task_id in current_labthing().actions.to_dict() + assert task_id in current_thing.actions.to_dict() # Send a DELETE request to terminate the task with thing_client as c: diff --git a/tests/test_find.py b/tests/test_find.py index d5152a3b..2d04e929 100644 --- a/tests/test_find.py +++ b/tests/test_find.py @@ -5,16 +5,16 @@ def test_current_labthing(thing, thing_ctx): with thing_ctx.test_request_context(): - assert find.current_labthing() is thing + assert find.current_labthing(thing.app) is thing -def test_current_labthing_explicit_app(thing, thing_ctx): +def test_current_thing(thing, thing_ctx): with thing_ctx.test_request_context(): - assert find.current_labthing(thing.app) is thing + assert find.current_thing._get_current_object() is thing -def test_current_labthing_missing_app(): - assert find.current_labthing() is None +def test_current_thing_missing_app(): + assert find.current_thing._get_current_object() is None def test_registered_extensions(thing_ctx):