diff --git a/src/pyxu/util/cache.py b/src/pyxu/util/cache.py index c085da3c5..21400615b 100644 --- a/src/pyxu/util/cache.py +++ b/src/pyxu/util/cache.py @@ -1,26 +1,41 @@ -import pyxu.info.config as config +import hashlib + +import pyxu.info.config as pxcfg __all__ = [ "cache_module", ] -def cache_module(name: str, code: str): +def cache_module(code: str) -> str: """ - Update a specific module in the dynamic module cache. + Save `code` as an importable module in Pyxu's dynamic module cache. The cached module is updated only if changes are detected. Parameters ---------- - name: str - Name of the module to update. code: str Contents of the module. When stored in a file, `code` should be a valid Python module. + + Returns + ------- + module_name: str + Name of the module in :py:func:`~pyxu.info.config.cache_dir`. + + Notes + ----- + `module_name` is chosen automatically based on the file's contents. """ - module_path = config.cache_dir / f"{name}.py" + # Compute a unique name + h = hashlib.blake2b(code.encode("utf-8"), digest_size=8) + module_name = "cached_" + h.hexdigest() + pxcfg.cache_dir().mkdir(parents=True, exist_ok=True) + module_path = pxcfg.cache_dir() / f"{module_name}.py" + + # Do we overwrite? write = True if module_path.exists(): with open(module_path, mode="r") as f: @@ -31,3 +46,5 @@ def cache_module(name: str, code: str): if write: with open(module_path, mode="w") as f: f.write(code) + + return module_name