5
5
import sys
6
6
import time
7
7
import uuid
8
- from io import BytesIO
8
+ from importlib . metadata import version as get_version
9
9
from pathlib import Path
10
10
from typing import TypedDict
11
11
@@ -56,6 +56,13 @@ class ImageExifExtractionJob(BaseJob):
56
56
"""Represent an ImageExifExtractionJob."""
57
57
58
58
59
+ @app .command ()
60
+ def version () -> None :
61
+ """Return the version of bma-cli and bma-client."""
62
+ click .echo (f"bma-cli version { get_version ('bma-cli' )} " )
63
+ click .echo (f"bma-client version { get_version ('bma-client' )} " )
64
+
65
+
59
66
@app .command ()
60
67
def fileinfo (file_uuid : uuid .UUID ) -> None :
61
68
"""Get info for a file."""
@@ -66,12 +73,11 @@ def fileinfo(file_uuid: uuid.UUID) -> None:
66
73
67
74
@app .command ()
68
75
def jobs () -> None :
69
- """Get info on assigned jobs."""
76
+ """Get info on unfinished jobs."""
70
77
client , config = init ()
71
- jobs = client .get_jobs (job_filter = f "?limit=0&finished=false&client_uuid= { client . uuid } " )
78
+ jobs = client .get_jobs (job_filter = "?limit=0&finished=false" )
72
79
click .echo (json .dumps (jobs ))
73
- click .echo (f"Total { len (jobs )} unfinished jobs assigned to this client." )
74
-
80
+ click .echo (f"Total { len (jobs )} unfinished jobs." , err = True )
75
81
76
82
77
83
@app .command ()
@@ -88,22 +94,24 @@ def grind() -> None:
88
94
"""Get jobs from the server and handle them."""
89
95
client , config = init ()
90
96
91
- # get any unfinished jobs already assigned to this client
92
- jobs = client .get_jobs (job_filter = f"?limit=0&finished=false&client_uuid={ client .uuid } " )
93
- if not jobs :
94
- # no unfinished jobs assigned to this client, ask for new assignment
95
- jobs = client .get_job_assignment ()
96
-
97
- if not jobs :
98
- click .echo ("Nothing to do." )
99
- return
100
-
101
- # loop over jobs and handle each
102
- for job in jobs :
103
- # make sure we have the original file locally
104
- fileinfo = client .download (file_uuid = job ["basefile_uuid" ])
105
- path = Path (config ["path" ], fileinfo ["filename" ])
106
- handle_job (f = path , job = job , client = client , config = config )
97
+ while True :
98
+ # get any unfinished jobs already assigned to this client
99
+ jobs = client .get_jobs (job_filter = f"?limit=0&finished=false&client_uuid={ client .uuid } " )
100
+ if not jobs :
101
+ # no unfinished jobs assigned to this client, ask for new assignment
102
+ jobs = client .get_job_assignment ()
103
+
104
+ if not jobs :
105
+ click .echo ("Nothing left to do." )
106
+ return
107
+
108
+ # loop over jobs and handle each
109
+ click .echo (f"Processing { len (jobs )} jobs for file { jobs [0 ]['basefile_uuid' ]} ..." )
110
+ for job in jobs :
111
+ # make sure we have the original file locally
112
+ fileinfo = client .download (file_uuid = job ["basefile_uuid" ])
113
+ path = Path (config ["path" ], fileinfo ["filename" ])
114
+ handle_job (f = path , job = job , client = client , config = config )
107
115
click .echo ("Done!" )
108
116
109
117
@@ -121,7 +129,7 @@ def upload(files: list[str]) -> None:
121
129
if metadata ["jobs_unfinished" ] == 0 :
122
130
continue
123
131
124
- # it seems there is work to do! ask for assignment
132
+ # it seems there is work to do for the newly uploaded file!
125
133
jobs = client .get_job_assignment (file_uuid = metadata ["uuid" ])
126
134
if not jobs :
127
135
click .echo ("No unassigned unfinished jobs found for this file." )
@@ -157,29 +165,7 @@ def handle_job(f: Path, job: ImageConversionJob | ImageExifExtractionJob, client
157
165
click .echo (f"Handling job { job ['job_type' ]} { job ['job_uuid' ]} ..." )
158
166
start = time .time ()
159
167
result = client .handle_job (job = job , orig = f )
160
- logger .debug (f"Getting result took { time .time () - start } seconds" )
161
- if not result :
162
- click .echo (f"No result returned for job { job ['job_type' ]} { job ['uuid' ]} - skipping ..." )
163
- return
164
-
165
- # got job result, do whatever is needed depending on job_type
166
- if job ["job_type" ] == "ImageConversionJob" :
167
- image , exif = result
168
- filename = job ["job_uuid" ] + "." + job ["filetype" ].lower ()
169
- logger .debug (f"Encoding result as { job ['filetype' ]} ..." )
170
- start = time .time ()
171
- with BytesIO () as buf :
172
- image .save (buf , format = job ["filetype" ], exif = exif , lossless = False , quality = 90 )
173
- logger .debug (f"Encoding result took { time .time () - start } seconds" )
174
- client .upload_job_result (job_uuid = job ["job_uuid" ], buf = buf , filename = filename )
175
- elif job ["job_type" ] == "ImageExifExtractionJob" :
176
- logger .debug (f"Got exif data { result } " )
177
- with BytesIO () as buf :
178
- buf .write (json .dumps (result ).encode ())
179
- client .upload_job_result (job_uuid = job ["job_uuid" ], buf = buf , filename = "exif.json" )
180
- else :
181
- logger .error ("Unsupported job type" )
182
- raise typer .Exit (1 )
168
+ logger .debug (f"Getting result took { time .time () - start } seconds: { result } " )
183
169
184
170
185
171
def load_config () -> dict [str , str ]:
0 commit comments