@@ -194,7 +194,9 @@ def export_queued_data_rows(self,
194
194
self .uid )
195
195
time .sleep (sleep_time )
196
196
197
- def video_label_generator (self , timeout_seconds = 600 ) -> LabelGenerator :
197
+ def video_label_generator (self ,
198
+ timeout_seconds = 600 ,
199
+ ** kwargs ) -> LabelGenerator :
198
200
"""
199
201
Download video annotations
200
202
@@ -203,7 +205,8 @@ def video_label_generator(self, timeout_seconds=600) -> LabelGenerator:
203
205
"""
204
206
_check_converter_import ()
205
207
json_data = self .export_labels (download = True ,
206
- timeout_seconds = timeout_seconds )
208
+ timeout_seconds = timeout_seconds ,
209
+ ** kwargs )
207
210
# assert that the instance this would fail is only if timeout runs out
208
211
assert isinstance (
209
212
json_data ,
@@ -222,7 +225,7 @@ def video_label_generator(self, timeout_seconds=600) -> LabelGenerator:
222
225
"Or use project.label_generator() for text and imagery data." )
223
226
return LBV1Converter .deserialize_video (json_data , self .client )
224
227
225
- def label_generator (self , timeout_seconds = 600 ) -> LabelGenerator :
228
+ def label_generator (self , timeout_seconds = 600 , ** kwargs ) -> LabelGenerator :
226
229
"""
227
230
Download text and image annotations
228
231
@@ -231,7 +234,8 @@ def label_generator(self, timeout_seconds=600) -> LabelGenerator:
231
234
"""
232
235
_check_converter_import ()
233
236
json_data = self .export_labels (download = True ,
234
- timeout_seconds = timeout_seconds )
237
+ timeout_seconds = timeout_seconds ,
238
+ ** kwargs )
235
239
# assert that the instance this would fail is only if timeout runs out
236
240
assert isinstance (
237
241
json_data ,
@@ -250,26 +254,69 @@ def label_generator(self, timeout_seconds=600) -> LabelGenerator:
250
254
"Or use project.video_label_generator() for video data." )
251
255
return LBV1Converter .deserialize (json_data )
252
256
253
- def export_labels (
254
- self ,
255
- download = False ,
256
- timeout_seconds = 600 ) -> Optional [Union [str , List [Dict [Any , Any ]]]]:
257
+ def export_labels (self ,
258
+ download = False ,
259
+ timeout_seconds = 600 ,
260
+ ** kwargs ) -> Optional [Union [str , List [Dict [Any , Any ]]]]:
257
261
""" Calls the server-side Label exporting that generates a JSON
258
262
payload, and returns the URL to that payload.
259
263
260
264
Will only generate a new URL at a max frequency of 30 min.
261
265
262
266
Args:
267
+ download (bool): Returns the url if False
263
268
timeout_seconds (float): Max waiting time, in seconds.
269
+ start (str): Earliest date for labels, formatted "YYYY-MM-DD"
270
+ end (str): Latest date for labels, formatted "YYYY-MM-DD"
264
271
Returns:
265
272
URL of the data file with this Project's labels. If the server didn't
266
273
generate during the `timeout_seconds` period, None is returned.
267
274
"""
275
+
276
+ def _string_from_dict (dictionary : dict , value_with_quotes = False ) -> str :
277
+ """Returns a concatenated string of the dictionary's keys and values
278
+
279
+ The string will be formatted as {key}: 'value' for each key. Value will be inclusive of
280
+ quotations while key will not. This can be toggled with `value_with_quotes`"""
281
+
282
+ quote = "\" " if value_with_quotes else ""
283
+ return "," .join ([
284
+ f"""{ c } : { quote } { dictionary .get (c )} { quote } """
285
+ for c in dictionary
286
+ if dictionary .get (c )
287
+ ])
288
+
289
+ def _validate_datetime (string_date : str ) -> bool :
290
+ """helper function validate that datetime is as follows: YYYY-MM-DD for the export"""
291
+ if string_date :
292
+ try :
293
+ datetime .strptime (string_date , "%Y-%m-%d" )
294
+ except ValueError :
295
+ raise ValueError (f"""Incorrect format for: { string_date } .
296
+ Format must be \" YYYY-MM-DD\" """ )
297
+ return True
298
+
268
299
sleep_time = 2
269
300
id_param = "projectId"
301
+ filter_param = ""
302
+ filter_param_dict = {}
303
+
304
+ if "start" in kwargs or "end" in kwargs :
305
+ created_at_dict = {
306
+ "start" : kwargs .get ("start" , "" ),
307
+ "end" : kwargs .get ("end" , "" )
308
+ }
309
+ [_validate_datetime (date ) for date in created_at_dict .values ()]
310
+ filter_param_dict ["labelCreatedAt" ] = "{%s}" % _string_from_dict (
311
+ created_at_dict , value_with_quotes = True )
312
+
313
+ if filter_param_dict :
314
+ filter_param = """, filters: {%s }""" % (_string_from_dict (
315
+ filter_param_dict , value_with_quotes = False ))
316
+
270
317
query_str = """mutation GetLabelExportUrlPyApi($%s: ID!)
271
- {exportLabels(data:{projectId: $%s }) {downloadUrl createdAt shouldPoll} }
272
- """ % (id_param , id_param )
318
+ {exportLabels(data:{projectId: $%s%s }) {downloadUrl createdAt shouldPoll} }
319
+ """ % (id_param , id_param , filter_param )
273
320
274
321
while True :
275
322
res = self .client .execute (query_str , {id_param : self .uid })
0 commit comments