Skip to content

Commit

Permalink
refactor: use plain Black code style
Browse files Browse the repository at this point in the history
Use default code line-length of 88 instead of custom 120.
Seems more suited for a public library :-)
  • Loading branch information
zehnm committed Nov 4, 2023
1 parent 84d2c1c commit 2f3660e
Show file tree
Hide file tree
Showing 19 changed files with 277 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-code-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
python -m isort ucapi/. --check --verbose
- name: Check code formatting with black
run: |
python -m black ucapi --check --verbose --line-length 120
python -m black ucapi --check --verbose
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Python API wrapper for the UC Integration API
[![PyPi](https://img.shields.io/pypi/v/ucapi.svg)](https://pypi.org/project/ucapi)
[![License](https://img.shields.io/github/license/unfoldedcircle/ucapi.svg)](LICENSE)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

This library simplifies writing Python based integrations for the [Unfolded Circle Remote Two](https://www.unfoldedcircle.com/)
by wrapping the [WebSocket Integration API](https://github.com/unfoldedcircle/core-api/tree/main/integration-api).
Expand All @@ -19,6 +22,12 @@ Not yet supported:
Requirements:
- Python 3.10 or newer

## Installation

Use pip:
```shell
pip3 install ucapi
```
## Usage

Install build tools:
Expand Down
15 changes: 7 additions & 8 deletions docs/code_guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Note: once <https://github.com/pypa/pip/issues/11440> is implemented, the requir
### Linting

```shell
python -m pylint ucapi
python3 -m pylint ucapi
```

- The tool is configured in `.pylintrc`.
Expand All @@ -34,7 +34,7 @@ Linting integration in PyCharm/IntelliJ IDEA:
Import statements must be sorted with [isort](https://pycqa.github.io/isort/):

```shell
python -m isort ucapi/.
python3 -m isort ucapi/.
```

- The tool is configured in `pyproject.toml` to use the `black` code-formatting profile.
Expand All @@ -44,23 +44,22 @@ python -m isort ucapi/.
Source code is formatted with the [Black code formatting tool](https://github.com/psf/black):

```shell
python -m black ucapi --line-length 120
python3 -m black ucapi --line-length 120
```

PyCharm/IntelliJ IDEA integration:
1. Go to `Preferences or Settings -> Tools -> Black`
2. Configure:
- Python interpreter
- Use Black formatter: `On code reformat` & optionally `On save`
- Arguments: `--line-length 120`

## Verify

The following tests are run as GitHub action for each push on the main branch and for pull requests.
They can also be run anytime on a local developer machine:
```shell
python -m pylint ucapi
python -m flake8 ucapi --count --show-source --statistics
python -m isort ucapi/. --check --verbose
python -m black ucapi --check --verbose --line-length 120
python3 -m pylint ucapi
python3 -m flake8 ucapi --count --show-source --statistics
python3 -m isort ucapi/. --check --verbose
python3 -m black ucapi --check --verbose
```
6 changes: 4 additions & 2 deletions examples/hello_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
api = ucapi.IntegrationAPI(loop)


async def cmd_handler(entity: ucapi.Button, cmd_id: str, _params: dict[str, Any] | None) -> ucapi.StatusCodes:
async def cmd_handler(
entity: ucapi.Button, cmd_id: str, _params: dict[str, Any] | None
) -> ucapi.StatusCodes:
"""
Push button command handler.
Expand All @@ -28,7 +30,7 @@ async def cmd_handler(entity: ucapi.Button, cmd_id: str, _params: dict[str, Any]

@api.listens_to(ucapi.Events.CONNECT)
async def on_connect() -> None:
"""When the remote connects, we just set the device state. We are ready all the time!"""
# When the remote connects, we just set the device state. We are ready all the time!
await api.set_device_state(ucapi.DeviceStates.CONNECTED)


Expand Down
18 changes: 12 additions & 6 deletions examples/setup_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ async def driver_setup_handler(msg: ucapi.SetupDriver) -> ucapi.SetupAction:
Either start the setup process or handle the provided user input data.
:param msg: the setup driver request object, either DriverSetupRequest or UserDataResponse
:param msg: the setup driver request object, either DriverSetupRequest,
UserDataResponse or UserConfirmationResponse
:return: the setup action on how to continue
"""
if isinstance(msg, ucapi.DriverSetupRequest):
Expand All @@ -31,16 +32,19 @@ async def driver_setup_handler(msg: ucapi.SetupDriver) -> ucapi.SetupAction:
return ucapi.SetupError()


async def handle_driver_setup(msg: ucapi.DriverSetupRequest) -> ucapi.RequestUserInput | ucapi.SetupError:
async def handle_driver_setup(
msg: ucapi.DriverSetupRequest,
) -> ucapi.SetupAction:
"""
Start driver setup.
Initiated by Remote Two to set up the driver.
:param msg: not used, value(s) of input fields in the first setup screen. See setup_data_schema in driver metadata.
:param msg: value(s) of input fields in the first setup screen.
:return: the setup action on how to continue
"""
# for our demo we clear everything, a real driver might have to handle this differently
# For our demo we simply clear everything!
# A real driver might have to handle this differently
api.available_entities.clear()
api.configured_entities.clear()

Expand Down Expand Up @@ -141,7 +145,9 @@ async def handle_user_data_response(msg: ucapi.UserDataResponse) -> ucapi.SetupA
return ucapi.SetupError()


async def cmd_handler(entity: ucapi.Button, cmd_id: str, _params: dict[str, Any] | None) -> ucapi.StatusCodes:
async def cmd_handler(
entity: ucapi.Button, cmd_id: str, _params: dict[str, Any] | None
) -> ucapi.StatusCodes:
"""
Push button command handler.
Expand All @@ -159,7 +165,7 @@ async def cmd_handler(entity: ucapi.Button, cmd_id: str, _params: dict[str, Any]

@api.listens_to(ucapi.Events.CONNECT)
async def on_connect() -> None:
"""When the remote connects, we just set the device state. We are ready all the time!"""
# When the remote connects, we just set the device state. We are ready all the time!
await api.set_device_state(ucapi.DeviceStates.CONNECTED)


Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ profile = "black"
overgeneral-exceptions = ["builtins.BaseException", "builtins.Exception"]

[tool.pylint.format]
max-line-length = "120"
max-line-length = "88"

[tool.pylint.MASTER]
ignore-paths = [
Expand All @@ -73,16 +73,20 @@ ignore-paths = [

[tool.pylint."messages control"]
# Reasons disabled:
# duplicate-code - unavoidable
# global-statement - not yet considered
# too-many-* - are not enforced for the sake of readability
# too-few-* - same as too-many-*
# line-too-long - handled with black & flake8
# fixme - refactoring in progress

disable = [
"duplicate-code",
"global-statement",
"too-many-arguments",
"too-many-instance-attributes",
"too-few-public-methods",
"line-too-long",
"fixme"
]

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[flake8]
max-line-length = 120
max-line-length = 88
1 change: 0 additions & 1 deletion ucapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Integration driver library for Remote Two.
Expand Down
Loading

0 comments on commit 2f3660e

Please sign in to comment.