Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use ruff #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense

.py text eol=lf
.rst text eol=lf
.txt text eol=lf
.yaml text eol=lf
.toml text eol=lf
.license text eol=lf
.md text eol=lf
42 changes: 11 additions & 31 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,42 +1,22 @@
# SPDX-FileCopyrightText: 2020 Diego Elio Pettenò
# SPDX-FileCopyrightText: 2024 Justin Myers
#
# SPDX-License-Identifier: Unlicense

repos:
- repo: https://github.com/python/black
rev: 23.3.0
hooks:
- id: black
- repo: https://github.com/fsfe/reuse-tool
rev: v1.1.2
hooks:
- id: reuse
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pycqa/pylint
rev: v3.3.1
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
hooks:
- id: pylint
name: pylint (library code)
types: [python]
args:
- --disable=consider-using-f-string
exclude: "^(docs/|examples/|tests/|setup.py$)"
- id: pylint
name: pylint (example code)
description: Run pylint rules on "examples/*.py" files
types: [python]
files: "^examples/"
args:
- --disable=missing-docstring,invalid-name,consider-using-f-string,duplicate-code
- id: pylint
name: pylint (test code)
description: Run pylint rules on "tests/*.py" files
types: [python]
files: "^tests/"
args:
- --disable=missing-docstring,consider-using-f-string,duplicate-code
- id: ruff-format
- id: ruff
args: ["--fix"]
- repo: https://github.com/fsfe/reuse-tool
rev: v3.0.1
hooks:
- id: reuse
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Introduction
:target: https://github.com/adafruit/Adafruit_CircuitPython_Display_Button/actions
:alt: Build Status

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black
:alt: Code Style: Black
.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
:target: https://github.com/astral-sh/ruff
:alt: Code Style: Ruff

UI Buttons for displayio

Expand Down
1 change: 1 addition & 0 deletions adafruit_button/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
https://github.com/adafruit/circuitpython/releases

"""

from adafruit_button.button import Button
17 changes: 8 additions & 9 deletions adafruit_button/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@

"""

from micropython import const
from adafruit_display_shapes.rect import Rect
from adafruit_display_shapes.roundrect import RoundRect
from micropython import const

from adafruit_button.button_base import ButtonBase, _check_color

try:
from typing import Optional, Union, Tuple
from fontio import FontProtocol
from typing import Optional, Tuple, Union

from displayio import Group
from fontio import FontProtocol
except ImportError:
pass

Expand All @@ -39,7 +41,6 @@


class Button(ButtonBase):
# pylint: disable=too-many-instance-attributes, too-many-locals
"""Helper class for creating UI buttons for ``displayio``. Provides the following
buttons:
RECT: A rectangular button. SHAWDOWRECT adds a drop shadow.
Expand Down Expand Up @@ -100,9 +101,7 @@ def _create_body(self) -> None:
outline=self._outline_color,
)
elif self.style == Button.SHADOWRECT:
self.shadow = Rect(
2, 2, self.width - 2, self.height - 2, fill=self.outline_color
)
self.shadow = Rect(2, 2, self.width - 2, self.height - 2, fill=self.outline_color)
self.body = Rect(
0,
0,
Expand Down Expand Up @@ -137,7 +136,7 @@ def _create_body(self) -> None:
SHADOWRECT = const(2)
SHADOWROUNDRECT = const(3)

def __init__(
def __init__( # noqa: PLR0913 Too many arguments
self,
*,
x: int,
Expand All @@ -154,7 +153,7 @@ def __init__(
selected_fill: Optional[Union[int, Tuple[int, int, int]]] = None,
selected_outline: Optional[Union[int, Tuple[int, int, int]]] = None,
selected_label: Optional[Union[int, Tuple[int, int, int]]] = None,
label_scale: Optional[int] = 1
label_scale: Optional[int] = 1,
):
super().__init__(
x=x,
Expand Down
19 changes: 8 additions & 11 deletions adafruit_button/button_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
https://github.com/adafruit/circuitpython/releases

"""

import terminalio
from adafruit_display_text.bitmap_label import Label
from displayio import Group
import terminalio

try:
from typing import Optional, Union, Tuple
from typing import Optional, Tuple, Union

from fontio import FontProtocol
except ImportError:
pass
Expand All @@ -41,7 +43,6 @@ def _check_color(color: Optional[Union[int, tuple[int, int, int]]]) -> int:


class ButtonBase(Group):
# pylint: disable=too-many-instance-attributes
"""Superclass for creating UI buttons for ``displayio``.

:param int x: The x position of the button.
Expand All @@ -60,7 +61,7 @@ class ButtonBase(Group):
:param Optional[int] label_scale: The scale factor used for the label. Defaults to 1.
"""

