Skip to content

Commit 3b1becb

Browse files
committed
Fix naming in zim path validation
1 parent 05984c3 commit 3b1becb

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

src/zimscraperlib/zim/filesystem.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -213,31 +213,31 @@ def make_zim_file(
213213
zim_file.finish()
214214

215215

216-
class BadZimfileDataError(Exception):
216+
class IncorrectZIMPathError(Exception):
217217
"""A generic exception for any problem encountered in validate_zimfile_creatable"""
218218

219219
pass
220220

221221

222-
class ZimFolderNotFoundError(BadZimfileDataError):
222+
class MissingZIMFolderError(IncorrectZIMPathError):
223223
"""Exception raised in validate_zimfile_creatable when folder does not exists"""
224224

225225
pass
226226

227227

228-
class BadZimFolderError(BadZimfileDataError):
228+
class NotADirectoryZIMFolderError(IncorrectZIMPathError):
229229
"""Exception raised in validate_zimfile_creatable when folder is not a directory"""
230230

231231
pass
232232

233233

234-
class ZimFolderNotWritableError(BadZimfileDataError):
234+
class NotWritableZIMFolderError(IncorrectZIMPathError):
235235
"""Exception raised in validate_zimfile_creatable when folder is not writable"""
236236

237237
pass
238238

239239

240-
class BadZimFilenameError(BadZimfileDataError):
240+
class IncorrectZIMFilenameError(IncorrectZIMPathError):
241241
"""
242242
Exception raised in validate_zimfile_creatable when filename is not creatable
243243
@@ -251,27 +251,27 @@ class BadZimFilenameError(BadZimfileDataError):
251251
def validate_zimfile_creatable(folder: str | pathlib.Path, filename: str):
252252
"""Validate that a ZIM can be created in given folder with given filename
253253
254-
Any problem encountered raises an exception inheriting from BadZimfileDataError
254+
Any problem encountered raises an exception inheriting from IncorrectZIMPathError
255255
256256
Checks that:
257-
- folder passed exists (or raise ZimFolderNotFoundError exception)
258-
- folder passed is a directory (or raise BadZimFolderError exception)
257+
- folder passed exists (or raise MissingZIMFolderError exception)
258+
- folder passed is a directory (or raise NotADirectoryZIMFolderError exception)
259259
- folder is writable, i.e. it is possible to create a file in folder (or raise
260-
ZimFolderNotWritableError exception with inner exception details)
260+
NotWritableZIMFolderError exception with inner exception details)
261261
- filename is creatable, i.e. there is no bad characters in filename (or raise
262-
BadZimFilenameError exception with inner exception details)
262+
IncorrectZIMFilenameError exception with inner exception details)
263263
"""
264264
folder = pathlib.Path(folder)
265265

266266
# ensure folder exists
267267
if not folder.exists():
268-
raise ZimFolderNotFoundError(
268+
raise MissingZIMFolderError(
269269
f"Folder to create the ZIM does not exist: {folder}"
270270
)
271271

272272
# ensure folder is a directory
273273
if not folder.is_dir():
274-
raise BadZimFolderError(
274+
raise NotADirectoryZIMFolderError(
275275
f"Folder to create the ZIM is not a directory: {folder}"
276276
)
277277

@@ -281,16 +281,18 @@ def validate_zimfile_creatable(folder: str | pathlib.Path, filename: str):
281281
# ensure folder is writable
282282
with tempfile.NamedTemporaryFile(dir=folder, delete=True) as fh:
283283
logger.debug(f"Output is writable. Temporary file used for test: {fh.name}")
284-
except Exception as e:
285-
raise ZimFolderNotWritableError(
284+
except Exception as exc:
285+
raise NotWritableZIMFolderError(
286286
f"Folder to create the ZIM is not writable: {folder}"
287-
) from e
287+
) from exc
288288

289289
# ensure ZIM file is creatable with the given name
290-
file_path = folder / filename
290+
fpath = folder / filename
291291
try:
292-
logger.debug(f"Confirming ZIM file can be created at {file_path}")
293-
file_path.touch()
294-
file_path.unlink()
295-
except Exception as e:
296-
raise BadZimFilenameError(f"ZIM filename is not creatable: {file_path}") from e
292+
logger.debug(f"Confirming ZIM file can be created at {fpath}")
293+
fpath.touch()
294+
fpath.unlink()
295+
except Exception as exc:
296+
raise IncorrectZIMFilenameError(
297+
f"ZIM filename is not creatable: {fpath}"
298+
) from exc

tests/zim/test_fs.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
from zimscraperlib.zim.archive import Archive
1111
from zimscraperlib.zim.filesystem import (
12-
BadZimFilenameError,
13-
BadZimFolderError,
1412
FileItem,
15-
ZimFolderNotFoundError,
16-
ZimFolderNotWritableError,
13+
IncorrectZIMFilenameError,
14+
MissingZIMFolderError,
15+
NotADirectoryZIMFolderError,
16+
NotWritableZIMFolderError,
1717
make_zim_file,
1818
validate_zimfile_creatable,
1919
)
@@ -165,25 +165,25 @@ def test_validate_zimfile_creatable_ok(tmp_path, valid_zim_filename):
165165

166166
def test_validate_zimfile_creatable_folder_not_exists(tmp_path, valid_zim_filename):
167167

168-
with pytest.raises(ZimFolderNotFoundError):
168+
with pytest.raises(MissingZIMFolderError):
169169
validate_zimfile_creatable(tmp_path / "foo", valid_zim_filename)
170170

171171

172172
def test_validate_zimfile_creatable_bad_folder(tmp_path, valid_zim_filename):
173173

174-
with pytest.raises(BadZimFolderError):
174+
with pytest.raises(NotADirectoryZIMFolderError):
175175
(tmp_path / "foo.txt").touch()
176176
validate_zimfile_creatable(tmp_path / "foo.txt", valid_zim_filename)
177177

178178

179179
def test_validate_zimfile_creatable_folder_not_writable(tmp_path, valid_zim_filename):
180180

181-
with pytest.raises(ZimFolderNotWritableError):
181+
with pytest.raises(NotWritableZIMFolderError):
182182
(tmp_path / "foo").mkdir(mode=111)
183183
validate_zimfile_creatable(tmp_path / "foo", valid_zim_filename)
184184

185185

186186
def test_validate_zimfile_creatable_bad_name(tmp_path):
187187

188-
with pytest.raises(BadZimFilenameError):
188+
with pytest.raises(IncorrectZIMFilenameError):
189189
validate_zimfile_creatable(tmp_path, "t\0t\0.zim")

0 commit comments

Comments
 (0)