about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2023-01-09 20:24:50 -0600
committerStarfall <us@starfall.systems>2023-01-09 20:24:50 -0600
commit7ab7f137af94d6f64f33dbf9dbec05e772b0d5c7 (patch)
treef1b850f28cb004249f9be9107a028f9e7e4ebedb
parent6636a4a0a59dc73cdd4a0e123363794574415df5 (diff)
gracefully handle keys with / HEAD main
-rw-r--r--README.md1
-rw-r--r--fscache.py6
2 files changed, 6 insertions, 1 deletions
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():