def __init__(
def __init__( # noqa: PLR0913 Too many arguments
self,
*,
x: int,
Expand Down Expand Up @@ -109,20 +110,16 @@ def label(self, newtext: str) -> None:
dims[2] *= self._label.scale
dims[3] *= self._label.scale
if dims[2] >= self.width or dims[3] >= self.height:
while len(self._label.text) > 1 and (
dims[2] >= self.width or dims[3] >= self.height
):
self._label.text = "{}.".format(self._label.text[:-2])
while len(self._label.text) > 1 and (dims[2] >= self.width or dims[3] >= self.height):
self._label.text = f"{self._label.text[:-2]}."
dims = list(self._label.bounding_box)
dims[2] *= self._label.scale
dims[3] *= self._label.scale
if len(self._label.text) <= 1:
raise RuntimeError("Button not large enough for label")
self._label.x = (self.width - dims[2]) // 2
self._label.y = self.height // 2
self._label.color = (
self._label_color if not self.selected else self._selected_label
)
self._label.color = self._label_color if not self.selected else self._selected_label
self.append(self._label)

if (self.selected_label is None) and (self._label_color is not None):
Expand Down
11 changes: 7 additions & 4 deletions adafruit_button/sprite_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
https://github.com/adafruit/circuitpython/releases

"""
from adafruit_imageload.tilegrid_inflator import inflate_tilegrid

from adafruit_imageload import load
from adafruit_imageload.tilegrid_inflator import inflate_tilegrid

from adafruit_button.button_base import ButtonBase

try:
from typing import Optional, Union, Tuple
from typing import Optional, Tuple, Union

from fontio import FontProtocol
except ImportError:
pass
Expand Down Expand Up @@ -56,7 +59,7 @@ class SpriteButton(ButtonBase):
:param Optional[int] label_scale: The scale multiplier of the button label. Defaults to 1.
"""

def __init__(
def __init__( # noqa: PLR0913 Too many arguments
self,
*,
x: int,
Expand All @@ -71,7 +74,7 @@ def __init__(
bmp_path: str = None,
selected_bmp_path: Optional[str] = None,
transparent_index: Optional[Union[int, Tuple]] = None,
label_scale: Optional[int] = 1
label_scale: Optional[int] = 1,
):
if bmp_path is None:
raise ValueError("Please supply bmp_path. It cannot be None.")
Expand Down
8 changes: 2 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# -*- coding: utf-8 -*-

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import datetime
import os
import sys
import datetime

sys.path.insert(0, os.path.abspath(".."))

Expand Down Expand Up @@ -48,9 +46,7 @@
creation_year = "2019"
current_year = str(datetime.datetime.now().year)
year_duration = (
current_year
if current_year == creation_year
else creation_year + " - " + current_year
current_year if current_year == creation_year else creation_year + " - " + current_year
)
copyright = year_duration + " Limor Fried"
author = "Limor Fried"
Expand Down
3 changes: 2 additions & 1 deletion examples/display_button_color_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
properties after the button has been initialized.
"""

import adafruit_touchscreen
import board
import displayio
import terminalio
import adafruit_touchscreen

from adafruit_button import Button

# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
Expand Down
4 changes: 3 additions & 1 deletion examples/display_button_customfont.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
"""

import os

import adafruit_touchscreen
import board
import displayio
from adafruit_bitmap_font import bitmap_font
import adafruit_touchscreen

from adafruit_button import Button

# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
Expand Down
3 changes: 2 additions & 1 deletion examples/display_button_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
Simple button example.
"""

import adafruit_touchscreen
import board
import displayio
import terminalio
import adafruit_touchscreen

from adafruit_button import Button

# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
Expand Down
2 changes: 2 additions & 0 deletions examples/display_button_soundboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"""

import time

from adafruit_pyportal import PyPortal

from adafruit_button import Button

SHOW_BUTTONS = False
Expand Down
4 changes: 3 additions & 1 deletion examples/display_button_spritebutton_simpletest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
# SPDX-License-Identifier: MIT
import time

import adafruit_touchscreen
import board
import displayio
import adafruit_touchscreen
import terminalio

from adafruit_button.sprite_button import SpriteButton

# These pins are used as both analog and digital! XL, XR and YU must be analog
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# SPDX-FileCopyrightText: 2024 DJDevon3
# SPDX-License-Identifier: MIT
import time
import displayio
import terminalio

import adafruit_stmpe610 # TFT Featherwing V1 touch driver
import board
import digitalio
import displayio
import terminalio
from adafruit_hx8357 import HX8357 # TFT Featherwing display driver
import adafruit_stmpe610 # TFT Featherwing V1 touch driver

from adafruit_button.sprite_button import SpriteButton

# 3.5" TFT Featherwing is 480x320
Expand Down
Loading
Loading