diff -r 85c8f5280d48 -r ad808d18c693 cleanimms --- a/cleanimms Sun Feb 08 17:27:21 2004 -0500 +++ b/cleanimms Mon Feb 09 23:29:08 2004 -0500 @@ -1,39 +1,41 @@ #!/usr/bin/python import os -import sqlite -from ogg.vorbis import VorbisFile -import ID3 import readline -import imms +from imms import IMMSCleaner, IMMSDb 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) +class CLICleaner(IMMSCleaner): + def check_if_uid_exist(self, uid): + other_paths = self.db.get_library_entry(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) + # if one, release it. + return 1 + return 0 + def check_and_edit_path(self, path, uid, sid): + """Must return the new file name, None to remove + it. If the new file name is already in the Db, + it will be skip.""" + # if path exist, skip it. + if os.path.isfile(path): + return path + # if exist another uid with a valid path, remove it. + if self.check_if_uid_exist(uid): + return None + # Elsewhere, first build a list of valid candidate from sid... + other_paths = self.db.get_library_entry(sid = sid) other_paths = unique(map(lambda x: x[0], other_paths)) + return self.edit_filename(path, other_paths) + def edit_filename(self, path, other_paths): + # Add them to the history for editing for opath in other_paths: if os.path.isfile(opath): readline.add_history(opath) + # And also add the current file as a base for editing. readline.add_history(path) + # Sniff! This doesn't seems to work readline.insert_text('Edit') while 1: cmd = raw_input("I can't find '%s'.\n" @@ -42,56 +44,18 @@ cmd = cmd.lstrip()[0].lower() if cmd == 'e': newpath = raw_input() + # Already exist, so delete. if newpath in other_paths: return None + # new file so keep it. if os.path.isfile(newpath): return newpath + # Elsewhere move it. 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() @@ -100,11 +64,15 @@ db_backup = db_file + '.bak' copy_file(db_file, db_backup).close() try: - clean_up = CleanupIMMS(db_file) + CLICleaner(IMMSDb(db_file)).clean_all() 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) + while 1: + ans = raw_input('Do you want to get back the backup file? ') + if len(ans) > 0: + ans = ans.lstrip()[0].lower() + if ans == 'y': + copy_file(db_backup, db_file) + elif ans == 'n': + break print "backup file preserved: %s." % db_backup