ocarina/libsaria/cache.py

36 lines
629 B
Python

# Bryan Schumaker (10/23/2010)
import libsaria
path = libsaria.path
expand = path.expand
mkdir = path.mkdir
exists = path.exists
join = path.join
ls = path.ls
CACHE_DIR = expand("~/.saria/cache")
mkdir(CACHE_DIR)
class CacheObject:
def __init__(self, path):
self.path = path
def get(self, key, func, *args):
path = join(self.path, key)
if not exists(path):
f = open(path, 'w')
if func(f, *args):
return path
else:
return path
class Cache:
def keys(self):
return ls(CACHE_DIR)
def __getitem__(self, key):
p = join(CACHE_DIR, key)
if not exists(p):
mkdir(p)
return CacheObject(p)