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 Sep 27, 2024
1 parent 4f4c5ba commit 49e1090
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 @@ -60,6 +60,13 @@ def main() -> None:
default=os.getenv("LIBRETEXTS_ZIMUI_DIST", "../zimui/dist"),
)

parser.add_argument(

Check warning on line 63 in scraper/src/libretexts2zim/entrypoint.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/entrypoint.py#L63

Added line #L63 was not covered by tests
"--overwrite",
help="Do not fail if ZIM already exists, overwrite it",
action="store_true",
default=False,
)

# ZIM configuration flags
ZimConfig.add_flags(

Check warning on line 71 in scraper/src/libretexts2zim/entrypoint.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/entrypoint.py#L71

Added line #L71 was not covered by tests
parser,
Expand Down Expand Up @@ -94,6 +101,7 @@ def main() -> None:
output_folder=args.output_folder,
zimui_dist=args.zimui_dist,
content_filter=doc_filter,
overwrite_existing_zim=args.overwrite,
).run()
except Exception as e:
logger.exception(e)
Expand Down
7 changes: 6 additions & 1 deletion scraper/src/libretexts2zim/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def __init__(
content_filter: ContentFilter,
output_folder: str,
zimui_dist: str,
*,
overwrite_existing_zim: bool,
) -> None:
"""Initializes Generator.
Expand All @@ -117,12 +119,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: Directory to write ZIMs into.
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 = Path(zimui_dist)
self.overwrite_existing_zim = overwrite_existing_zim

Check warning on line 130 in scraper/src/libretexts2zim/generator.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/generator.py#L125-L130

Added lines #L125 - L130 were not covered by tests

os.makedirs(self.output_folder, exist_ok=True)

Check warning on line 132 in scraper/src/libretexts2zim/generator.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/generator.py#L132

Added line #L132 was not covered by tests

Expand Down Expand Up @@ -154,7 +159,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")

Check warning on line 160 in scraper/src/libretexts2zim/generator.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/generator.py#L159-L160

Added lines #L159 - L160 were not covered by tests

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

Check warning on line 164 in scraper/src/libretexts2zim/generator.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/generator.py#L163-L164

Added lines #L163 - L164 were not covered by tests

Expand Down

0 comments on commit 49e1090

Please sign in to comment.