Skip to content

Commit

Permalink
Use Ruff for Python linting and formatting
Browse files Browse the repository at this point in the history
This replaces the black, pyupgrade, reorder-python-imports and flake8
hooks in pre-commit with ruff.
  • Loading branch information
replaceafill authored Jun 20, 2024
1 parent 7ca466e commit e97b72c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 68 deletions.
16 changes: 0 additions & 16 deletions .flake8

This file was deleted.

26 changes: 5 additions & 21 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.16.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.9
hooks:
- id: pyupgrade
args: [--py38-plus]
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.13.0
hooks:
- id: reorder-python-imports
args: [--py38-plus]
- repo: https://github.com/psf/black
rev: "23.12.1"
hooks:
- id: black
args: [--safe, --quiet]
- repo: https://github.com/pycqa/flake8
rev: "7.1.0"
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear==24.4.26
- flake8-comprehensions==3.14.0
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.41.0
hooks:
Expand Down
17 changes: 6 additions & 11 deletions amclient/amclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Module and CLI that holds functionality for interacting with the various
Archivematica APIs.
"""

import base64
import binascii
import io
Expand Down Expand Up @@ -218,7 +219,7 @@ def _close_completed_units(self, unit_type):
if _completed_units is None:
msg = (
"Something went wrong when attempting to retrieve the"
" completed {}s.".format(unit_type)
f" completed {unit_type}s."
)
LOGGER.warning(msg)
else:
Expand Down Expand Up @@ -500,9 +501,7 @@ def get_processing_config(self, assume_json=False):
return the default processing config from the AM server.
"""
return utils._call_url_json(
"{}/api/processing-configuration/{}".format(
self.am_url, self.processing_config
),
f"{self.am_url}/api/processing-configuration/{self.processing_config}",
headers=self._am_auth_headers(),
assume_json=assume_json,
)
Expand Down Expand Up @@ -682,9 +681,7 @@ def validate_csv(self, validator, file_obj):
url = f"{self.am_url}/api/v2beta/validate/{validator}/"
if not (isinstance(file_obj, io.TextIOBase) or hasattr(file_obj, "read")):
raise TypeError(
"Expected an io.TextIOWrapper file object but got {} instead".format(
type(file_obj)
)
f"Expected an io.TextIOWrapper file object but got {type(file_obj)} instead"
)
data = file_obj.read()
headers = self._am_auth_headers()
Expand Down Expand Up @@ -720,9 +717,7 @@ def extract_file(self):
then the stream contents are output to the console.
"""
self.output_mode = "" # TODO: don't overwrite mode
url = "{}/api/v2/file/{}/extract_file/?relative_path_to_file={}".format(
self.ss_url, self.package_uuid, self.relative_path
)
url = f"{self.ss_url}/api/v2/file/{self.package_uuid}/extract_file/?relative_path_to_file={self.relative_path}"
response = requests.get(url, params=self._ss_auth(), stream=True)
if getattr(self, "stream", None):
if getattr(self, "cli_call", None):
Expand Down Expand Up @@ -787,7 +782,7 @@ def list_location_purposes(self):

def create_location(self):
"""Create a new location in the Storage Service."""
if not self.location_purpose.upper() in self.list_location_purposes():
if self.location_purpose.upper() not in self.list_location_purposes():
return {
"error": "location purpose not permitted",
"valid_purposes": self.list_location_purposes(),
Expand Down
4 changes: 1 addition & 3 deletions amclient/amclientargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@
PROCESSING_CONFIG = Opt(
name="processing-config",
metavar="PROCESSING",
help="Processing configuration. Default: {}".format(
defaults.DEFAULT_PROCESSING_CONFIG
),
help=f"Processing configuration. Default: {defaults.DEFAULT_PROCESSING_CONFIG}",
default=defaults.DEFAULT_PROCESSING_CONFIG,
type=None,
)
Expand Down
3 changes: 1 addition & 2 deletions amclient/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import re
from tempfile import mkdtemp


DEF_AM_URL = "http://127.0.0.1:62080"
DEF_SS_URL = "http://127.0.0.1:62081"
DEF_USER_NAME = "test"
Expand All @@ -13,7 +12,7 @@
UNDECODABLE = "UNABLE TO DECODE"
UNDEC_MSG = (
"Unable to decode a transfer source component; giving up and"
" returning {}".format(UNDECODABLE)
f" returning {UNDECODABLE}"
)

# Global for logfile if not set.
Expand Down
2 changes: 1 addition & 1 deletion amclient/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Where you put stuff when you can't think of a good name for a module."""

