Skip to content

Commit

Permalink
fix(lib): prevent filename collision
Browse files Browse the repository at this point in the history
Apparently, ManimCE can produce two different animations with the same name (i.e., the same hash). As documented, ManimGL would any produce files with the same name so this fix was needed.

Closes #428
  • Loading branch information
jeertmans committed May 10, 2024
1 parent 962ff2b commit f955a77
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 7 additions & 3 deletions manim_slides/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,19 @@ def to_file(self, path: Path) -> None:
f.write(self.model_dump_json(indent=2))

def copy_to(
self, folder: Path, use_cached: bool = True, include_reversed: bool = True
self,
folder: Path,
use_cached: bool = True,
include_reversed: bool = True,
prefix: str = "",
) -> "PresentationConfig":
"""Copy the files to a given directory."""
for slide_config in self.slides:
file = slide_config.file
rev_file = slide_config.rev_file

dest = folder / file.name
rev_dest = folder / rev_file.name
dest = folder / f"{prefix}{file.name}"
rev_dest = folder / f"{prefix}{rev_file.name}"

slide_config.file = dest
slide_config.rev_file = rev_dest
Expand Down
23 changes: 21 additions & 2 deletions manim_slides/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,27 @@ def convert_to(self, dest: Path) -> None:

full_assets_dir.mkdir(parents=True, exist_ok=True)

for presentation_config in self.presentation_configs:
presentation_config.copy_to(full_assets_dir, include_reversed=False)
num_presentation_configs = len(self.presentation_configs)

if num_presentation_configs > 1:
# Prevent possible name collision, see:
# https://github.com/jeertmans/manim-slides/issues/428
# With ManimCE, this should not happen (but happens...)
# With ManimGL, this can easily occur since filenames are just basic integers...
num_digits = len(str(num_presentation_configs - 1))

Check warning on line 414 in manim_slides/convert.py

View check run for this annotation

Codecov / codecov/patch

manim_slides/convert.py#L414

Added line #L414 was not covered by tests

def prefix(i: int) -> str:
return f"s{i:0{num_digits}d}_"

Check warning on line 417 in manim_slides/convert.py

View check run for this annotation

Codecov / codecov/patch

manim_slides/convert.py#L416-L417

Added lines #L416 - L417 were not covered by tests

else:

def prefix(i: int) -> str:
return ""

for i, presentation_config in enumerate(self.presentation_configs):
presentation_config.copy_to(
full_assets_dir, include_reversed=False, prefix=prefix(i)
)

dest.parent.mkdir(parents=True, exist_ok=True)

Expand Down

0 comments on commit f955a77

Please sign in to comment.