3b1b-manim/manimlib/utils/file_ops.py
Grant Sanderson 744e695340
Misc. clean up (#2269)
* Comment tweak

* Directly print traceback

Since the shell.showtraceback is giving some issues

* Make InteracrtiveSceneEmbed into a class

This way it can keep track of it's internal shell; use of get_ipython has a finicky relationship with reloading.

* Move remaining checkpoint_paste logic into scene_embed.py

This involved making a few context managers for Scene: temp_record, temp_skip, temp_progress_bar, which seem useful in and of themselves.

* Change null key to be the empty string

* Ensure temporary svg paths for Text are deleted

* Remove unused dict_ops.py functions

* Remove break_into_partial_movies from file_writer configuration

* Rewrite guarantee_existence using Path

* Clean up SceneFileWriter

It had a number of vestigial functions no longer used, and some setup that could be made more organized.

* Remove --save_pngs CLI arg (which did nothing)

* Add --subdivide CLI arg

* Remove add_extension_if_not_present

* Remove get_sorted_integer_files

* Have find_file return Path

* Minor clean up

* Clean up num_tex_symbols

* Fix find_file

* Minor cleanup for extract_scene.py

* Add preview_frame_while_skipping option to scene config

* Use shell.showtraceback function

* Move keybindings to config, instead of in-place constants

* Replace DEGREES -> DEG
2024-12-12 08:39:54 -08:00

57 lines
1.5 KiB
Python

from __future__ import annotations
import os
from pathlib import Path
import hashlib
import numpy as np
import validators
import urllib.request
import manimlib.utils.directories
from manimlib.utils.simple_functions import hash_string
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Iterable
def guarantee_existence(path: str | Path) -> Path:
path = Path(path)
path.mkdir(parents=True, exist_ok=True)
return path.absolute()
def find_file(
file_name: str,
directories: Iterable[str] | None = None,
extensions: Iterable[str] | None = None
) -> Path:
# Check if this is a file online first, and if so, download
# it to a temporary directory
if validators.url(file_name):
suffix = Path(file_name).suffix
file_hash = hash_string(file_name)
folder = manimlib.utils.directories.get_downloads_dir()
path = Path(folder, file_hash).with_suffix(suffix)
urllib.request.urlretrieve(file_name, path)
return path
# Check if what was passed in is already a valid path to a file
if os.path.exists(file_name):
return Path(file_name)
# Otherwise look in local file system
directories = directories or [""]
extensions = extensions or [""]
possible_paths = (
Path(directory, file_name + extension)
for directory in directories
for extension in extensions
)
for path in possible_paths:
if path.exists():
return path
raise IOError(f"{file_name} not Found")