Generic cache

I have created a generic cache to hold information outside of the
library.  This could include album art or an artist bio.
This commit is contained in:
Bryan Schumaker 2010-10-24 17:26:53 -04:00
parent 03ed1489dc
commit 5c842def01
4 changed files with 53 additions and 1 deletions

View File

@ -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

35
libsaria/cache.py Normal file
View File

@ -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)

View File

@ -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)

9
tests/cache.py Normal file
View File

@ -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"]