summaryrefslogtreecommitdiffstats
path: root/freestyle_hid
diff options
context:
space:
mode:
authorDiego Elio Pettenò <flameeyes@flameeyes.com>2021-03-01 23:46:46 +0100
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>2021-03-03 17:04:35 +0100
commit9b16c69bb68b979cbd3e9b6913bf342617dec510 (patch)
tree60d1ebe41f9d81799d4e8903b049b0dc837bfe1b /freestyle_hid
parentUpdate pre-commit configuration. (diff)
downloadfreestyle-hid-9b16c69bb68b979cbd3e9b6913bf342617dec510.tar
freestyle-hid-9b16c69bb68b979cbd3e9b6913bf342617dec510.tar.gz
freestyle-hid-9b16c69bb68b979cbd3e9b6913bf342617dec510.tar.bz2
freestyle-hid-9b16c69bb68b979cbd3e9b6913bf342617dec510.tar.lz
freestyle-hid-9b16c69bb68b979cbd3e9b6913bf342617dec510.tar.xz
freestyle-hid-9b16c69bb68b979cbd3e9b6913bf342617dec510.tar.zst
freestyle-hid-9b16c69bb68b979cbd3e9b6913bf342617dec510.zip
Diffstat (limited to 'freestyle_hid')
-rw-r--r--freestyle_hid/_session.py21
1 files changed, 14 insertions, 7 deletions
diff --git a/freestyle_hid/_session.py b/freestyle_hid/_session.py
index 7529ef3..aab1f6c 100644
--- a/freestyle_hid/_session.py
+++ b/freestyle_hid/_session.py
@@ -81,7 +81,7 @@ _TEXT_REPLY_FORMAT = re.compile(
)
_MULTIRECORDS_FORMAT = re.compile(
- "^(?P<message>.+\r\n)(?P<count>[0-9]+),(?P<checksum>[0-9A-F]{8})\r\n$", re.DOTALL
+ b"^(?P<message>.+\r\n)(?P<count>[0-9]+),(?P<checksum>[0-9A-F]{8})\r\n$", re.DOTALL
)
@@ -191,7 +191,7 @@ class Session:
return message
- def send_text_command(self, command: bytes) -> str:
+ def _send_text_command(self, command: bytes) -> bytes:
"""Send a command to the device that expects a text reply."""
self.send_command(self._text_message_type, command)
@@ -224,11 +224,14 @@ class Session:
if match.group("status") != b"OK":
raise CommandError(repr(message) or "Command failed")
+ return message
+
+ def send_text_command(self, command: bytes) -> bytes:
# If there is anything in the response that is not ASCII-safe, this is
# probably in the patient name. The Windows utility does not seem to
# validate those, so just replace anything non-ASCII with the correct
# unknown codepoint.
- return message.decode("ascii", "replace")
+ return self._send_text_command(command).decode("ascii", "replace")
def query_multirecord(self, command: bytes) -> Iterator[Sequence[str]]:
"""Queries for, and returns, "multirecords" results.
@@ -247,18 +250,22 @@ class Session:
A CSV reader object that returns a record for each line in the
reply buffer.
"""
- message = self.send_text_command(command)
- logging.debug(f"Received multirecord message:\n{message}")
- if message == "Log Empty\r\n":
+ message = self._send_text_command(command)
+ logging.debug(f"Received multi-record message:\n{message!r}")
+ if message == b"Log Empty\r\n":
return iter(())
match = _MULTIRECORDS_FORMAT.search(message)
if not match:
- raise CommandError(message)
+ raise CommandError(repr(message))
records_str = match.group("message")
_verify_checksum(records_str, match.group("checksum"))
+ # Decode here with replacement; the software does not deal with UTF-8
+ # correctly, and appears to truncate incorrectly the strings.
+ records_str = records_str.decode("utf-8", "replace")
+
logging.debug(f"Received multi-record string: {records_str}")
return csv.reader(records_str.split("\r\n"))