author | Fabien Ninoles <fabien@tzone.org> |
Sun, 07 Dec 2008 16:05:33 -0500 | |
branch | immsview |
changeset 39 | a26e907b8022 |
parent 38 | 785c66feccd3 |
child 40 | 7a7e5a853937 |
permissions | -rw-r--r-- |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
1 |
import os.path |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
2 |
from sys import stderr |
39 | 3 |
from pysqlite2 import dbapi2 as sqlite |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
4 |
from utils import sql_quote, unique |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
5 |
|
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
6 |
_log = stderr |
31 | 7 |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
8 |
def rating_to_color(rating): |
39 | 9 |
i = rating |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
10 |
red = green = blue = 0 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
11 |
if i <= 25: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
12 |
red = 255 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
13 |
green = i * 255 / 25 |
39 | 14 |
elif i <= 75: |
15 |
red = (75-i) * 255 / 50 |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
16 |
green = 255 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
17 |
else: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
18 |
green = 255 |
39 | 19 |
blue = (i-75) * 255 / 25 |
20 |
print >>stderr,"Rating:",rating,"R:",red,"G:",green,"B:",blue |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
21 |
return "#%02x%02x%02x" % (red, green, blue) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
22 |
|
31 | 23 |
class IMMSDb: |
24 |
def __init__(self, dbname = None): |
|
25 |
if not dbname: |
|
39 | 26 |
dbname = os.environ['HOME'] + '/.imms/imms2.db' |
31 | 27 |
# autocommit = 1 disable autocommit! |
39 | 28 |
self.cx = sqlite.connect(dbname, timeout = 5) |
31 | 29 |
self.cu = self.cx.cursor() |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
30 |
def get_library_entry(self, **kw): |
39 | 31 |
qry = "SELECT path, l.uid, l.sid FROM Library l NATURAL INNER JOIN Identify" |
31 | 32 |
first = 1 |
33 |
for key in kw.keys(): |
|
34 |
if first: |
|
35 |
qry += " WHERE " |
|
36 |
first = 0 |
|
37 |
else: |
|
38 |
qry += " AND " |
|
39 |
if key in ['uid', 'sid']: |
|
39 | 40 |
qry += "l.%s = %d" % (key, kw[key]) |
31 | 41 |
else: |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
42 |
qry += "%s = '%s'" % (key, sql_quote(kw[key])) |
31 | 43 |
qry += ";" |
44 |
self.cu.execute(qry) |
|
45 |
return self.cu.fetchall() |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
46 |
return res |
31 | 47 |
def update_filename(self, oldname, newname): |
48 |
self.cu.execute("""UPDATE Library |
|
49 |
SET path = '%s' |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
50 |
WHERE path = '%s';""" % (sql_quote(newname), |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
51 |
sql_quote(oldname))) |
31 | 52 |
def erase_filename(self, name): |
53 |
self.cu.execute("""DELETE FROM Library |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
54 |
WHERE path = '%s';""" % sql_quote(name)) |
31 | 55 |
def erase_uid(self, uid): |
56 |
self.cu.execute("""BEGIN TRANSACTION; |
|
57 |
DELETE FROM Library WHERE uid = %d; |
|
39 | 58 |
DELETE FROM Ratings WHERE uid = %d; |
31 | 59 |
DELETE FROM Acoustic WHERE uid = %d; |
60 |
COMMIT;""" % (uid, uid, uid)) |
|
61 |
def erase_sid(self, sid): |
|
62 |
self.cu.execute("""BEGIN TRANSACTION; |
|
63 |
DELETE FROM Library WHERE sid = %d; |
|
64 |
DELETE FROM Info WHERE sid = %d; |
|
65 |
DELETE FROM Last WHERE sid = %d; |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
66 |
COMMIT;""" % (sid, sid, sid)) |
31 | 67 |
def erase_path(self, path): |
68 |
self.cu.execute("DELETE FROM Library WHERE path = '%s';" \ |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
69 |
% sql_quote(path)) |
32 | 70 |
def get_paths(self, uids = None, sids = None): |
71 |
qry = "SELECT uid, sid, path FROM Library" |
|
72 |
first = 1 |
|
73 |
for uid in uids: |
|
74 |
if first: |
|
75 |
qry += ' WHERE' |
|
76 |
first = 0 |
|
77 |
else: |
|
78 |
qry += ' OR' |
|
79 |
qry += " uid = %d" % uid |
|
80 |
for uid in uids: |
|
81 |
if first: |
|
82 |
qry += ' WHERE' |
|
83 |
first = 0 |
|
84 |
else: |
|
85 |
qry += ' OR' |
|
86 |
qry += " sid = %d" % uid |
|
87 |
qry += ';' |
|
88 |
self.cu.execute(qry) |
|
89 |
return self.cu.fetchall() |
|
90 |
def get_ratings(self, min = 0, max = 150): |
|
39 | 91 |
self.cu.execute('''SELECT Ratings.uid, Ratings.rating |
92 |
FROM Ratings |
|
93 |
WHERE Ratings.rating >= %d |
|
94 |
AND Ratings.rating <= %d |
|
95 |
ORDER BY Ratings.rating;''' % (min, max)) |
|
32 | 96 |
return self.cu.fetchall() |
31 | 97 |
def get_acoustics(self, uids = None): |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
98 |
qry = "SELECT uid, bpm, spectrum FROM Acoustic" |
31 | 99 |
first = 1 |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
100 |
if uids: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
101 |
for uid in uids: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
102 |
if first: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
103 |
qry += ' WHERE' |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
104 |
first = 0 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
105 |
else: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
106 |
qry += ' OR' |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
107 |
qry += " uid = %d" % uid |
31 | 108 |
qry += ';' |
109 |
self.cu.execute(qry) |
|
32 | 110 |
return self.cu.fetchall() |
31 | 111 |
def get_infos(self, sids = None): |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
112 |
qry = "SELECT sid, artist, title FROM Info" |
31 | 113 |
first = 1 |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
114 |
if sids: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
115 |
for sid in sids: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
116 |
if first: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
117 |
qry += ' WHERE' |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
118 |
first = 0 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
119 |
else: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
120 |
qry += ' OR' |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
121 |
qry += " sid = %d" % id |
31 | 122 |
qry += ';' |
123 |
self.cu.execute(qry) |
|
32 | 124 |
return self.cu.fetchall() |
31 | 125 |
def get_last(self, sids = None): |
126 |
qry = "SELECT sid, last FROM Last" |
|
127 |
first = 1 |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
128 |
if sids: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
129 |
for sid in sids: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
130 |
if first: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
131 |
qry += ' WHERE' |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
132 |
first = 0 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
133 |
else: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
134 |
qry += ' OR' |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
135 |
qry += " sid = %d" % id |
31 | 136 |
qry += ';' |
137 |
self.cu.execute(qry) |
|
32 | 138 |
return self.cu.fetchall() |
31 | 139 |
def get_uid_by_path(self, path): |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
140 |
entries = self.get_library_entry(path = path) |
31 | 141 |
return map(lambda x: x[1], entries) |
34
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
142 |
def get_alike(self, uid): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
143 |
self.cu.execute('SELECT sid FROM Library WHERE uid = %d;' % uid) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
144 |
sids = self.cu.fetchall() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
145 |
res = {} |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
146 |
for sid in sids: |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
147 |
sid = int(sid) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
148 |
self.cu.execute("SELECT uid FROM Library " |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
149 |
"WHERE sid = %d;" % sid) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
150 |
res[sid] = [] |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
151 |
for uid in self.cu.fetchall(): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
152 |
res[sid].append(int(uid)) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
153 |
return res |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
154 |
def get_ratings_and_paths(self, uids = None): |
39 | 155 |
qry = '''SELECT i.uid, rating, path, last |
156 |
FROM Identify i NATURAL INNER JOIN Ratings NATURAL INNER JOIN Library NATURAL INNER JOIN Last''' |
|
31 | 157 |
if uids: |
39 | 158 |
qry += ' WHERE (i.uid = %d' % (uids.pop()) |
31 | 159 |
for uid in uids: |
39 | 160 |
qry += ' OR i.uid = %d' % uid |
31 | 161 |
qry += ')' |
162 |
qry += ';' |
|
32 | 163 |
self.cu.execute(qry) |
31 | 164 |
# Better to fetch everything since locking can really mess |
165 |
# things in imms plugin. |
|
166 |
results = {} |
|
32 | 167 |
tune = self.cu.fetchone() |
31 | 168 |
while tune: |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
169 |
uid = int(tune[0]) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
170 |
if results.has_key(uid): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
171 |
results[uid]['path'].append(tune[2]) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
172 |
else: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
173 |
results[uid] = { |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
174 |
'rating' : int(tune[1]), |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
175 |
'path' : [ tune[2] ], |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
176 |
'last' : int(tune[3])} |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
177 |
tune = self.cu.fetchone() |
31 | 178 |
return results |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
179 |
def get_ratings_and_infos(self): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
180 |
self.cu.execute('''SELECT r.rating, i.artist, i.title |
39 | 181 |
FROM Library l, Ratings r, Info i |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
182 |
WHERE l.uid = r.uid AND l.sid = i.sid;''') |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
183 |
return self.cu.fetchall() |
34
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
184 |
def clean_info(self): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
185 |
self.cu.execute("""DELETE FROM Info |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
186 |
WHERE sid NOT IN |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
187 |
(SELECT sid FROM Library);""") |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
188 |
def clean_last(self): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
189 |
self.cu.execute("""DELETE FROM Last |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
190 |
WHERE sid NOT IN |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
191 |
(SELECT sid FROM Library);""") |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
192 |
def clean_rating(self): |
39 | 193 |
self.cu.execute("""DELETE FROM Ratings |
34
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
194 |
WHERE uid NOT IN |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
195 |
(SELECT uid FROM Library);""") |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
196 |
def clean_acoustic(self): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
197 |
self.cu.execute("""DELETE FROM Acoustic |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
198 |
WHERE uid NOT IN |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
199 |
(SELECT uid FROM Library);""") |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
200 |
def clean_correlations(self): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
201 |
self.cu.execute("""DELETE FROM Correlations |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
202 |
WHERE origin NOT IN (SELECT sid FROM Library) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
203 |
OR destination NOT IN (SELECT sid FROM Library);""") |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
204 |
def clean_all(self): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
205 |
self.cu.execute("BEGIN TRANSACTION;") |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
206 |
self.clean_info() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
207 |
self.clean_last() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
208 |
self.clean_rating() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
209 |
self.clean_acoustic() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
210 |
self.clean_correlations() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
211 |
self.cu.execute("COMMIT;") |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
212 |
|
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
213 |
class IMMSCleaner: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
214 |
def __init__(self, db): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
215 |
self.db = db |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
216 |
def check_uid(self, uid): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
217 |
lib = self.db.get_library_entry(uid = uid) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
218 |
if len(lib) == 0: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
219 |
print >> _log, "Erased uid = ", uid |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
220 |
self.db.erase_uid(uid) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
221 |
def check_sid(self, sid): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
222 |
lib = self.db.get_library_entry(sid = sid) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
223 |
if len(lib) == 0: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
224 |
print >> _log, "Erased sid = ", sid |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
225 |
self.db.erase_sid(sid) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
226 |
def is_path_in_db(self, path): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
227 |
return len(self.db.get_library_entry(path = path)) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
228 |
# Note: I doesn't much how I handle the two following functions... |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
229 |
# May be I must just have the second one and handle everything |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
230 |
# else in the derived class. |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
231 |
def check_and_edit_path(self, path, uid, sid): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
232 |
"""Must return the new path, None to remove |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
233 |
it. If the new file name is already in the Db, |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
234 |
it will be skip. The skip is more efficient if path |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
235 |
is return. |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
236 |
This is the default handler which always skip the file by |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
237 |
returning path directly. |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
238 |
""" |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
239 |
# The right thing (but not safe) would be to erase the |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
240 |
# file if it already exist in the db... But I find it |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
241 |
# too much unsafe... Erasing a file shouldn't be easy to |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
242 |
# do. |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
243 |
return path |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
244 |
def clean_library(self): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
245 |
lib = self.db.get_library_entry() |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
246 |
print >> _log, "Processing %d entries" % len(lib) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
247 |
deleted_uids = [] |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
248 |
deleted_sids = [] |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
249 |
for entry in lib: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
250 |
path, uid, sid = entry |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
251 |
uid = int(uid) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
252 |
sid = int(sid) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
253 |
newfile = self.check_and_edit_path(path, uid, sid) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
254 |
if not newfile: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
255 |
print >> _log, "Erasing ", path |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
256 |
self.db.erase_filename(path) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
257 |
deleted_uids.append(uid) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
258 |
deleted_sids.append(sid) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
259 |
elif (path == newfile): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
260 |
pass |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
261 |
elif self.is_path_in_db(newfile): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
262 |
print >> _log, "Skipping ", path |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
263 |
pass |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
264 |
else: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
265 |
print >> _log, "Renaming ", path, " into ", newfile |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
266 |
self.db.update_filename(path, newfile) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
267 |
map(self.check_uid, unique(deleted_uids)) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
268 |
map(self.check_sid, unique(deleted_sids)) |
34
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
269 |
## def clean_rating(self): |
39 | 270 |
## print >> _log, "Clean Ratings" |
34
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
271 |
## rates = self.db.get_ratings() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
272 |
## rates = unique(map(lambda x: x[0], rates)) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
273 |
## map(self.check_uid, rates) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
274 |
## def clean_acoustic(self): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
275 |
## print >> _log, "Clean Acoustic" |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
276 |
## uids = self.db.get_acoustics() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
277 |
## uids = map(lambda x: x[0], uids ) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
278 |
## map(self.check_uid, uids) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
279 |
## def clean_info(self): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
280 |
## print >> _log, "Clean Info" |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
281 |
## sids = map(lambda x: x[0], self.db.get_infos()) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
282 |
## map(self.check_sid, sids) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
283 |
## def clean_last(self): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
284 |
## print >> _log, "Clean Last" |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
285 |
## sids = map(lambda x: x[0], self.db.get_last()) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
286 |
## map(self.check_sid, sids) |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
287 |
def clean_all(self): |
38 | 288 |
self.clean_library() |
34
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
289 |
## self.clean_rating() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
290 |
## self.clean_acoustic() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
291 |
## self.clean_info() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
292 |
## self.clean_last() |
38 | 293 |
self.db.clean_all() |