Skip to content

Commit 38d25b8

Browse files
committed
update formatting of code
1 parent 23e1fbd commit 38d25b8

File tree

13 files changed

+123
-101
lines changed

13 files changed

+123
-101
lines changed

Taskfile.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: 3
2+
3+
tasks:
4+
install:
5+
cmds:
6+
- pip install black
7+
format:
8+
cmds:
9+
- black .
10+
test:
11+
cmds:
12+
- pytest

demo_server.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Counter:
1717
def increment(self):
1818
self.count += 1
1919
# notify all registered clients
20-
RemoteNode.notify_property_change('demo.Counter/count', self.count)
20+
RemoteNode.notify_property_change("demo.Counter/count", self.count)
2121

2222

2323
class CounterAdapter(IObjectSource):
@@ -30,7 +30,7 @@ def __init__(self, impl):
3030

3131
def olink_object_name(self):
3232
# name this source is registered under
33-
return 'demo.Counter'
33+
return "demo.Counter"
3434

3535
def olink_invoke(self, name: str, args: list[Any]) -> Any:
3636
# called on incoming invoke message
@@ -48,7 +48,7 @@ def olink_linked(self, name: str, node: "RemoteNode"):
4848
self.impl._node = node
4949

5050
def olink_collect_properties(self) -> object:
51-
return {k: getattr(self.impl, k) for k in ['count']}
51+
return {k: getattr(self.impl, k) for k in ["count"]}
5252

5353

5454
counter = Counter()
@@ -61,26 +61,27 @@ class RemoteEndpoint(WebSocketEndpoint):
6161
queue = Queue()
6262

6363
async def sender(self, ws):
64-
print('start sender')
64+
print("start sender")
6565
while True:
66-
print('001')
66+
print("001")
6767
msg = await self.queue.get()
68-
print('send', msg)
68+
print("send", msg)
6969
await ws.send_text(msg)
7070
self.queue.task_done()
7171

7272
async def on_connect(self, ws: WebSocket):
73-
print('on_connect')
73+
print("on_connect")
7474
asyncio.create_task(self.sender(ws))
7575

7676
def writer(msg: str):
77-
print('writer', msg)
77+
print("writer", msg)
7878
self.queue.put_nowait(msg)
79+
7980
self.node.on_write(writer)
8081
await super().on_connect(ws)
8182

8283
async def on_receive(self, ws: WebSocket, data: Any) -> None:
83-
print('on_receive', data)
84+
print("on_receive", data)
8485
self.node.handle_message(data)
8586

8687
async def on_disconnect(self, websocket: WebSocket, close_code: int) -> None:
@@ -89,8 +90,6 @@ async def on_disconnect(self, websocket: WebSocket, close_code: int) -> None:
8990
await self.queue.join()
9091

9192

92-
routes = [
93-
WebSocketRoute("/ws", RemoteEndpoint)
94-
]
93+
routes = [WebSocketRoute("/ws", RemoteEndpoint)]
9594

9695
app = Starlette(routes=routes)

examples/app.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,37 @@ class CounterSink(IObjectSink):
1414
def __init__(self):
1515
# register sink with client node
1616
self.client = ClientNode.register_sink(self)
17-
print('client', self.client)
17+
print("client", self.client)
1818

1919
def increment(self):
2020
# remote call the increment method
2121
if self.client:
22-
self.client.invoke_remote('demo.Counter/increment', [], None)
22+
self.client.invoke_remote("demo.Counter/increment", [], None)
2323

2424
def olink_object_name(self):
2525
# return the name of the sink
26-
return 'demo.Counter'
26+
return "demo.Counter"
2727

2828
def olink_on_signal(self, name: str, args: list[Any]):
2929
# handle the incoming signal from the remote source
3030
path = Name.path_from_name(name)
31-
print('on signal: %s: %s' % (path, args))
31+
print("on signal: %s: %s" % (path, args))
3232

3333
def olink_on_property_changed(self, name: str, value: Any) -> None:
3434
# handle the property change from the remote source
3535
path = Name.path_from_name(name)
36-
print('on property changed: %s: %s' % (path, value))
36+
print("on property changed: %s: %s" % (path, value))
3737

3838
def olink_on_init(self, name: str, props: object, node: ClientNode):
3939
# handle the initialization of the sink,
4040
# called when the sink is linked to remote source
41-
print('on init: %s: %s' % (name, props))
41+
print("on init: %s: %s" % (name, props))
4242
self.client = node
4343

4444
def olink_on_release(self):
4545
# handle the release of the sink,
4646
# called when the sink is unlinked from remote source
47-
print('on release')
47+
print("on release")
4848

