-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from unity-sds/output-to-file
feat: writing output content to a file if ENV is provided
- Loading branch information
Showing
12 changed files
with
99 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import logging | ||
import os | ||
|
||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import logging | ||
import os | ||
|
||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
cumulus_lambda_functions/stage_in_out/stage_in_out_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import json | ||
import logging | ||
import os | ||
from typing import Union | ||
|
||
from cumulus_lambda_functions.lib.utils.file_utils import FileUtils | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class StageInOutUtils: | ||
OUTPUT_FILE = 'OUTPUT_FILE' | ||
|
||
@staticmethod | ||
def write_output_to_file(output_json: Union[dict, str, list]): | ||
if StageInOutUtils.OUTPUT_FILE not in os.environ: | ||
LOGGER.debug(f'Not writing output to file due to missing {StageInOutUtils.OUTPUT_FILE} in ENV') | ||
return | ||
output_filepath = os.environ.get(StageInOutUtils.OUTPUT_FILE) | ||
FileUtils.mk_dir_p(os.path.dirname(output_filepath)) | ||
output_str = json.dumps(output_json) if not isinstance(output_json, str) else output_json | ||
with open(output_filepath, 'w') as ff: | ||
ff.write(output_str) | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
22 changes: 22 additions & 0 deletions
22
tests/cumulus_lambda_functions/stage_in_out/test_stage_in_out_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import os | ||
import tempfile | ||
from unittest import TestCase | ||
|
||
from cumulus_lambda_functions.lib.utils.file_utils import FileUtils | ||
from cumulus_lambda_functions.stage_in_out.stage_in_out_utils import StageInOutUtils | ||
|
||
|
||
class TestStageInOutUtils(TestCase): | ||
def test_01(self): | ||
with tempfile.TemporaryDirectory() as tmp_dir_name: | ||
os.environ[StageInOutUtils.OUTPUT_FILE] = os.path.join(tmp_dir_name, 'SAMPLE', 'output.json') | ||
StageInOutUtils.write_output_to_file({'test1': True}) | ||
self.assertTrue(FileUtils.file_exist(os.environ.get(StageInOutUtils.OUTPUT_FILE))) | ||
return | ||
|
||
def test_02(self): | ||
with tempfile.TemporaryDirectory() as tmp_dir_name: | ||
os.environ[StageInOutUtils.OUTPUT_FILE] = os.path.join(tmp_dir_name, 'output.json') | ||
StageInOutUtils.write_output_to_file({'test1': True}) | ||
self.assertTrue(FileUtils.file_exist(os.environ.get(StageInOutUtils.OUTPUT_FILE))) | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters