Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace os by pathlib and adapt some tests #23

Merged
merged 6 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ dependencies:
- python=3.7
- scipy=1.7.3
- setuptools=58.0.4
- tixi3=3.0.3
- wheel=0.37.0

# CI and test
Expand Down
27 changes: 13 additions & 14 deletions src/cpacspy/cpacspy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

"""

import os
from pathlib import Path

import numpy as np
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):

Expand Down
4 changes: 2 additions & 2 deletions src/cpacspy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions tests/test_aeromap.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

"""

import os
import numpy as np
from pathlib import Path

Expand Down Expand Up @@ -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():
Expand Down
45 changes: 24 additions & 21 deletions tests/test_cpacspy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

"""

import os
from pathlib import Path
import pytest

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()