# Bryan Schumaker (5 / 6 / 2011) import os join = os.path.join sep = os.path.sep isfile = os.path.isfile isdir = os.path.isdir getsize = os.path.getsize CACHE_DIR = None def set_basedir(saria_dir): global CACHE_DIR CACHE_DIR = join(saria_dir, "cache") 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_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): text = cache_func(artist, album, title) if text != None: try: f = open(cache_path, 'w') f.write(text) f.close() except: pass return True return False def get_item(artist, album, title, file, cache_func, default): cache_path = make_cache_path(artist, album, file) filled = True if not file_exists(cache_path): filled = fill_cache(artist, album, title, cache_func, cache_path) if filled == False: return default return cache_path def test_item(artist, album, file, default): cache_path = make_cache_path(artist, album, file) if not file_exists(cache_path): return default 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()