From ff46761b7c1be56e84ecb7b2eb937cd0169867bb Mon Sep 17 00:00:00 2001 From: "Teemu R." Date: Sun, 27 Oct 2024 02:48:34 +0200 Subject: [PATCH] Use ruff for formatting (#1980) Also, simplify the CI pipeline to use pre-commit instead of individual checks --- .github/workflows/ci.yml | 34 +++---------------- .pre-commit-config.yaml | 13 ++++--- README.md | 1 - miio/extract_tokens.py | 4 ++- .../acpartner/airconditioningcompanion.py | 4 +-- miio/miioprotocol.py | 2 +- miio/push_server/server.py | 4 +-- 7 files changed, 20 insertions(+), 42 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ad7614418..62c3c6797 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: python-version: ["3.12"] steps: - - uses: "actions/checkout@v3" + - uses: "actions/checkout@v4" - uses: "actions/setup-python@v4" with: python-version: "${{ matrix.python-version }}" @@ -26,30 +26,9 @@ jobs: run: | python -m pip install --upgrade pip poetry poetry install --extras docs - - name: "Run pyupgrade" + - name: "Run pre-commit hooks" run: | - poetry run pre-commit run pyupgrade --all-files - - name: "Code formating (black)" - run: | - poetry run pre-commit run black --all-files - - name: "Code formating (flake8)" - run: | - poetry run pre-commit run flake8 --all-files - - name: "Order of imports (isort)" - run: | - poetry run pre-commit run isort --all-files -# - name: "Docstring formating (docformatter)" -# run: | -# poetry run pre-commit run docformatter --all-files - - name: "Potential security issues (bandit)" - run: | - poetry run pre-commit run bandit --all-files - - name: "Documentation build (sphinx)" - run: | - poetry run sphinx-build docs/ generated_docs - - name: "Typing checks (mypy)" - run: | - poetry run pre-commit run mypy --all-files + poetry run pre-commit run --all-files --verbose tests: name: "Python ${{ matrix.python-version}} on ${{ matrix.os }}" @@ -61,14 +40,9 @@ jobs: matrix: python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "pypy3.9"] os: [ubuntu-latest, macos-latest, windows-latest] -# Exclude example, in case needed again in the future: -# exclude: -# - python-version: pypy3.8 -# os: macos-latest - steps: - - uses: "actions/checkout@v3" + - uses: "actions/checkout@v4" - uses: "actions/setup-python@v4" with: python-version: "${{ matrix.python-version }}" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index aa04a88dd..bb7ea47e8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,11 +11,16 @@ repos: - id: debug-statements - id: check-ast -- repo: https://github.com/psf/black - rev: 24.2.0 + +repos: +- repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.7.1 hooks: - - id: black - language_version: python3 + # Run the linter. + #- id: ruff + # Run the formatter. + - id: ruff-format - repo: https://github.com/pre-commit/mirrors-isort rev: v5.10.1 diff --git a/README.md b/README.md index 8c0c9c564..c5acc8aa3 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,6 @@ Status](https://github.com/rytilahti/python-miio/actions/workflows/ci.yml/badge. [![Coverage Status](https://codecov.io/gh/rytilahti/python-miio/branch/master/graph/badge.svg?token=lYKWubxkLU)](https://codecov.io/gh/rytilahti/python-miio) [![Documentation status](https://readthedocs.org/projects/python-miio/badge/?version=latest)](https://python-miio.readthedocs.io/en/latest/?badge=latest) -[![Black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) This library (and its accompanying cli tool, `miiocli`) can be used to control devices using Xiaomi's [miIO](https://github.com/OpenMiHome/mihome-binary-protocol/blob/master/doc/PROTOCOL.md) diff --git a/miio/extract_tokens.py b/miio/extract_tokens.py index cf23cfb4d..aecd97e20 100644 --- a/miio/extract_tokens.py +++ b/miio/extract_tokens.py @@ -81,7 +81,9 @@ def decrypt_ztoken(ztoken): keystring = "00000000000000000000000000000000" key = bytes.fromhex(keystring) cipher = Cipher( - algorithms.AES(key), modes.ECB(), backend=default_backend() # nosec + algorithms.AES(key), + modes.ECB(), # nosec + backend=default_backend(), ) decryptor = cipher.decryptor() token = decryptor.update(bytes.fromhex(ztoken[:64])) + decryptor.finalize() diff --git a/miio/integrations/lumi/acpartner/airconditioningcompanion.py b/miio/integrations/lumi/acpartner/airconditioningcompanion.py index a727b1ddf..520363f23 100644 --- a/miio/integrations/lumi/acpartner/airconditioningcompanion.py +++ b/miio/integrations/lumi/acpartner/airconditioningcompanion.py @@ -327,9 +327,9 @@ def send_ir_code(self, model: str, code: str, slot: int = 0): command_bytes = ( code_bytes[0:1] + model_bytes[2:8] - + b"\x94\x70\x1F\xFF" + + b"\x94\x70\x1f\xff" + slot_bytes - + b"\xFF" + + b"\xff" + code_bytes[13:16] + b"\x27" ) diff --git a/miio/miioprotocol.py b/miio/miioprotocol.py index 5feb67c93..c05d92882 100644 --- a/miio/miioprotocol.py +++ b/miio/miioprotocol.py @@ -151,7 +151,7 @@ def send( parameters: Optional[Any] = None, retry_count: int = 3, *, - extra_parameters: Optional[dict] = None + extra_parameters: Optional[dict] = None, ) -> Any: """Build and send the given command. Note that this will implicitly call :func:`send_handshake` to do a handshake, and will re-try in case of errors diff --git a/miio/push_server/server.py b/miio/push_server/server.py index 91f086e7e..9080f3ee1 100644 --- a/miio/push_server/server.py +++ b/miio/push_server/server.py @@ -253,9 +253,7 @@ def _construct_event( # nosec command = f"{self.server_model}.{info.action}:{source_id}" key = f"event.{info.source_model}.{info.event}" message_id = 0 - magic_number = randint( - 1590161094, 1642025774 - ) # nosec, min/max taken from packet captures, unknown use + magic_number = randint(1590161094, 1642025774) # nosec, min/max taken from packet captures, unknown use if len(command) > 49: _LOGGER.error(