diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-30 22:05:12 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-30 22:05:12 -0500 |
| commit | 1964c1f2d8ff54aa0ba93bebfbfef30e5ef46f7a (patch) | |
| tree | b1ccf0f5c48ce50d42c12c8655f647dd30a2dab1 | |
| parent | Add `transitions` and update dependencies (diff) | |
| download | lirc-1964c1f2d8ff54aa0ba93bebfbfef30e5ef46f7a.zip | |
Add DefaultConnection
| -rw-r--r-- | lirc/client.py | 8 | ||||
| -rw-r--r-- | lirc/connection/abstract_connection.py | 4 | ||||
| -rw-r--r-- | lirc/connection/default_connection.py | 29 | ||||
| -rw-r--r-- | lirc/connection/lircd_connection.py | 43 |
4 files changed, 48 insertions, 36 deletions
diff --git a/lirc/client.py b/lirc/client.py index 504ef49..39750df 100644 --- a/lirc/client.py +++ b/lirc/client.py @@ -8,7 +8,7 @@ from .reply_packet_parser import ReplyPacketParser class Client: """Communicate with the lircd daemon.""" - def __init__(self, connection: LircdConnection = None) -> None: + def __init__(self, connection: LircdConnection = LircdConnection()) -> None: """ Initialize the client by connecting to the lircd socket. @@ -17,7 +17,8 @@ class Client: depending on the operating system if one is not provided. Raises: - LircdConnectionError: If the socket cannot connect to the address. + ValueError: If connection is not a LircdConnection. + LircdConnectionError: If the socket cannot connect to the address. """ # Used for start_repeat and stop_repeat self.__last_send_start_remote = None @@ -26,7 +27,8 @@ class Client: if not isinstance(connection, LircdConnection): raise ValueError("`connection` must be an instance of `LircdConnection`") - self.__connection = LircdConnection if connection is None else connection + self.__connection = connection + self.__connection.connect() def __send_command(self, command: str) -> Union[str, List[str]]: """ diff --git a/lirc/connection/abstract_connection.py b/lirc/connection/abstract_connection.py index a515fd1..ca855a6 100644 --- a/lirc/connection/abstract_connection.py +++ b/lirc/connection/abstract_connection.py @@ -5,6 +5,10 @@ from typing import Union class AbstractConnection(ABC): @abstractmethod + def connect(self) -> None: + pass + + @abstractmethod def readline(self) -> str: pass diff --git a/lirc/connection/default_connection.py b/lirc/connection/default_connection.py new file mode 100644 index 0000000..e471fc7 --- /dev/null +++ b/lirc/connection/default_connection.py @@ -0,0 +1,29 @@ +import platform +import socket as stdlib_socket +from typing import Tuple, Union + + +class DefaultConnection: + def __init__(self): + self.__operating_system = platform.system().capitalize() + + @property + def socket(self) -> stdlib_socket.socket: + unix_socket = stdlib_socket.socket( + stdlib_socket.AF_UNIX, stdlib_socket.SOCK_STREAM + ) + tcp_socket = stdlib_socket.socket( + stdlib_socket.AF_INET, stdlib_socket.SOCK_STREAM + ) + + return {"Linux": unix_socket, "Darwin": unix_socket, "Windows": tcp_socket}[ + self.__operating_system + ] + + @property + def address(self) -> Union[str, Tuple[str, int]]: + return { + "Linux": "/var/run/lirc/lircd", + "Windows": ("localhost", 8765), + "Darwin": "/opt/run/var/run/lirc/lircd", + }[self.__operating_system] diff --git a/lirc/connection/lircd_connection.py b/lirc/connection/lircd_connection.py index 39d6876..8b68bdb 100644 --- a/lirc/connection/lircd_connection.py +++ b/lirc/connection/lircd_connection.py @@ -1,49 +1,26 @@ -import platform -import socket as system_socket +import socket as stdlib_socket from collections import deque from typing import Union from ..exceptions import LircdConnectionError, LircdSocketError from .abstract_connection import AbstractConnection +from .default_connection import DefaultConnection class LircdConnection(AbstractConnection): def __init__( self, - address: Union[str, tuple] = None, - socket: system_socket.socket = None, + address: Union[str, tuple] = DefaultConnection().address, + socket: stdlib_socket.socket = DefaultConnection().socket, timeout: float = 5.0, ): - operating_system = platform.system().capitalize() - - self.__default_address = { - "Linux": "/var/run/lirc/lircd", - "Windows": ("localhost", 8765), - "Darwin": "/opt/run/var/run/lirc/lircd", - } - self.__default_socket = { - "Linux": system_socket.socket( - system_socket.AF_UNIX, system_socket.SOCK_STREAM - ), - "Darwin": system_socket.socket( - system_socket.AF_UNIX, system_socket.SOCK_STREAM - ), - "Windows": system_socket.socket( - system_socket.AF_INET, system_socket.SOCK_STREAM - ), - } - self.__buffer = deque() self.__buffer_size = 4096 - self.__socket = ( - socket if socket is not None else self.__default_socket[operating_system] - ) - self.__address = ( - address if address is not None else self.__default_address[operating_system] - ) - + self.__address = address + self.__socket = socket self.__socket.settimeout(timeout) + def connect(self): try: self.__socket.connect(self.__address) except FileNotFoundError: @@ -55,7 +32,7 @@ class LircdConnection(AbstractConnection): raise LircdConnectionError(error) @property - def socket(self) -> system_socket.socket: + def socket(self) -> stdlib_socket.socket: return self.__socket @property @@ -92,12 +69,12 @@ class LircdConnection(AbstractConnection): self.__buffer.append(packet.decode("utf-8")) return self.__buffer.popleft() - except system_socket.timeout: + except stdlib_socket.timeout: raise TimeoutError( "could not find any data on the socket after " f"{self.__socket.gettimeout()} seconds, socket timed out." ) - except system_socket.error as error: + except stdlib_socket.error as error: raise LircdSocketError( f"An error occurred while reading from the lircd socket: {error}" ) |