Skip to content

Commit

Permalink
Reverted to current_labthing function
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Jul 19, 2020
1 parent 070bfcd commit 6b79698
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 65 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* Code formatting ([a86bc21](https://github.com/labthings/python-labthings/commit/a86bc21))
* Code formatting ([9399433](https://github.com/labthings/python-labthings/commit/9399433))
* Code formatting ([cc676b7](https://github.com/labthings/python-labthings/commit/cc676b7))
* Created a current_thing LocalProxy ([2b421b8](https://github.com/labthings/python-labthings/commit/2b421b8))
* Created a current_labthing() LocalProxy ([2b421b8](https://github.com/labthings/python-labthings/commit/2b421b8))
* Deleted test file ([2585abd](https://github.com/labthings/python-labthings/commit/2585abd))
* Fix HTTP method check ([8ae6060](https://github.com/labthings/python-labthings/commit/8ae6060))
* FIx OpenAPI formatting ([c90a646](https://github.com/labthings/python-labthings/commit/c90a646))
Expand Down
6 changes: 6 additions & 0 deletions docs/api_reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ LabThing
--------

.. autoclass:: labthings.LabThing
:members:

Module
------

.. automodule:: labthings
:members:
51 changes: 41 additions & 10 deletions src/labthings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
# Main LabThing class
from .labthing import LabThing
from .wsgi import Server

# Quick-create app+LabThing function
from .quick import create_app
from . import views, sync, fields, schema, semantics

# Suggested WSGI+WebSocket server class
from .wsgi import Server

# Functions to speed up finding global objects
from .find import (
current_labthing,
registered_extensions,
registered_components,
find_extension,
find_component,
)

# Synchronisation classes
from .sync import StrictLock, CompositeLock, ClientEvent

# Task management functions
from .tasks import (
current_task,
current_task_stopped,
update_task_progress,
update_task_data,
TaskKillException,
)

# Submodules
from . import extensions
from . import views
from . import fields
from . import schema
from . import semantics
from . import json

__all__ = [
"LabThing",
"Server",
"create_app",
"views",
"sync",
"fields",
"schema",
"semantics",
"current_labthing()",
"registered_extensions",
"registered_components",
"find_extension",
"find_component",
]

8 changes: 4 additions & 4 deletions src/labthings/default_views/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ..views.marshalling import marshal_with
from ..views.args import use_args
from ..schema import ActionSchema
from ..find import current_thing
from ..find import current_labthing
from .. import fields


Expand All @@ -14,7 +14,7 @@ class ActionQueue(View):
"""

def get(self):
return ActionSchema(many=True).dump(current_thing.actions.threads)
return ActionSchema(many=True).dump(current_labthing().actions.threads)


class ActionView(View):
Expand All @@ -31,7 +31,7 @@ def get(self, task_id):
Includes progress and intermediate data.
"""
task_dict = current_thing.actions.to_dict()
task_dict = current_labthing().actions.to_dict()

if task_id not in task_dict:
return abort(404) # 404 Not Found
Expand All @@ -48,7 +48,7 @@ def delete(self, args, task_id):
If the task is finished, deletes its entry.
"""
timeout = args.get("timeout", 5)
task_dict = current_thing.actions.to_dict()
task_dict = current_labthing().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 ...views import View
from ...find import current_thing
from ...find import current_labthing


class APISpecView(View):
"""OpenAPI v3 documentation"""

def get(self):
"""OpenAPI v3 documentation"""
return current_thing.spec.to_dict()
return current_labthing().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_thing
from ..find import current_labthing
from ..views import View


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

def get(self):
return current_thing.thing_description.to_dict()
return current_labthing().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_thing
from ..find import current_labthing

import logging

Expand All @@ -10,7 +10,7 @@
def socket_handler(ws):
# Create a socket subscriber
wssub = SocketSubscriber(ws)
current_thing.subscribers.add(wssub)
current_labthing().subscribers.add(wssub)
logging.info(f"Added subscriber {wssub}")
# Start the socket connection handler loop
while not ws.closed:
Expand All @@ -21,7 +21,7 @@ def socket_handler(ws):
if response:
ws.send(response)
# Remove the subscriber once the loop returns
current_thing.subscribers.remove(wssub)
current_labthing().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 ..views import View
from ..views.marshalling import marshal_with
from ..schema import TaskSchema
from ..find import current_thing
from ..find import current_labthing


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_thing.actions.threads)
return TaskSchema(many=True).dump(current_labthing().actions.threads)


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_thing.actions.to_dict()
task_dict = current_labthing().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_thing.actions.to_dict()
task_dict = current_labthing().actions.to_dict()

if task_id not in task_dict:
return abort(404) # 404 Not Found
Expand Down
7 changes: 1 addition & 6 deletions src/labthings/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
from flask import current_app, url_for
import weakref

from werkzeug.local import LocalProxy

from .names import EXTENSION_NAME

__all__ = [
"current_app",
"url_for",
"current_labthing",
"current_thing",
"current_labthing()",
"registered_extensions",
"registered_components",
"find_component",
Expand Down Expand Up @@ -106,6 +104,3 @@ def find_extension(extension_name, labthing_instance=None):
return labthing_instance.extensions[extension_name]
else:
return None


current_thing = LocalProxy(current_labthing)
3 changes: 3 additions & 0 deletions src/labthings/json/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .encoder import LabThingsJSONEncoder, encode_json

__all__ = ["LabThingsJSONEncoder", "encode_json"]
2 changes: 1 addition & 1 deletion src/labthings/labthing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .td import ThingDescription
from .event import Event

from .tasks import Pool
from .tasks.pool import Pool

from .views.builder import property_of, action_from

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_thing
from .find import current_labthing

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_thing:
current_thing.emit("logging", log_event)
if current_labthing():
current_labthing().emit("logging", log_event)

def rest_format_record(self, record):
return {"message": str(record.msg), "level": record.levelname.lower()}
2 changes: 0 additions & 2 deletions src/labthings/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
__all__ = [
"Pool",
"current_task",
"current_task_stopped",
"update_task_progress",
Expand All @@ -9,7 +8,6 @@
]

from .pool import (
Pool,
current_task,
current_task_stopped,
update_task_progress,
Expand Down
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_thing
from .find import current_labthing
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_thing.url_for(
"href": current_labthing().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_thing.types,
"@type": current_labthing().types,
"id": url_for("root", _external=True),
"base": request.host_url,
"title": current_thing.title,
"description": current_thing.description,
"title": current_labthing().title,
"description": current_labthing().description,
"properties": self.properties,
"actions": self.actions,
# "events": self.events, # TODO: Enable once properly populated
Expand Down
16 changes: 10 additions & 6 deletions src/labthings/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

from ..utilities import unpack, get_docstring, get_summary, merge
from ..representations import DEFAULT_REPRESENTATIONS
from ..find import current_thing
from ..find import current_labthing
from ..event import PropertyStatusEvent
from ..schema import Schema, ActionSchema, build_action_schema
from ..tasks import Pool
from ..deque import Deque, resize_deque
from ..json.schemas import schema_to_json
from ..tasks.pool import Pool
from .. import fields

import logging
Expand Down Expand Up @@ -43,7 +43,9 @@ def __init__(self, *args, **kwargs):

# Set the default representations
self.representations = (
current_thing.representations if current_thing else DEFAULT_REPRESENTATIONS
current_labthing().representations
if current_labthing()
else DEFAULT_REPRESENTATIONS
)

@classmethod
Expand Down Expand Up @@ -246,7 +248,9 @@ def dispatch_request(self, *args, **kwargs):
meth = marshal_with(self.schema)(meth)

# Try to find a pool on the current LabThing, but fall back to Views emergency pool
pool = current_thing.actions if current_thing else self._emergency_pool
pool = (
current_labthing().actions if current_labthing() else self._emergency_pool
)
# Make a task out of the views `post` method
task = pool.spawn(meth, *args, **kwargs)

Expand Down Expand Up @@ -379,8 +383,8 @@ def dispatch_request(self, *args, **kwargs):
self, "__name__", "unknown"
)

if current_thing:
current_thing.message(
if current_labthing():
current_labthing().message(
PropertyStatusEvent(property_name), property_value,
)

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from labthings.server.labthing import LabThing
from labthings.tasks import Pool
from labthings.tasks.pool import Pool

from flask.views import MethodView
from labthings.views import View
Expand Down
Loading

0 comments on commit 6b79698

Please sign in to comment.