Skip to content
This repository was archived by the owner on Jun 7, 2021. It is now read-only.

Commit 8da7b7e

Browse files
author
Joel Collins
committed
Code formatting and Windows fix in examples
1 parent 7b2991b commit 8da7b7e

6 files changed

Lines changed: 34 additions & 70 deletions

File tree

example/mjpeg-stream.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import os
77
import pathlib
8+
import sys
89
import time
910
import uuid
1011
from datetime import datetime
@@ -13,6 +14,13 @@
1314

1415
from webthing import Action, Event, Property, Thing, Value, WebThingServer
1516

17+
if (
18+
sys.version_info[0] == 3
19+
and sys.version_info[1] >= 8
20+
and sys.platform.startswith("win")
21+
):
22+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
23+
1624
"""
1725
PIL spams the logger with debug-level information. This is a pain when debugging api.app.
1826
We override the logging settings in api.app by setting a level for PIL here.
@@ -35,11 +43,7 @@ def _start_runner(self):
3543

3644
def generate_new_dummy_image(self):
3745
# Create a dummy image to serve in the stream
38-
image = Image.new(
39-
"RGB",
40-
(640, 480),
41-
color=(0, 0, 0),
42-
)
46+
image = Image.new("RGB", (640, 480), color=(0, 0, 0),)
4347

4448
draw = ImageDraw.Draw(image)
4549
draw.text(

example/single-thing.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22

33
import asyncio
44
import logging
5+
import sys
56
import time
67
import uuid
78

89
from webthing import Action, Event, Property, Thing, Value, WebThingServer
910

11+
if (
12+
sys.version_info[0] == 3
13+
and sys.version_info[1] >= 8
14+
and sys.platform.startswith("win")
15+
):
16+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
17+
1018

1119
class OverheatedEvent(Event):
1220
def __init__(self, thing, data):
@@ -64,28 +72,18 @@ async def fade_function(args):
6472
thing,
6573
"fade",
6674
fade_function,
67-
metadata={
68-
"title": "Fade",
69-
"description": "Fade the lamp to a given level",
70-
},
75+
metadata={"title": "Fade", "description": "Fade the lamp to a given level",},
7176
input_={
7277
"type": "object",
73-
"required": [
74-
"brightness",
75-
"duration",
76-
],
78+
"required": ["brightness", "duration",],
7779
"properties": {
7880
"brightness": {
7981
"type": "integer",
8082
"minimum": 0,
8183
"maximum": 100,
8284
"unit": "percent",
8385
},
84-
"duration": {
85-
"type": "integer",
86-
"minimum": 1,
87-
"unit": "milliseconds",
88-
},
86+
"duration": {"type": "integer", "minimum": 1, "unit": "milliseconds",},
8987
},
9088
},
9189
output={"type": "string"},

webthing/event.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ def as_event_description(self):
2626
Returns a dictionary describing the event.
2727
"""
2828
description = {
29-
self.name: {
30-
"timestamp": self.time,
31-
},
29+
self.name: {"timestamp": self.time,},
3230
}
3331

3432
if self.data is not None:

webthing/property.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ class Property:
1313
"""A Property represents an individual state value of a thing."""
1414

1515
def __init__(
16-
self,
17-
thing,
18-
name,
19-
value,
20-
metadata=None,
21-
content_type="application/json",
16+
self, thing, name, value, metadata=None, content_type="application/json",
2217
):
2318
"""
2419
Initialize the object.

webthing/server.py

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,15 @@ def get(self):
163163

164164
description = self.thing.as_thing_description()
165165
description["links"].append(
166-
{
167-
"rel": "alternate",
168-
"href": "{}{}".format(ws_href, self.thing.get_href()),
169-
}
166+
{"rel": "alternate", "href": "{}{}".format(ws_href, self.thing.get_href()),}
170167
)
171168
description["base"] = "{}://{}{}".format(
172169
self.request.protocol,
173170
self.request.headers.get("Host", ""),
174171
self.thing.get_href(),
175172
)
176173
description["securityDefinitions"] = {
177-
"nosec_sc": {
178-
"scheme": "nosec",
179-
},
174+
"nosec_sc": {"scheme": "nosec",},
180175
}
181176
description["security"] = "nosec_sc"
182177

@@ -312,9 +307,7 @@ def update_property(self, property_):
312307
message = json.dumps(
313308
{
314309
"messageType": "propertyStatus",
315-
"data": {
316-
property_.name: property_.get_value(),
317-
},
310+
"data": {property_.name: property_.get_value(),},
318311
},
319312
cls=self.json_encoder,
320313
)
@@ -328,10 +321,7 @@ def update_action(self, action):
328321
:param action: Action
329322
"""
330323
message = json.dumps(
331-
{
332-
"messageType": "actionStatus",
333-
"data": action.as_action_description(),
334-
},
324+
{"messageType": "actionStatus", "data": action.as_action_description(),},
335325
cls=self.json_encoder,
336326
)
337327

@@ -344,10 +334,7 @@ def update_event(self, event):
344334
:param event: Event
345335
"""
346336
message = json.dumps(
347-
{
348-
"messageType": "event",
349-
"data": event.as_event_description(),
350-
},
337+
{"messageType": "event", "data": event.as_event_description(),},
351338
cls=self.json_encoder,
352339
)
353340

@@ -630,19 +617,13 @@ def __init__(
630617

631618
for address in get_addresses():
632619
self.hosts.extend(
633-
[
634-
address,
635-
"{}:{}".format(address, self.port),
636-
]
620+
[address, "{}:{}".format(address, self.port),]
637621
)
638622

639623
if self.hostname is not None:
640624
self.hostname = self.hostname.lower()
641625
self.hosts.extend(
642-
[
643-
self.hostname,
644-
"{}:{}".format(self.hostname, self.port),
645-
]
626+
[self.hostname, "{}:{}".format(self.hostname, self.port),]
646627
)
647628

648629
self.thing.set_href_prefix(self.base_path)
@@ -724,9 +705,7 @@ def start(self):
724705
]
725706
kwargs = {
726707
"port": self.port,
727-
"properties": {
728-
"path": "/",
729-
},
708+
"properties": {"path": "/",},
730709
"server": "{}.local.".format(socket.gethostname()),
731710
}
732711

webthing/thing.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,8 @@ def as_thing_description(self):
5555
"rel": "properties",
5656
"href": "{}/properties".format(self.href_prefix),
5757
},
58-
{
59-
"rel": "actions",
60-
"href": "{}/actions".format(self.href_prefix),
61-
},
62-
{
63-
"rel": "events",
64-
"href": "{}/events".format(self.href_prefix),
65-
},
58+
{"rel": "actions", "href": "{}/actions".format(self.href_prefix),},
59+
{"rel": "events", "href": "{}/events".format(self.href_prefix),},
6660
],
6761
}
6862

@@ -77,11 +71,7 @@ def as_thing_description(self):
7771

7872
if self.ui_href is not None:
7973
thing["links"].append(
80-
{
81-
"rel": "alternate",
82-
"mediaType": "text/html",
83-
"href": self.ui_href,
84-
}
74+
{"rel": "alternate", "mediaType": "text/html", "href": self.ui_href,}
8575
)
8676

8777
if self.description:
@@ -190,7 +180,7 @@ def get_action_object_descriptions(self, action_name=None):
190180
191181
Returns the action descriptions.
192182
"""
193-
183+
194184
if action_name is None:
195185
descriptions = {}
196186
for name, action in self.actions.items():

0 commit comments

Comments
 (0)