Skip to content

Commit a599d44

Browse files
authored
Allow relative paths for custom file (#198)
1 parent 048c4e2 commit a599d44

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed

ratapi/models.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,7 @@ class CustomFile(RATModel):
313313
filename: str = ""
314314
function_name: str = ""
315315
language: Languages = Languages.Python
316-
path: pathlib.Path = pathlib.Path(".").resolve()
317-
318-
@field_validator("path")
319-
@classmethod
320-
def resolve_relative_paths(cls, path: pathlib.Path) -> pathlib.Path:
321-
"""Return the absolute path of the given path."""
322-
return path.resolve()
316+
path: pathlib.Path = pathlib.Path(".")
323317

324318
def model_post_init(self, __context: Any) -> None:
325319
"""Autogenerate the function name from the filename if not set.

ratapi/project.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,12 +956,16 @@ def make_data_dict(item):
956956
elif field == "custom_files":
957957

958958
def make_custom_file_dict(item):
959-
return {
959+
file_dict = {
960960
"name": item.name,
961961
"filename": item.filename,
962962
"language": item.language,
963963
"path": try_relative_to(item.path, filepath.parent),
964964
}
965+
if item.name != item.function_name:
966+
file_dict["function_name"] = item.function_name
967+
968+
return file_dict
965969

966970
json_dict["custom_files"] = [make_custom_file_dict(file) for file in attr]
967971

@@ -1062,7 +1066,9 @@ def try_relative_to(path: Path, relative_to: Path) -> str:
10621066
"""
10631067
path = Path(path)
10641068
relative_to = Path(relative_to)
1065-
if path.is_relative_to(relative_to):
1069+
if not path.is_absolute():
1070+
return str(path)
1071+
elif path.is_relative_to(relative_to):
10661072
return str(path.relative_to(relative_to))
10671073
else:
10681074
warnings.warn(

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8537,3 +8537,11 @@ def absorption():
85378537
"""The project from the absorption example."""
85388538
project, _ = ratapi.examples.absorption()
85398539
return project
8540+
8541+
8542+
@pytest.fixture
8543+
def absorption_different_function():
8544+
"""The project from the absorption example with a function name different from filename."""
8545+
project, _ = ratapi.examples.absorption()
8546+
project.custom_files[0].function_name = "test_func"
8547+
return project

tests/test_models.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Test the pydantic models."""
22

3-
import pathlib
43
import re
54
from collections.abc import Callable
65

@@ -101,11 +100,11 @@ def test_initialise_with_extra_fields(self, model: Callable, model_params: dict)
101100
model(new_field=1, **model_params)
102101

103102

104-
def test_custom_file_path_is_absolute() -> None:
105-
"""If we use provide a relative path to the custom file model, it should be converted to an absolute path."""
106-
relative_path = pathlib.Path("./relative_path")
107-
custom_file = ratapi.models.CustomFile(path=relative_path)
108-
assert custom_file.path.is_absolute()
103+
# def test_custom_file_path_is_absolute() -> None:
104+
# """If we use provide a relative path to the custom file model, it should be converted to an absolute path."""
105+
# relative_path = pathlib.Path("./relative_path")
106+
# custom_file = ratapi.models.CustomFile(path=relative_path)
107+
# assert custom_file.path.is_absolute()
109108

110109

111110
def test_data_eq() -> None:

tests/test_project.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,7 @@ def test_wrap_extend(test_project, class_list: str, model_type: str, field: str,
16161616
"domains_custom_layers",
16171617
"domains_custom_xy",
16181618
"absorption",
1619+
"absorption_different_function",
16191620
],
16201621
)
16211622
def test_save_load(project, request):
@@ -1637,24 +1638,25 @@ def test_save_load(project, request):
16371638
def test_relative_paths():
16381639
"""Test that ``try_relative_to`` correctly creates relative paths to subfolders."""
16391640

1640-
with tempfile.TemporaryDirectory() as tmp:
1641-
data_path = Path(tmp, "data/myfile.dat")
1641+
cur_path = Path(".").resolve()
1642+
data_path = cur_path / "data/myfile.dat"
1643+
assert Path(ratapi.project.try_relative_to(data_path, cur_path)) == Path("data/myfile.dat")
16421644

1643-
assert Path(ratapi.project.try_relative_to(data_path, tmp)) == Path("data/myfile.dat")
1645+
# relative path will be left relative.
1646+
data_path = "data/myfile.dat"
1647+
assert Path(ratapi.project.try_relative_to(data_path, cur_path)) == Path("data/myfile.dat")
16441648

16451649

16461650
def test_relative_paths_warning():
16471651
"""Test that we get a warning for trying to walk up paths."""
16481652

1649-
data_path = "/tmp/project/data/mydata.dat"
1650-
relative_path = "/tmp/project/project_path/myproj.dat"
1653+
cur_path = Path(".").resolve()
1654+
data_path = cur_path / "tmp/project/data/mydata.dat"
1655+
relative_path = cur_path / "tmp/project/project_path/myproj.dat"
16511656

16521657
with pytest.warns(
16531658
match="Could not write custom file path as relative to the project directory, "
16541659
"which means that it may not work on other devices. If you would like to share your project, "
16551660
"make sure your custom files are in a subfolder of the project save location.",
16561661
):
1657-
assert (
1658-
Path(ratapi.project.try_relative_to(data_path, relative_path))
1659-
== Path("/tmp/project/data/mydata.dat").resolve()
1660-
)
1662+
assert Path(ratapi.project.try_relative_to(data_path, relative_path)) == data_path

0 commit comments

Comments
 (0)