Skip to content

Commit

Permalink
Add pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPlasse committed Oct 30, 2022
1 parent 7ab7cc6 commit 2a7b06d
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
ignore = E301, E302, E501
per-file-ignores = setup.py: F821
58 changes: 58 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.2.0
hooks:
- id: pyupgrade
args: ["--py36-plus"]

- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: 22.8.0
hooks:
- id: black

- repo: https://github.com/pycqa/flake8
rev: 5.0.4
hooks:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.982
hooks:
- id: mypy
additional_dependencies:
- types-paho-mqtt == 1.6.0.1

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.0-alpha.4
hooks:
- id: prettier

- repo: https://github.com/markdownlint/markdownlint
rev: v0.12.0
hooks:
- id: markdownlint
args:
- "--rules"
- "~MD002,~MD007,~MD013,~MD024,~MD026,~MD029,~MD033,~MD036"

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: check-added-large-files
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: mixed-line-ending
args: ["--fix=lf"]
- id: trailing-whitespace

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.9.0
hooks:
- id: python-check-blanket-type-ignore
- id: python-no-eval
38 changes: 31 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
Expand Down Expand Up @@ -112,7 +113,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix `TypeError` on invalid username and password combination

- Check that _misc_task is not None before trying to cancel it. Contributed by [Ellis Percival (@flyte)](https://github.com/flyte) in [#41](https://github.com/sbtinstruments/asyncio-mqtt/pull/41)
- Check that \_misc_task is not None before trying to cancel it. Contributed by [Ellis Percival (@flyte)](https://github.com/flyte) in [#41](https://github.com/sbtinstruments/asyncio-mqtt/pull/41)

- Fix exception in `on_socket_open`: Non-thread-safe operation invoked on an event loop other than the current one. Contributed by [Ellis Percival (@flyte)](https://github.com/flyte) in [#40](https://github.com/sbtinstruments/asyncio-mqtt/pull/40)

Expand All @@ -136,12 +137,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add `transport` argument to `Client`
- Add `transport` argument to `Client`
Contributed by [@opengs](https://github.com/opengs) in [#21](https://github.com/sbtinstruments/asyncio-mqtt/pull/21)
- Add `clean_session` argument to `Client`
- Add `clean_session` argument to `Client`
Contributed by [@nadyka](https://github.com/madnadyka) in [#17](https://github.com/sbtinstruments/asyncio-mqtt/pull/17)


## [0.7.0] - 2020-08-04

I've tested the library for production use at SBT Instruments.
Expand All @@ -150,13 +150,15 @@ in this release. We are approaching a 1.0.0 release. Let me know
if you want something changed before that via the issue tracker on GitHub.

### Added

- Add support for MQTTv5.
- Add `will` keyword argument to `Client`.
- Add `MqttConnectError` with specific error messages for connection failures.
- Add `Client.id` property that returns the client ID (or `None` if
- Add `Client.id` property that returns the client ID (or `None` if
the client ID was not specified during construction).

### Fixed

- Fix unhandled exception error.
- Fix "Task was destroyed but it is pending" error.
- Fix compatibility with `asyncqt`'s event loop.
Expand All @@ -166,24 +168,31 @@ if you want something changed before that via the issue tracker on GitHub.
Contributed by [Derrick Lyndon Pallas (@pallas)](https://github.com/pallas) in [#12](https://github.com/sbtinstruments/asyncio-mqtt/pull/12)

## [0.6.0] - 2020-06-26

### Changed

- No longer logs exception in `Client.__aexit__`. It's perfectly valid
to exit due to, e.g., `asyncio.CancelledError` so let's not treat it as an
error.

## [0.5.0] - 2020-06-08

### Added

- Add support for python 3.6.
Contributed by [Derrick Lyndon Pallas (@pallas)](https://github.com/pallas) in [#7](https://github.com/sbtinstruments/asyncio-mqtt/pull/7) (1/2).
- Add `client_id` and `tls_context` keyword arguments to the `Client` constructor.
Contributed by [Derrick Lyndon Pallas (@pallas)](https://github.com/pallas) in [#7](https://github.com/sbtinstruments/asyncio-mqtt/pull/7) (2/2).
- Add `timeout` keyword argument to both `Client.connect` and `Client.disconnect`. Default value of `10` seconds (like the other functions).

### Changed

- Propagate disconnection errors to subscribers. This enables user code to detect if a disconnect occurs. E.g., due to network errors.

## [0.4.0] - 2020-05-06

### Changed

- **BREAKING CHANGE:** Forward the [MQTTMessage](https://github.com/eclipse/paho.mqtt.python/blob/1eec03edf39128e461e6729694cf5d7c1959e5e4/src/paho/mqtt/client.py#L355)
class from paho-mqtt instead of just the `payload`. This applies to both
`Client.filtered_messages` and `Client.unfiltered_messages`.
Expand All @@ -193,42 +202,57 @@ if you want something changed before that via the issue tracker on GitHub.
Contributed by [Matthew Bradbury (@MBradbury)](https://github.com/MBradbury) in [#3](https://github.com/sbtinstruments/asyncio-mqtt/pull/3).

## [0.3.0] - 2020-04-13

### Added

- Add `username` and `password` keyword arguments to the `Client` constructor.
Contributed by [@gluap](https://github.com/gluap) in [#1](https://github.com/sbtinstruments/asyncio-mqtt/pull/1).

### Fixed

- Fix log message context for `Client.filtered_messages`.

## [0.2.1] - 2020-04-07

### Fixed

- Fix regression with the `Client._wait_for` helper method introduced in the latest
release.

## [0.2.0] - 2020-04-07

### Changed

- **BREAKING CHANGE:** Replace all uses of `asyncio.TimeoutError` with `MqttError`.

Calls to `Client.subscribe`/`unsubscribe`/`publish` will no longer raise `asyncio.TimeoutError.`

The new behaviour makes it easier to reason about, which exceptions the library
throws:

- Wrong input parameters? Raise `ValueError`.
- Network or protocol failures? `MqttError`.
- Broken library state? `RuntimeError`.

## [0.1.3] - 2020-04-07

### Fixed

- Fix how keyword arguments are forwarded in `Client.publish` and `Client.subscribe`.

## [0.1.2] - 2020-04-06

### Fixed

- Remove log call that was erroneously put in while debugging the latest release.

## [0.1.1] - 2020-04-06

### Fixed

- Add missing parameters to `Client.publish`.
- Fix error in `Client.publish` when paho-mqtt publishes immediately.

## [0.1.0] - 2020-04-06

Initial release.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Redistribution and use in source and binary forms, with or without modification,

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<a href="https://github.com/sbtinstruments/asyncio-mqtt/blob/main/LICENSE"><img alt="License: BSD-3-Clause" src="https://img.shields.io/github/license/sbtinstruments/asyncio-mqtt"></a>
<a href="https://pypi.org/project/asyncio-mqtt"><img alt="PyPI version" src="https://img.shields.io/pypi/v/asyncio-mqtt"></a>
<a href="https://pypi.org/project/asyncio-mqtt"><img alt="PyPI downloads" src="https://img.shields.io/pypi/dm/asyncio-mqtt"></a>
<a href="https://results.pre-commit.ci/latest/github/sbtinstruments/asyncio-mqtt/master"><img alt="pre-commit.ci status" src="https://results.pre-commit.ci/badge/github/sbtinstruments/asyncio-mqtt/master.svg"></a>
<a href="https://github.com/sbtinstruments/asyncio-mqtt"><img alt="Typing: strict" src="https://img.shields.io/badge/typing-strict-green.svg"></a>
<a href="https://github.com/sbtinstruments/asyncio-mqtt"><img alt="Code Style: Black" src="https://img.shields.io/badge/code%20style-black-black"></a>
</p>

Write code like this:
Expand Down
2 changes: 1 addition & 1 deletion asyncio_mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ async def connect(self, *, timeout: int = 10) -> None:
# paho.mqtt.Client.connect may raise one of several exceptions.
# We convert all of them to the common MqttError for user convenience.
# See: https://github.com/eclipse/paho.mqtt.python/blob/v1.5.0/src/paho/mqtt/client.py#L1770
except (socket.error, OSError, mqtt.WebsocketConnectionError) as error:
except (OSError, mqtt.WebsocketConnectionError) as error:
raise MqttError(str(error))
await self._wait_for(self._connected, timeout=timeout)

Expand Down
20 changes: 11 additions & 9 deletions examples/EXAMPLES.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Examples

![license](https://img.shields.io/github/license/sbtinstruments/asyncio-mqtt)
![semver](https://img.shields.io/github/v/tag/sbtinstruments/asyncio-mqtt?sort=semver)

## Sending a JSON payload

### Sending a JSON payload
### Subscriber

##### Subscriber
The following example describes a subscriber expecting to receive a JSON
payload, which, also, connects to the client using Basic Auth and specifying
the MQTT protocol to be used. Please beware that some MQTT brokers requires you
Expand All @@ -14,7 +16,6 @@ accept, please check [client.py](https://github.com/sbtinstruments/asyncio-mqtt/
Please observe that the content which is decoded is not the message received but
the content (payload) which is of the type [MQTTMessage](https://github.com/eclipse/paho.mqtt.python/blob/c339cea2652a957d47de68eafb2a76736c1514e6/src/paho/mqtt/client.py#L355)


```python
import json
from asyncio_mqtt import Client, ProtocolVersion
Expand All @@ -26,14 +27,16 @@ async with Client(
protocol=ProtocolVersion.V31
) as client:
async with client.filtered_messages("floors/+/humidity") as messages:
# subscribe is done afterwards so that we just start receiving messages
# subscribe is done afterwards so that we just start receiving messages
# from this point on
await client.subscribe("floors/#")
async for message in messages:
print(message.topic)
print(json.loads(message.payload))
```
##### Publisher

### Publisher

The publisher, besides specifying the Protocol and using also Basic Auth to
acess the broker, it also specifies the [QoS](https://www.hivemq.com/blog/mqtt-essentials-part-6-mqtt-quality-of-service-levels/) desired

Expand All @@ -52,12 +55,11 @@ async with Client(
payload=json.dumps(message), qos=2, retain=False)
```



## License

![license](https://img.shields.io/github/license/sbtinstruments/asyncio-mqtt)

Note that the underlying paho-mqtt library is dual-licensed. One of the licenses is the so-called [Eclipse Distribution License v1.0](https://www.eclipse.org/org/documents/edl-v10.php). It is almost word-for-word identical to the [BSD 3-clause License](https://opensource.org/licenses/BSD-3-Clause). The only differences are:
* One use of "COPYRIGHT OWNER" (EDL) instead of "COPYRIGHT HOLDER" (BSD)
* One use of "Eclipse Foundation, Inc." (EDL) instead of "copyright holder" (BSD)

- One use of "COPYRIGHT OWNER" (EDL) instead of "COPYRIGHT HOLDER" (BSD)
- One use of "Eclipse Foundation, Inc." (EDL) instead of "copyright holder" (BSD)
3 changes: 0 additions & 3 deletions mypy.ini

This file was deleted.

6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[tool.isort]
profile = "black"

[tool.mypy]
strict = true
show_error_codes = true
10 changes: 7 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# SPDX-License-Identifier: BSD-3-Clause
from setuptools import find_packages, setup

with open("asyncio_mqtt/version.py", "r") as f:
with open("asyncio_mqtt/version.py") as f:
exec(f.read())

with open("README.md", "r", encoding="utf-8") as readme_file:
with open("README.md", encoding="utf-8") as readme_file:
readme = readme_file.read()

setup(
name="asyncio_mqtt",
version=__version__,
version=__version__, # type: ignore[name-defined]
packages=find_packages(),
package_data={
"asyncio_mqtt": ["py.typed"],
Expand Down Expand Up @@ -39,4 +39,8 @@
"async_generator;python_version<'3.7'",
"typing_extensions;python_version<'3.10'",
],
extras_require={
"lint": ["mypy>=0.982", "flake8>=5.0.4", "types-paho-mqtt>=1.6.0.1"],
"format": ["black>=22.10.0", "isort>=5.10.1"],
},
)

0 comments on commit 2a7b06d

Please sign in to comment.