Skip to content

Commit 2b421b8

Browse files
author
Joel Collins
committed
Created a current_thing LocalProxy
1 parent d67b5b0 commit 2b421b8

File tree

10 files changed

+34
-34
lines changed

10 files changed

+34
-34
lines changed

src/labthings/default_views/actions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ..view import View
44
from ..view.marshalling import marshal_with
55
from ..schema import ActionSchema
6-
from ..find import current_labthing
6+
from ..find import current_thing
77

88

99
class ActionQueue(View):
@@ -12,7 +12,7 @@ class ActionQueue(View):
1212
"""
1313

1414
def get(self):
15-
return ActionSchema(many=True).dump(current_labthing().actions.greenlets)
15+
return ActionSchema(many=True).dump(current_thing.actions.greenlets)
1616

1717

1818
class ActionView(View):
@@ -29,7 +29,7 @@ def get(self, task_id):
2929
3030
Includes progress and intermediate data.
3131
"""
32-
task_dict = current_labthing().actions.to_dict()
32+
task_dict = current_thing.actions.to_dict()
3333

3434
if task_id not in task_dict:
3535
return abort(404) # 404 Not Found
@@ -44,7 +44,7 @@ def delete(self, task_id):
4444
4545
If the task is finished, deletes its entry.
4646
"""
47-
task_dict = current_labthing().actions.to_dict()
47+
task_dict = current_thing.actions.to_dict()
4848

4949
if task_id not in task_dict:
5050
return abort(404) # 404 Not Found

src/labthings/default_views/docs/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from flask import render_template, Blueprint, make_response
22

33
from ...view import View
4-
from ...find import current_labthing
4+
from ...find import current_thing
55

66

77
class APISpecView(View):
88
"""OpenAPI v3 documentation"""
99

1010
def get(self):
1111
"""OpenAPI v3 documentation"""
12-
return current_labthing().spec.to_dict()
12+
return current_thing.spec.to_dict()
1313

1414

1515
class SwaggerUIView(View):

src/labthings/default_views/root.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from ..find import current_labthing
1+
from ..find import current_thing
22
from ..view import View
33

44

55
class RootView(View):
66
"""W3C Thing Description"""
77

88
def get(self):
9-
return current_labthing().thing_description.to_dict()
9+
return current_thing.thing_description.to_dict()

src/labthings/default_views/sockets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from ..sockets import SocketSubscriber
2-
from ..find import current_labthing
2+
from ..find import current_thing
33

44
import gevent
55
import logging
@@ -11,7 +11,7 @@
1111
def socket_handler(ws):
1212
# Create a socket subscriber
1313
wssub = SocketSubscriber(ws)
14-
current_labthing().subscribers.add(wssub)
14+
current_thing.subscribers.add(wssub)
1515
logging.info(f"Added subscriber {wssub}")
1616
# Start the socket connection handler loop
1717
while not ws.closed:
@@ -23,7 +23,7 @@ def socket_handler(ws):
2323
ws.send(response)
2424
gevent.sleep(0.1)
2525
# Remove the subscriber once the loop returns
26-
current_labthing().subscribers.remove(wssub)
26+
current_thing.subscribers.remove(wssub)
2727
logging.info(f"Removed subscriber {wssub}")
2828

2929

src/labthings/default_views/tasks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from ..view import View
55
from ..view.marshalling import marshal_with
66
from ..schema import TaskSchema
7-
from ..find import current_labthing
7+
from ..find import current_thing
88

99

1010
class TaskList(View):
@@ -18,7 +18,7 @@ def get(self):
1818
logging.warning(
1919
"TaskList is deprecated and will be removed in a future version. Use the Actions list instead."
2020
)
21-
return TaskSchema(many=True).dump(current_labthing().actions.greenlets)
21+
return TaskSchema(many=True).dump(current_thing.actions.greenlets)
2222

2323

2424
class TaskView(View):
@@ -40,7 +40,7 @@ def get(self, task_id):
4040
logging.warning(
4141
"TaskView is deprecated and will be removed in a future version. Use the Action view instead."
4242
)
43-
task_dict = current_labthing().actions.to_dict()
43+
task_dict = current_thing.actions.to_dict()
4444

4545
if task_id not in task_dict:
4646
return abort(404) # 404 Not Found
@@ -58,7 +58,7 @@ def delete(self, task_id):
5858
logging.warning(
5959
"TaskView is deprecated and will be removed in a future version. Use the Action view instead."
6060
)
61-
task_dict = current_labthing().actions.to_dict()
61+
task_dict = current_thing.actions.to_dict()
6262

6363
if task_id not in task_dict:
6464
return abort(404) # 404 Not Found

src/labthings/logging.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .find import current_labthing
1+
from .find import current_thing
22

33
from logging import StreamHandler
44

@@ -11,8 +11,8 @@ def emit(self, record):
1111
log_event = self.rest_format_record(record)
1212

1313
# Broadcast to subscribers
14-
if current_labthing():
15-
current_labthing().emit("logging", log_event)
14+
if current_thing:
15+
current_thing.emit("logging", log_event)
1616

1717
def rest_format_record(self, record):
1818
return {"message": str(record.msg), "level": record.levelname.lower()}

src/labthings/td.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .event import Event
55
from .json.schemas import schema_to_json
66
from .json.paths import rule_to_params, rule_to_path
7-
from .find import current_labthing
7+
from .find import current_thing
88
from .utilities import get_docstring, snake_to_camel
99

1010

@@ -114,7 +114,7 @@ def links(self):
114114
td_links.append(
115115
{
116116
"rel": link_description.get("rel"),
117-
"href": current_labthing().url_for(
117+
"href": current_thing.url_for(
118118
link_description.get("view"),
119119
**link_description.get("params"),
120120
_external=True,
@@ -139,11 +139,11 @@ def to_dict(self):
139139
"https://www.w3.org/2019/wot/td/v1",
140140
"https://iot.mozilla.org/schemas/",
141141
],
142-
"@type": current_labthing().types,
142+
"@type": current_thing.types,
143143
"id": url_for("root", _external=True),
144144
"base": request.host_url,
145-
"title": current_labthing().title,
146-
"description": current_labthing().description,
145+
"title": current_thing.title,
146+
"description": current_thing.description,
147147
"properties": self.properties,
148148
"actions": self.actions,
149149
# "events": self.events, # TODO: Enable once properly populated

src/labthings/view/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from ..utilities import unpack, get_docstring, get_summary, merge
1010
from ..representations import DEFAULT_REPRESENTATIONS
11-
from ..find import current_labthing, current_thing
11+
from ..find import current_thing
1212
from ..event import PropertyStatusEvent
1313
from ..schema import Schema, ActionSchema, build_action_schema
1414
from ..tasks import Pool

tests/test_default_views.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from labthings.find import current_labthing
1+
from labthings.find import current_thing
22

33
import gevent
44

@@ -22,7 +22,7 @@ def test_actions_list(thing_client):
2222
def task_func():
2323
pass
2424

25-
task_obj = current_labthing().actions.spawn(task_func)
25+
task_obj = current_thing.actions.spawn(task_func)
2626

2727
with thing_client as c:
2828
response = c.get("/actions").json
@@ -34,7 +34,7 @@ def test_action_representation(thing_client):
3434
def task_func():
3535
pass
3636

37-
task_obj = current_labthing().actions.spawn(task_func)
37+
task_obj = current_thing.actions.spawn(task_func)
3838
task_id = str(task_obj.id)
3939

4040
with thing_client as c:
@@ -52,12 +52,12 @@ def task_func():
5252
while True:
5353
gevent.sleep(0)
5454

55-
task_obj = current_labthing().actions.spawn(task_func)
55+
task_obj = current_thing.actions.spawn(task_func)
5656
task_id = str(task_obj.id)
5757

5858
# Wait for task to start
5959
task_obj.started_event.wait()
60-
assert task_id in current_labthing().actions.to_dict()
60+
assert task_id in current_thing.actions.to_dict()
6161

6262
# Send a DELETE request to terminate the task
6363
with thing_client as c:

tests/test_find.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
def test_current_labthing(thing, thing_ctx):
77
with thing_ctx.test_request_context():
8-
assert find.current_labthing() is thing
8+
assert find.current_labthing(thing.app) is thing
99

1010

11-
def test_current_labthing_explicit_app(thing, thing_ctx):
11+
def test_current_thing(thing, thing_ctx):
1212
with thing_ctx.test_request_context():
13-
assert find.current_labthing(thing.app) is thing
13+
assert find.current_thing._get_current_object() is thing
1414

1515

16-
def test_current_labthing_missing_app():
17-
assert find.current_labthing() is None
16+
def test_current_thing_missing_app():
17+
assert find.current_thing._get_current_object() is None
1818

1919

2020
def test_registered_extensions(thing_ctx):

0 commit comments

Comments
 (0)