From 89ddfadf6b2d21cc76b334c9fecebdc9fb190353 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Wed, 4 Dec 2024 20:50:42 -0600 Subject: [PATCH] Allow for a configurable cache location --- manimlib/default_config.yml | 10 +++++----- manimlib/utils/cache.py | 6 +++--- manimlib/utils/customization.py | 4 ++++ manimlib/utils/directories.py | 4 ++++ 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/manimlib/default_config.yml b/manimlib/default_config.yml index 637175fb..211ec110 100644 --- a/manimlib/default_config.yml +++ b/manimlib/default_config.yml @@ -10,12 +10,12 @@ directories: vector_images: "" # If you want to use sounds, manim will look here to find it. sounds: "" - # Manim often generates tex_files or other kinds of serialized data - # to keep from having to generate the same thing too many times. By - # default, these will be stored at tempfile.gettempdir(), e.g. this might - # return whatever is at to the TMPDIR environment variable. If you want to - # specify them elsewhere, temporary_storage: "" + # For certain object types, especially Tex and Text, manim will save information + # to file to prevent the need to re-compute, e.g. recompiling the latex. By default, + # it stores this saved data to whatever directory appdirs.user_cache_dir("manim") returns, + # but here a user can specify a different cache location + cache: "" universal_import_line: "from manimlib import *" style: tex_template: "default" diff --git a/manimlib/utils/cache.py b/manimlib/utils/cache.py index 0b3f3d2e..2c1ce860 100644 --- a/manimlib/utils/cache.py +++ b/manimlib/utils/cache.py @@ -1,15 +1,15 @@ -import appdirs import os from diskcache import Cache from contextlib import contextmanager +from manimlib.utils.directories import get_cache_dir + CACHE_SIZE = 1e9 # 1 Gig def get_cached_value(key, value_func, message=""): - cache_dir = appdirs.user_cache_dir("manim") - cache = Cache(cache_dir, size_limit=CACHE_SIZE) + cache = Cache(get_cache_dir(), size_limit=CACHE_SIZE) value = cache.get(key) if value is None: diff --git a/manimlib/utils/customization.py b/manimlib/utils/customization.py index 94923b43..7426deb6 100644 --- a/manimlib/utils/customization.py +++ b/manimlib/utils/customization.py @@ -1,5 +1,6 @@ import os import tempfile +import appdirs from manimlib.config import get_custom_config from manimlib.config import get_manim_dir @@ -17,6 +18,9 @@ def get_customization(): if not directories["temporary_storage"]: directories["temporary_storage"] = tempfile.gettempdir() + if not directories["cache"]: + directories["cache"] = appdirs.user_cache_dir("manim") + # Assumes all shaders are written into manimlib/shaders directories["shaders"] = os.path.join( get_manim_dir(), "manimlib", "shaders" diff --git a/manimlib/utils/directories.py b/manimlib/utils/directories.py index 9bccc1be..8ef3c850 100644 --- a/manimlib/utils/directories.py +++ b/manimlib/utils/directories.py @@ -10,6 +10,10 @@ def get_directories() -> dict[str, str]: return get_customization()["directories"] +def get_cache_dir() -> str: + return get_directories()["cache"] + + def get_temp_dir() -> str: return get_directories()["temporary_storage"]