Skip to content

Commit

Permalink
Support python 3.12 & minor improvements (#10)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
qqaatw and pre-commit-ci[bot] authored Jan 21, 2024
1 parent dbe4bbe commit 9a0cd85
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 18 additions & 15 deletions JciHitachi/aws_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
28 changes: 27 additions & 1 deletion tests/test_aws_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9a0cd85

Please sign in to comment.