cleanimms
author fabien
Thu, 12 Feb 2004 09:38:06 -0500
branchimmsview
changeset 35 ab72cbd172b8
parent 33 ad808d18c693
permissions -rwxr-xr-x
[svn] Add periodic timeout.

#!/usr/bin/python

import os
import readline
from imms import IMMSCleaner, IMMSDb
from utils import unique, copy_file, set_file_completer

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):
	        # 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"
                            "Edit, Skip or Remove? " % path)
            if len(cmd) > 0:
                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

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:
        CLICleaner(IMMSDb(db_file)).clean_all()
    except Exception, inst:
        print inst
        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