cleanimms
branchimmsview
changeset 31 13f56bb29b96
child 33 ad808d18c693
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cleanimms	Sun Feb 08 16:55:24 2004 -0500
@@ -0,0 +1,110 @@
+#!/usr/bin/python
+
+import os
+import sqlite
+from ogg.vorbis import VorbisFile
+import ID3
+import readline
+import imms
+from utils import unique, copy_file, set_file_completer
+
+
+class CleanupIMMS:
+    def __init__(self, db_file):
+        self.db = imms.IMMSDb(db_file)
+    def check_uid(self, uid):
+        lib = self.db.get_library_entries(uid = uid)
+        if len(lib) == 0:
+            print "Erased uid = ", uid
+            self.db.erase_uid(uid)
+    def check_sid(self, sid):
+        lib = self.db.get_library_entries(sid = sid)
+        if len(lib) == 0:
+            print "Erased sid = ", uid
+            self.db.erase_sid(sid)
+    def handle_no_file(self, path, uid, sid):
+        other_paths = self.db.get_library_entries(uid = uid)
+        other_paths = unique(map(lambda x: x[0], other_paths))
+        for opath in other_paths:
+            if os.path.isfile(opath):
+                return None
+        other_paths = self.db.get_library_entries(sid = sid)
+        other_paths = unique(map(lambda x: x[0], other_paths))
+        for opath in other_paths:
+            if os.path.isfile(opath):
+                readline.add_history(opath)
+        readline.add_history(path)
+        readline.insert_text('Edit')
+        while 1:
+            cmd = raw_input("I can't find '%s'.\n"
+                            "Edit, Skip or Remove? " % path)
+            if len(cmd) > 0:
+                cmd = cmd.lstrip()[0].lower()
+                if cmd == 'e':
+                    newpath = raw_input()
+                    if newpath in other_paths:
+                        return None
+                    if os.path.isfile(newpath):
+                        return newpath
+                    print "Invalid filename!"
+                elif cmd == 's':
+                    return path;
+                elif cmd == 'r':
+                    return None
+    def clean_library(self):
+        lib = self.db.get_library_entries()
+        deleted_uids = []
+        deleted_sids = []
+        for entry in lib:
+            path, uid, sid = entry
+            if not os.path.isfile(path):
+                uid = int(uid)
+                sid = int(sid)
+                newfile = self.handle_no_file(path, uid, sid)
+                if not newfile:
+                    print "Erasing ", path
+                    self.db.erase_filename(path)
+                    deleted_uids.append(uid)
+                    deleted_sids.append(sid)
+                elif newfile != path:
+                    print "Renaming ", path, " into ", newfile
+                    self.db.update_filename(path, newfile)
+                else:
+                    print "Skipping ", path
+        map(self.check_uid, unique(deleted_uids))
+        map(self.check_sid, unique(deleted_sids))
+    def clean_rating(self):
+        rates = self.db.get_ratings()
+        rates = unique(map(lambda x: x[0], rates))
+        map(self.check_uid, rates)
+    def clean_acoustic(self):
+        uids = map(lambda x: x[0], self.db.get_acoustics())
+        map(self.check_uid, uids)
+    def clean_info(self):
+        sids = map(lambda x: x[0], self.db.get_infos())
+        map(self.check_sid, sids)
+    def clean_last(self):
+        sids = map(lambda x: x[0], self.db.get_last())
+        map(self.check_sid, sids)
+    def clean_all(self):
+        self.clean_library()
+        self.clean_rating()
+        self.clean_acoustic()
+        self.clean_info()
+        self.clean_last()
+
+if __name__ == '__main__':
+    set_file_completer()
+    readline.parse_and_bind('tab: complete')
+    db_file = os.environ['HOME'] + '/.imms/imms.db'
+    db_backup = db_file + '.bak'
+    copy_file(db_file, db_backup).close()
+    try:
+        clean_up = CleanupIMMS(db_file)
+    except Exception, inst:
+        print inst
+        ans = raw_input('Do you want to get back the backup file? ')
+        if len(ans) > 0:
+            if ans.lstrip()[0].lower() == 'y':
+                copy_file(db_backup, db_file)
+    print "backup file preserved: %s." % db_backup