Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions dali/driver/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
###############################################################################
# driver contracts
###############################################################################
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable, Optional

if TYPE_CHECKING:
from dali.command import Command
from dali.frame import Frame


class DALIDriver:
"""Object for handling wrapped DALI frames sent to and received from
Expand All @@ -10,10 +18,10 @@ class DALIDriver:
communicate with a DALI bus.
"""

backend = None
backend: Optional[Backend] = None
"""``Backend`` instance used for reading and writing."""

def construct(self, command):
def construct(self, command: Command) -> bytes:
"""Construct raw data containing the packed DALI frame from command
which can be sent to DALI driver.

Expand All @@ -23,7 +31,7 @@ def construct(self, command):
raise NotImplementedError(
'Abstract ``DaliDriver`` does not implement ```construct`')

def extract(self, data):
def extract(self, data: bytes) -> Frame:
"""Extract and return DALI Frame from data which has been received
from DALI driver.

Expand All @@ -39,7 +47,7 @@ class SyncDALIDriver(DALIDriver):
expecting and returning a result.
"""

def send(self, command, timeout=None):
def send(self, command: Command, timeout: Optional[int] = None) -> Any:
"""Send command to gateway and return response.

@param command: DALI command to send.
Expand All @@ -55,11 +63,16 @@ class AsyncDALIDriver(DALIDriver):
DALI drivers.
"""

dispatcher = None
dispatcher: Optional[Callable[..., Any]] = None
"""Callable used for dispatching incoming forward frames.
"""

def send(self, command, callback=None, **kw):
def send(
self,
command: Command,
callback: Optional[Callable[..., Any]] = None,
**kw: Any,
) -> None:
"""Send command to gateway.

@param command: DALI command to send.
Expand All @@ -70,7 +83,7 @@ def send(self, command, callback=None, **kw):
raise NotImplementedError(
'Abstract ``AsyncDALIDriver`` does not implement ``send``')

def receive(self, frame):
def receive(self, frame: Frame) -> None:
"""Receive DALI Frame.

If a forward frame is received, ``self.dispatcher`` is used for
Expand All @@ -93,15 +106,15 @@ class Backend:
"""Object representing a backend.
"""

def read(self, timeout=None):
def read(self, timeout: Optional[int] = None) -> Any:
raise NotImplementedError(
'Abstract ``Backend`` does not implement ``read``')

def write(data):
def write(self, data: bytes) -> None:
raise NotImplementedError(
'Abstract ``Backend`` does not implement ``write``')

def close(self):
def close(self) -> None:
raise NotImplementedError(
'Abstract ``Backend`` does not implement ``close``')

Expand All @@ -111,11 +124,11 @@ class Listener(Backend):
incoming frames.
"""

driver = None
driver: Optional[AsyncDALIDriver] = None
"""``AsyncDALIDriver`` instance.
"""

def listen(self):
def listen(self) -> None:
"""Listen to backend and dispatch receiced data to ``self.driver``.
"""
raise NotImplementedError(
Expand Down
22 changes: 12 additions & 10 deletions dali/driver/daliserver.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from dali.command import Command
from typing import Any, Optional

from dali.command import Command, Response
from dali.exceptions import CommunicationError
import dali.frame
import logging
Expand All @@ -19,23 +21,23 @@ class DaliServer:
90e34a0cd2945dc7a15681f11647e708f858521e or later.
"""

def __init__(self, host="localhost", port=55825,
multiple_frames_per_connection=False):
def __init__(self, host: str = "localhost", port: int = 55825,
multiple_frames_per_connection: bool = False) -> None:
self._target = (host, port)
self._s = None
self._s: Optional[socket.socket] = None
self._multiple_frames_per_connection = multiple_frames_per_connection

def __enter__(self):
def __enter__(self) -> "DaliServer":
if self._multiple_frames_per_connection:
self._s = socket.create_connection(self._target)
return self

def __exit__(self, *vpass):
if self._multiple_frames_per_connection:
def __exit__(self, *vpass: Any) -> None:
if self._s is not None:
self._s.close()
self._s = None

def send(self, command):
def send(self, command: Command) -> Optional[Response]:
if self._s:
s = self._s
else:
Expand All @@ -48,7 +50,7 @@ def send(self, command):
command, " (twice)" if command.sendtwice else ""))

# Set a default result which may be used if the first send fails
result = "\x02\xff\x00\x00"
result = b"\x02\xff\x00\x00"

try:
s.send(message)
Expand All @@ -69,7 +71,7 @@ def send(self, command):

return response

def unpack_response(self, command, result):
def unpack_response(self, command: Command, result: bytes) -> Optional[Response]:
"""Unpack result from the given bytestream and creates the
corresponding response object

Expand Down
Loading
Loading