ocarina/libsaria/storage/cache.py
Bryan Schumaker 0f1b5550aa libsaria: Change cache_func() argument order
I want to pass the file object last, I just think it looks better.
2011-05-07 15:58:33 -04:00

39 lines
1017 B
Python

# Bryan Schumaker (5 / 6 / 2011)
import os
import libsaria
join = os.path.join
sep = os.path.sep
isfile = os.path.isfile
isdir = os.path.isdir
getsize = os.path.getsize
def file_exists(cache_path):
if not isfile(cache_path):
return False
return getsize(cache_path) != 0
def make_cache_path(artist, album, file):
cache_dir = libsaria.storage.full_path("cache")
cache_file = "%s%s%s%s" % (artist, sep, album, sep)
cache_path = join(cache_dir, cache_file)
if not isdir(cache_path):
os.makedirs(cache_path)
return join(cache_path, file)
def fill_cache(artist, album, title, cache_func, cache_path):
f = open(cache_path, 'w')
cache_func(artist, album, title, f)
f.close()
if not file_exists(cache_path):
os.remove(cache_path)
return None
return cache_path
def get_item(artist, album, title, file, cache_func):
cache_path = make_cache_path(artist, album, file)
if not file_exists(cache_path):
cache_path = fill_cache(artist, album, title, cache_func, cache_path)
return cache_path