From 7ab7f137af94d6f64f33dbf9dbec05e772b0d5c7 Mon Sep 17 00:00:00 2001 From: Starfall Date: Mon, 9 Jan 2023 20:24:50 -0600 Subject: gracefully handle keys with / --- README.md | 1 - fscache.py | 6 ++++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 05a35a9..d86b1df 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,6 @@ TODO / Issues - Work around known issues in the NWS API: - retry when gridpoints 500s - reduce gridX and gridY by 1 for AFC (AER/ALU), AFG, AJK, BOX, CAE, DLH, FSD, HGX, HNX, LIX, LWX, MAF, MFR, MLB, MRX, MTR, PIH offices/gridIds -- Gracefully handle cache keys that include '/' Reference --------- diff --git a/fscache.py b/fscache.py index e549e92..8e57a6e 100644 --- a/fscache.py +++ b/fscache.py @@ -2,6 +2,9 @@ import os import time from pathlib import Path +def key_rewrite(key): + return key.replace('/', '-') + class CacheMiss(Exception): pass @@ -14,7 +17,9 @@ class Cache: self.cache_dir = Path(os.environ['HOME'])/'.cache'/name self.cache_dir.mkdir(exist_ok=True) + def write(self, key, value, ttl=None): + key = key_rewrite(key) loc = self.cache_dir/key loc.write_text(value, errors='ignore') @@ -24,6 +29,7 @@ class Cache: os.utime(loc, (stat.st_atime, stat.st_mtime + ttl)) def read(self, key): + key = key_rewrite(key) loc = self.cache_dir/key if not loc.exists(): -- cgit