From 00f91e02786d340d10d0a42efeacc9cbde9c3fe0 Mon Sep 17 00:00:00 2001 From: Jason Sohn <75611662+tensorturtle@users.noreply.github.com> Date: Tue, 14 Nov 2023 01:27:29 +0900 Subject: [PATCH 1/6] replace importlib.resources.file to path for backward compatibility to python3.8 --- pycycling/sterzo.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pycycling/sterzo.py b/pycycling/sterzo.py index 9ffcdb9..3aca74b 100644 --- a/pycycling/sterzo.py +++ b/pycycling/sterzo.py @@ -20,12 +20,14 @@ async def enable_steering_measurement_notifications(self): while self._latest_challenge is None: await asyncio.sleep(2) await self._activate_steering_measurements() - + async def _activate_steering_measurements(self): - with importlib.resources.files(__package__).joinpath('data/sterzo-challenge-codes.dat').open('rb') as fp: - fp.seek(self._latest_challenge * 2, 1) - code_1 = int.from_bytes(fp.read(1), 'little') - code_2 = int.from_bytes(fp.read(1), 'little') + # Use importlib.resources.path as a context manager + with importlib.resources.path(__package__, 'data/sterzo-challenge-codes.dat') as file_path: + with open(file_path, 'rb') as fp: + fp.seek(self._latest_challenge * 2, 1) + code_1 = int.from_bytes(fp.read(1), 'little') + code_2 = int.from_bytes(fp.read(1), 'little') byte_array = bytearray([0x03, 0x11, code_1, code_2]) await self._client.write_gatt_char(sterzo_control_point_id, byte_array) From f11275b4d65ac13eda3b029e08b9c075f68bb6c9 Mon Sep 17 00:00:00 2001 From: tensorturtle Date: Tue, 14 Nov 2023 02:22:56 +0900 Subject: [PATCH 2/6] test legacy support --- examples/sterzo_example.py | 2 +- pycycling/{data => }/sterzo-challenge-codes.dat | Bin pycycling/sterzo.py | 16 ++++++++++++---- 3 files changed, 13 insertions(+), 5 deletions(-) rename pycycling/{data => }/sterzo-challenge-codes.dat (100%) diff --git a/examples/sterzo_example.py b/examples/sterzo_example.py index a85429d..78f09fe 100644 --- a/examples/sterzo_example.py +++ b/examples/sterzo_example.py @@ -20,6 +20,6 @@ def steering_handler(steering_angle): os.environ["PYTHONASYNCIODEBUG"] = str(1) - device_address = "36A444C9-2A18-4B6B-B671-E0A8D3DADB1D" + device_address = "EA:1B:D7:96:1A:A1" loop = asyncio.get_event_loop() loop.run_until_complete(run(device_address)) diff --git a/pycycling/data/sterzo-challenge-codes.dat b/pycycling/sterzo-challenge-codes.dat similarity index 100% rename from pycycling/data/sterzo-challenge-codes.dat rename to pycycling/sterzo-challenge-codes.dat diff --git a/pycycling/sterzo.py b/pycycling/sterzo.py index 3aca74b..d5287ee 100644 --- a/pycycling/sterzo.py +++ b/pycycling/sterzo.py @@ -1,3 +1,4 @@ +import sys import asyncio import struct import importlib.resources @@ -20,14 +21,21 @@ async def enable_steering_measurement_notifications(self): while self._latest_challenge is None: await asyncio.sleep(2) await self._activate_steering_measurements() - + async def _activate_steering_measurements(self): - # Use importlib.resources.path as a context manager - with importlib.resources.path(__package__, 'data/sterzo-challenge-codes.dat') as file_path: - with open(file_path, 'rb') as fp: + # moved .dat file out of 'data' because accessing directories not possible for importlib.resources.path + # importlib.resources.path is deprecated since 3.11 + if sys.version_info >= (3,11): + with importlib.resources.files(__package__).joinpath('sterzo-challenge-codes.dat').open('rb') as fp: fp.seek(self._latest_challenge * 2, 1) code_1 = int.from_bytes(fp.read(1), 'little') code_2 = int.from_bytes(fp.read(1), 'little') + else: # legacy support < 3.9 + with importlib.resources.path(__package__, 'sterzo-challenge-codes.dat') as file_path: + with open(file_path, 'rb') as fp: + fp.seek(self._latest_challenge * 2, 1) + code_1 = int.from_bytes(fp.read(1), 'little') + code_2 = int.from_bytes(fp.read(1), 'little') byte_array = bytearray([0x03, 0x11, code_1, code_2]) await self._client.write_gatt_char(sterzo_control_point_id, byte_array) From ff6bf7fc0a47352e9f8061ab55c5b4ad14cb306a Mon Sep 17 00:00:00 2001 From: tensorturtle Date: Tue, 14 Nov 2023 02:32:42 +0900 Subject: [PATCH 3/6] revert example address --- examples/sterzo_example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sterzo_example.py b/examples/sterzo_example.py index 78f09fe..a85429d 100644 --- a/examples/sterzo_example.py +++ b/examples/sterzo_example.py @@ -20,6 +20,6 @@ def steering_handler(steering_angle): os.environ["PYTHONASYNCIODEBUG"] = str(1) - device_address = "EA:1B:D7:96:1A:A1" + device_address = "36A444C9-2A18-4B6B-B671-E0A8D3DADB1D" loop = asyncio.get_event_loop() loop.run_until_complete(run(device_address)) From 1c3f919fde3283f0c23e8003028a8597f2d4ac2f Mon Sep 17 00:00:00 2001 From: tensorturtle Date: Mon, 20 Nov 2023 02:43:14 +0900 Subject: [PATCH 4/6] use open_binary, remove duplication --- examples/sterzo_example.py | 2 +- pycycling/sterzo.py | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/sterzo_example.py b/examples/sterzo_example.py index a85429d..78f09fe 100644 --- a/examples/sterzo_example.py +++ b/examples/sterzo_example.py @@ -20,6 +20,6 @@ def steering_handler(steering_angle): os.environ["PYTHONASYNCIODEBUG"] = str(1) - device_address = "36A444C9-2A18-4B6B-B671-E0A8D3DADB1D" + device_address = "EA:1B:D7:96:1A:A1" loop = asyncio.get_event_loop() loop.run_until_complete(run(device_address)) diff --git a/pycycling/sterzo.py b/pycycling/sterzo.py index d5287ee..272d9d9 100644 --- a/pycycling/sterzo.py +++ b/pycycling/sterzo.py @@ -31,11 +31,10 @@ async def _activate_steering_measurements(self): code_1 = int.from_bytes(fp.read(1), 'little') code_2 = int.from_bytes(fp.read(1), 'little') else: # legacy support < 3.9 - with importlib.resources.path(__package__, 'sterzo-challenge-codes.dat') as file_path: - with open(file_path, 'rb') as fp: - fp.seek(self._latest_challenge * 2, 1) - code_1 = int.from_bytes(fp.read(1), 'little') - code_2 = int.from_bytes(fp.read(1), 'little') + with importlib.resources.open_binary(__package__, 'sterzo-challenge-codes.dat') as fp: + fp.seek(self._latest_challenge * 2, 1) + code_1 = int.from_bytes(fp.read(1), 'little') + code_2 = int.from_bytes(fp.read(1), 'little') byte_array = bytearray([0x03, 0x11, code_1, code_2]) await self._client.write_gatt_char(sterzo_control_point_id, byte_array) From 69f17d2c011045eb1745c0aa9e655a58d8c158c3 Mon Sep 17 00:00:00 2001 From: tensorturtle Date: Mon, 20 Nov 2023 02:43:50 +0900 Subject: [PATCH 5/6] revert example --- examples/sterzo_example.py | 2 +- pycycling/sterzo.py | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/examples/sterzo_example.py b/examples/sterzo_example.py index 78f09fe..a85429d 100644 --- a/examples/sterzo_example.py +++ b/examples/sterzo_example.py @@ -20,6 +20,6 @@ def steering_handler(steering_angle): os.environ["PYTHONASYNCIODEBUG"] = str(1) - device_address = "EA:1B:D7:96:1A:A1" + device_address = "36A444C9-2A18-4B6B-B671-E0A8D3DADB1D" loop = asyncio.get_event_loop() loop.run_until_complete(run(device_address)) diff --git a/pycycling/sterzo.py b/pycycling/sterzo.py index 272d9d9..0d8a437 100644 --- a/pycycling/sterzo.py +++ b/pycycling/sterzo.py @@ -26,15 +26,14 @@ async def _activate_steering_measurements(self): # moved .dat file out of 'data' because accessing directories not possible for importlib.resources.path # importlib.resources.path is deprecated since 3.11 if sys.version_info >= (3,11): - with importlib.resources.files(__package__).joinpath('sterzo-challenge-codes.dat').open('rb') as fp: - fp.seek(self._latest_challenge * 2, 1) - code_1 = int.from_bytes(fp.read(1), 'little') - code_2 = int.from_bytes(fp.read(1), 'little') + challenge_file = importlib.resources.files(__package__).joinpath('sterzo-challenge-codes.dat') else: # legacy support < 3.9 - with importlib.resources.open_binary(__package__, 'sterzo-challenge-codes.dat') as fp: - fp.seek(self._latest_challenge * 2, 1) - code_1 = int.from_bytes(fp.read(1), 'little') - code_2 = int.from_bytes(fp.read(1), 'little') + challenge_file = importlib.resources.open_binary(__package__, 'sterzo-challenge-codes.dat') + + with challenge_file.open('rb') as fp: + fp.seek(self._latest_challenge * 2, 1) + code_1 = int.from_bytes(fp.read(1), 'little') + code_2 = int.from_bytes(fp.read(1), 'little') byte_array = bytearray([0x03, 0x11, code_1, code_2]) await self._client.write_gatt_char(sterzo_control_point_id, byte_array) From 7368e7a982d263b76def78cdaffada6b07e92661 Mon Sep 17 00:00:00 2001 From: tensorturtle Date: Mon, 20 Nov 2023 02:52:46 +0900 Subject: [PATCH 6/6] restore sterzo data file into directory --- pycycling/data/__init__.py | 0 pycycling/{ => data}/sterzo-challenge-codes.dat | Bin pycycling/sterzo.py | 13 +++++++------ 3 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 pycycling/data/__init__.py rename pycycling/{ => data}/sterzo-challenge-codes.dat (100%) diff --git a/pycycling/data/__init__.py b/pycycling/data/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pycycling/sterzo-challenge-codes.dat b/pycycling/data/sterzo-challenge-codes.dat similarity index 100% rename from pycycling/sterzo-challenge-codes.dat rename to pycycling/data/sterzo-challenge-codes.dat diff --git a/pycycling/sterzo.py b/pycycling/sterzo.py index 0d8a437..ff580ab 100644 --- a/pycycling/sterzo.py +++ b/pycycling/sterzo.py @@ -2,6 +2,7 @@ import asyncio import struct import importlib.resources +import pycycling.data sterzo_measurement_id = '347b0030-7635-408b-8918-8ff3949ce592' sterzo_control_point_id = '347b0031-7635-408b-8918-8ff3949ce592' @@ -26,14 +27,14 @@ async def _activate_steering_measurements(self): # moved .dat file out of 'data' because accessing directories not possible for importlib.resources.path # importlib.resources.path is deprecated since 3.11 if sys.version_info >= (3,11): - challenge_file = importlib.resources.files(__package__).joinpath('sterzo-challenge-codes.dat') + challenge_file = importlib.resources.files(pycycling.data).joinpath('sterzo-challenge-codes.dat').open('rb') else: # legacy support < 3.9 - challenge_file = importlib.resources.open_binary(__package__, 'sterzo-challenge-codes.dat') + challenge_file = importlib.resources.open_binary(pycycling.data, 'sterzo-challenge-codes.dat') - with challenge_file.open('rb') as fp: - fp.seek(self._latest_challenge * 2, 1) - code_1 = int.from_bytes(fp.read(1), 'little') - code_2 = int.from_bytes(fp.read(1), 'little') + with challenge_file: + challenge_file.seek(self._latest_challenge * 2, 1) + code_1 = int.from_bytes(challenge_file.read(1), 'little') + code_2 = int.from_bytes(challenge_file.read(1), 'little') byte_array = bytearray([0x03, 0x11, code_1, code_2]) await self._client.write_gatt_char(sterzo_control_point_id, byte_array)