libsaria: Began new cache functions

I think the new cache will be more generic, and should work for just
about anything.  We'll see.
This commit is contained in:
Bryan Schumaker 2011-05-06 23:52:46 -04:00
parent c1a2d74f7c
commit 904b475363
3 changed files with 42 additions and 5 deletions

View File

@ -6,6 +6,7 @@ from libsaria import web
from libsaria import xm
from libsaria import callbacks
from libsaria import threads
from libsaria import storage
file_id = libsaria.path.file_id
cache = libsaria.cache
@ -47,7 +48,7 @@ class LastFmRequest(dict):
else:
return None
def lfm_cache_album(file, artist, album):
def lfm_cache_album(file, artist, album, title):
req = LastFmRequest("album.getInfo")
req["album"] = album
req["artist"] = artist
@ -76,9 +77,10 @@ def set_art_from_file(file, path):
return False
return True
def get_artwork_tags(artist, album):
def get_artwork_tags(artist, album, title):
storage.cache.get_item(artist, album, title, "Folder.jpg", lfm_cache_album)
cached = cache[artist]
file = cached.get("%s.jpg" % album, lfm_cache_album, artist, album)
file = cached.get("%s.jpg" % album, lfm_cache_album, artist, album, title)
return file
def set_artwork_tags(artist, album, path):
@ -87,8 +89,8 @@ def set_artwork_tags(artist, album, path):
file = cached.get("%s.jpg" % album, set_art_from_file, path)
def get_artwork_id(id):
artist, album = sources.get_attrs("artist", "album")
return get_artwork_tags(artist, album)
artist, album, title = sources.get_attrs("artist", "album", "title")
return get_artwork_tags(artist, album, title)
def get_artwork_bg(file):
id = sources.get_attrs("id")

View File

@ -4,6 +4,7 @@ import os
from os import path
import cPickle as pickle
from libsaria import version
import cache
USER_DIR = path.expanduser("~")
PROTO = pickle.HIGHEST_PROTOCOL

34
libsaria/storage/cache.py Normal file
View File

@ -0,0 +1,34 @@
# 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 get_item(artist, album, title, file, cache_func):
cache_path = make_cache_path(artist, album, file)
if not file_exists(cache_path):
f = open(cache_path, 'w')
cache_func(f, artist, album, title)
f.close()
if not file_exists(cache_path):
os.remove(cache_path)
print "File doesn't exist!"
return cache_path