Skip to content

Commit

Permalink
Meet new ruff linting conventions
Browse files Browse the repository at this point in the history
Side effects of this change:
* When generating a result_error_message, datetime.now() has been
updated to set the timezone as UTC.  It is believed this is the default
behavior in AWS for a normal now(), but it WILL change the datetime for
local testing to be UTC time.

Relevant ticket(s):
* https://mitlibraries.atlassian.net/browse/IN-1059
  • Loading branch information
ghukill committed Aug 30, 2024
1 parent a19972b commit 6f8686c
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 93 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ ignore = [
"D102",
"D103",
"D104",
"D200",
"D212",
"D415",
"G004",
"PLR0912",
"PLR0913",
"PLR0915",
Expand Down
12 changes: 7 additions & 5 deletions submitter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ class Config:
def __init__(self) -> None:
try:
self.ENV = os.environ["WORKSPACE"]
except KeyError as e:
print("Env variable 'WORKSPACE' is required, please set it and try again.")
raise e
except KeyError:
logger.error( # noqa: TRY400
"Env variable 'WORKSPACE' is required, please set it and try again."
)
raise
self.AWS_REGION_NAME = "us-east-1"
print(f"Configuring dspace-submission-service for env={self.ENV}")
logger.info(f"Configuring dspace-submission-service for env={self.ENV}")
self.load_config_variables(self.ENV)

def load_config_variables(self, env: str) -> None:
Expand All @@ -33,7 +35,7 @@ def load_config_variables(self, env: str) -> None:
if env == "test":
self.DSPACE_API_URL = "mock://dspace.edu/rest/"
self.DSPACE_USER = "test"
self.DSPACE_PASSWORD = "test" # nosec
self.DSPACE_PASSWORD = "test" # nosec # noqa: S105
self.DSPACE_TIMEOUT = 3.0
self.INPUT_QUEUE = "test_queue_with_messages"
self.LOG_FILTER = "true"
Expand Down
12 changes: 5 additions & 7 deletions submitter/message.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Iterator
import json
from collections.abc import Iterator


def generate_submission_messages_from_file(
Expand All @@ -8,14 +8,14 @@ def generate_submission_messages_from_file(
with open(filepath) as file:
messages = json.load(file)

for message_name, message_json in messages.items():
for message_json in messages.values():
attributes = attributes_from_json(message_json, output_queue)
body = body_from_json(message_json)
yield attributes, body


def attributes_from_json(message_json: dict, output_queue: str) -> dict:
attributes = {
return {
"PackageID": {
"DataType": "String",
"StringValue": message_json["package id"],
Expand All @@ -29,7 +29,6 @@ def attributes_from_json(message_json: dict, output_queue: str) -> dict:
"StringValue": output_queue,
},
}
return attributes


def body_from_json(message_json: dict) -> dict:
Expand All @@ -55,14 +54,14 @@ def generate_result_messages_from_file(
with open(filepath) as file:
messages = json.load(file)

for message_name, message_json in messages.items():
for message_json in messages.values():
attributes = result_attributes_from_json(message_json)
body = result_body_from_json(message_json)
yield attributes, body


def result_attributes_from_json(message_json: dict) -> dict:
attributes = {
return {
"PackageID": {
"DataType": "String",
"StringValue": message_json["package id"],
Expand All @@ -72,7 +71,6 @@ def result_attributes_from_json(message_json: dict) -> dict:
"StringValue": message_json["source"],
},
}
return attributes


def result_body_from_json(message_json: dict) -> dict:
Expand Down
12 changes: 4 additions & 8 deletions submitter/sqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@


def sqs_client() -> "SQSServiceResource":
sqs = boto3.resource(
return boto3.resource(
service_name="sqs",
region_name=CONFIG.AWS_REGION_NAME,
endpoint_url=CONFIG.SQS_ENDPOINT_URL,
)

return sqs


def message_loop(queue: str, wait: int, visibility: int = 30) -> None:
logger.info("Message loop started")
Expand Down Expand Up @@ -103,24 +101,22 @@ def write_message_to_queue(
) -> "SendMessageResultTypeDef":
sqs = sqs_client()
queue = sqs.get_queue_by_name(QueueName=output_queue)
response = queue.send_message(
return queue.send_message(
MessageAttributes=attributes,
MessageBody=json.dumps(body),
)
return response


def create(name: str) -> "Queue":
sqs = sqs_client()
queue = sqs.create_queue(QueueName=name)
return queue
return sqs.create_queue(QueueName=name)


def verify_sent_message(
sent_message_body: dict | str | None,
sqs_send_message_response: "SendMessageResultTypeDef",
) -> bool:
body_md5 = hashlib.md5( # nosec
body_md5 = hashlib.md5( # nosec # noqa: S324
json.dumps(sent_message_body).encode("utf-8")
).hexdigest()
return body_md5 == sqs_send_message_response["MD5OfMessageBody"]
37 changes: 18 additions & 19 deletions submitter/submission.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from collections.abc import Iterator
import json
import logging
import sys
import traceback
from datetime import datetime
from collections.abc import Iterator
from datetime import UTC, datetime
from typing import TYPE_CHECKING

import dspace
from dspace.client import DSpaceClient
import requests
import smart_open
from dspace.client import DSpaceClient

from submitter import CONFIG, errors

Expand Down Expand Up @@ -65,7 +65,9 @@ def from_message(cls, message: "Message") -> "Submission":
"SubmissionSource": message.message_attributes["SubmissionSource"],
}
except KeyError as e:
raise errors.SubmitMessageMissingAttributeError(message.message_id, e.args[0])
raise errors.SubmitMessageMissingAttributeError(
message.message_id, e.args[0]
) from e

try:
body = json.loads(message.body)
Expand Down Expand Up @@ -95,44 +97,41 @@ def from_message(cls, message: "Message") -> "Submission":

def create_item(self) -> dspace.item.Item:
"""Create item instance with metadata entries from submission message."""
logger.debug("Creating local item instance from submission message")
item = dspace.item.Item()
try:
logger.debug("Creating local item instance from submission message")
item = dspace.item.Item()
for entry in self.get_metadata_entries_from_file():
metadata_entry = dspace.item.MetadataEntry.from_dict(entry)
item.metadata.append(metadata_entry)
return item
except KeyError as e:
raise errors.ItemCreateError(self.metadata_location) from e
return item

def get_metadata_entries_from_file(self) -> Iterator[dict]:
with smart_open.open(self.metadata_location) as f:
metadata = json.load(f)
for entry in metadata["metadata"]:
yield entry
yield from metadata["metadata"]

def add_bitstreams_to_item(self, item: dspace.item.Item) -> dspace.item.Item:
"""Add bitstreams to item from files in submission message."""
logger.debug("Adding bitstreams to local item instance from submission message")
try:
logger.debug(
"Adding bitstreams to local item instance from submission message"
)
for file in self.files or []:
bitstream = dspace.bitstream.Bitstream(
file_path=file["FileLocation"],
name=file["BitstreamName"],
description=file.get("BitstreamDescription"),
)
item.bitstreams.append(bitstream)
return item
except KeyError as e:
raise errors.BitstreamAddError() from e
raise errors.BitstreamAddError from e
return item

def result_error_message(
self, message: str, dspace_response: str | None = None
) -> None:
"""Set result message on Submission object on submit error."""
time = datetime.now()
time = datetime.now(tz=UTC)
tb = traceback.format_exception(*sys.exc_info())
self.result_message = {
"ResultType": "error",
Expand Down Expand Up @@ -195,11 +194,11 @@ def submit(self, client: DSpaceClient) -> None:
except (errors.BitstreamOpenError, errors.BitstreamPostError) as e:
self.result_error_message(e.message, getattr(e, "dspace_error", None))
clean_up_partial_success(client, item)
except Exception as e:
except Exception:
logger.exception(
"Unexpected exception, aborting DSpace Submission Service processing"
)
raise e
raise


def post_item(
Expand All @@ -216,8 +215,8 @@ def post_item(
)
item.post(client, collection_handle=collection_handle)
logger.debug("Posted item to Dspace with handle '%s'", item.handle)
except requests.exceptions.Timeout as e:
raise e
except requests.exceptions.Timeout:
raise
except requests.exceptions.HTTPError as e:
raise errors.ItemPostError(e, collection_handle) from e

Expand Down
Loading

0 comments on commit 6f8686c

Please sign in to comment.