author | Fabien Ninoles <fabien@tzone.org> |
Sun, 07 Dec 2008 16:05:33 -0500 | |
branch | immsview |
changeset 39 | a26e907b8022 |
parent 33 | ad808d18c693 |
permissions | -rwxr-xr-x |
31 | 1 |
#!/usr/bin/python |
2 |
||
3 |
import os |
|
4 |
import readline |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
5 |
from imms import IMMSCleaner, IMMSDb |
31 | 6 |
from utils import unique, copy_file, set_file_completer |
7 |
||
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
8 |
class CLICleaner(IMMSCleaner): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
9 |
def check_if_uid_exist(self, uid): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
10 |
other_paths = self.db.get_library_entry(uid = uid) |
31 | 11 |
other_paths = unique(map(lambda x: x[0], other_paths)) |
12 |
for opath in other_paths: |
|
13 |
if os.path.isfile(opath): |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
14 |
# if one, release it. |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
15 |
return 1 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
16 |
return 0 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
17 |
def check_and_edit_path(self, path, uid, sid): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
18 |
"""Must return the new file name, None to remove |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
19 |
it. If the new file name is already in the Db, |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
20 |
it will be skip.""" |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
21 |
# if path exist, skip it. |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
22 |
if os.path.isfile(path): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
23 |
return path |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
24 |
# if exist another uid with a valid path, remove it. |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
25 |
if self.check_if_uid_exist(uid): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
26 |
return None |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
27 |
# Elsewhere, first build a list of valid candidate from sid... |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
28 |
other_paths = self.db.get_library_entry(sid = sid) |
31 | 29 |
other_paths = unique(map(lambda x: x[0], other_paths)) |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
30 |
return self.edit_filename(path, other_paths) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
31 |
def edit_filename(self, path, other_paths): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
32 |
# Add them to the history for editing |
31 | 33 |
for opath in other_paths: |
34 |
if os.path.isfile(opath): |
|
35 |
readline.add_history(opath) |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
36 |
# And also add the current file as a base for editing. |
31 | 37 |
readline.add_history(path) |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
38 |
# Sniff! This doesn't seems to work |
31 | 39 |
readline.insert_text('Edit') |
40 |
while 1: |
|
41 |
cmd = raw_input("I can't find '%s'.\n" |
|
42 |
"Edit, Skip or Remove? " % path) |
|
43 |
if len(cmd) > 0: |
|
44 |
cmd = cmd.lstrip()[0].lower() |
|
45 |
if cmd == 'e': |
|
46 |
newpath = raw_input() |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
47 |
# Already exist, so delete. |
31 | 48 |
if newpath in other_paths: |
49 |
return None |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
50 |
# new file so keep it. |
31 | 51 |
if os.path.isfile(newpath): |
52 |
return newpath |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
53 |
# Elsewhere move it. |
31 | 54 |
print "Invalid filename!" |
55 |
elif cmd == 's': |
|
56 |
return path; |
|
57 |
elif cmd == 'r': |
|
58 |
return None |
|
59 |
||
60 |
if __name__ == '__main__': |
|
61 |
set_file_completer() |
|
62 |
readline.parse_and_bind('tab: complete') |
|
63 |
db_file = os.environ['HOME'] + '/.imms/imms.db' |
|
64 |
db_backup = db_file + '.bak' |
|
65 |
copy_file(db_file, db_backup).close() |
|
66 |
try: |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
67 |
CLICleaner(IMMSDb(db_file)).clean_all() |
31 | 68 |
except Exception, inst: |
69 |
print inst |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
70 |
while 1: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
71 |
ans = raw_input('Do you want to get back the backup file? ') |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
72 |
if len(ans) > 0: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
73 |
ans = ans.lstrip()[0].lower() |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
74 |
if ans == 'y': |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
75 |
copy_file(db_backup, db_file) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
76 |
elif ans == 'n': |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
31
diff
changeset
|
77 |
break |
31 | 78 |
print "backup file preserved: %s." % db_backup |