From d81a8d7a3073c3d88057fa09b7198fde4f34b07c Mon Sep 17 00:00:00 2001 From: aidanjungo Date: Tue, 15 Nov 2022 13:01:14 +0100 Subject: [PATCH 1/6] Replace os by pathlib and adapt some tests --- src/cpacspy/cpacspy.py | 27 ++++++++++++------------- tests/test_aeromap.py | 5 ++--- tests/test_cpacspy.py | 45 ++++++++++++++++++++++-------------------- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/cpacspy/cpacspy.py b/src/cpacspy/cpacspy.py index 6dace5d..bd0ae79 100644 --- a/src/cpacspy/cpacspy.py +++ b/src/cpacspy/cpacspy.py @@ -22,7 +22,6 @@ """ -import os from pathlib import Path import numpy as np @@ -122,12 +121,14 @@ def create_aeromap(self, uid): def create_aeromap_from_csv(self, csv_path, uid=None): """Create a new aeromap object from a CSV file.""" + if isinstance(csv_path, str): + csv_path = Path(csv_path) + if not uid: - _, tail = os.path.split(csv_path) - uid = tail.split(".")[0] + uid = csv_path.stem - if not os.path.exists(csv_path): - raise ValueError(f"CSV file not found at {os.path.abspath(csv_path)}") + if not csv_path.exists(): + raise ValueError(f"CSV file not found at {csv_path.absolute}") new_aeromap = self.create_aeromap(uid) @@ -182,28 +183,26 @@ def save_cpacs(self, cpacs_file, overwrite=False): """Save a CPACS file from the TIXI object at a chosen path.""" # To accept either a Path or a string - if isinstance(cpacs_file, Path): - cpacs_file = str(cpacs_file) - else: - cpacs_file = cpacs_file + if isinstance(cpacs_file, str): + cpacs_file = Path(cpacs_file) # Check for .xml file - if not cpacs_file.endswith(".xml"): + if cpacs_file.suffix != ".xml": raise ValueError("The CPACS file name must be a .xml file!") # Check if file name must be change to avoid overwrite - if os.path.exists(cpacs_file) and not overwrite: + if cpacs_file.exists() and not overwrite: find_name = False i = 1 while not find_name: - cpacs_file_new_name = cpacs_file.split(".xml")[0] + f"_{str(i)}.xml" - if not os.path.exists(cpacs_file_new_name): + cpacs_file_new_name = Path(cpacs_file.parent, f"{cpacs_file.stem}_{i}.xml") + if not cpacs_file_new_name.exists(): find_name = True cpacs_file = cpacs_file_new_name else: i += 1 - self.tixi.save(cpacs_file) + self.tixi.save(str(cpacs_file)) def __str__(self): diff --git a/tests/test_aeromap.py b/tests/test_aeromap.py index 343da1f..a1c8c83 100644 --- a/tests/test_aeromap.py +++ b/tests/test_aeromap.py @@ -22,7 +22,6 @@ """ -import os import numpy as np from pathlib import Path @@ -361,8 +360,8 @@ def test_csv(): assert csv_in == csv_export # Delete test file from a past run - if os.path.exists(CSV_OUT_FILE): - os.remove(CSV_OUT_FILE) + if CSV_OUT_FILE.exists(): + CSV_OUT_FILE.unlink() def test_get_cd0_oswald(): diff --git a/tests/test_cpacspy.py b/tests/test_cpacspy.py index d5a3c67..091e743 100644 --- a/tests/test_cpacspy.py +++ b/tests/test_cpacspy.py @@ -22,7 +22,6 @@ """ -import os from pathlib import Path import pytest @@ -96,7 +95,7 @@ def test_create_aeromap_from_csv(): # Raise error when uid already exist with pytest.raises(ValueError): - cpacs.create_aeromap_from_csv("/not/exiting/path.csv") + cpacs.create_aeromap_from_csv(Path("not", "exiting", "path.csv")) # Create a new aeromap from a CSV file cpacs.create_aeromap_from_csv(CSV_PATH) @@ -160,41 +159,45 @@ def test_delete_aeromap(): def test_save_cpacs(): - test_path = "tests/output.xml" - test_path_1 = "tests/output_1.xml" - test_path_2 = Path("tests", "output_2.xml") + test_path = Path(TESTS_PATH, "output.xml") + test_path_str = str(test_path) + test_path_1 = Path(TESTS_PATH, "output_1.xml") + test_path_2 = Path(TESTS_PATH, "output_2.xml") cpacs = CPACS(D150_TESTS_PATH) # Raise error when trying to save a not xml file with pytest.raises(ValueError): - cpacs.save_cpacs("tests/output.txt") + cpacs.save_cpacs(Path(TESTS_PATH, "output.txt")) # Delete test file from a past run (could be still there if an error occurs) - if os.path.exists(test_path): - os.remove(test_path) + if test_path.exists(): + test_path.unlink() - if os.path.exists(test_path_1): - os.remove(test_path_1) + if test_path_1.exists(): + test_path_1.unlink() - # Save CPACS file - cpacs.save_cpacs(test_path, True) - assert os.path.exists(test_path) + if test_path_2.exists(): + test_path_2.unlink() + + # Save CPACS file with a path given as a string + cpacs.save_cpacs(test_path_str, True) + assert test_path.exists() # Save CPACS file with a Path object cpacs.save_cpacs(test_path_2, True) - assert os.path.exists(test_path_2) + assert test_path_2.exists() # Save CPACS file with already existing name (no overwrite) cpacs.save_cpacs(test_path, False) - assert os.path.exists(test_path) + assert test_path.exists() # Delete test file - if os.path.exists(test_path): - os.remove(test_path) + if test_path.exists(): + test_path.unlink() - if os.path.exists(test_path_1): - os.remove(test_path_1) + if test_path_1.exists(): + test_path_1.unlink() - if os.path.exists(test_path_2): - os.remove(test_path_2) + if test_path_2.exists(): + test_path_2.unlink() From 1220cef7c79c779c158c6471042fc6fb4291e7c1 Mon Sep 17 00:00:00 2001 From: aidanjungo Date: Tue, 15 Nov 2022 14:08:27 +0100 Subject: [PATCH 2/6] remove string paths --- src/cpacspy/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cpacspy/utils.py b/src/cpacspy/utils.py index 71aa3e7..fbc9d6e 100644 --- a/src/cpacspy/utils.py +++ b/src/cpacspy/utils.py @@ -30,8 +30,8 @@ CPACSPY_ROOT = CPACSPY_LIB.parents[1] EXAMPLES_PATH = Path(CPACSPY_ROOT, "examples") TESTS_PATH = Path(CPACSPY_ROOT, "tests") -D150_TESTS_PATH = str(Path(TESTS_PATH, "D150_simple.xml")) -PROPELLER_TESTS_PATH = str(Path(TESTS_PATH, "SimpleAircraft_propeller.xml")) +D150_TESTS_PATH = Path(TESTS_PATH, "D150_simple.xml") +PROPELLER_TESTS_PATH = Path(TESTS_PATH, "SimpleAircraft_propeller.xml") # XPATH AC_NAME_XPATH = "/cpacs/header/name" From 197a47703bdd878ca01e3fa129979a01ba5f5ab1 Mon Sep 17 00:00:00 2001 From: aidanjungo Date: Tue, 15 Nov 2022 14:16:04 +0100 Subject: [PATCH 3/6] Erase conda env cache --- .github/workflows/ubuntu.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index aee8d11..99ae922 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -9,7 +9,7 @@ on: - main env: - CACHE_NUMBER: 0 # increase to reset cache manually + CACHE_NUMBER: 1 # increase to reset cache manually jobs: build-linux: From 8fc51d67b4686fd7f56d0ad4dbb1a943bd81f96d Mon Sep 17 00:00:00 2001 From: aidanjungo Date: Tue, 15 Nov 2022 14:50:50 +0100 Subject: [PATCH 4/6] test with python3.8 --- environment.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/environment.yml b/environment.yml index 744a10a..e49d1ea 100644 --- a/environment.yml +++ b/environment.yml @@ -7,7 +7,7 @@ # # See also: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html -name: cpacspy_env +name: cpacspy_env_2 channels: - conda-forge @@ -23,7 +23,7 @@ dependencies: - pandas=1.3.4 - pip=21.2.2 - plotly=5.1.0 - - python=3.7 + - python=3.8 - scipy=1.7.3 - setuptools=58.0.4 - tixi3=3.0.3 From c4d48009b44c24e3f9bc2f05502f84b7eaac7250 Mon Sep 17 00:00:00 2001 From: aidanjungo Date: Tue, 15 Nov 2022 14:53:55 +0100 Subject: [PATCH 5/6] Remove tixi from requierements --- environment.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/environment.yml b/environment.yml index e49d1ea..ff4a9e1 100644 --- a/environment.yml +++ b/environment.yml @@ -23,10 +23,9 @@ dependencies: - pandas=1.3.4 - pip=21.2.2 - plotly=5.1.0 - - python=3.8 + - python=3.7 - scipy=1.7.3 - setuptools=58.0.4 - - tixi3=3.0.3 - wheel=0.37.0 # CI and test From 0d294965ce5405a5ab3eba00e766d7e630261a92 Mon Sep 17 00:00:00 2001 From: aidanjungo Date: Tue, 15 Nov 2022 15:01:49 +0100 Subject: [PATCH 6/6] fix env name --- environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index ff4a9e1..fa76704 100644 --- a/environment.yml +++ b/environment.yml @@ -7,7 +7,7 @@ # # See also: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html -name: cpacspy_env_2 +name: cpacspy_env channels: - conda-forge