mirror of
https://github.com/3b1b/manim.git
synced 2025-04-13 09:47:07 +00:00
49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from diskcache import Cache
|
|
from contextlib import contextmanager
|
|
from functools import wraps
|
|
|
|
from manimlib.utils.directories import get_cache_dir
|
|
from manimlib.utils.simple_functions import hash_string
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
T = TypeVar('T')
|
|
|
|
|
|
CACHE_SIZE = 1e9 # 1 Gig
|
|
_cache = Cache(get_cache_dir(), size_limit=CACHE_SIZE)
|
|
|
|
|
|
def cache_on_disk(func: Callable[..., T]) -> Callable[..., T]:
|
|
@wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
key = hash_string("".join(map(str, [func.__name__, args, kwargs])))
|
|
value = _cache.get(key)
|
|
if value is None:
|
|
# print(f"Executing {func.__name__}({args[0]}, ...)")
|
|
value = func(*args, **kwargs)
|
|
_cache.set(key, value)
|
|
return value
|
|
return wrapper
|
|
|
|
|
|
def clear_cache():
|
|
_cache.clear()
|
|
|
|
|
|
@contextmanager
|
|
def display_during_execution(message: str):
|
|
# Merge into a single line
|
|
to_print = message.replace("\n", " ")
|
|
max_characters = os.get_terminal_size().columns - 1
|
|
if len(to_print) > max_characters:
|
|
to_print = to_print[:max_characters - 3] + "..."
|
|
try:
|
|
print(to_print, end="\r")
|
|
yield
|
|
finally:
|
|
print(" " * len(to_print), end="\r")
|