From c38801458c840a7fa634b88f85f0cae9e7defd9e Mon Sep 17 00:00:00 2001 From: Ryan Snodgrass Date: Sun, 28 Jan 2024 23:23:52 -0800 Subject: [PATCH] changed sync client to return bytes instead of decoded str --- example-async.py | 4 +++- example-sync.py | 10 +++++++--- pyavcontrol/client/base.py | 7 ++++--- pyavcontrol/connection/sync_connection.py | 6 ++---- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/example-async.py b/example-async.py index 8e77e5a..16d2f7b 100755 --- a/example-async.py +++ b/example-async.py @@ -53,8 +53,10 @@ async def main(): await client.ping.ping() #help(client.volume) + result = await client.volume.get() - print(f"WHAT: {result}") + print(f"Response: {result}") + await client.volume.set(volume=20) await client.power.off() diff --git a/example-sync.py b/example-sync.py index 99cc858..ed6e9d6 100755 --- a/example-sync.py +++ b/example-sync.py @@ -42,10 +42,14 @@ def main(): connection_config=config_overrides) client.send_raw(b'!PING?') - print(client.ping.ping()) - client.power.off() - client.ping.ping() client.ping.ping() + result = client.volume.get() + print(f"Response: {result}") + + client.volume.set(volume=15) + + client.power.off() + main() diff --git a/pyavcontrol/client/base.py b/pyavcontrol/client/base.py index c1da2f0..6c4239a 100644 --- a/pyavcontrol/client/base.py +++ b/pyavcontrol/client/base.py @@ -152,9 +152,12 @@ def _prepare_request(**kwargs): def _extract_vars_in_response(response: bytes) -> dict: """Given a response, extract all the known values using the response message regex defined for this action.""" + response_text = response.decode(client.encoding()) + if msg := action.definition.get('msg'): if regex := msg.get('regex'): - return re.match(regex, response).groupdict() + return re.match(regex, response_text).groupdict() + return {} # noinspection PyUnusedLocal @@ -162,7 +165,6 @@ def _activity_call_sync(self, **kwargs): """Synchronous version of making a client call""" if request := _prepare_request(**kwargs): if response := client.send_raw(request, wait_for_response=action.response_expected): - response = response.decode(client.encoding()) return _extract_vars_in_response(response) return LOG.warning(f'Failed to make request for {action.group}.{action.name}') @@ -177,7 +179,6 @@ async def _activity_call_async(self, **kwargs): if request := _prepare_request(**kwargs): # noinspection PyUnresolvedReferences if response := await client.send_raw(request, wait_for_response=action.response_expected): - response = response.decode(client.encoding()) return _extract_vars_in_response(response) return LOG.warning(f'Failed to make request for {action.group}.{action.name}') diff --git a/pyavcontrol/connection/sync_connection.py b/pyavcontrol/connection/sync_connection.py index 75919a8..f00bf7b 100644 --- a/pyavcontrol/connection/sync_connection.py +++ b/pyavcontrol/connection/sync_connection.py @@ -101,11 +101,9 @@ def write_rate_limited(data_bytes: bytes): callback(result) return result - def handle_receive(self) -> str: + def handle_receive(self) -> bytes: skip = 0 - print(self._eol) - len_eol = len(self._eol) # FIXME: implement a much better receive mechanism, without timeouts. @@ -128,4 +126,4 @@ def handle_receive(self) -> str: ret = bytes(result) LOG.debug(f'Received {self._url} "%s"', ret) - return ret.decode(self._encoding) + return ret