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 764e09a commit 86c6d71
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 86 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
4 changes: 1 addition & 3 deletions fixity/fixity.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ def scan(
else:
report_dict = {
"success": "None",
"message": "Exception encountered while scanning AIP {}: {} ({})".format(
aip, type(e).__name__, str(e)
),
"message": f"Exception encountered while scanning AIP {aip}: {type(e).__name__} ({str(e)})",
"traceback": traceback.format_exc(),
"errors": None,
}
Expand Down
2 changes: 1 addition & 1 deletion fixity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from sqlalchemy import Boolean
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import DateTime
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import create_engine
from sqlalchemy.orm import backref
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import relationship
Expand Down
8 changes: 2 additions & 6 deletions fixity/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,11 @@ def post_success_report(aip, report, report_url, report_auth=(), session_id=None

if response.status_code == 500:
raise ReportServiceException(
"Report service encountered an internal error when attempting to POST report for AIP {}".format(
aip
)
f"Report service encountered an internal error when attempting to POST report for AIP {aip}"
)
elif response.status_code != 201:
raise ReportServiceException(
"Report service returned {} when attempting to POST report for AIP {}".format(
response.status_code, aip
)
f"Report service returned {response.status_code} when attempting to POST report for AIP {aip}"
)

return report.posted
49 changes: 12 additions & 37 deletions fixity/storage_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from .models import AIP
from .models import Report


UNABLE_TO_CONNECT_ERROR = (
"Unable to connect to storage service instance at {} (is it running?)"
)
Expand Down Expand Up @@ -43,27 +42,19 @@ def _get_aips(ss_url, ss_user, ss_key, uri=None):

if response.status_code == 500:
raise StorageServiceError(
'Storage service at "{}" encountered an internal error while requesting AIPs'.format(
ss_url
)
f'Storage service at "{ss_url}" encountered an internal error while requesting AIPs'
)
elif response.status_code == 504:
raise StorageServiceError(
'Storage service at "{}" encountered a gateway timeout while requesting AIPs'.format(
ss_url
)
f'Storage service at "{ss_url}" encountered a gateway timeout while requesting AIPs'
)
elif response.status_code == 401:
raise StorageServiceError(
'Storage service at "{}" failed authentication while requesting AIPs'.format(
ss_url
)
f'Storage service at "{ss_url}" failed authentication while requesting AIPs'
)
elif response.status_code != 200:
raise StorageServiceError(
'Storage service at "{}" returned {} while requesting AIPs'.format(
ss_url, response.status_code
)
f'Storage service at "{ss_url}" returned {response.status_code} while requesting AIPs'
)

results = response.json()
Expand Down Expand Up @@ -113,27 +104,19 @@ def get_single_aip(uuid, ss_url, ss_user, ss_key):

if response.status_code == 500:
raise StorageServiceError(
'Storage service at "{}" encountered an internal error while requesting AIP with UUID {}'.format(
ss_url, uuid
)
f'Storage service at "{ss_url}" encountered an internal error while requesting AIP with UUID {uuid}'
)
elif response.status_code == 504:
raise StorageServiceError(
'Storage service at "{}" encounterd a gateway timeout while requesting AIP with UUID {}'.format(
ss_url, uuid
)
f'Storage service at "{ss_url}" encounterd a gateway timeout while requesting AIP with UUID {uuid}'
)
elif response.status_code == 401:
raise StorageServiceError(
'Storage service at "{}" failed authentication while requesting AIP with UUID {}'.format(
ss_url, uuid
)
f'Storage service at "{ss_url}" failed authentication while requesting AIP with UUID {uuid}'
)
if response.status_code != 200:
raise StorageServiceError(
'Storage service at "{}" returned {} while requesting AIP with UUID {}'.format(
ss_url, response.status_code, uuid
)
f'Storage service at "{ss_url}" returned {response.status_code} while requesting AIP with UUID {uuid}'
)
return response.json()

Expand Down Expand Up @@ -219,9 +202,7 @@ def scan_aip(
}
report = create_report(aip, None, begun, ended, json.dumps(json_report))
raise StorageServiceError(
'A fixity scan could not be started for the AIP with uuid "{}"'.format(
aip.uuid
),
f'A fixity scan could not be started for the AIP with uuid "{aip.uuid}"',
report=report,
)
if response.status_code == 500:
Expand All @@ -233,9 +214,7 @@ def scan_aip(
}
report = create_report(aip, None, begun, ended, json.dumps(json_report))
raise StorageServiceError(
'Storage service at "{}" encountered an internal error while scanning AIP {}'.format(
ss_url, aip.uuid
),
f'Storage service at "{ss_url}" encountered an internal error while scanning AIP {aip.uuid}',
report=report,
)
if response.status_code == 401:
Expand All @@ -247,9 +226,7 @@ def scan_aip(
}
report = create_report(aip, None, begun, ended, json.dumps(json_report))
raise StorageServiceError(
'Storage service at "{}" failed authentication while scanning AIP {}'.format(
ss_url, aip.uuid
),
f'Storage service at "{ss_url}" failed authentication while scanning AIP {aip.uuid}',
report=report,
)
if response.status_code != 200:
Expand All @@ -261,9 +238,7 @@ def scan_aip(
}
report = create_report(aip, None, begun, ended, json.dumps(json_report))
raise StorageServiceError(
'Storage service at "{}" returned {} while scanning AIP {}'.format(
ss_url, response.status_code, aip.uuid
),
f'Storage service at "{ss_url}" returned {response.status_code} while scanning AIP {aip.uuid}',
report=report,
)

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 = "fixity.__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 @@ -46,6 +46,8 @@ pytest-cov==5.0.0
# via fixity (pyproject.toml)
requests==2.32.3
# via fixity (pyproject.toml)
ruff==0.4.9
# via fixity (pyproject.toml)
sqlalchemy==2.0.31
# via fixity (pyproject.toml)
tomli==2.0.1
Expand Down
1 change: 0 additions & 1 deletion tests/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from fixity.models import Report
from fixity.utils import InvalidUUID


REPORT_URL = "http://localhost:8003/"


Expand Down
1 change: 0 additions & 1 deletion tests/test_storage_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from fixity.models import Session
from fixity.utils import InvalidUUID


SESSION = Session()
STORAGE_SERVICE_URL = "http://localhost:8000/"
STORAGE_SERVICE_USER = "test"
Expand Down

0 comments on commit 86c6d71

Please sign in to comment.