zimscraperlib.zim.filesystem
zimwriterfs-like tools to convert a build folder into a ZIM
make_zim_file behaves in a similar way to zimwriterfs and expects the same options:
- Guesses file mime-type from filenames
- Add all files to respective namespaces based on mime type
- Add redirects from a zimwriterfs-compatible redirects TSV
- Adds common metadata
Also included: - Add redirect from a list of (source, destination, title) strings
Note: due to the lack of a cancel() method in the libzim itself, it is not possible to stop a zim creation process. Should an error occur in your code, a Zim file with up-to-that-moment content will be created at destination.
To prevent this (creating an unwanted Zim file) from happening, a workaround is in place. It prevents the libzim from finishing its process. While it results in no Zim file being created, it results in the zim temp folder to be left on disk and very frequently leads to a segmentation fault at garbage collection (on exit mostly).
Meaning you should exit right after an exception in your code (during zim creation) Use workaround_nocancel=False to disable the workaround.
Classes:
-
FileItem–libzim.writer.Article reflecting a local file within a root folder
-
IncorrectFilenameError–Exception raised when filename is not creatable
-
IncorrectPathError–A generic exception for any problem encountered while working with filepaths
-
MissingFolderError–Exception raised when folder does not exist
-
NotADirectoryFolderError–Exception raised when folder is not a directory
-
NotWritableFolderError–Exception raised when folder is not writable
Functions:
-
add_redirects_to_zim–add redirects from list of source/target or redirects file to zim
-
add_to_zim–recursively add a path to a zim file
-
make_zim_file–Creates a zimwriterfs-like ZIM file at {fpath} from {build_dir}
-
validate_file_creatable–Validate that a file can be created in given folder with given filename
-
validate_folder_writable–Validate that a file can be created in a given folder.
FileItem
Bases: StaticItem
libzim.writer.Article reflecting a local file within a root folder
Methods:
Attributes:
-
filepath– -
get_indexdata(Callable[[], IndexData]) – -
mimetype– -
root– -
should_index– -
title–
Source code in src/zimscraperlib/zim/filesystem.py
44 45 46 47 48 49 50 51 52 53 54 55 56 | |
filepath
instance-attribute
filepath = filepath
root
instance-attribute
root = root
should_index
property
should_index
title
instance-attribute
title = title
get_contentprovider
get_contentprovider() -> ContentProvider
Source code in src/zimscraperlib/zim/items.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
get_hints
Source code in src/zimscraperlib/zim/items.py
60 61 | |
get_mimetype
get_mimetype() -> str
Source code in src/zimscraperlib/zim/items.py
57 58 | |
get_path
get_path() -> str
Source code in src/zimscraperlib/zim/filesystem.py
58 59 | |
get_title
get_title() -> str
Source code in src/zimscraperlib/zim/filesystem.py
61 62 | |
IncorrectFilenameError
Bases: IncorrectPathError
Exception raised when filename is not creatable
This usually occurs when bad characters are present in filename (typically characters not supported on current filesystem).
IncorrectPathError
Bases: Exception
A generic exception for any problem encountered while working with filepaths
MissingFolderError
Bases: IncorrectPathError
Exception raised when folder does not exist
NotADirectoryFolderError
Bases: IncorrectPathError
Exception raised when folder is not a directory
NotWritableFolderError
Bases: IncorrectPathError
Exception raised when folder is not writable
add_redirects_to_zim
add_redirects_to_zim(
zim_file: Creator,
redirects: Sequence[tuple[str, str, str | None]]
| None = None,
redirects_file: Path | None = None,
)
add redirects from list of source/target or redirects file to zim
Source code in src/zimscraperlib/zim/filesystem.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
add_to_zim
recursively add a path to a zim file
root
main folder containing all content
zim_file: zim Creator fpath: path to the file/folder to add to the ZIM rewrite_links: whether HTML and CSS files should have their links fixed for namespaces
Source code in src/zimscraperlib/zim/filesystem.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
make_zim_file
make_zim_file(
*,
build_dir: Path,
fpath: Path,
name: str,
main_page: str,
illustration: str,
title: str,
description: str,
date: date | None = None,
language: str = "eng",
creator: str = "-",
publisher: str = "-",
tags: Sequence[str] | None = None,
source: str | None = None,
flavour: str | None = None,
scraper: str | None = None,
long_description: str | None = None,
without_fulltext_index: bool = False,
redirects: Sequence[tuple[str, str, str]] | None = None,
redirects_file: Path | None = None,
rewrite_links: bool = True,
workaround_nocancel: bool = True,
ignore_duplicates: bool = True,
disable_metadata_checks: bool = False,
)
Creates a zimwriterfs-like ZIM file at {fpath} from {build_dir}
main_page: path of item to serve as main page illustration: relative path to illustration file in build_dir tags: list of str tags to add to meta redirects: list of (src, dst, title) tuple to create redirects from rewrite_links controls whether to rewrite HTML/CSS content -> add namespaces to relative links workaround_nocancel: disable workaround to prevent ZIM creation on error
Source code in src/zimscraperlib/zim/filesystem.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
validate_file_creatable
Validate that a file can be created in given folder with given filename
Any problem encountered raises an exception inheriting from IncorrectPathError
Checks that:
- folder is writable (or raise exception from validate_folder_writable)
- file can be created (or raise IncorrectFilenameError exception with
inner exception details)
Source code in src/zimscraperlib/zim/filesystem.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
validate_folder_writable
validate_folder_writable(folder: Path)
Validate that a file can be created in a given folder.
Any problem encountered raises an exception inheriting from IncorrectPathError
Checks that: - folder passed exists (or raise MissingFolderError exception) - folder passed is a directory (or raise NotADirectoryFolderError exception) - folder is writable, i.e. it is possible to create a file in folder (or raise NotWritableFolderError exception with inner exception details)
Source code in src/zimscraperlib/zim/filesystem.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |