ocarina/libsaria/storage/cache.py
Bryan Schumaker bafdc6ef20 libsaria: Manually set artwork through a copy_in_file() function
I created this function in the cache to make a generic way to copy
outside files into the cache tree.  It makes sense to do this in the
cache, since I want to keep everything self-contained.
2011-05-07 16:49:41 -04:00

46 lines
1.2 KiB
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
def copy_in_file(artist, album, file, orig_file):
cache_path = make_cache_path(artist, album, file)
orig = open(orig_file)
f = open(cache_path, 'w')
f.write(orig.read())
f.close()