4949

5050
class ClientWebsocketAdapter:
@@ -59,7 +59,7 @@ def __init__(self, node):
5959

6060
def writer(self, data):
6161
# don't send directly, first write to queue
62-
print('write to queue')
62+
print("write to queue")
6363
self.queue.put_nowait(data)
6464

6565
async def _reader(self, ws):
@@ -86,20 +86,20 @@ async def connect(self, address: str):
8686
await asyncio.gather(sender_task, reader_task)
8787
await self.queue.join()
8888
except Exception as e:
89-
print('exception while connecting: ', e)
89+
print("exception while connecting: ", e)
9090

9191

92-
address = 'ws://localhost:8282/ws'
92+
address = "ws://localhost:8282/ws"
9393
# create a client node for ObjectLink registry and protocol
9494
node = ClientNode()
9595
# link the node to the service name
96-
node.link_node('demo.Counter')
96+
node.link_node("demo.Counter")
9797

9898
# create a ws client which handles the ws adapter
9999
client = ClientWebsocketAdapter(node)
100100

101101
counter = CounterSink()
102-
node.link_remote('demo.Counter')
102+
node.link_remote("demo.Counter")
103103
counter.increment()
104104

105105

examples/server.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class CounterService:
1616

1717
def increment(self):
1818
self.count += 1
19-
self._node.notify_property_change('demo.Counter/count', self.count)
19+
self._node.notify_property_change("demo.Counter/count", self.count)
2020

2121

2222
class CounterWebsocketAdapter(IObjectSource):
@@ -30,7 +30,7 @@ def __init__(self, impl):
3030

3131
def olink_object_name(self):
3232
# return service name
33-
return 'demo.Counter'
33+
return "demo.Counter"
3434

3535
def olink_invoke(self, name: str, args: list[Any]) -> Any:
3636
# handle the remote call from client node
@@ -42,7 +42,7 @@ def olink_invoke(self, name: str, args: list[Any]) -> Any:
4242
result = func(**args)
4343
except Exception as e:
4444
# need to have proper exception handling here
45-
print('error: %s' % e)
45+
print("error: %s" % e)
4646
result = None
4747
# results will be send back to calling client node
4848
return result
@@ -58,7 +58,7 @@ def olink_linked(self, name: str, node: "RemoteNode"):
5858

5959
def olink_collect_properties(self) -> object:
6060
# collect properties from implementation to send back to client node initially
61-
return {k: getattr(self.impl, k) for k in ['count']}
61+
return {k: getattr(self.impl, k) for k in ["count"]}
6262

6363

6464
# create the service implementation
@@ -77,31 +77,32 @@ class RemoteEndpoint(WebSocketEndpoint):
7777

7878
async def sender(self, ws):
7979
# sender coroutine, messages from queue are send to client
80-
print('start sender')
80+
print("start sender")
8181
while True:
8282
msg = await self.queue.get()
83-
print('send', msg)
83+
print("send", msg)
8484
await ws.send_text(msg)
8585
self.queue.task_done()
8686

8787
async def on_connect(self, ws: WebSocket):
8888
# handle a socket connection
89-
print('on_connect')
89+
print("on_connect")
9090
# register a sender to the connection
9191
asyncio.create_task(self.sender(ws))
9292

9393
# a writer function to queue messages
9494
def writer(msg: str):
95-
print('write to queue:', msg)
95+
print("write to queue:", msg)
9696
self.queue.put_nowait(msg)
97+
9798
# register the writer function to the node
9899
self.node.on_write(writer)
99100
# call the super connection handler
100101
await super().on_connect(ws)
101102

102103
async def on_receive(self, ws: WebSocket, data: Any) -> None:
103104
# handle a message from a client socket
104-
print('on_receive', data)
105+
print("on_receive", data)
105106
self.node.handle_message(data)
106107

107108
async def on_disconnect(self, websocket: WebSocket, close_code: int) -> None:
@@ -114,9 +115,7 @@ async def on_disconnect(self, websocket: WebSocket, close_code: int) -> None:
114115

115116

116117
# see https://www.starlette.io/routing/
117-
routes = [
118-
WebSocketRoute("/ws", RemoteEndpoint)
119-
]
118+
routes = [WebSocketRoute("/ws", RemoteEndpoint)]
120119

121120

122121
# call with `uvicorn server:app --port 8282`

src/olink/core/protocol.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ def handle_message(self, msg: list[Any]) -> bool:
124124
_, msgType, id, error = msg
125125
self.listener.handle_error(msgType, id, error)
126126
else:
127-
self.emit_log(LogLevel.DEBUG,
128-
f"not supported message type: {msgType}")
127+
self.emit_log(LogLevel.DEBUG, f"not supported message type: {msgType}")
129128
return False
130129
return True

