author | fabien |
Thu, 12 Feb 2004 09:38:06 -0500 | |
branch | immsview |
changeset 35 | ab72cbd172b8 |
parent 34 | 5bef7600193c |
child 38 | 785c66feccd3 |
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 |
31 | 3 |
import 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): |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
9 |
i = rating - 75 |
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 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
14 |
elif i <= 50: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
15 |
red = (50-i) * 255 / 25 |
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 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
19 |
blue = (i-50) * 255 / 25 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
20 |
return "#%02x%02x%02x" % (red, green, blue) |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
21 |
|
31 | 22 |
class IMMSDb: |
23 |
def __init__(self, dbname = None): |
|
24 |
if not dbname: |
|
25 |
dbname = os.environ['HOME'] + '/.imms/imms.db' |
|
26 |
# autocommit = 1 disable autocommit! |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
27 |
self.cx = sqlite.connect(dbname, autocommit = 1, timeout = 5) |
31 | 28 |
self.cu = self.cx.cursor() |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
29 |
def get_library_entry(self, **kw): |
31 | 30 |
qry = "SELECT path, uid, sid FROM Library"; |
31 |
first = 1 |
|
32 |
for key in kw.keys(): |
|
33 |
if first: |
|
34 |
qry += " WHERE " |
|
35 |
first = 0 |
|
36 |
else: |
|
37 |
qry += " AND " |
|
38 |
if key in ['uid', 'sid']: |
|
39 |
qry += "%s = %d" % (key, kw[key]) |
|
40 |
else: |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
41 |
qry += "%s = '%s'" % (key, sql_quote(kw[key])) |
31 | 42 |
qry += ";" |
43 |
self.cu.execute(qry) |
|
44 |
return self.cu.fetchall() |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
45 |
return res |
31 | 46 |
def update_filename(self, oldname, newname): |
47 |
self.cu.execute("""UPDATE Library |
|
48 |
SET path = '%s' |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
49 |
WHERE path = '%s';""" % (sql_quote(newname), |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
50 |
sql_quote(oldname))) |
31 | 51 |
def erase_filename(self, name): |
52 |
self.cu.execute("""DELETE FROM Library |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
53 |
WHERE path = '%s';""" % sql_quote(name)) |
31 | 54 |
def erase_uid(self, uid): |
55 |
self.cu.execute("""BEGIN TRANSACTION; |
|
56 |
DELETE FROM Library WHERE uid = %d; |
|
57 |
DELETE FROM Rating WHERE uid = %d; |
|
58 |
DELETE FROM Acoustic WHERE uid = %d; |
|
59 |
COMMIT;""" % (uid, uid, uid)) |
|
60 |
def erase_sid(self, sid): |
|
61 |
self.cu.execute("""BEGIN TRANSACTION; |
|
62 |
DELETE FROM Library WHERE sid = %d; |
|
63 |
DELETE FROM Info WHERE sid = %d; |
|
64 |
DELETE FROM Last WHERE sid = %d; |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
65 |
COMMIT;""" % (sid, sid, sid)) |
31 | 66 |
def erase_path(self, path): |
67 |
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
|
68 |
% sql_quote(path)) |
32 | 69 |
def get_paths(self, uids = None, sids = None): |
70 |
qry = "SELECT uid, sid, path FROM Library" |
|
71 |
first = 1 |
|
72 |
for uid in uids: |
|
73 |
if first: |
|
74 |
qry += ' WHERE' |
|
75 |
first = 0 |
|
76 |
else: |
|
77 |
qry += ' OR' |
|
78 |
qry += " uid = %d" % uid |
|
79 |
for uid in uids: |
|
80 |
if first: |
|
81 |
qry += ' WHERE' |
|
82 |
first = 0 |
|
83 |
else: |
|
84 |
qry += ' OR' |
|
85 |
qry += " sid = %d" % uid |
|
86 |
qry += ';' |
|
87 |
self.cu.execute(qry) |
|
88 |
return self.cu.fetchall() |
|
89 |
def get_ratings(self, min = 0, max = 150): |
|
31 | 90 |
self.cu.execute('''SELECT Rating.uid, Rating.rating |
91 |
FROM Rating |
|
92 |
WHERE Rating.rating >= %d |
|
93 |
AND Rating.rating <= %d |
|
94 |
ORDER BY Rating.rating;''' % (min, max)) |
|
32 | 95 |
return self.cu.fetchall() |
31 | 96 |
def get_acoustics(self, uids = None): |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
97 |
qry = "SELECT uid, bpm, spectrum FROM Acoustic" |
31 | 98 |
first = 1 |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
99 |
if uids: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
100 |
for uid in uids: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
101 |
if first: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
102 |
qry += ' WHERE' |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
103 |
first = 0 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
104 |
else: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
105 |
qry += ' OR' |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
106 |
qry += " uid = %d" % uid |
31 | 107 |
qry += ';' |
108 |
self.cu.execute(qry) |
|
32 | 109 |
return self.cu.fetchall() |
31 | 110 |
def get_infos(self, sids = None): |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
111 |
qry = "SELECT sid, artist, title FROM Info" |
31 | 112 |
first = 1 |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
113 |
if sids: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
114 |
for sid in sids: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
115 |
if first: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
116 |
qry += ' WHERE' |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
117 |
first = 0 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
118 |
else: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
119 |
qry += ' OR' |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
120 |
qry += " sid = %d" % id |
31 | 121 |
qry += ';' |
122 |
self.cu.execute(qry) |
|
32 | 123 |
return self.cu.fetchall() |
31 | 124 |
def get_last(self, sids = None): |
125 |
qry = "SELECT sid, last FROM Last" |
|
126 |
first = 1 |
|
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
127 |
if sids: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
128 |
for sid in sids: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
129 |
if first: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
130 |
qry += ' WHERE' |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
131 |
first = 0 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
132 |
else: |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
133 |
qry += ' OR' |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
134 |
qry += " sid = %d" % id |
31 | 135 |
qry += ';' |
136 |
self.cu.execute(qry) |
|
32 | 137 |
return self.cu.fetchall() |
31 | 138 |
def get_uid_by_path(self, path): |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
139 |
entries = self.get_library_entry(path = path) |
31 | 140 |
return map(lambda x: x[1], entries) |
34
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
141 |
def get_alike(self, uid): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
142 |
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
|
143 |
sids = self.cu.fetchall() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
144 |
res = {} |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
145 |
for sid in sids: |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
146 |
sid = int(sid) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
147 |
self.cu.execute("SELECT uid FROM Library " |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
148 |
"WHERE sid = %d;" % sid) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
149 |
res[sid] = [] |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
150 |
for uid in self.cu.fetchall(): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
151 |
res[sid].append(int(uid)) |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
152 |
return res |
33
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
153 |
def get_ratings_and_paths(self, uids = None): |
31 | 154 |
qry = '''SELECT l.uid, r.rating, l.path, ls.last |
32 | 155 |
FROM Library l, Rating r, Last ls |
156 |
WHERE l.uid = r.uid AND l.sid = ls.sid''' |
|
31 | 157 |
if uids: |
158 |
qry += ' AND (l.uid = %d' % (uids.pop()) |
|
159 |
for uid in uids: |
|
160 |
qry += ' OR l.uid = %d' % uid |
|
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 |
ad808d18c693
[svn] Many cleanup, both architecture (division of interface), encoding
fabien
parents:
32
diff
changeset
|
181 |
FROM Library l, Rating r, Info i |
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): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
193 |
self.cu.execute("""DELETE FROM Rating |
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): |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
270 |
## print >> _log, "Clean Rating" |
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): |
34
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
288 |
self.db.clean_all() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
289 |
## self.clean_library() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
290 |
## self.clean_rating() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
291 |
## self.clean_acoustic() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
292 |
## self.clean_info() |
5bef7600193c
[svn] Correction and optimization of the clean_all() interface.
fabien
parents:
33
diff
changeset
|
293 |
## self.clean_last() |