Skip to content

Commit

Permalink
chore: Update ruff lint configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Sep 16, 2024
1 parent 7bc03ba commit 9d01843
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 111 deletions.
11 changes: 3 additions & 8 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@
def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)


Expand Down
16 changes: 4 additions & 12 deletions process/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,15 @@

class CollectionCommand(BaseCommand):
def add_arguments(self, parser):
"""
Add default arguments to the command.
"""
"""Add default arguments to the command."""
parser.add_argument("collection_id", help=_("the ID of the collection"))
self.add_collection_arguments(parser)

def add_collection_arguments(self, parser):
"""
Add arguments specific to this command.
"""
"""Add arguments specific to this command."""

def handle(self, *args, **options):
"""
Get the collection.
"""
"""Get the collection."""
self.stderr.style_func = None

logging.getLogger("process").setLevel(logging.DEBUG)
Expand All @@ -37,7 +31,5 @@ def handle(self, *args, **options):
self.handle_collection(collection, *args, **options)

def handle_collection(self, collection, *args, **options):
"""
Run the command.
"""
"""Run the command."""
raise NotImplementedError("collection commands must implement handle_collection()")
56 changes: 14 additions & 42 deletions process/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,7 @@ def clean_fields(self, exclude=None):
)

def get_upgraded_collection(self) -> Self | None:
"""
Return the upgraded collection or None.
"""
"""Return the upgraded collection or None."""
# This is a shortcut to avoid a query. It is based on the logic in clean_fields().
if self.transform_type:
return None
Expand All @@ -165,27 +163,21 @@ def get_upgraded_collection(self) -> Self | None:
return None

def get_compiled_collection(self) -> Self | None:
"""
Return the compiled collection or None.
"""
"""Return the compiled collection or None."""
try:
return Collection.objects.get(transform_type=Collection.Transform.COMPILE_RELEASES, parent=self)
except Collection.DoesNotExist:
return None

def get_root_parent(self) -> Self:
"""
Return the "root" ancestor of the collection.
"""
"""Return the "root" ancestor of the collection."""
if self.parent is None:
return self
return self.parent.get_root_parent()


class CollectionNote(models.Model):
"""
A note an analyst made about the collection.
"""
"""A note an analyst made about the collection."""

class Level(models.TextChoices):
INFO = "INFO"
Expand All @@ -210,9 +202,7 @@ def __str__(self):


class CollectionFile(models.Model):
"""
A file within the collection.
"""
"""A file within the collection."""

collection = models.ForeignKey(Collection, on_delete=models.CASCADE, db_index=False)

Expand All @@ -234,9 +224,7 @@ def __str__(self):


class ProcessingStep(models.Model):
"""
A step in the lifecycle of collection file.
"""
"""A step in the lifecycle of collection file."""

class Name(models.TextChoices):
LOAD = "LOAD"
Expand Down Expand Up @@ -265,9 +253,7 @@ def __str__(self):


class CollectionFileItem(models.Model):
"""
An item within a file in the collection.
"""
"""An item within a file in the collection."""

collection_file = models.ForeignKey(CollectionFile, on_delete=models.CASCADE, db_index=False)

Expand All @@ -292,9 +278,7 @@ def __str__(self):


class Data(models.Model):
"""
The contents of a release, record or compiled release.
"""
"""The contents of a release, record or compiled release."""

hash_md5 = models.TextField()
data = models.JSONField(encoder=JSONEncoder)
Expand All @@ -311,9 +295,7 @@ def __str__(self):


class PackageData(models.Model):
"""
The contents of a package, excluding the releases or records.
"""
"""The contents of a package, excluding the releases or records."""

hash_md5 = models.TextField()
data = models.JSONField(encoder=JSONEncoder)
Expand All @@ -330,9 +312,7 @@ def __str__(self):


class Release(models.Model):
"""
A release.
"""
"""A release."""

collection = models.ForeignKey(Collection, on_delete=models.CASCADE, db_index=False)
collection_file_item = models.ForeignKey(CollectionFileItem, on_delete=models.CASCADE, db_index=False)
Expand Down Expand Up @@ -364,9 +344,7 @@ def __str__(self):


class Record(models.Model):
"""
A record.
"""
"""A record."""

collection = models.ForeignKey(Collection, on_delete=models.CASCADE, db_index=False)
collection_file_item = models.ForeignKey(CollectionFileItem, on_delete=models.CASCADE, db_index=False)
Expand Down Expand Up @@ -395,9 +373,7 @@ def __str__(self):


class CompiledRelease(models.Model):
"""
A compiled release.
"""
"""A compiled release."""

collection = models.ForeignKey(Collection, on_delete=models.CASCADE, db_index=False)
collection_file_item = models.ForeignKey(CollectionFileItem, on_delete=models.CASCADE, db_index=False)
Expand All @@ -422,9 +398,7 @@ def __str__(self):


class ReleaseCheck(models.Model):
"""
The result of checking a release.
"""
"""The result of checking a release."""

release = models.OneToOneField(Release, on_delete=models.CASCADE)
cove_output = models.JSONField()
Expand All @@ -437,9 +411,7 @@ def __str__(self):


class RecordCheck(models.Model):
"""
The result of checking a record.
"""
"""The result of checking a record."""

record = models.OneToOneField(Record, on_delete=models.CASCADE)
cove_output = models.JSONField()
Expand Down
4 changes: 1 addition & 3 deletions process/processors/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@