src/olink/core/types.py

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

66

77
class MsgType(IntEnum):
8-
LINK = 10,
9-
INIT = 11,
10-
UNLINK = 12,
11-
SET_PROPERTY = 20,
12-
PROPERTY_CHANGE = 21,
13-
INVOKE = 30,
14-
INVOKE_REPLY = 31,
15-
SIGNAL = 40,
16-
ERROR = 90,
8+
LINK = (10,)
9+
INIT = (11,)
10+
UNLINK = (12,)
11+
SET_PROPERTY = (20,)
12+
PROPERTY_CHANGE = (21,)
13+
INVOKE = (30,)
14+
INVOKE_REPLY = (31,)
15+
SIGNAL = (40,)
16+
ERROR = (90,)
1717

1818

1919
class MessageFormat(IntEnum):
20-
JSON = 1,
21-
BSON = 2,
22-
MSGPACK = 3,
23-
CBOR = 4,
20+
JSON = (1,)
21+
BSON = (2,)
22+
MSGPACK = (3,)
23+
CBOR = (4,)
2424

2525

2626
class Name:
@@ -29,22 +29,22 @@ class Name:
2929
@staticmethod
3030
def resource_from_name(name: str) -> str:
3131
# return the resource name from a name
32-
return name.split('/')[0]
32+
return name.split("/")[0]
3333

3434
@staticmethod
3535
def path_from_name(name: str) -> str:
3636
# return the path from a name
37-
return name.split('/')[-1]
37+
return name.split("/")[-1]
3838

3939
@staticmethod
4040
def has_path(name: str) -> bool:
4141
# return true if name has a path
42-
return '/' in name
42+
return "/" in name
4343

4444
@staticmethod
4545
def create_name(resource: str, path: str) -> str:
4646
# create a name from a resource and a path
47-
return f'{resource}/{path}'
47+
return f"{resource}/{path}"
4848

4949

5050
class MessageConverter:
@@ -65,10 +65,10 @@ def to_string(self, data: list[Any]) -> str:
6565

6666

6767
class LogLevel:
68-
DEBUG = 1,
69-
INFO = 2,
70-
WARNING = 3,
71-
ERROR = 4,
68+
DEBUG = (1,)
69+
INFO = (2,)
70+
WARNING = (3,)
71+
ERROR = (4,)
7272

7373

7474
WriteLogFunc = Callable[[LogLevel, str], None]

src/olink/mocks/mocksink.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,48 @@
22
from typing import Any, Optional
33
from olink.clientnode import ClientNode, IObjectSink, InvokeReplyArg
44

5+
56
class MockSink(IObjectSink):
67
name: str
78
events: list[Any] = []
89
node: Optional[ClientNode] = None
910
properties: dict[str, Any] = {}
11+
1012
def __init__(self, name: str):
1113
self.name = name
1214
self.node = ClientNode.register_sink(self)
1315

1416
def invoke(self, name: str, args: list[Any]):
1517
if self.node:
18+
1619
def func(arg: InvokeReplyArg):
17-
self.events.append({'type': 'invoke-reply', 'name': arg.name, 'value': arg.value})
20+
self.events.append(
21+
{"type": "invoke-reply", "name": arg.name, "value": arg.value}
22+
)
23+
1824
self.node.invoke_remote(name, args, func)
19-
25+
2026
def olink_object_name(self) -> str:
2127
return self.name
2228

2329
def olink_on_signal(self, name: str, args: list[Any]) -> None:
24-
self.events.append({'type': 'signal', 'name': name, 'args': args})
30+
self.events.append({"type": "signal", "name": name, "args": args})
2531

2632
def olink_on_property_changed(self, name: str, value: Any) -> None:
2733
path = Name.path_from_name(name)
28-
self.events.append({ 'type': 'property_change', 'name': name, 'value': value})
34+
self.events.append({"type": "property_change", "name": name, "value": value})
2935
self.properties[path] = value
3036

3137
def olink_on_init(self, name: str, props: object, node: "ClientNode"):
32-
self.events.append({'type': 'init', 'name': name, 'props': props})
38+
self.events.append({"type": "init", "name": name, "props": props})
3339
self.node = node
3440
self.properties = props
3541

3642
def olink_on_release(self) -> None:
37-
self.events.append({'type': 'release'})
43+
self.events.append({"type": "release"})
3844
self.node = None
3945

4046
def clear(self):
4147
self.events = []
4248
self.properties = {}
4349
self.node = None
44-
45-
46-

0 commit comments

Comments
 (0)