Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
paaragon committed Jan 16, 2025
1 parent a63fbb7 commit 6c82a24
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions gateway/api/services/result_storage.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

"""
This module handle the access to the result store
"""
import os
import logging
import mimetypes
from typing import Optional, Tuple
from wsgiref.util import FileWrapper
from django.conf import settings
from django.core.files import File

logger = logging.getLogger("gateway")

Expand All @@ -19,12 +20,15 @@ class ResultStorage:
def __init__(self, username: str):
"""Initialize the storage path for a given user."""
self.user_results_directory = os.path.join(
settings.MEDIA_ROOT, username, "results")
settings.MEDIA_ROOT, username, "results"
)
os.makedirs(self.user_results_directory, exist_ok=True)

def __build_result_path(self, job_id: str) -> str:
"""Construct the full path for a result file."""
return os.path.join(self.user_results_directory, f"{job_id}{self.RESULT_FILE_EXTENSION}")
return os.path.join(
self.user_results_directory, f"{job_id}{self.RESULT_FILE_EXTENSION}"
)

def get(self, job_id: str) -> Optional[Tuple[FileWrapper, str, int]]:
"""
Expand All @@ -48,8 +52,9 @@ def get(self, job_id: str) -> Optional[Tuple[FileWrapper, str, int]]:

with open(result_path, "rb") as result_file:
file_wrapper = FileWrapper(result_file)
file_type = mimetypes.guess_type(
result_path)[0] or "application/octet-stream"
file_type = (
mimetypes.guess_type(result_path)[0] or "application/octet-stream"
)
file_size = os.path.getsize(result_path)
return file_wrapper, file_type, file_size

Expand All @@ -58,7 +63,7 @@ def save(self, job_id: str, result: str) -> None:
Save the result content to a file associated with the given job ID.
Args:
job_id (str): The unique identifier for the job. This will be used as the base
job_id (str): The unique identifier for the job. This will be used as the base
name for the result file.
result (str): The job result content to be saved in the file.
"""
Expand All @@ -67,4 +72,7 @@ def save(self, job_id: str, result: str) -> None:
with open(result_path, "w", encoding=self.ENCODING) as result_file:
result_file.write(result)
logger.info(
"Result for job ID '%s' successfully saved at '%s'.", job_id, result_path)
"Result for job ID '%s' successfully saved at '%s'.",
job_id,
result_path,
)

0 comments on commit 6c82a24

Please sign in to comment.