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

🩹 Fix Project.import_data path resolving for different script and cwd #1214

Merged
merged 4 commits into from
Jan 8, 2023
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- 🩹 Fix yaml result saving with relative paths (#1199)
- 🩹 Fix model markdown render for items without label (#1213)
- 🩹 Fix wrong file loading due to partial filename matching in Project (#1212)
- 🩹 Fix `Project.import_data` path resolving for different script and cwd (#1214)
<!-- Fix within the 0.7.0 release cycle, therefore hidden:
- 🩹 Fix the matrix provider alignment/reduction ('grouping') issues introduced in #1175 (#1190)
-->
Expand Down
2 changes: 1 addition & 1 deletion glotaran/plugin_system/data_io_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def save_dataset(
by default True
**kwargs : Any
Additional keyword arguments passes to the ``write_dataset`` implementation
of the data io plugin. If you aren't sure about those use ``get_datawriter``
of the data io plugin. If you aren't sure about those use ``get_datasaver``
to get the implementation with the proper help and autocomplete.
"""
protect_from_overwrite(file_name, allow_overwrite=allow_overwrite)
Expand Down
10 changes: 8 additions & 2 deletions glotaran/plugin_system/io_plugin_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from collections.abc import Callable
from functools import partial
from functools import wraps
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import TypeVar
Expand Down Expand Up @@ -94,6 +95,8 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
def protect_from_overwrite(path: str | os.PathLike[str], *, allow_overwrite: bool = False) -> None:
"""Raise FileExistsError if files already exists and allow_overwrite isn't True.

As a side effect this also creates the parent directory of a file if it does not exist.

Parameters
----------
path : str
Expand All @@ -113,11 +116,14 @@ def protect_from_overwrite(path: str | os.PathLike[str], *, allow_overwrite: boo
"If you are absolutely sure this is what you want and need to do you can "
"use the argument 'allow_overwrite=True'."
)
path = Path(path).resolve()
if path.parent.is_file() is False:
path.parent.mkdir(parents=True, exist_ok=True)
if allow_overwrite:
return
elif os.path.isfile(path):
elif path.is_file():
raise FileExistsError(f"The file {path!r} already exists. \n{user_info}")
elif os.path.isdir(path) and os.listdir(str(path)):
elif path.is_dir() and os.listdir(str(path)):
raise FileExistsError(
f"The folder {path!r} already exists and is not empty. \n{user_info}"
)
Expand Down
5 changes: 3 additions & 2 deletions glotaran/plugin_system/test/test_data_io_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@ def test_protect_from_overwrite_write_functions(tmp_path: Path):
save_dataset(xr.DataArray([1, 2]), str(file_path))


@pytest.mark.parametrize("sub_dir", ("", "sub_dir"))
@pytest.mark.usefixtures("mocked_registry")
def test_write_dataset(tmp_path: Path):
def test_write_dataset(tmp_path: Path, sub_dir: str):
"""All args and kwargs are passes correctly."""
file_path = tmp_path / "dummy.mock"
file_path = tmp_path / sub_dir / "dummy.mock"

result: dict[str, Any] = {}
save_dataset(
Expand Down
5 changes: 3 additions & 2 deletions glotaran/plugin_system/test/test_project_io_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def test_load_functions(tmp_path: Path, load_function: Callable[..., Any]):
assert result.source_path == Path(file_path).as_posix()


@pytest.mark.parametrize("sub_dir", ("", "sub_dir"))
@pytest.mark.parametrize(
"save_function",
(
Expand All @@ -253,10 +254,10 @@ def test_load_functions(tmp_path: Path, load_function: Callable[..., Any]):
@pytest.mark.parametrize("update_source_path", (True, False))
@pytest.mark.usefixtures("mocked_registry")
def test_write_functions(
tmp_path: Path, save_function: Callable[..., Any], update_source_path: bool
tmp_path: Path, save_function: Callable[..., Any], update_source_path: bool, sub_dir: str
):
"""All args and kwargs are passes correctly."""
file_path = tmp_path / "model.mock"
file_path = tmp_path / "sub_dir" / "model.mock"
mock_obj = MockFileLoadable()

save_function(
Expand Down
3 changes: 3 additions & 0 deletions glotaran/project/project_data_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def import_data(
"""
path = Path(path)

if path.is_absolute() is False:
path = (self.directory.parent / path).resolve()

name = name or path.stem
data_path = self.directory / f"{name}.nc"

Expand Down
34 changes: 34 additions & 0 deletions glotaran/project/test/test_project.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import annotations

import sys
from importlib.metadata import distribution
from pathlib import Path
from shutil import rmtree
from textwrap import dedent
from typing import Literal

import pytest
from _pytest.monkeypatch import MonkeyPatch
from _pytest.recwarn import WarningsRecorder
from IPython.core.formatters import format_display_data

Expand All @@ -24,6 +26,7 @@
from glotaran.testing.simulated_data.sequential_spectral_decay import (
PARAMETERS as example_parameter,
)
from glotaran.utils.io import chdir_context


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -345,6 +348,37 @@ def test_generators_allow_overwrite(project_folder: Path, project_file: Path):
assert len(list(filter(lambda p: p.label.startswith("rates"), parameters.all()))) == 3


def test_import_data_relative_paths_script_folder_not_cwd(
tmp_path: Path, monkeypatch: MonkeyPatch
):
"""Import data using relative paths where cwd and script folder differ."""
script_folder = tmp_path / "project"
script_folder.mkdir(parents=True, exist_ok=True)
with chdir_context(tmp_path), monkeypatch.context() as m:
# Pretend the currently running script is located at ``script_folder / "script.py"``
m.setattr(
sys.modules[globals()["__name__"]],
"__file__",
(script_folder / "script.py").as_posix(),
)
cwd_data_import_path = "import_data.nc"
save_dataset(example_dataset, cwd_data_import_path)
assert (tmp_path / cwd_data_import_path).is_file() is True

script_folder_data_import_path = "original/import_data.nc"
save_dataset(example_dataset, script_folder / script_folder_data_import_path)
assert (script_folder / script_folder_data_import_path).is_file() is True

project = Project.open("project.gta")
assert (script_folder / "project.gta").is_file() is True

project.import_data(f"../{cwd_data_import_path}", name="dataset_1")
assert (script_folder / "data/dataset_1.nc").is_file() is True

project.import_data(script_folder_data_import_path, name="dataset_2")
assert (script_folder / "data/dataset_2.nc").is_file() is True


def test_missing_file_errors(tmp_path: Path, project_folder: Path):
"""Error when accessing non existing files."""
with pytest.raises(FileNotFoundError) as exc_info:
Expand Down