diff options
| -rw-r--r-- | lirc/connection/__init__.py | 0 | ||||
| -rw-r--r-- | lirc/connection/abstract_connection.py | 33 | ||||
| -rw-r--r-- | lirc/connection/default_address.py | 12 | ||||
| -rw-r--r-- | lirc/connection/default_socket.py | 13 | ||||
| -rw-r--r-- | lirc/connection/lircd_connection.py | 116 |
5 files changed, 174 insertions, 0 deletions
diff --git a/lirc/connection/__init__.py b/lirc/connection/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lirc/connection/__init__.py diff --git a/lirc/connection/abstract_connection.py b/lirc/connection/abstract_connection.py new file mode 100644 index 0000000..a515fd1 --- /dev/null +++ b/lirc/connection/abstract_connection.py @@ -0,0 +1,33 @@ +import socket +from abc import ABC, abstractmethod +from typing import Union + + +class AbstractConnection(ABC): + @abstractmethod + def readline(self) -> str: + pass + + @abstractmethod + def send(self, data: str): + pass + + @abstractmethod + def close(self) -> None: + pass + + @property + @abstractmethod + def socket(self) -> socket.socket: + pass + + @property + @abstractmethod + def address(self) -> Union[str, tuple]: + pass + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + return self.close() diff --git a/lirc/connection/default_address.py b/lirc/connection/default_address.py new file mode 100644 index 0000000..4d87703 --- /dev/null +++ b/lirc/connection/default_address.py @@ -0,0 +1,12 @@ +from enum import Enum + + +class DefaultAddress(Enum): + """ + Default addresses to use for connecting to the + lircd daemon, depending on the operating system. + """ + + LINUX = "/var/run/lirc/lircd" + WINDOWS = ("localhost", 8765) + DARWIN = "/opt/run/var/run/lirc/lircd" diff --git a/lirc/connection/default_socket.py b/lirc/connection/default_socket.py new file mode 100644 index 0000000..ee95529 --- /dev/null +++ b/lirc/connection/default_socket.py @@ -0,0 +1,13 @@ +import socket +from enum import Enum + + +class DefaultSocket(Enum): + """ + Default socket to use for the lircd socket, depending + on the operating system. + """ + + LINUX = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + DARWIN = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + WINDOWS = socket.socket(socket.AF_INET, socket.SOCK_STREAM) diff --git a/lirc/connection/lircd_connection.py b/lirc/connection/lircd_connection.py new file mode 100644 index 0000000..f4aa572 --- /dev/null +++ b/lirc/connection/lircd_connection.py @@ -0,0 +1,116 @@ +import platform +import socket as system_socket +from collections import deque +from typing import Union + +from ..exceptions import LircdConnectionError, LircdSocketError, LircdSocketTimeoutError +from .abstract_connection import AbstractConnection +from .default_address import DefaultAddress +from .default_socket import DefaultSocket + + +class LircdConnection(AbstractConnection): + def __init__( + self, + address: Union[str, tuple] = None, + socket: system_socket.socket = None, + timeout: float = 5.0, + ): + """ + Initialize the connection by connecting to the lircd socket. + + The default address depends on the operating system + you are using. On Linux, it is "/var/run/lirc/lircd". + On macOS, it is "/opt/run/var/run/lirc/lircd". And on + Windows, it is ("localhost", 8765) to connect to localhost:8765, + which is the default address for WinLIRC. + + The socket is also determined for you if one is not provided. A unix + domain socket is connected to on macOS and Linux. A TCP socket + is used for Windows. + + Args: + address: The address to lircd, determined for you if + one is not provided. + + socket: The socket.socket used to connect to the lircd socket. + + timeout: The amount of time in seconds to wait before timing out when + receiving data from lircd + + Raises: + LircConnectionError: If the socket cannot connect to the address. + """ + operating_system = platform.system().upper() + + self.__buffer = deque() + self.__buffer_size = 4096 + self.__socket = ( + socket if socket is not None else DefaultSocket[operating_system].value + ) + self.__address = ( + address if address is not None else DefaultAddress[operating_system].value + ) + + self.__socket.settimeout(timeout) + + try: + self.__socket.connect(self.__address) + except FileNotFoundError: + raise LircdConnectionError( + f"Could not connect to lircd at {self.__address} with socket " + f"{self.__socket}. Did you start the `lircd` daemon?" + ) + + @property + def socket(self) -> system_socket.socket: + return self.__socket + + @property + def address(self) -> str: + return self.__address + + def close(self): + self.__socket.close() + + def send(self, data: str): + if not data.endswith("\n"): + data += "\n" + + self.__socket.sendall(data.encode("utf-8")) + + def readline(self) -> str: + """ + Read the reply packet that is on the socket after a + command is sent to lircd. + + Raises: + LircdSocketTimeoutError: If recv does not find any data after + the specified timeout amount of seconds (called without a command + being sent?). + + LircdSocketError: If something else went wrong with the socket. + """ + if len(self.__buffer) >= 1: + return self.__buffer.popleft() + + try: + packet = self.__socket.recv(self.__buffer_size) + + if b"\n" in packet: + for line in packet.split(b"\n"): + self.__buffer.append(line.decode("utf-8")) + else: + self.__buffer.append(packet.decode("utf-8")) + + return self.__buffer.popleft() + + except system_socket.timeout: + raise LircdSocketTimeoutError( + "could not find any data on the socket after " + f"{self.__socket.gettimeout()} seconds, socket timed out." + ) + except system_socket.error as error: + raise LircdSocketError( + f"An error occurred while reading from the lircd socket: {error}" + ) |