def file_or_directory(path):
"""
Check whether the path exists. Raise an exception if not.
"""
"""Check whether the path exists. Raise an exception if not."""
if not os.path.exists(path):
raise argparse.ArgumentTypeError(t("No such file or directory %(path)r") % {"path": path})
return path
Expand Down
8 changes: 2 additions & 6 deletions process/scrapyd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@


def configured() -> bool:
"""
Return whether the connection to Scrapyd is configured.
"""
"""Return whether the connection to Scrapyd is configured."""
return bool(settings.SCRAPYD["url"])


def spiders() -> list[str]:
"""
Return the names of the spiders in the Scrapyd project.
"""
"""Return the names of the spiders in the Scrapyd project."""
# https://scrapyd.readthedocs.io/en/stable/api.html#listspiders-json
url = urljoin(settings.SCRAPYD["url"], "/listspiders.json")
response = requests.get(url, params={"project": settings.SCRAPYD["project"]}, timeout=10)
Expand Down
8 changes: 2 additions & 6 deletions process/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@


def wrap(string):
"""
Format a long string as a help message, and return it.
"""
"""Format a long string as a help message, and return it."""
return "\n".join(fill(paragraph, width=78, replace_whitespace=False) for paragraph in string.split("\n"))


Expand Down Expand Up @@ -123,9 +121,7 @@ def create_step(name, collection_id, **kwargs):

@contextmanager
def delete_step(*args, **kwargs):
"""
Delete the named step and run any finish callback only if successful or if the error is expected.
"""
"""Delete the named step and run any finish callback only if successful or if the error is expected."""
try:
yield
# Delete the step so that the collection is completable, only if the error was expected.
Expand Down
20 changes: 5 additions & 15 deletions process/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ class CollectionViewSet(viewsets.ViewSet):
},
)
def create(self, request):
"""
Create an original collection and any derived collections.
"""
"""Create an original collection and any derived collections."""
serializer = CreateCollectionSerializer(data=request.data)
serializer.is_valid(raise_exception=True)

Expand Down Expand Up @@ -112,9 +110,7 @@ def create(self, request):
@extend_schema(request=CloseCollectionSerializer, responses={202: None})
@action(detail=True, methods=["post"])
def close(self, request, pk=None):
"""
Publish a message to RabbitMQ to close a root collection and its derived collections, if any.
"""
"""Publish a message to RabbitMQ to close a root collection and its derived collections, if any."""
serializer = CloseCollectionSerializer(data=request.data)
serializer.is_valid(raise_exception=True)

Expand Down Expand Up @@ -151,9 +147,7 @@ def close(self, request, pk=None):

@extend_schema(responses={202: None})
def destroy(self, request, pk=None):
"""
Publish a message to RabbitMQ to wipe the dataset.
"""
"""Publish a message to RabbitMQ to wipe the dataset."""
with get_publisher() as client:
client.publish({"collection_id": pk}, routing_key="wiper")

Expand All @@ -175,9 +169,7 @@ def destroy(self, request, pk=None):
)
@action(detail=True)
def metadata(self, request, pk=None):
"""
Return the compiled collection's metadata.
"""
"""Return the compiled collection's metadata."""
compiled_collection = get_object_or_404(Collection, pk=pk)
collection = compiled_collection.get_root_parent()

Expand Down Expand Up @@ -243,9 +235,7 @@ def metadata(self, request, pk=None):
@extend_schema(responses=TreeSerializer(many=True))
@action(detail=True)
def tree(self, request, pk=None):
"""
Return the original collection and its derived collections, if any.
"""
"""Return the original collection and its derived collections, if any."""
result = Collection.objects.raw(
"""\
WITH RECURSIVE tree (
Expand Down
37 changes: 18 additions & 19 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,32 @@ target-version = "py311"
[tool.ruff.lint]
select = ["ALL"]
ignore = [
"ANN", "COM", "EM",
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
"W191", "D206", "Q000", "Q001", "Q002", "Q003", "ISC001",
"D203", "D212", # ignore incompatible rules
"D200", # documentation preferences
"C901", "PLR091", # complexity preferences
"D1", # docstrings
"PTH", # pathlib
"ARG001", "ARG002", "RUF012", # Django
"PLW2901", # release_or_record overridden
"TRY003", # Django command errors
"ANN", "C901", "COM812", "D203", "D212", "D415", "EM", "PERF203", "PLR091", "Q000",
"D1",
"PTH",
]

[tool.ruff.lint.flake8-builtins]
builtins-ignorelist = ["copyright"]

[tool.ruff.lint.flake8-unused-arguments]
ignore-variadic-names = true

[tool.ruff.lint.per-file-ignores]
"docs/conf.py" = ["INP001"] # no __init__.py file
"docs/*" = ["D100", "INP001"]
"{*/signals,*/views,*/migrations/*}.py" = ["ARG001"]
"{*/admin,*/routers,*/views,*/commands/*}.py" = ["ARG002"]
"{*/admin,*/forms,*/models,*/routers,*/serializers,*/translation,*/migrations/*,tests/*}.py" = ["RUF012"]
"*/migrations/*" = ["E501"]
"tests/*" = [
"D", # docstring
"PT", # unittest
"ARG002", # mocks
"BLE001", # Exception
"PLR2004", # Magic value used
"S101", # assert
"D", "FBT003", "INP001", "PLR2004", "PT", "S", "TRY003",
"ARG002", # mock
]
"*/commands/*" = [
"ARG001", # pika
"PLW2901",
"TRY003", # errors
]

[tool.coverage.run]
omit = ['*/migrations/*']
omit = ["*/migrations/*"]

0 comments on commit 9d01843

Please sign in to comment.