From d21c19febd9424d692dd8be181ace9d98ae53494 Mon Sep 17 00:00:00 2001 From: Hyoungsooo Date: Fri, 16 Feb 2024 12:44:48 +0900 Subject: [PATCH 1/3] Create the directory automatically if it doesn't exist #70 --- target_csv/serialization.py | 17 +++++++++++++++++ tests/test_csv.py | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/target_csv/serialization.py b/target_csv/serialization.py index adc8acd..0b29bc3 100644 --- a/target_csv/serialization.py +++ b/target_csv/serialization.py @@ -1,8 +1,25 @@ import csv # noqa: D100 from pathlib import Path from typing import Any, List +import os +def create_folder_if_not_exists(func) -> None: + + def wrapper(*args, **kwargs) -> None: + try: + filepath = args[0] + except: + filepath = kwargs["filepath"] + folder = os.path.dirname(filepath) + if not os.path.exists(folder) and folder != "": + os.makedirs(folder) + return func(*args, **kwargs) + + return wrapper + + +@create_folder_if_not_exists def write_csv(filepath: Path, records: List[dict], schema: dict, **kwargs: Any) -> int: """Write a CSV file.""" if "properties" not in schema: diff --git a/tests/test_csv.py b/tests/test_csv.py index a371196..6cc17ce 100644 --- a/tests/test_csv.py +++ b/tests/test_csv.py @@ -58,11 +58,31 @@ def output_filepath(output_dir) -> Path: return result +@pytest.fixture +def test_file_paths(output_dir) -> List[Path]: + paths = [] + for dir in range(4): + path = Path(output_dir / f"test-dir-{dir}/csv-test-output-{dir}.csv") + if path.exists(): + path.unlink() + + paths.append(path) + + return paths + + def test_csv_write(output_filepath) -> None: for schema, records in SAMPLE_DATASETS: write_csv(filepath=output_filepath, records=records, schema=schema) +def test_csv_write_if_not_exists(test_file_paths) -> None: + + for path in test_file_paths: + for schema, records in SAMPLE_DATASETS: + write_csv(filepath=path, records=records, schema=schema) + + def test_csv_roundtrip(output_filepath) -> None: for schema, records in SAMPLE_DATASETS: write_csv(filepath=output_filepath, records=records, schema=schema) From fc19d3b6c67dac96c61869e9dad1ecb0ec9712fc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 16 Feb 2024 03:51:11 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- target_csv/serialization.py | 1 - tests/test_csv.py | 1 - 2 files changed, 2 deletions(-) diff --git a/target_csv/serialization.py b/target_csv/serialization.py index 0b29bc3..efbe699 100644 --- a/target_csv/serialization.py +++ b/target_csv/serialization.py @@ -5,7 +5,6 @@ def create_folder_if_not_exists(func) -> None: - def wrapper(*args, **kwargs) -> None: try: filepath = args[0] diff --git a/tests/test_csv.py b/tests/test_csv.py index 6cc17ce..11d46c0 100644 --- a/tests/test_csv.py +++ b/tests/test_csv.py @@ -77,7 +77,6 @@ def test_csv_write(output_filepath) -> None: def test_csv_write_if_not_exists(test_file_paths) -> None: - for path in test_file_paths: for schema, records in SAMPLE_DATASETS: write_csv(filepath=path, records=records, schema=schema) From 31bd0f51774b89ce65f27848ca9ae27dfa40b7a4 Mon Sep 17 00:00:00 2001 From: Hyoungsooo Date: Fri, 16 Feb 2024 12:58:59 +0900 Subject: [PATCH 3/3] fix: include docstring and type hint --- target_csv/serialization.py | 18 ++++++++++++++++++ tests/test_csv.py | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/target_csv/serialization.py b/target_csv/serialization.py index adc8acd..b141e06 100644 --- a/target_csv/serialization.py +++ b/target_csv/serialization.py @@ -1,8 +1,26 @@ import csv # noqa: D100 from pathlib import Path from typing import Any, List +import os +def create_folder_if_not_exists(func: Any) -> None: + """Decorator to create folder if it does not exist.""" + + def wrapper(*args: Any, **kwargs: Any) -> None: + try: + filepath = kwargs["filepath"] + except KeyError: + filepath = args[0] + folder = os.path.dirname(filepath) + if not os.path.exists(folder) and folder != "": + os.makedirs(folder) + return func(*args, **kwargs) + + return wrapper + + +@create_folder_if_not_exists def write_csv(filepath: Path, records: List[dict], schema: dict, **kwargs: Any) -> int: """Write a CSV file.""" if "properties" not in schema: diff --git a/tests/test_csv.py b/tests/test_csv.py index a371196..6cc17ce 100644 --- a/tests/test_csv.py +++ b/tests/test_csv.py @@ -58,11 +58,31 @@ def output_filepath(output_dir) -> Path: return result +@pytest.fixture +def test_file_paths(output_dir) -> List[Path]: + paths = [] + for dir in range(4): + path = Path(output_dir / f"test-dir-{dir}/csv-test-output-{dir}.csv") + if path.exists(): + path.unlink() + + paths.append(path) + + return paths + + def test_csv_write(output_filepath) -> None: for schema, records in SAMPLE_DATASETS: write_csv(filepath=output_filepath, records=records, schema=schema) +def test_csv_write_if_not_exists(test_file_paths) -> None: + + for path in test_file_paths: + for schema, records in SAMPLE_DATASETS: + write_csv(filepath=path, records=records, schema=schema) + + def test_csv_roundtrip(output_filepath) -> None: for schema, records in SAMPLE_DATASETS: write_csv(filepath=output_filepath, records=records, schema=schema)