From 140656d8eb10c4caae2717a124a0814206d1ee02 Mon Sep 17 00:00:00 2001 From: ibrahimjaved12 Date: Mon, 15 Jan 2024 14:34:54 +0500 Subject: [PATCH] Refactor output paths --- ocw_oer_export/create_csv.py | 22 ++++++++++++---------- ocw_oer_export/create_json.py | 11 +++++++---- ocw_oer_export/data_handler.py | 5 ++++- tests/test_csv_creation_from_json.py | 2 +- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/ocw_oer_export/create_csv.py b/ocw_oer_export/create_csv.py index 6a9eb53..f1e37f5 100644 --- a/ocw_oer_export/create_csv.py +++ b/ocw_oer_export/create_csv.py @@ -129,8 +129,8 @@ def transform_single_course(course, ocw_topics_mapping): "CR_PROVIDER_SET": "MIT OpenCourseWare", "CR_COU_URL": "https://creativecommons.org/licenses/by-nc-sa/4.0/", "CR_COU_COPYRIGHT_HOLDER": "MIT", - "CR_EDUCATIONAL_USE": get_cr_educational_use(course["resource_content_tags"]), - "CR_ACCESSIBILITY": get_cr_accessibility(course["resource_content_tags"]), + "CR_EDUCATIONAL_USE": get_cr_educational_use(course["course_feature"]), + "CR_ACCESSIBILITY": get_cr_accessibility(course["course_feature"]), } @@ -146,9 +146,14 @@ def transform_data(data, ocw_topics_mapping): def create_csv( - source="api", input_file="ocw_api_data.json", output_file="ocw_oer_export.csv" + source="api", + input_file="ocw_api_data.json", + output_path="/output/ocw_oer_export.csv", ): - """Create a CSV file from either the MIT OpenCourseWare API or a locally stored JSON file.""" + """ + Create a CSV file from either the MIT OpenCourseWare API or a locally stored JSON file. + output_path: The output path inside the docker container. + """ api_data_json = {} if source == "api": @@ -182,14 +187,11 @@ def create_csv( "CR_EDUCATIONAL_USE", "CR_ACCESSIBILITY", ] - with open(output_file, "w", newline="", encoding="utf-8") as csv_file: + with open(output_path, "w", newline="", encoding="utf-8") as csv_file: writer = csv.DictWriter(csv_file, fieldnames=fieldnames) writer.writeheader() writer.writerows(transformed_data) - - current_dir = os.getcwd() logging.info( - "CSV file '%s' successfully created at directory: %s", - output_file, - current_dir, + "CSV file '%s' successfully created.", + output_path, ) diff --git a/ocw_oer_export/create_json.py b/ocw_oer_export/create_json.py index 74ac91d..b025245 100644 --- a/ocw_oer_export/create_json.py +++ b/ocw_oer_export/create_json.py @@ -11,12 +11,15 @@ logger = logging.getLogger(__name__) -def create_json(output_file="ocw_api_data.json"): - """Fetches data from MIT OpenCourseWare API and writes it to a JSON file.""" +def create_json(output_path="/output/ocw_api_data.json"): + """ + Fetches data from MIT OpenCourseWare API and writes it to a JSON file. + output_path: The output path inside the docker container. + """ api_data = extract_data_from_api(api_url=API_URL) try: - with open(output_file, "w", encoding="utf-8") as json_file: + with open(output_path, "w", encoding="utf-8") as json_file: json.dump(api_data, json_file, ensure_ascii=False, indent=4) - logger.info("Data saved to %s at present directory.", output_file) + logger.info("JSON file '%s' successfully created.", output_path) except IOError as e: logger.error("Error saving data to JSON: %s", e) diff --git a/ocw_oer_export/data_handler.py b/ocw_oer_export/data_handler.py index eec3fde..21ae11a 100644 --- a/ocw_oer_export/data_handler.py +++ b/ocw_oer_export/data_handler.py @@ -3,10 +3,13 @@ """ import json import logging +import os -def extract_data_from_json(file_path): +def extract_data_from_json(file_name): """Extract data from a JSON file.""" + file_dir = "/output" # Set the output directory inside the container + file_path = os.path.join(file_dir, file_name) try: with open(file_path, "r", encoding="utf-8") as json_file: data = json.load(json_file) diff --git a/tests/test_csv_creation_from_json.py b/tests/test_csv_creation_from_json.py index b379bc3..e0c22b6 100644 --- a/tests/test_csv_creation_from_json.py +++ b/tests/test_csv_creation_from_json.py @@ -21,7 +21,7 @@ def test_csv_creation_from_json(self): create_csv( source="json", input_file=self.sample_json_path, - output_file=self.generated_csv_path, + output_path=self.generated_csv_path, ) generated_csv_data = extract_data_from_file(self.generated_csv_path) expected_csv_data = extract_data_from_file(self.expected_csv_path)