Skip to content

Commit

Permalink
Created a current_thing LocalProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Jul 9, 2020
1 parent d67b5b0 commit 2b421b8
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 34 deletions.
8 changes: 4 additions & 4 deletions src/labthings/default_views/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/labthings/default_views/docs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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):
"""OpenAPI v3 documentation"""

def get(self):
"""OpenAPI v3 documentation"""
return current_labthing().spec.to_dict()
return current_thing.spec.to_dict()


class SwaggerUIView(View):
Expand Down
4 changes: 2 additions & 2 deletions src/labthings/default_views/root.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from ..find import current_labthing
from ..find import current_thing
from ..view import View


class RootView(View):
"""W3C Thing Description"""

def get(self):
return current_labthing().thing_description.to_dict()
return current_thing.thing_description.to_dict()
6 changes: 3 additions & 3 deletions src/labthings/default_views/sockets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ..sockets import SocketSubscriber
from ..find import current_labthing
from ..find import current_thing

import gevent
import logging
Expand All @@ -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:
Expand All @@ -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}")


Expand Down
8 changes: 4 additions & 4 deletions src/labthings/default_views/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/labthings/logging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .find import current_labthing
from .find import current_thing

from logging import StreamHandler

Expand All @@ -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()}
10 changes: 5 additions & 5 deletions src/labthings/td.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/labthings/view/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tests/test_default_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from labthings.find import current_labthing
from labthings.find import current_thing

import gevent

Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_find.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 2b421b8

Please sign in to comment.