|
31 | 31 | import datetime |
32 | 32 | import pathlib |
33 | 33 | import re |
| 34 | +import tempfile |
34 | 35 | from collections.abc import Sequence |
35 | 36 |
|
36 | 37 | from zimscraperlib import logger |
@@ -210,3 +211,86 @@ def make_zim_file( |
210 | 211 | raise |
211 | 212 | finally: |
212 | 213 | zim_file.finish() |
| 214 | + |
| 215 | + |
| 216 | +class BadZimfileDataError(Exception): |
| 217 | + """A generic exception for any problem encountered in validate_zimfile_creatable""" |
| 218 | + |
| 219 | + pass |
| 220 | + |
| 221 | + |
| 222 | +class ZimFolderNotFoundError(BadZimfileDataError): |
| 223 | + """Exception raised in validate_zimfile_creatable when folder does not exists""" |
| 224 | + |
| 225 | + pass |
| 226 | + |
| 227 | + |
| 228 | +class BadZimFolderError(BadZimfileDataError): |
| 229 | + """Exception raised in validate_zimfile_creatable when folder is not a directory""" |
| 230 | + |
| 231 | + pass |
| 232 | + |
| 233 | + |
| 234 | +class ZimFolderNotWritableError(BadZimfileDataError): |
| 235 | + """Exception raised in validate_zimfile_creatable when folder is not writable""" |
| 236 | + |
| 237 | + pass |
| 238 | + |
| 239 | + |
| 240 | +class BadZimFilenameError(BadZimfileDataError): |
| 241 | + """ |
| 242 | + Exception raised in validate_zimfile_creatable when filename is not creatable |
| 243 | +
|
| 244 | + This usually occurs when bad characters are present in filename (typically |
| 245 | + characters not supported on current filesystem). |
| 246 | + """ |
| 247 | + |
| 248 | + pass |
| 249 | + |
| 250 | + |
| 251 | +def validate_zimfile_creatable(folder: str | pathlib.Path, filename: str): |
| 252 | + """Validate that a ZIM can be created in given folder with given filename |
| 253 | +
|
| 254 | + Any problem encountered raises an exception inheriting from BadZimfileDataError |
| 255 | +
|
| 256 | + Checks that: |
| 257 | + - folder passed exists (or raise ZimFolderNotFoundError exception) |
| 258 | + - folder passed is a directory (or raise BadZimFolderError exception) |
| 259 | + - folder is writable, i.e. it is possible to create a file in folder (or raise |
| 260 | + ZimFolderNotWritableError exception with inner exception details) |
| 261 | + - filename is creatable, i.e. there is no bad characters in filename (or raise |
| 262 | + BadZimFilenameError exception with inner exception details) |
| 263 | + """ |
| 264 | + folder = pathlib.Path(folder) |
| 265 | + |
| 266 | + # ensure folder exists |
| 267 | + if not folder.exists(): |
| 268 | + raise ZimFolderNotFoundError( |
| 269 | + f"Folder to create the ZIM does not exist: {folder}" |
| 270 | + ) |
| 271 | + |
| 272 | + # ensure folder is a directory |
| 273 | + if not folder.is_dir(): |
| 274 | + raise BadZimFolderError( |
| 275 | + f"Folder to create the ZIM is not a directory: {folder}" |
| 276 | + ) |
| 277 | + |
| 278 | + logger.debug(f"Attempting to confirm output is writable in directory {folder}") |
| 279 | + |
| 280 | + try: |
| 281 | + # ensure folder is writable |
| 282 | + with tempfile.NamedTemporaryFile(dir=folder, delete=True) as fh: |
| 283 | + logger.debug(f"Output is writable. Temporary file used for test: {fh.name}") |
| 284 | + except Exception as e: |
| 285 | + raise ZimFolderNotWritableError( |
| 286 | + f"Folder to create the ZIM is not writable: {folder}" |
| 287 | + ) from e |
| 288 | + |
| 289 | + # ensure ZIM file is creatable with the given name |
| 290 | + file_path = folder / filename |
| 291 | + 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 |
0 commit comments