Skip to content

Commit ddbf524

Browse files
committed
rename validate_zimfile_creatable to validate_file_creatable
1 parent ea6505f commit ddbf524

File tree

3 files changed

+92
-20
lines changed

3 files changed

+92
-20
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- **BREAKING** Renamed `filesystem.validate_zimfile_creatable` to `filesystem.file_creatable` to reflect general applicability to check file creation beyond ZIM files #200
13+
14+
### Added
15+
16+
- Add `filesystem.validate_folder_writable` to check if a folder can be written to #200
17+
1018
## [4.0.0] - 2024-08-05
1119

1220
### Added
@@ -38,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3846
- **BREAKING** Rename `i18.NotFound` to `i18n.NotFoundError`
3947

4048
### Removed
49+
4150
- **BREAKING** Remove translation features in `i18n`: `Locale` class + `_` and `setlocale` functions #134
4251

4352
### Fixed

src/zimscraperlib/zim/filesystem.py

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import pathlib
3333
import re
3434
import tempfile
35+
import warnings
3536
from collections.abc import Sequence
3637

3738
from zimscraperlib import logger
@@ -254,8 +255,8 @@ class IncorrectZIMFilenameError(IncorrectZIMPathError):
254255
pass
255256

256257

257-
def validate_zimfile_creatable(folder: str | pathlib.Path, filename: str):
258-
"""Validate that a ZIM can be created in given folder with given filename
258+
def validate_folder_writable(folder: pathlib.Path):
259+
"""Validate that a file can be created in a given folder.
259260
260261
Any problem encountered raises an exception inheriting from IncorrectZIMPathError
261262
@@ -264,22 +265,14 @@ def validate_zimfile_creatable(folder: str | pathlib.Path, filename: str):
264265
- folder passed is a directory (or raise NotADirectoryZIMFolderError exception)
265266
- folder is writable, i.e. it is possible to create a file in folder (or raise
266267
NotWritableZIMFolderError exception with inner exception details)
267-
- filename is creatable, i.e. there is no bad characters in filename (or raise
268-
IncorrectZIMFilenameError exception with inner exception details)
269268
"""
270-
folder = pathlib.Path(folder)
271-
272269
# ensure folder exists
273270
if not folder.exists():
274-
raise MissingZIMFolderError(
275-
f"Folder to create the ZIM does not exist: {folder}"
276-
)
271+
raise MissingZIMFolderError(f"Folder does not exist: {folder}")
277272

278273
# ensure folder is a directory
279274
if not folder.is_dir():
280-
raise NotADirectoryZIMFolderError(
281-
f"Folder to create the ZIM is not a directory: {folder}"
282-
)
275+
raise NotADirectoryZIMFolderError(f"Folder is not a directory: {folder}")
283276

284277
logger.debug(f"Attempting to confirm output is writable in directory {folder}")
285278

@@ -288,17 +281,50 @@ def validate_zimfile_creatable(folder: str | pathlib.Path, filename: str):
288281
with tempfile.NamedTemporaryFile(dir=folder, delete=True) as fh:
289282
logger.debug(f"Output is writable. Temporary file used for test: {fh.name}")
290283
except Exception as exc:
291-
raise NotWritableZIMFolderError(
292-
f"Folder to create the ZIM is not writable: {folder}"
293-
) from exc
284+
raise NotWritableZIMFolderError(f"Folder is not writable: {folder}") from exc
285+
286+
287+
def validate_file_creatable(folder: str | pathlib.Path, filename: str):
288+
"""Validate that a file can be created in given folder with given filename
294289
295-
# ensure ZIM file is creatable with the given name
290+
Any problem encountered raises an exception inheriting from IncorrectZIMPathError
291+
292+
Checks that:
293+
- folder is writable (or raise exception from `validate_folder_writable`)
294+
- file can be created (or raise IncorrectZIMFilenameError exception with
295+
inner exception details)
296+
"""
297+
folder = pathlib.Path(folder)
298+
299+
validate_folder_writable(folder)
300+
301+
# ensure file is creatable with the given name
296302
fpath = folder / filename
297303
try:
298-
logger.debug(f"Confirming ZIM file can be created at {fpath}")
304+
logger.debug(f"Confirming file can be created at {fpath}")
299305
fpath.touch()
300306
fpath.unlink()
301307
except Exception as exc:
302-
raise IncorrectZIMFilenameError(
303-
f"ZIM filename is not creatable: {fpath}"
304-
) from exc
308+
raise IncorrectZIMFilenameError(f"File is not creatable: {fpath}") from exc
309+
310+
311+
def validate_zimfile_creatable(folder: str | pathlib.Path, filename: str):
312+
"""Validate that a ZIM can be created in given folder with given filename
313+
314+
Any problem encountered raises an exception inheriting from IncorrectZIMPathError
315+
316+
Checks that:
317+
- folder passed exists (or raise MissingZIMFolderError exception)
318+
- folder passed is a directory (or raise NotADirectoryZIMFolderError exception)
319+
- folder is writable, i.e. it is possible to create a file in folder (or raise
320+
NotWritableZIMFolderError exception with inner exception details)
321+
- filename is creatable, i.e. there is no bad characters in filename (or raise
322+
IncorrectZIMFilenameError exception with inner exception details)
323+
"""
324+
warnings.warn(
325+
"'validate_zimfile_creatable' is deprecated and will be removed. "
326+
"Use 'validate_file_creatable` to ensure future compatibility.",
327+
DeprecationWarning,
328+
stacklevel=2,
329+
)
330+
validate_file_creatable(folder, filename)

tests/zim/test_fs.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
NotADirectoryZIMFolderError,
1717
NotWritableZIMFolderError,
1818
make_zim_file,
19+
validate_file_creatable,
20+
validate_folder_writable,
1921
validate_zimfile_creatable,
2022
)
2123

@@ -200,3 +202,38 @@ def test_validate_zimfile_creatable_bad_name(tmp_path):
200202

201203
with pytest.raises(IncorrectZIMFilenameError):
202204
validate_zimfile_creatable(tmp_path, "t\0t\0.zim")
205+
206+
207+
def test_validate_folder_writable_not_exists(tmp_path):
208+
209+
with pytest.raises(MissingZIMFolderError):
210+
validate_folder_writable(tmp_path / "foo")
211+
212+
213+
def test_validate_folder_writable_not_dir(tmp_path):
214+
215+
with pytest.raises(NotADirectoryZIMFolderError):
216+
(tmp_path / "foo.txt").touch()
217+
validate_folder_writable(tmp_path / "foo.txt")
218+
219+
220+
def test_validate_folder_writable_not_writable(tmp_path):
221+
222+
with pytest.raises(NotWritableZIMFolderError):
223+
(tmp_path / "foo").mkdir(mode=111)
224+
validate_folder_writable(tmp_path / "foo")
225+
226+
227+
def test_validate_folder_writable_ok(tmp_path):
228+
validate_folder_writable(tmp_path)
229+
230+
231+
def test_validate_file_creatable_ok(tmp_path, valid_zim_filename):
232+
233+
validate_file_creatable(tmp_path, valid_zim_filename)
234+
235+
236+
def test_validate_file_creatable_bad_name(tmp_path):
237+
238+
with pytest.raises(IncorrectZIMFilenameError):
239+
validate_file_creatable(tmp_path, "t\0t\0.zim")

0 commit comments

Comments
 (0)