Skip to content

Commit

Permalink
Add option to overwrite existing ZIM
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Oct 3, 2024
1 parent 9fa9b7a commit a71de74
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions scraper/src/libretexts2zim/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ def main() -> None:
required=True,
)

parser.add_argument(
"--overwrite",
help="Do not fail if ZIM already exists, overwrite it",
action="store_true",
default=False,
)

# ZIM configuration flags
add_zim_config_flags(parser, zim_defaults())

Expand Down Expand Up @@ -201,6 +208,7 @@ def main() -> None:
output_folder=Path(args.output_folder),
zimui_dist=Path(args.zimui_dist),
content_filter=doc_filter,
overwrite_existing_zim=args.overwrite,
).run()
except SystemExit:
logger.error("Generation failed, exiting")
Expand Down
7 changes: 6 additions & 1 deletion scraper/src/libretexts2zim/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def __init__(
content_filter: ContentFilter,
output_folder: Path,
zimui_dist: Path,
*,
overwrite_existing_zim: bool,
) -> None:
"""Initializes Processor.
Expand All @@ -105,12 +107,15 @@ def __init__(
zim_config: Configuration for ZIM metadata.
content_filter: User supplied filter selecting with content to convert.
output_folder: Directory to write ZIMs into.
zimui_dist: Build directory where Vite placed compiled Vue.JS frontend.
overwrite_existing_zim: Do not fail if ZIM already exists, overwrite it.
"""
self.libretexts_client = libretexts_client
self.zim_config = zim_config
self.doc_filter = content_filter
self.output_folder = output_folder
self.zimui_dist = zimui_dist
self.overwrite_existing_zim = overwrite_existing_zim

self.output_folder.mkdir(exist_ok=True)

Expand Down Expand Up @@ -142,7 +147,7 @@ def run(self) -> Path:
formatted_config = self.zim_config.format(metadata.placeholders())
zim_path = Path(self.output_folder, f"{formatted_config.file_name_format}.zim")

if zim_path.exists():
if zim_path.exists() and not self.overwrite_existing_zim:
logger.error(f" {zim_path} already exists, aborting.")
raise SystemExit(2)

Expand Down

0 comments on commit a71de74

Please sign in to comment.