summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-07-12 18:41:07 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-07-12 18:41:07 -0500
commit5286eecfb5fbf4325ce9c9382752ecf4bb3d542a (patch)
tree32c3e1cd749f5ab45a76861160c5f116fca75161
parentUse lint task for ci (diff)
downloadlirc-5286eecfb5fbf4325ce9c9382752ecf4bb3d542a.zip
Seperate lirc into seperate files and add error types
-rw-r--r--lirc/__init__.py18
-rw-r--r--lirc/exceptions.py18
-rw-r--r--lirc/lirc.py59
-rw-r--r--lirc/response.py18
4 files changed, 70 insertions, 43 deletions
diff --git a/lirc/__init__.py b/lirc/__init__.py
index 7b7c9f9..bc43eb3 100644
--- a/lirc/__init__.py
+++ b/lirc/__init__.py
@@ -1,5 +1,19 @@
-from .lirc import Lirc, LircResponse, LircSocketError, LircSocketTimeoutError
+from .exceptions import (
+ InvalidReplyPacketFormat,
+ LircError,
+ LircSocketError,
+ LircSocketTimeoutError,
+)
+from .lirc import Lirc
+from .response import LircResponse
__version__ = "0.0.0"
-__all__ = ["Lirc", "LircResponse", "LircSocketError", "LircSocketTimeoutError"]
+__all__ = [
+ "Lirc",
+ "LircResponse",
+ "LircError",
+ "LircSocketError",
+ "LircSocketTimeoutError",
+ "InvalidReplyPacketFormat",
+]
diff --git a/lirc/exceptions.py b/lirc/exceptions.py
new file mode 100644
index 0000000..2a0b7ea
--- /dev/null
+++ b/lirc/exceptions.py
@@ -0,0 +1,18 @@
+class LircError(Exception):
+ """A generic error that comes from this package."""
+
+
+class InvalidReplyPacketFormat(LircError):
+ """The reply packet from LIRC was in an invalid format."""
+
+
+class LircSocketError(LircError):
+ """For when a generic error occurs with the lircd socket"""
+
+
+class LircSocketTimeoutError(LircSocketError):
+ """
+ For when a timeout error occurs with the socket.
+ This can happen when recv does not find any data for
+ a given amount of time.
+ """
diff --git a/lirc/lirc.py b/lirc/lirc.py
index 4101c17..bc9a147 100644
--- a/lirc/lirc.py
+++ b/lirc/lirc.py
@@ -2,37 +2,12 @@ import socket
import threading
from itertools import islice
-
-class LircResponse:
- """
- A response from the LIRC daemon. Stores the command that had
- been sent, whether or not it was successful, and the parsed
- reply packet data.
- """
-
- def __init__(self, command: str, success: bool, data: list):
- self.command = command
- self.success = success
- self.data = data
-
- def __repr__(self):
- return (
- f"LircResponse(command={self.command}, "
- f"success={self.success}, "
- f"data={self.data})"
- )
-
-
-class LircSocketError(Exception):
- """For when a generic error occurs with the lircd socket"""
-
-
-class LircSocketTimeoutError(LircSocketError):
- """
- For when a timeout error occurs with the socket.
- This can happen when recv does not find any data for
- a given amount of time.
- """
+from lirc.exceptions import (
+ InvalidReplyPacketFormat,
+ LircSocketError,
+ LircSocketTimeoutError,
+)
+from lirc.response import LircResponse
class Lirc:
@@ -67,7 +42,7 @@ class Lirc:
socket_path: str = DEFAULT_SOCKET_PATH,
socket: socket.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM),
socket_timeout: int = 5,
- ):
+ ) -> None:
"""
Initialize Lirc by connecting to the lircd socket.
@@ -75,10 +50,10 @@ class Lirc:
:param socket: The socket.socket used to connect to the lircd socket.
"""
self.__lock = threading.Lock()
- self.socket = socket
- self.socket_timeout = socket_timeout
- self.socket.settimeout(self.socket_timeout)
- self.socket.connect(socket_path)
+ self.__socket = socket
+ self.__socket_timeout = socket_timeout
+ self.__socket.settimeout(self.__socket_timeout)
+ self.__socket.connect(socket_path)
def __send_command(self, command: str) -> LircResponse:
"""
@@ -96,7 +71,7 @@ class Lirc:
try:
self.__lock.acquire()
- self.socket.sendall(command.encode(self.ENCODING))
+ self.__socket.sendall(command.encode(self.ENCODING))
reply_packet = self.__read_reply_packet()
return self.__parse_reply_packet(reply_packet)
@@ -125,16 +100,16 @@ class Lirc:
try:
BUFFER_LENGTH = 256
buffer = ""
- data = self.socket.recv(BUFFER_LENGTH)
+ data = self.__socket.recv(BUFFER_LENGTH)
# Ignore recieve requests that the socket caches
while "BEGIN" not in data.decode(self.ENCODING):
- data = self.socket.recv(BUFFER_LENGTH)
+ data = self.__socket.recv(BUFFER_LENGTH)
buffer += data.decode(self.ENCODING)
while not buffer.endswith("END\n"):
- data = self.socket.recv(BUFFER_LENGTH)
+ data = self.__socket.recv(BUFFER_LENGTH)
buffer += data.decode(self.ENCODING)
return buffer
@@ -179,7 +154,9 @@ class Lirc:
data_length = int(lines[current_index])
current_index += 1
else:
- raise ValueError(f"Unknown format for reply packet: \n{lines}")
+ raise InvalidReplyPacketFormat(
+ f"Unknown format for reply packet: \n{lines}"
+ )
for line in islice(lines, current_index, current_index + data_length):
response.data.append(line)
diff --git a/lirc/response.py b/lirc/response.py
new file mode 100644
index 0000000..3491037
--- /dev/null
+++ b/lirc/response.py
@@ -0,0 +1,18 @@
+class LircResponse:
+ """
+ A response from the LIRC daemon. Stores the command that had
+ been sent, whether or not it was successful, and the parsed
+ reply packet data.
+ """
+
+ def __init__(self, command: str, success: bool, data: list) -> None:
+ self.command = command
+ self.success = success
+ self.data = data
+
+ def __repr__(self) -> str:
+ return (
+ f"LircResponse(command={self.command}, "
+ f"success={self.success}, "
+ f"data={self.data})"
+ )