Skip to content

Commit fc486d7

Browse files
committed
support unassigning unsupported jobs, introduce custom exception for unsupported jobs, fix misc little bugs
1 parent 3c0cc49 commit fc486d7

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/bma_client_lib/bma_client.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717
import magic
1818
from PIL import Image, ImageOps
1919

20-
from .datastructures import ImageConversionJob, ImageExifExtractionJob, Job, ThumbnailJob, ThumbnailSourceJob
20+
from .datastructures import (
21+
ImageConversionJob,
22+
ImageExifExtractionJob,
23+
Job,
24+
JobNotSupportedError,
25+
ThumbnailJob,
26+
ThumbnailSourceJob,
27+
)
2128

2229
logger = logging.getLogger("bma_client")
2330

@@ -98,7 +105,7 @@ def get_server_settings(self) -> dict[str, dict[str, dict[str, list[str]]]]:
98105
self.settings = r.json()["bma_response"]
99106
return r.json()
100107

101-
def get_jobs(self, job_filter: str = "?limit=0") -> list[dict[str, str]]:
108+
def get_jobs(self, job_filter: str = "?limit=0") -> list[Job]:
102109
"""Get a filtered list of the jobs this user has access to."""
103110
r = self.client.get(self.base_url + f"/api/v1/json/jobs/{job_filter}").raise_for_status()
104111
response = r.json()["bma_response"]
@@ -125,14 +132,14 @@ def download_job_source(self, job: Job) -> Path:
125132
path=self.path / job.source_filename,
126133
)
127134

128-
def get_job_assignment(self, file_uuid: uuid.UUID | None = None) -> list[Job]:
135+
def get_job_assignment(self, job_filter: str = "") -> list[Job]:
129136
"""Ask for new job(s) from the API."""
130137
url = self.base_url + "/api/v1/json/jobs/assign/"
131-
if file_uuid:
132-
url += f"?file_uuid={file_uuid}"
138+
if job_filter:
139+
url += job_filter
133140
data = {
134141
"client_uuid": self.uuid,
135-
"client_version": "bma-client-lib {__version__}",
142+
"client_version": f"bma-client-lib {__version__}",
136143
}
137144
try:
138145
r = self.client.post(url, json=data).raise_for_status()
@@ -145,6 +152,14 @@ def get_job_assignment(self, file_uuid: uuid.UUID | None = None) -> list[Job]:
145152
logger.debug(f"Returning {len(response)} assigned jobs")
146153
return response
147154

155+
def unassign_job(self, job: Job) -> bool:
156+
"""Unassign a job."""
157+
logger.debug(f"Unassigning job {job.job_uuid}")
158+
self.client.post(
159+
self.base_url + f"/api/v1/json/jobs/{job.job_uuid}/unassign/",
160+
).raise_for_status()
161+
return True
162+
148163
def upload_file(self, path: Path, attribution: str, file_license: str) -> dict[str, dict[str, str]]:
149164
"""Upload a file."""
150165
# get mimetype
@@ -169,7 +184,7 @@ def upload_file(self, path: Path, attribution: str, file_license: str) -> dict[s
169184
if rotated is None:
170185
raise ValueError("Rotation")
171186
logger.debug(
172-
f"Image has exif rotation info, using post-rotate size {rotated.size}"
187+
f"Image has exif rotation info, using post-rotate size {rotated.size} "
173188
f"instead of raw size {image.size}"
174189
)
175190
width, height = rotated.size
@@ -217,7 +232,7 @@ def handle_job(self, job: Job) -> None:
217232
filename = job.source_filename
218233

219234
else:
220-
raise TypeError(type(job))
235+
raise JobNotSupportedError(job=job)
221236

222237
self.write_and_upload_result(job=job, result=result, filename=filename)
223238

@@ -252,7 +267,7 @@ def write_and_upload_result(self, job: Job, result: "JobResult", filename: str)
252267

253268
else:
254269
logger.error("Unsupported job type")
255-
raise TypeError(job.job_type)
270+
raise JobNotSupportedError(job=job)
256271

257272
self.upload_job_result(job=job, buf=buf, filename=filename, metadata=metadata)
258273

@@ -391,4 +406,4 @@ def create_thumbnail_source(self, job: ThumbnailSourceJob) -> "ThumbnailSourceJo
391406
orig=path,
392407
)
393408
# unsupported filetype
394-
raise ValueError(info["filetype"])
409+
raise JobNotSupportedError(job=job)

src/bma_client_lib/datastructures.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,11 @@ class ThumbnailJob(ImageConversionJob):
5858
ThumbnailSourceJobResult: TypeAlias = ImageConversionJobResult
5959
ExifExtractionJobResult: TypeAlias = dict[str, dict[str, str]]
6060
JobResult: TypeAlias = ImageConversionJobResult | ExifExtractionJobResult | ThumbnailSourceJobResult
61+
62+
63+
class JobNotSupportedError(Exception):
64+
"""Exception raised when a job is not supported by bma_client_lib for some reason."""
65+
66+
def __init__(self, job: Job) -> None:
67+
"""Exception raised when a job is not supported by bma_client_lib for some reason."""
68+
super().__init__(f"{job.job_type} {job.job_uuid} for file {job.basefile_uuid} not supported by this client.")

0 commit comments

Comments
 (0)