Skip to content

Commit 7b41b04

Browse files
authored
Merge pull request #210 from elfkuzco/rename-validate-zimfile-creatable
rename validate_zimfile_creatable to validate_file_creatable
2 parents ea6505f + baa3e9d commit 7b41b04

File tree

4 files changed

+74
-56
lines changed

4 files changed

+74
-56
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
### Breaking Changes
11+
12+
- Renamed `filesystem.validate_zimfile_creatable` to `filesystem.file_creatable` to reflect general applicability to check file creation beyond ZIM files #200
13+
- Remove any "ZIM" reference in exceptions while working with files #200
14+
15+
### Added
16+
17+
- Add `filesystem.validate_folder_writable` to check if a folder can be written to #200
18+
1019
## [4.0.0] - 2024-08-05
1120

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

4049
### Removed
50+
4151
- **BREAKING** Remove translation features in `i18n`: `Locale` class + `_` and `setlocale` functions #134
4252

4353
### Fixed

src/zimscraperlib/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "4.0.1-dev0"
1+
__version__ = "5.0.0-dev0"

src/zimscraperlib/zim/filesystem.py

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -219,33 +219,33 @@ def make_zim_file(
219219
zim_file.finish()
220220

221221

222-
class IncorrectZIMPathError(Exception):
223-
"""A generic exception for any problem encountered in validate_zimfile_creatable"""
222+
class IncorrectPathError(Exception):
223+
"""A generic exception for any problem encountered while working with filepaths"""
224224

225225
pass
226226

227227

228-
class MissingZIMFolderError(IncorrectZIMPathError):
229-
"""Exception raised in validate_zimfile_creatable when folder does not exists"""
228+
class MissingFolderError(IncorrectPathError):
229+
"""Exception raised when folder does not exist"""
230230

231231
pass
232232

233233

234-
class NotADirectoryZIMFolderError(IncorrectZIMPathError):
235-
"""Exception raised in validate_zimfile_creatable when folder is not a directory"""
234+
class NotADirectoryFolderError(IncorrectPathError):
235+
"""Exception raised when folder is not a directory"""
236236

237237
pass
238238

239239

240-
class NotWritableZIMFolderError(IncorrectZIMPathError):
241-
"""Exception raised in validate_zimfile_creatable when folder is not writable"""
240+
class NotWritableFolderError(IncorrectPathError):
241+
"""Exception raised when folder is not writable"""
242242

243243
pass
244244

245245

246-
class IncorrectZIMFilenameError(IncorrectZIMPathError):
246+
class IncorrectFilenameError(IncorrectPathError):
247247
"""
248-
Exception raised in validate_zimfile_creatable when filename is not creatable
248+
Exception raised when filename is not creatable
249249
250250
This usually occurs when bad characters are present in filename (typically
251251
characters not supported on current filesystem).
@@ -254,32 +254,24 @@ class IncorrectZIMFilenameError(IncorrectZIMPathError):
254254
pass
255255

256256

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
257+
def validate_folder_writable(folder: pathlib.Path):
258+
"""Validate that a file can be created in a given folder.
259259
260-
Any problem encountered raises an exception inheriting from IncorrectZIMPathError
260+
Any problem encountered raises an exception inheriting from IncorrectPathError
261261
262262
Checks that:
263-
- folder passed exists (or raise MissingZIMFolderError exception)
264-
- folder passed is a directory (or raise NotADirectoryZIMFolderError exception)
263+
- folder passed exists (or raise MissingFolderError exception)
264+
- folder passed is a directory (or raise NotADirectoryFolderError exception)
265265
- folder is writable, i.e. it is possible to create a file in folder (or raise
266-
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)
266+
NotWritableFolderError exception with inner exception details)
269267
"""
270-
folder = pathlib.Path(folder)
271-
272268
# ensure folder exists
273269
if not folder.exists():
274-
raise MissingZIMFolderError(
275-
f"Folder to create the ZIM does not exist: {folder}"
276-
)
270+
raise MissingFolderError(f"Folder does not exist: {folder}")
277271

278272
# ensure folder is a directory
279273
if not folder.is_dir():
280-
raise NotADirectoryZIMFolderError(
281-
f"Folder to create the ZIM is not a directory: {folder}"
282-
)
274+
raise NotADirectoryFolderError(f"Folder is not a directory: {folder}")
283275

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

@@ -288,17 +280,28 @@ def validate_zimfile_creatable(folder: str | pathlib.Path, filename: str):
288280
with tempfile.NamedTemporaryFile(dir=folder, delete=True) as fh:
289281
logger.debug(f"Output is writable. Temporary file used for test: {fh.name}")
290282
except Exception as exc:
291-
raise NotWritableZIMFolderError(
292-
f"Folder to create the ZIM is not writable: {folder}"
293-
) from exc
283+
raise NotWritableFolderError(f"Folder is not writable: {folder}") from exc
284+
285+
286+
def validate_file_creatable(folder: str | pathlib.Path, filename: str):
287+
"""Validate that a file can be created in given folder with given filename
288+
289+
Any problem encountered raises an exception inheriting from IncorrectPathError
290+
291+
Checks that:
292+
- folder is writable (or raise exception from `validate_folder_writable`)
293+
- file can be created (or raise IncorrectFilenameError exception with
294+
inner exception details)
295+
"""
296+
folder = pathlib.Path(folder)
297+
298+
validate_folder_writable(folder)
294299

295-
# ensure ZIM file is creatable with the given name
300+
# ensure file is creatable with the given name
296301
fpath = folder / filename
297302
try:
298-
logger.debug(f"Confirming ZIM file can be created at {fpath}")
303+
logger.debug(f"Confirming file can be created at {fpath}")
299304
fpath.touch()
300305
fpath.unlink()
301306
except Exception as exc:
302-
raise IncorrectZIMFilenameError(
303-
f"ZIM filename is not creatable: {fpath}"
304-
) from exc
307+
raise IncorrectFilenameError(f"File is not creatable: {fpath}") from exc

tests/zim/test_fs.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
from zimscraperlib.zim.archive import Archive
1212
from zimscraperlib.zim.filesystem import (
1313
FileItem,
14-
IncorrectZIMFilenameError,
15-
MissingZIMFolderError,
16-
NotADirectoryZIMFolderError,
17-
NotWritableZIMFolderError,
14+
IncorrectFilenameError,
15+
MissingFolderError,
16+
NotADirectoryFolderError,
17+
NotWritableFolderError,
1818
make_zim_file,
19-
validate_zimfile_creatable,
19+
validate_file_creatable,
20+
validate_folder_writable,
2021
)
2122

2223

@@ -171,32 +172,36 @@ def valid_zim_filename():
171172
return "myfile.zim"
172173

173174

174-
def test_validate_zimfile_creatable_ok(tmp_path, valid_zim_filename):
175+
def test_validate_folder_writable_not_exists(tmp_path):
175176

176-
validate_zimfile_creatable(tmp_path, valid_zim_filename)
177+
with pytest.raises(MissingFolderError):
178+
validate_folder_writable(tmp_path / "foo")
177179

178180

179-
def test_validate_zimfile_creatable_folder_not_exists(tmp_path, valid_zim_filename):
181+
def test_validate_folder_writable_not_dir(tmp_path):
180182

181-
with pytest.raises(MissingZIMFolderError):
182-
validate_zimfile_creatable(tmp_path / "foo", valid_zim_filename)
183+
with pytest.raises(NotADirectoryFolderError):
184+
(tmp_path / "foo.txt").touch()
185+
validate_folder_writable(tmp_path / "foo.txt")
183186

184187

185-
def test_validate_zimfile_creatable_bad_folder(tmp_path, valid_zim_filename):
188+
def test_validate_folder_writable_not_writable(tmp_path):
186189

187-
with pytest.raises(NotADirectoryZIMFolderError):
188-
(tmp_path / "foo.txt").touch()
189-
validate_zimfile_creatable(tmp_path / "foo.txt", valid_zim_filename)
190+
with pytest.raises(NotWritableFolderError):
191+
(tmp_path / "foo").mkdir(mode=111)
192+
validate_folder_writable(tmp_path / "foo")
190193

191194

192-
def test_validate_zimfile_creatable_folder_not_writable(tmp_path, valid_zim_filename):
195+
def test_validate_folder_writable_ok(tmp_path):
196+
validate_folder_writable(tmp_path)
193197

194-
with pytest.raises(NotWritableZIMFolderError):
195-
(tmp_path / "foo").mkdir(mode=111)
196-
validate_zimfile_creatable(tmp_path / "foo", valid_zim_filename)
198+
199+
def test_validate_file_creatable_ok(tmp_path, valid_zim_filename):
200+
201+
validate_file_creatable(tmp_path, valid_zim_filename)
197202

198203

199-
def test_validate_zimfile_creatable_bad_name(tmp_path):
204+
def test_validate_file_creatable_bad_name(tmp_path):
200205

201-
with pytest.raises(IncorrectZIMFilenameError):
202-
validate_zimfile_creatable(tmp_path, "t\0t\0.zim")
206+
with pytest.raises(IncorrectFilenameError):
207+
validate_file_creatable(tmp_path, "t\0t\0.zim")

0 commit comments

Comments
 (0)