Skip to content

Commit

Permalink
Refactor output paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimjaved12 committed Jan 15, 2024
1 parent 932f25c commit 140656d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
22 changes: 12 additions & 10 deletions ocw_oer_export/create_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
}


Expand All @@ -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":
Expand Down Expand Up @@ -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,
)
11 changes: 7 additions & 4 deletions ocw_oer_export/create_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 4 additions & 1 deletion ocw_oer_export/data_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_csv_creation_from_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 140656d

Please sign in to comment.