5
5
import os
6
6
from os .path import join , exists , basename
7
7
import json
8
+ import ast
8
9
import shutil
9
10
import zipfile
10
11
import traceback
@@ -208,19 +209,23 @@ def stop_task(name):
208
209
task .finished_at = timezone .now ()
209
210
task .save ()
210
211
211
- def write_file_size (response ):
212
- LOG .debug ("Logging response %s" ,response )
212
+ def format_response (res_item ):
213
+ if isinstance (res_item , str ):
214
+ return ast .literal_eval (res_item )
215
+ return res_item
213
216
217
+ def write_file_size (response ):
218
+ LOG .debug ("Logging response %s" , response )
214
219
if response :
215
220
for item in response :
216
- if item :
217
- config = configparser .ConfigParser ()
218
- config ["FileInfo" ] = {"FileSize" : str (item ["zip_file_size_bytes" ])}
219
- size_path = join (
220
- download_dir , f"{ item ['download_url' ].split ('/' )[- 1 ]} _size.ini"
221
- )
222
- with open (size_path , "w" ) as configfile :
223
- config .write (configfile )
221
+ item = format_response ( item )
222
+ config = configparser .ConfigParser ()
223
+ config ["FileInfo" ] = {"FileSize" : str (item ["zip_file_size_bytes" ])}
224
+ size_path = join (
225
+ download_dir , f"{ item ['download_url' ].split ('/' )[- 1 ]} _size.ini"
226
+ )
227
+ with open (size_path , "w" ) as configfile :
228
+ config .write (configfile )
224
229
225
230
def finish_task (name , created_files = None , response_back = None , planet_file = False ):
226
231
LOG .debug ("Task Finish: {0} for run: {1}" .format (name , run_uid ))
@@ -229,15 +234,18 @@ def finish_task(name, created_files=None, response_back=None, planet_file=False)
229
234
task .finished_at = timezone .now ()
230
235
# assumes each file only has one part (all are zips or PBFs)
231
236
if response_back :
232
- task .filenames = [r ["download_url" ] for r in response_back ]
237
+ task .filenames = [
238
+ format_response (item )["download_url" ] for item in response_back
239
+ ]
233
240
else :
234
241
task .filenames = [basename (file .parts [0 ]) for file in created_files ]
235
242
if planet_file is False :
236
243
if response_back :
237
244
total_bytes = 0
238
- for r in response_back :
245
+ for item in response_back :
246
+ item = format_response (item )
239
247
total_bytes += int (
240
- str (r ["zip_file_size_bytes" ])
248
+ str (item ["zip_file_size_bytes" ])
241
249
) # getting filesize bytes
242
250
task .filesize_bytes = total_bytes
243
251
else :
@@ -269,7 +277,7 @@ def finish_task(name, created_files=None, response_back=None, planet_file=False)
269
277
set (galaxy_supported_outputs )
270
278
):
271
279
use_only_galaxy = True
272
- LOG .debug ("Using Only galaxy to Perform Request" )
280
+ LOG .debug ("Using Only Raw Data API to Perform Request" )
273
281
274
282
if is_hdx_export :
275
283
planet_file = HDXExportRegion .objects .get (job_id = run .job_id ).planet_file
@@ -469,15 +477,7 @@ def add_metadata(z, theme):
469
477
try :
470
478
LOG .debug ("Raw Data API fetch started for csv run: {0}" .format (run_uid ))
471
479
response_back = csv .fetch ("csv" , is_hdx_export = True )
472
- for r in response_back :
473
- config = configparser .ConfigParser ()
474
- config ["FileInfo" ] = {"FileSize" : str (r ["zip_file_size_bytes" ])}
475
- size_path = join (
476
- download_dir , f"{ r ['download_url' ].split ('/' )[- 1 ]} _size.ini"
477
- )
478
- with open (size_path , "w" ) as configfile :
479
- config .write (configfile )
480
-
480
+ write_file_size (response_back )
481
481
LOG .debug ("Raw Data API fetch ended for csv run: {0}" .format (run_uid ))
482
482
finish_task ("csv" , response_back = response_back )
483
483
all_zips += response_back
0 commit comments