From 03602bd81693660af079129c99a3d4fe30aaecd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Sierro?= Date: Thu, 11 Jul 2024 15:55:37 +0200 Subject: [PATCH] cache from toml must be used with a title --- src/scgenerator/cache.py | 9 ++++++--- tests/test_cache.py | 6 +++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/scgenerator/cache.py b/src/scgenerator/cache.py index 1f11bb5..4cacab7 100644 --- a/src/scgenerator/cache.py +++ b/src/scgenerator/cache.py @@ -65,9 +65,12 @@ class Cache: return cls(group) @classmethod - def from_toml(cls, s: str, /, create: bool = True) -> Self: - hashed = hashlib.md5(pickle.dumps(sort_dict(tomllib.loads(s)))).hexdigest() - group = f"TOML-{hashed}" + def from_toml(cls, name: str, toml_str: str, /, create: bool = True) -> Self: + hashed = hashlib.md5(pickle.dumps(sort_dict(tomllib.loads(toml_str)))).hexdigest() + if len(name) > 50: + warnings.warn(f"name {name!r} is longer than 30 characters") + name = name[:50] + group = f"TOML-{name}-{hashed}" if create: os.makedirs(CACHE_DIR / group, exist_ok=True) return cls(group) diff --git a/tests/test_cache.py b/tests/test_cache.py index 994f369..6b5d4a9 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -51,9 +51,9 @@ def test_toml(): rin_measurement='./DualComb1GHz_updated_corrected_extrapolated_1GHz_noqn.csv' """ - cache1 = Cache.from_toml(s1) - cache3 = Cache.from_toml(s3, create=False) - assert cache1.dir == Cache.from_toml(s2).dir + cache1 = Cache.from_toml("test", s1) + cache3 = Cache.from_toml("test", s3, create=False) + assert cache1.dir == Cache.from_toml("test", s2).dir assert cache1.dir != cache3.dir assert cache1.dir.exists() cache1.delete()