From 984babd7bbf462e8483104203f2010202a8a6d5a Mon Sep 17 00:00:00 2001 From: leo-desbureaux-tellae <73165148+leo-desbureaux-tellae@users.noreply.github.com> Date: Thu, 23 Jun 2022 15:37:04 +0200 Subject: [PATCH] fix: move file information generation to utils (#58) --- .../basemodel/output/output_factory.py | 29 +++++++------ starling_sim/utils/utils.py | 42 +++++++++++++++++++ 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/starling_sim/basemodel/output/output_factory.py b/starling_sim/basemodel/output/output_factory.py index 67cec04..b603f46 100644 --- a/starling_sim/basemodel/output/output_factory.py +++ b/starling_sim/basemodel/output/output_factory.py @@ -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 @@ -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. diff --git a/starling_sim/utils/utils.py b/starling_sim/utils/utils.py index c6f4497..4a26e3d 100644 --- a/starling_sim/utils/utils.py +++ b/starling_sim/utils/utils.py @@ -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