Skip to content

Commit

Permalink
Merge pull request #75 from hnez/ruff
Browse files Browse the repository at this point in the history
QA: Use ruff instead of black + flake8 for formatting and linting
  • Loading branch information
SmithChart authored May 8, 2024
2 parents 57e91b2 + ba27294 commit b0c2903
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 70 deletions.
19 changes: 6 additions & 13 deletions .github/workflows/check-and-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,26 @@ name: Check and Build
on: [push, pull_request]

jobs:
black:
name: Python black
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make qa-black

codespell:
name: Codespell
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make qa-codespell

flake8:
name: Python flake8
pytest:
name: Python Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make qa-flake8
- run: make qa-pytest

pytest:
name: Python pytest
ruff:
name: Python Format and Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make qa-pytest
- run: make qa-ruff

build:
name: Python Build
Expand Down
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ clean:
envs: packaging-env qa-env

# testing #####################################################################
.PHONY: qa qa-env qa-black qa-codespell qa-flake8 qa-pytest
.PHONY: qa qa-env qa-codespell qa-pytest qa-ruff

$(PYTHON_QA_ENV)/.created: REQUIREMENTS.qa.txt
rm -rf $(PYTHON_QA_ENV) && \
Expand All @@ -47,11 +47,7 @@ $(PYTHON_QA_ENV)/.created: REQUIREMENTS.qa.txt

qa-env: $(PYTHON_QA_ENV)/.created

qa: qa-black qa-codespell qa-flake8 qa-pytest

qa-black: qa-env
. $(PYTHON_QA_ENV)/bin/activate && \
$(PYTHON) -m black --check --diff .
qa: qa-codespell qa-pytest qa-ruff

qa-codespell: qa-env
. $(PYTHON_QA_ENV)/bin/activate && \
Expand All @@ -61,10 +57,14 @@ qa-codespell-fix: qa-env
. $(PYTHON_QA_ENV)/bin/activate && \
codespell -w

qa-flake8: qa-env
. $(PYTHON_QA_ENV)/bin/activate && \
$(PYTHON) -m flake8

qa-pytest: qa-env
. $(PYTHON_QA_ENV)/bin/activate && \
$(PYTHON) -m pytest -vv

qa-ruff: qa-env
. $(PYTHON_QA_ENV)/bin/activate && \
ruff format --check --diff && ruff check

