Skip to content

Commit f6212e9

Browse files
Format response from raw data api
1 parent 4458355 commit f6212e9

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

tasks/task_runners.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
from os.path import join, exists, basename
77
import json
8+
import ast
89
import shutil
910
import zipfile
1011
import traceback
@@ -208,19 +209,23 @@ def stop_task(name):
208209
task.finished_at = timezone.now()
209210
task.save()
210211

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
213216

217+
def write_file_size(response):
218+
LOG.debug("Logging response %s", response)
214219
if response:
215220
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)
224229

225230
def finish_task(name, created_files=None, response_back=None, planet_file=False):
226231
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)
229234
task.finished_at = timezone.now()
230235
# assumes each file only has one part (all are zips or PBFs)
231236
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+
]
233240
else:
234241
task.filenames = [basename(file.parts[0]) for file in created_files]
235242
if planet_file is False:
236243
if response_back:
237244
total_bytes = 0
238-
for r in response_back:
245+
for item in response_back:
246+
item = format_response(item)
239247
total_bytes += int(
240-
str(r["zip_file_size_bytes"])
248+
str(item["zip_file_size_bytes"])
241249
) # getting filesize bytes
242250
task.filesize_bytes = total_bytes
243251
else:
@@ -269,7 +277,7 @@ def finish_task(name, created_files=None, response_back=None, planet_file=False)
269277
set(galaxy_supported_outputs)
270278
):
271279
use_only_galaxy = True
272-
LOG.debug("Using Only galaxy to Perform Request")
280+
LOG.debug("Using Only Raw Data API to Perform Request")
273281

274282
if is_hdx_export:
275283
planet_file = HDXExportRegion.objects.get(job_id=run.job_id).planet_file
@@ -469,15 +477,7 @@ def add_metadata(z, theme):
469477
try:
470478
LOG.debug("Raw Data API fetch started for csv run: {0}".format(run_uid))
471479
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)
481481
LOG.debug("Raw Data API fetch ended for csv run: {0}".format(run_uid))
482482
finish_task("csv", response_back=response_back)
483483
all_zips += response_back

0 commit comments

Comments
 (0)