Commits

Damián Nohales committed 7c25467

Implemented analyze_cache_limit to clear cache folder.

Comments (0)

Files changed (1)

 import os
 import gtk
-from enviroment import env
+from enviroment import env, config
 
 _missed_pixbuf = None
 _mem_cache = {}
     return _missed_pixbuf
 
 def analyze_cache_limit():
-    pass
+    filelist = os.listdir(os.path.join(env().get_config_directory(), "covers"))
+    filelist_full = []
+    totalsize = 0
+    limitsize = int(config()["cover_cache_limit"])
+    
+    for f in filelist:
+        fname = os.path.join(env().get_config_directory(), "covers", f)
+        fstat = os.stat(fname)
+        
+        filelist_full.append((fname, fstat.st_atime, fstat.st_size))
+        totalsize += fstat.st_size
+    
+    if totalsize > limitsize:
+        filelist_full = sorted(filelist_full, key=lambda file: file[1])
+        i = 0
+        while totalsize > limitsize:
+            file = filelist_full[i]
+            totalsize -= file[2]
+            os.remove(file[0])
+            i += 1