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

feat: handle list of filenames in absolute_path and file_mtime #89

Merged
merged 1 commit into from
Apr 17, 2024
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
46 changes: 34 additions & 12 deletions src/safeds_runner/server/_pipeline_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,41 +393,63 @@ def memoized_dynamic_call(
)


def file_mtime(filename: str) -> int | None:
@typing.overload
def file_mtime(filenames: str) -> int | None: ...


@typing.overload
def file_mtime(filenames: list[str]) -> list[int | None]: ...


def file_mtime(filenames: str | list[str]) -> int | None | list[int | None]:
"""
Get the last modification timestamp of the provided file.

Parameters
----------
filename: str
Name of the file
filenames:
Names of the files

Returns
-------
int | None
Last modification timestamp if the provided file exists, otherwise None
timestamps:
Last modification timestamp or None for each provided file, depending on whether the file exists or not.
"""
if isinstance(filenames, list):
return [file_mtime(f) for f in filenames]

try:
return Path(filename).stat().st_mtime_ns
return Path(filenames).stat().st_mtime_ns
except FileNotFoundError:
return None


def absolute_path(filename: str) -> str:
@typing.overload
def absolute_path(filenames: str) -> str: ...


@typing.overload
def absolute_path(filenames: list[str]) -> list[str]: ...


def absolute_path(filenames: str | list[str]) -> str | list[str]:
"""
Get the absolute path of the provided file.

Parameters
----------
filename:
Name of the file
filenames:
Names of the files.

Returns
-------
absolute_path:
Absolute path of the provided file
absolute_paths:
Absolute paths of the provided files.
"""
return str(Path(filename).resolve())
if isinstance(filenames, list):
return [absolute_path(f) for f in filenames]

return str(Path(filenames).resolve())


def get_backtrace_info(error: BaseException) -> list[dict[str, Any]]:
Expand Down
12 changes: 12 additions & 0 deletions tests/safeds_runner/server/test_memoization.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ def test_file_mtime_exists() -> None:
assert mtime is not None


def test_file_mtime_exists_list() -> None:
with tempfile.NamedTemporaryFile() as file:
mtime = file_mtime([file.name, file.name])
assert isinstance(mtime, list)
assert all(it is not None for it in mtime)


def test_file_mtime_not_exists() -> None:
mtime = file_mtime(f"file_not_exists.{datetime.now(tz=UTC).timestamp()}")
assert mtime is None
Expand All @@ -282,6 +289,11 @@ def test_absolute_path() -> None:
assert Path(result).is_absolute()


def test_absolute_path_list() -> None:
result = absolute_path(["table.csv"])
assert all(Path(it).is_absolute() for it in result)


@pytest.mark.parametrize(
argnames="cache,greater_than_zero",
argvalues=[(MemoizationMap({}, {}), False), (MemoizationMap({}, {"a": MemoizationStats([], [], [], [20])}), True)],
Expand Down