This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
351 lines (308 loc) · 10.8 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import os, sys, md5, time
if 'modules' not in sys.path: sys.path.append('modules')
import util, organize
from sqlalchemy import create_engine, event
from sqlalchemy import Column, Integer, BigInteger, String, Boolean, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, backref, \
joinedload
Base = declarative_base()
Session = sessionmaker()
class MultipleRootError(Exception):
def __init__(self, count):
message = "Error: expected one root Folder entry; found %s." % count
Exception.__init__(message)
# Paths are stored in cdp.db databases as if cdp.db resided in os.curdir
# In practice, the database is loaded from program.py, two levels above
# the database. This database controller must be aware of that when
# using os.path methods, etc. Thus path from the database root is called 'path'
# and path from os.curdir is called 'rel_path'.
path_to_root = None
root_join = lambda a: os.path.join(path_to_root, a)
artists = []
CONTENTS_HASH_IGNORE = ["cdp.db-journal"]
#safe
def get_contents_hash(path):
mr_itchy = md5.md5()
contents = os.listdir(path)
for node in CONTENTS_HASH_IGNORE:
if node in contents:
contents.remove(node)
for node in contents:
mr_itchy.update(node)
return mr_itchy.hexdigest()
#safe
load_listener = lambda target, context: target.on_load()
#externally safe unless stated
class Folder(Base):
__tablename__ = 'folders'
path = Column(String, primary_key = True)
def __eq__(self, other):
return self.path == other.path
root = Column(Boolean)
parent_path = Column(String, ForeignKey('folders.path'))
parent = relationship("Folder",
remote_side="Folder.path",
backref = "children")
hash = Column(String)
#evaluate
def __init__(self, path, parent = None):
self.path = path
self.rel_path = root_join(path)
if parent:
self.parent = parent
else:
self.root = True
self.update_hash()
#evaluate
@staticmethod
def build(path = None, parent = None):
rel_path = root_join(path or ".")
folder = Folder(path or ".", parent)
for node in os.listdir(rel_path):
rel_node = os.path.join(rel_path, node)
if path: node = os.path.join(path, node)
if os.path.isfile(rel_node):
node = File(node, folder)
folder.files.append(node)
else:
node = Folder.build(node, folder)
return folder
def update_hash(self):
self.hash = get_contents_hash(self.rel_path)
def check_fs(self):
return os.path.exists(self.rel_path) \
and self.hash == get_contents_hash(self.rel_path)
def on_load(self):
self.rel_path = root_join(self.path)
def __str__(self):
return self.__repr__()
def __repr__(self):
return "<Folder: '%s'>" % self.path
event.listen(Folder, 'load', load_listener)
class File(Base):
__tablename__ = 'files'
path = Column(String, primary_key = True)
def __eq__(self, other):
return self.path == other.path
supported = Column(Boolean)
size = Column(BigInteger)
track = Column(String)
album = Column(String)
artist = Column(String)
album_artist = Column(String)
artist_id = Column(Integer, ForeignKey("artists.id"))
parent_path = Column(String, ForeignKey("folders.path"))
parent = relationship(Folder, backref = "files")
#evaluate
def __init__(self, path, parent = None):
self.path = path
if parent:
self.parent = parent
if util.is_supported(root_join(path)):
self.supported = True
tags = util.get_metadata(root_join(path))
self.artist = tags['artist']
self.album_artist = tags['performer']
if not self.album_artist:
self.album_artist = self.artist
self.album = tags['album']
self.track = tags['title']
else:
self.supported = False
self.size = os.path.getsize(root_join(self.path))
self.rel_path = root_join(self.path)
self.change_alerted = False
#safe
def get_dict(self):
return {'title' : self.track,
'album' : self.album,
'artist': self.artist}
#safe
def check_fs(self):
return os.path.exists(self.rel_path) \
and self.supported \
and os.path.getsize(self.rel_path) == self.size
#safe
def on_load(self):
self.rel_path = root_join(self.path)
def __str__(self):
return self.__repr__()
def __repr__(self):
return "<File: '%s'>" % self.path
event.listen(File, 'load', load_listener)
class Artist(Base):
__tablename__ = 'artists'
id = Column(Integer, primary_key = True)
def __eq__(self, other):
return self.id == other.id
name = Column(String)
songs = relationship("File", backref="artist_ref")
#evaluate
def __init__(self, name):
self.name = name
def __str__(self):
return `self`
def __repr__(self):
return "<Artist \"%s\" with %s song%s>" % (self.name, len(self.songs),
'' if len(self.songs) == 1 else 's')
#safe
def connect(root_path, db_path = None):
global root, path_to_root, engine, artists
path_to_root = root_path
if not db_path:
db_path = 'sqlite:///' + root_join('cdp.db')
engine = create_engine(db_path)
engine.text_factory = str
Session.configure(bind=engine)
session = Session()
Base.metadata.bind = engine
Base.metadata.create_all()
print "Reading database."
root = get_root(session)
if not root:
print "Builing database. This may take a while."
build(session)
root = get_root(session)
session.commit()
artists = load_artists(session)
session.close()
session = None
# must be called after tables have been created
#internal
def build(session):
root = Folder.build()
session.add(root)
for song in session.query(File).filter(File.supported == True):
add_to_artist(session, song)
def add_to_artist(session, file_object):
artist_query = session.query(Artist) \
.filter(Artist.name == file_object.album_artist)
if artist_query.count():
artist_query.one().songs.append(file_object)
else:
artist = Artist(file_object.album_artist)
artist.songs.append(file_object)
session.add(artist)
#internal
def add_dir(src, delete = False):
global artists
session = Session()
root = get_root(session)
def handle(src, errors = []):
for f in map(lambda f: os.path.join(src, f), os.listdir(src)):
if os.path.isdir(f):
errors = handle(f, errors)
if not os.listdir(f):
os.rmdir(f)
elif os.path.splitext(f)[1] in util.FORMATS:
file_path, e = organize.moveFile(f, path_to_root, delete)
if e:
errors += (f, file_path, e)
else:
build_structure(session, file_path, root = root)
return errors
errors = handle(src)
session.commit()
artists = load_artists(session)
session.close()
return errors
#internal
#rename
def build_structure(session, file_path, root = None):
if not root: root = get_root(session)
print "path to root: " + path_to_root
print "file_path: " + file_path
path = util.path_relative_to(path_to_root, file_path)
pathsplit = util.split(path)
file_segment = pathsplit.pop()
cumulative_path = ""
root.update_hash()
parent = root
for segment in pathsplit:
cumulative_path = os.path.join(cumulative_path, segment)
folder_q = session.query(Folder) \
.filter(Folder.path == cumulative_path)
if folder_q.count():
folder = folder_q.one()
folder.update_hash()
parent = folder
else:
parent = Folder(cumulative_path, parent)
f = File(path, parent)
add_to_artist(session, f)
#external
def remove(file_object):
"""Removes a file from the database. Updates the filesystem accordingly"""
if type(file_object) is not File:
raise TypeError("file_object must be of type database.File")
session = Session()
file_object = session.query(File).filter(File.path == file_object.path).one()
parent = file_object.parent
parent.files.remove(file_object)
artist = file_object.artist_ref
artist.songs.remove(file_object)
os.remove(file_object.rel_path)
session.delete(file_object)
parent = remove_if_empty(session, parent)
if parent: parent.update_hash() # parent is only none if root is removed
if len(artist.songs) == 0:
session.delete(artist)
session.commit()
session.close()
#internal
def remove_if_empty(session, folder_object):
"""Returns the parent of the highest-up removed folder.
Returns the folder itself if not empty.
Returns None if root is removed, which it shouldn't be."""
while len(os.listdir(folder_object.rel_path)) == 0:
parent = folder_object.parent
os.rmdir(folder_object.rel_path)
session.delete(folder_object)
folder_object = parent
if not folder_object: # This shouldn't happen, root is never empty
break # because of cdp.db file.
return folder_object
#internal
def get_root(session):
query = session.query(Folder).filter(Folder.root == True) \
## .options(joinedload("*"),
## joinedload("*.*")
## )
count = query.count()
if count == 1:
return query.one()
elif count > 1:
raise MultipleRootError(query.count())
#internal
def load_artists(session):
query = session.query(Artist).options(joinedload("songs"))
if query.count(): return query.all()
#external
def hard_reset():
global root
session = Session()
print "Rebuilding database from scratch. This may take a while."
Base.metadata.drop_all()
session.commit()
Base.metadata.create_all()
build(session)
session.commit()
artists = load_artists(session)
root = get_root(session)
session.close()
#safe
def scan(wait_seconds = (0.1)):
session = Session()
def scan(item, dirty = []):
if (type(item) != File or item.supported) \
and not item.check_fs():
dirty.append(item)
time.sleep(wait_seconds)
if type(item) == Folder:
for i in item.children + item.files:
scan(i, dirty)
return dirty
dirty = scan(get_root(session))
session.close()
return dirty