Skip to content

Commit

Permalink
fix: move file information generation to utils (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-desbureaux-tellae authored Jun 23, 2022
1 parent 1a7245c commit 984babd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
29 changes: 14 additions & 15 deletions starling_sim/basemodel/output/output_factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from starling_sim.basemodel.output.geojson_output import new_geojson_output
from starling_sim.utils.utils import json_pretty_dump
from starling_sim.utils.utils import json_pretty_dump, create_file_information
from starling_sim.utils.config import config
from starling_sim.utils.constants import RUN_SUMMARY_FILENAME

Expand Down Expand Up @@ -115,23 +115,22 @@ def new_output_file(
:param subject: subject
"""

if content is None:
raise ValueError("'content' metadata was not provided for output {}".format(filepath))

if compressed_mimetype is None:
compressed_mimetype = mimetype

metadata = {"compressed-mimetype": compressed_mimetype, "content": content}

if subject is not None:
metadata["subject"] = subject

logging.info("Generated {} output in file {}".format(metadata["content"], filepath))
output_file_information = create_file_information(
filepath,
mimetype,
compressed_mimetype=compressed_mimetype,
content=content,
subject=subject,
)

self.output_files.append(
{"filename": os.path.basename(filepath), "mimetype": mimetype, "metadata": metadata}
logging.info(
"Generated {} output in file {}".format(
output_file_information["metadata"]["content"], filepath
)
)

self.output_files.append(output_file_information)

def extract_simulation(self, simulation_model):
"""
This method will be called for the output generation.
Expand Down
42 changes: 42 additions & 0 deletions starling_sim/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,48 @@ def gz_decompression(filepath, delete_source=True):
os.remove(filepath)


# starling file information


def create_file_information(
filepath,
mimetype,
compressed_mimetype=None,
content=None,
subject=None,
):
"""
Create a dict containing file information.
:param filepath: file path
:param mimetype: file mimetype
:param compressed_mimetype: file mimetype after decompression
:param content: content metadata
:param subject: subject metadata
:return: { filename, mimetype, metadata }
"""

if content is None:
raise ValueError("'content' metadata was not provided for file {}".format(filepath))

if compressed_mimetype is None:
compressed_mimetype = mimetype

metadata = {"compressed-mimetype": compressed_mimetype, "content": content}

if subject is not None:
metadata["subject"] = subject

file_information = {
"filename": os.path.basename(filepath),
"mimetype": mimetype,
"metadata": metadata,
}

return file_information


# json schema validation

# use the type check from Draft4Validator, because Draft7 considers 1.0 as integer
Expand Down

0 comments on commit 984babd

Please sign in to comment.