From 9a0cd85f88ac7586b5d013e99b757b1b614054be Mon Sep 17 00:00:00 2001 From: "Li-Huai (Allan) Lin" Date: Sun, 21 Jan 2024 13:31:58 -0800 Subject: [PATCH] Support python 3.12 & minor improvements (#10) * aws_connection.py improvement * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Python 3.12 support * Update setup.py * docs update --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .github/workflows/CI.yml | 2 +- JciHitachi/aws_connection.py | 33 ++++++++++++++++++--------------- setup.py | 3 ++- tests/test_aws_connection.py | 28 +++++++++++++++++++++++++++- 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 65c9009..99eb181 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -16,7 +16,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, windows-latest] - python-version: ['3.9', '3.10', '3.11'] + python-version: ['3.9', '3.10', '3.11', '3.12'] steps: - uses: actions/checkout@v2 diff --git a/JciHitachi/aws_connection.py b/JciHitachi/aws_connection.py index a2047c2..2943f59 100644 --- a/JciHitachi/aws_connection.py +++ b/JciHitachi/aws_connection.py @@ -548,15 +548,15 @@ class JciHitachiAWSMqttConnection: def __init__( self, get_credentials_callable: Callable, print_response: bool = False ): - self._get_credentials_callable = get_credentials_callable - self._print_response = print_response + self._get_credentials_callable: Callable = get_credentials_callable + self._print_response: bool = print_response - self._mqttc = None - self._shadow_mqttc = None - self._client_tokens = {} - self._mqtt_events = JciHitachiMqttEvents() - self._execution_lock = threading.Lock() - self._execution_pools = JciHitachiExecutionPools() + self._mqttc: Optional[awscrt.mqtt.Connection] = None + self._shadow_mqttc: Optional[iotshadow.IotShadowClient] = None + self._client_tokens: dict[str, str] = {} + self._mqtt_events: JciHitachiMqttEvents = JciHitachiMqttEvents() + self._execution_lock: threading.Lock = threading.Lock() + self._execution_pools: JciHitachiExecutionPools = JciHitachiExecutionPools() def __del__(self): self.disconnect() @@ -695,7 +695,13 @@ def disconnect(self) -> None: self._mqttc.disconnect() def configure(self, identity_id) -> None: - """Configure MQTT.""" + """Configure MQTT. + + Parameters + ---------- + identity_id : str + Identity ID. + """ cred_provider = awscrt.auth.AwsCredentialsProvider.new_delegate( self._get_credentials_callable @@ -1055,15 +1061,12 @@ async def runner(): return a, b, c, d locked = self._execution_lock.locked() + if locked: + _LOGGER.debug("Other execution in progress, waiting for a lock.") - try: - if locked: - _LOGGER.debug("Other execution in progress, waiting for a lock.") - self._execution_lock.acquire() + with self._execution_lock: if locked: _LOGGER.debug("Lock acquired.") results = asyncio.run(runner()) - finally: - self._execution_lock.release() return results diff --git a/setup.py b/setup.py index 1990130..82e6689 100644 --- a/setup.py +++ b/setup.py @@ -36,12 +36,13 @@ "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", ], packages=setuptools.find_packages(include=["JciHitachi"]), package_data={"JciHitachi": ["cert/*.pem"]}, - python_requires=">=3.8", + python_requires=">=3.9", install_requires=install_requires, tests_require=tests_require, ) diff --git a/tests/test_aws_connection.py b/tests/test_aws_connection.py index 1b7b103..51b1009 100644 --- a/tests/test_aws_connection.py +++ b/tests/test_aws_connection.py @@ -261,8 +261,34 @@ def publish_shadow_func(request, qos): def test_execute(self, fixture_aws_mock_mqtt_connection): mqtt = fixture_aws_mock_mqtt_connection + # The executed functions are no-op. + mqtt._execution_pools.support_execution_pool.append( + mqtt._wrap_async("support_identifier", lambda: None) + ) + mqtt._execution_pools.shadow_execution_pool.append( + mqtt._wrap_async("shadow_identifier", lambda: None) + ) + mqtt._execution_pools.status_execution_pool.append( + mqtt._wrap_async("status_identifier", lambda: None) + ) + mqtt._execution_pools.control_execution_pool.append( + mqtt._wrap_async("control_identifier", lambda: None) + ) + results = mqtt.execute() - assert results == (None, None, None, None) + assert results == ( + ["support_identifier"], + ["shadow_identifier"], + ["status_identifier"], + None, + ) + assert len(mqtt._execution_pools.support_execution_pool) == 0 + assert len(mqtt._execution_pools.shadow_execution_pool) == 0 + assert len(mqtt._execution_pools.status_execution_pool) == 0 + + results = mqtt.execute(control=True) + assert results == (None, None, None, ["control_identifier"]) + assert len(mqtt._execution_pools.control_execution_pool) == 0 class TestJciHitachiAWSCognitoConnection: