summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-07-26 21:40:58 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-07-26 21:40:58 -0500
commit95ef79c2ff578cf3005a0c8f19ffa7a4e85203bc (patch)
tree1073cc62b08e577dfbe298cad1fc76c7721691c0
parentRemove DefaultAddress and DefaultSocket and rename test files (diff)
downloadlirc-95ef79c2ff578cf3005a0c8f19ffa7a4e85203bc.zip
Add default address and default socket to internal state
-rw-r--r--lirc/connection/lircd_connection.py75
1 files changed, 31 insertions, 44 deletions
diff --git a/lirc/connection/lircd_connection.py b/lirc/connection/lircd_connection.py
index f4aa572..18d7726 100644
--- a/lirc/connection/lircd_connection.py
+++ b/lirc/connection/lircd_connection.py
@@ -5,8 +5,6 @@ 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):
@@ -16,40 +14,32 @@ class LircdConnection(AbstractConnection):
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()
+ 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 DefaultSocket[operating_system].value
+ socket if socket is not None else self.__default_socket[operating_system]
)
self.__address = (
- address if address is not None else DefaultAddress[operating_system].value
+ address if address is not None else self.__default_address[operating_system]
)
self.__socket.settimeout(timeout)
@@ -61,6 +51,8 @@ class LircdConnection(AbstractConnection):
f"Could not connect to lircd at {self.__address} with socket "
f"{self.__socket}. Did you start the `lircd` daemon?"
)
+ except Exception as error:
+ raise LircdConnectionError(error)
@property
def socket(self) -> system_socket.socket:
@@ -74,37 +66,32 @@ class LircdConnection(AbstractConnection):
self.__socket.close()
def send(self, data: str):
+ if not isinstance(data, str):
+ raise TypeError("data parameter to send() must be a string")
+
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 packet.endswith(b"\n"):
+ packet = packet.strip()
+
if b"\n" in packet:
- for line in packet.split(b"\n"):
- self.__buffer.append(line.decode("utf-8"))
+ self.__buffer.extend(
+ line.decode("utf-8") for line in packet.split(b"\n")
+ )
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 "