Skip to content

Commit 4f9a652

Browse files
committed
simplified
1 parent db59ec9 commit 4f9a652

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

RATapi/project.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -967,19 +967,16 @@ def try_relative_to(path: Path, relative_to: Path) -> Path:
967967
The relative path if successful, else the absolute path.
968968
969969
"""
970-
# we use the absolute paths to resolve symlinks and so on
971-
abs_path = Path(path).resolve()
972-
abs_base = Path(relative_to).resolve()
973-
974-
try:
975-
relative_path = abs_path.relative_to(abs_base)
976-
except ValueError:
970+
path = Path(path)
971+
relative_to = Path(relative_to)
972+
if path.is_relative_to(relative_to):
973+
return str(path.relative_to(relative_to))
974+
else:
977975
warnings.warn(
978-
"Could not save custom file path as relative to the project directory. "
979-
"To ensure that your project works on other devices, make sure your custom files "
976+
"Could not save custom file path as relative to the project directory, "
977+
"which means that it may not work on other devices."
978+
"If you would like to share your project, make sure your custom files "
980979
"are in a subfolder of the project save location.",
981980
stacklevel=2,
982981
)
983-
return abs_path
984-
985-
return relative_path
982+
return str(path.resolve())

tests/test_project.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,20 +1578,19 @@ def test_relative_paths():
15781578
with tempfile.TemporaryDirectory() as tmp:
15791579
data_path = Path(tmp, "data/myfile.dat")
15801580

1581-
assert RATapi.project.try_relative_to(data_path, tmp) == Path("./data/myfile.dat")
1581+
assert RATapi.project.try_relative_to(data_path, tmp) == "data/myfile.dat"
15821582

15831583

1584-
def test_relative_paths_version():
1584+
def test_relative_paths_warning():
15851585
"""Test that we get a warning for trying to walk up paths."""
15861586

15871587
data_path = "/tmp/project/data/mydata.dat"
15881588
relative_path = "/tmp/project/project_path/myproj.dat"
15891589

15901590
with pytest.warns(
1591-
match="Could not save custom file path as relative to the project directory. "
1592-
"To ensure that your project works on other devices, make sure your custom files "
1593-
"are in a subfolder of the project save location."
1591+
match="Could not save custom file path as relative to the project directory, "
1592+
"which means that it may not work on other devices."
1593+
"If you would like to share your project, make sure your custom files "
1594+
"are in a subfolder of the project save location.",
15941595
):
1595-
assert (
1596-
RATapi.project.try_relative_to(data_path, relative_path) == Path("/tmp/project/data/mydata.dat").resolve()
1597-
)
1596+
assert RATapi.project.try_relative_to(data_path, relative_path) == "/tmp/project/data/mydata.dat"

0 commit comments

Comments
 (0)