diff --git a/libsaria/__init__.py b/libsaria/__init__.py index 830e41e1..2c778b3c 100644 --- a/libsaria/__init__.py +++ b/libsaria/__init__.py @@ -13,10 +13,13 @@ if __dev__ == True: import event import path from map import Map +from cache import Cache # Variables are not saved across sessions, but preferences are +# The cache is also saved across sessions vars = None prefs = None +cache = Cache() plugin = None music = None diff --git a/libsaria/cache.py b/libsaria/cache.py new file mode 100644 index 00000000..6e4f1936 --- /dev/null +++ b/libsaria/cache.py @@ -0,0 +1,35 @@ +# 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) diff --git a/libsaria/path.py b/libsaria/path.py index c1299525..05d2cbc9 100644 --- a/libsaria/path.py +++ b/libsaria/path.py @@ -7,7 +7,7 @@ is_dir = os.path.isdir expand = os.path.expanduser splitext = os.path.splitext join = os.path.join -mkdir = os.mkdir +makedir = os.mkdir rm = os.remove ls = os.listdir walk = os.walk @@ -58,3 +58,8 @@ def cp_once(src, dest): cp = shutil.copy cp(src, dest) cp = cp_once + + +def mkdir(path): + if not exists(path): + makedir(path) diff --git a/tests/cache.py b/tests/cache.py new file mode 100644 index 00000000..c13b0148 --- /dev/null +++ b/tests/cache.py @@ -0,0 +1,9 @@ +# Bryan Schumaker (10/23/2010) + +import libsaria + +cache = libsaria.cache + +obj = cache["test"] +#obj = cache["Train"] +#print obj["Save Me, San Francisco.jpg"]