Skip to content

Commit

Permalink
Ignore silently if file is already gone in delete callback
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Jan 6, 2025
1 parent db251ee commit d97df06
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/zimscraperlib/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Shortcuts to retrieve mime type using magic"""

import os
import pathlib
from contextlib import contextmanager
from tempfile import TemporaryDirectory
Expand Down Expand Up @@ -40,10 +39,9 @@ def get_content_mimetype(content: bytes | str) -> str:
return MIME_OVERRIDES.get(detected_mime, detected_mime)


def delete_callback(fpath: str | pathlib.Path):
def delete_callback(fpath: pathlib.Path):
"""helper deleting passed filepath"""

os.unlink(fpath)
fpath.unlink(missing_ok=True)


@contextmanager
Expand Down
6 changes: 6 additions & 0 deletions tests/filesystem/test_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def test_delete_callback(tmp_path: pathlib.Path):

assert not fpath.exists()

# file already gone should not be a problem
delete_callback(fpath)

# wrong path should not be a problem
delete_callback(pathlib.Path("/foo.txt"))


def test_path_from_tmp_dir():
tempdir = TemporaryDirectory()
Expand Down

0 comments on commit d97df06

Please sign in to comment.