qa-ruff-fix: qa-env
. $(PYTHON_QA_ENV)/bin/activate && \
ruff format && ruff check --fix
5 changes: 1 addition & 4 deletions REQUIREMENTS.qa.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
black
codespell
flake8
flake8-pyproject
flake8-bugbear
pytest
pytest-mock
ruff
10 changes: 6 additions & 4 deletions fastentrypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
(c) 2016, Aaron Christianson
https://github.com/ninjaaron/fast-entry_points
"""
from setuptools.command import easy_install

import re

from setuptools.command import easy_install

TEMPLATE = """\
# -*- coding: utf-8 -*-
import re
Expand All @@ -58,7 +60,7 @@ def get_args(cls, dist, header=None):
"""
if header is None:
header = cls.get_header()
spec = str(dist.as_requirement())
spec = str(dist.as_requirement()) # noqa: F841
for type_ in "console", "gui":
group = type_ + "_scripts"
for name, ep in dist.get_entry_map(group).items():
Expand Down Expand Up @@ -92,14 +94,14 @@ def main():
with open(manifest_path, "a+") as manifest:
manifest.seek(0)
manifest_content = manifest.read()
if not "include fastentrypoints.py" in manifest_content:
if "include fastentrypoints.py" not in manifest_content:
manifest.write(("\n" if manifest_content else "") + "include fastentrypoints.py")

# Insert the import statement to setup.py if not present
with open(setup_path, "a+") as setup:
setup.seek(0)
setup_content = setup.read()
if not "import fastentrypoints" in setup_content:
if "import fastentrypoints" not in setup_content:
setup.seek(0)
setup.truncate()
setup.write("import fastentrypoints\n" + setup_content)
Expand Down
19 changes: 14 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
[tool.black]
[tool.ruff]
line-length = 119
exclude = [
"__pycache__",
"usbsdmux.egg-info",
".pybuild",
"build",
"debian",
"env",
"venv",
"envs",
"dist",
]

[tool.flake8]
max-line-length = 119
count = true
exclude = "build,debian,dist,envs,fastentrypoints.py,__pycache__,contrib,setup.py,venv,usbsdmux.egg-info,.tox"
[tool.ruff.lint]
select = ["B", "E", "F", "I", "SIM"]
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python3

from setuptools import setup
import fastentrypoints

import fastentrypoints # noqa: F401

setup(
name="usbsdmux",
Expand Down
18 changes: 10 additions & 8 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ def test_help_in_readme(capsys, mocker):

readme_path = os.path.join(os.path.dirname(__file__), "../", "README.rst")
readme_lines = None
for line in open(readme_path).readlines():
line = line.rstrip()
if line == " $ usbsdmux -h":
readme_lines = []
elif readme_lines is not None:
if line and not line.startswith(" "):
break
readme_lines.append(line)

with open(readme_path) as readme:
for line in readme.readlines():
line = line.rstrip()
if line == " $ usbsdmux -h":
readme_lines = []
elif readme_lines is not None:
if line and not line.startswith(" "):
break
readme_lines.append(line)

assert readme_lines is not None, "Bash command not found. Did you include ' $ usbsdmux -h'?"
assert readme_lines, "No output lines found. Did you indent the output correctly?"
Expand Down
16 changes: 11 additions & 5 deletions tests/test_sd_regs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os.path
import json
import os.path

import pytest

from usbsdmux.sd_regs import SCR, CID, decode_csd
from usbsdmux.sd_regs import CID, SCR, decode_csd

REFS = [
"02544d53413034471027b7748500bc00",
Expand All @@ -16,7 +16,9 @@
@pytest.mark.parametrize("cid", REFS)
def test_decode(cid):
ref_name = os.path.join(os.path.dirname(__file__), "reference", f"{cid}.json")
ref = json.load(open(ref_name))

with open(ref_name) as ref_file:
ref = json.load(ref_file)

res = {}
res["scr"] = SCR(ref["scr"]["raw"]).decode()
Expand All @@ -33,8 +35,12 @@ def test_decode(cid):
def test_to_text(cid):
ref_name_json = os.path.join(os.path.dirname(__file__), "reference", f"{cid}.json")
ref_name_text = os.path.join(os.path.dirname(__file__), "reference", f"{cid}.text")
ref_json = json.load(open(ref_name_json))
ref_text = open(ref_name_text).read().split("\n")[:-1]

with open(ref_name_json) as ref_file_json:
ref_json = json.load(ref_file_json)

with open(ref_name_text) as ref_file_text:
ref_text = ref_file_text.read().split("\n")[:-1]

res = []

Expand Down
6 changes: 3 additions & 3 deletions usbsdmux/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

import argparse
import errno
import sys
import json
import sys

from .usbsdmux import autoselect_driver, UnknownUsbSdMuxRevisionException, NotInHostModeException
from .sd_regs import decoded_to_text
from .mqtthelper import Config, publish_info
from .sd_regs import decoded_to_text
from .usbsdmux import NotInHostModeException, UnknownUsbSdMuxRevisionException, autoselect_driver


def main():
Expand Down
6 changes: 2 additions & 4 deletions usbsdmux/ctypehelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ def string_to_uint8_array(str, array_length, c_string=False, padding=0xFF, encod

# Determine number of bytes to copy
nbytes = str.encode(encoding)
if c_string:
count = min(len(nbytes), array_length - 1)
else:
count = min(len(nbytes), array_length)
length = array_length - 1 if c_string else array_length
count = min(len(nbytes), length)

# Do the actual copy
for i in range(count):
Expand Down
3 changes: 1 addition & 2 deletions usbsdmux/i2c_gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

from .usb2642 import Usb2642
from abc import ABC


class I2cGpio(ABC):
class I2cGpio(object):
# Registers inside the supported GPIO expanders
_register_inputPort = 0x00
_register_outputPort = 0x01
Expand Down
5 changes: 3 additions & 2 deletions usbsdmux/mqtthelper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys
import configparser
import json
import os
import sys


class Config:
Expand Down Expand Up @@ -63,6 +63,7 @@ def _read_int(filename, base=10):

def _gather_data(ctl, sg, mode):
import socket

import pkg_resources

base_sg = os.path.realpath(sg)
Expand Down
13 changes: 6 additions & 7 deletions usbsdmux/sd_regs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def decoded_to_text(decoded):

class RegisterDecoder:
"decode a register based on a mostly declarative description of the contents."

FIELDS = {}

def __init__(self, raw_hex):
Expand Down Expand Up @@ -136,10 +137,7 @@ def decode_TAAC(self):
scale = TIME_SCALE_ENUM[bitslice(v[0], 2, 0)]
value = self.TIME_VALUE_ENUM[bitslice(v[0], 6, 3)]

if isinstance(value, float):
scaled_value = value * scale
else:
scaled_value = None
scaled_value = value * scale if isinstance(value, float) else None

return {
"decoded": (value, unit),
Expand All @@ -155,10 +153,10 @@ def decode_TRAN_SPEED(self):
scale = RATE_SCALE_ENUM[bitslice(v[0], 2, 0)]
value = self.TIME_VALUE_ENUM[bitslice(v[0], 6, 3)]

scaled_value = None

if isinstance(value, float) and isinstance(scale, int):
scaled_value = value * scale
else:
scaled_value = None

return {
"decoded": (value, unit),
Expand Down Expand Up @@ -516,7 +514,8 @@ class SCR(RegisterDecoder):

args = parser.parse_args()

data = json.loads(open(args.input).read())
with open(args.input) as fd:
data = json.loads(fd.read())

if args.json:
res = {}
Expand Down
2 changes: 1 addition & 1 deletion usbsdmux/usb2642eeprom.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import time

from .ctypehelper import (
list_to_uint8_array,
string_to_microchip_unicode_uint8_array,
string_to_uint8_array,
list_to_uint8_array,
)
from .usb2642 import Usb2642

Expand Down
2 changes: 1 addition & 1 deletion usbsdmux/usbsdmux.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import os
import time

from .i2c_gpio import Pca9536, Tca6408
from . import sd_regs
from .i2c_gpio import Pca9536, Tca6408


class UnknownUsbSdMuxRevisionException(Exception):
Expand Down

0 comments on commit b0c2903

Please sign in to comment.