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