import logging
from pathlib import Path

Expand All @@ -7,7 +8,6 @@

from . import errors


LOGGER = logging.getLogger("amclient")

METHOD_GET = "GET"
Expand Down
20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,32 @@ dev = [
"pip-tools",
"pytest-cov",
"pytest",
"ruff",
]

[tool.setuptools.dynamic]
version = {attr = "amclient.version.__version__"}
readme = {file = ["README.md"], content-type = "text/markdown"}

[tool.ruff.lint]
# Rule reference: https://docs.astral.sh/ruff/rules/
select = [
"B",
"C4",
"E",
"F",
"I",
"UP",
"W",
]
ignore = [
"B904",
"E501",
]

[tool.ruff.lint.isort]
force-single-line = true

[tool.pytest.ini_options]
python_files = [
"test_*.py",
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pytest-cov==5.0.0
# via amclient (pyproject.toml)
requests==2.32.3
# via amclient (pyproject.toml)
ruff==0.4.9
# via amclient (pyproject.toml)
tomli==2.0.1
# via
# build
Expand Down
19 changes: 5 additions & 14 deletions tests/test_amclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from amclient import amclient
from amclient import errors


AM_URL = "http://192.168.168.192"
SS_URL = "http://192.168.168.192:8000"
AM_USER_NAME = "test"
Expand Down Expand Up @@ -1822,11 +1821,9 @@ def test_reingest_aip(call_url: mock.Mock):
message = response["message"]
assert error is False
assert message == (
"Package {aip_uuid} sent "
f"Package {aip_uuid} sent "
"to pipeline Archivematica on 4e2f66a7a29f "
"({pipeline_uuid}) for re-ingest".format(
aip_uuid=aip_uuid, pipeline_uuid=pipeline_uuid
)
f"({pipeline_uuid}) for re-ingest"
)

assert call_url.mock_calls == [
Expand Down Expand Up @@ -2399,9 +2396,7 @@ def test_extract_individual_file(requests_get: mock.Mock, tmp_path):
tmp_dir.mkdir()
filename_to_test = "bird.mp3"
package_uuid = "2ad1bf0d-23fa-44e0-a128-9feadfe22c42"
path = "amclient-transfer_1-{}/data/objects/{}".format(
package_uuid, filename_to_test
)
path = f"amclient-transfer_1-{package_uuid}/data/objects/{filename_to_test}"
filename = "bird_download.mp3"
response = amclient.AMClient(
ss_api_key=SS_API_KEY,
Expand Down Expand Up @@ -2454,9 +2449,7 @@ def test_extract_and_stream_individual_file(requests_get: mock.Mock, tmp_path):
tmp_dir.mkdir()
filename_to_test = "bird.mp3"
package_uuid = "2ad1bf0d-23fa-44e0-a128-9feadfe22c42"
path = "amclient-transfer_1-{}/data/objects/{}".format(
package_uuid, filename_to_test
)
path = f"amclient-transfer_1-{package_uuid}/data/objects/{filename_to_test}"
response = amclient.AMClient(
ss_api_key=SS_API_KEY,
ss_user_name=SS_USER_NAME,
Expand Down Expand Up @@ -2509,9 +2502,7 @@ def test_extract_and_stream_individual_file_cli(
tmp_dir.mkdir()
filename_to_test = "bird.mp3"
package_uuid = "2ad1bf0d-23fa-44e0-a128-9feadfe22c42"
path = "amclient-transfer_1-{}/data/objects/{}".format(
package_uuid, filename_to_test
)
path = f"amclient-transfer_1-{package_uuid}/data/objects/{filename_to_test}"
amclient.AMClient(
ss_api_key=SS_API_KEY,
ss_user_name=SS_USER_NAME,
Expand Down

0 comments on commit e97b72c

Please sign in to comment.