Source

spazzer / spazzer / collection / model.py

Full commit
  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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
"""
Database model for collection metadata
"""
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.schema import Column
from sqlalchemy.types import Unicode, Integer, DateTime
from sqlalchemy.sql import or_
from meta import _s
import os
from cStringIO import StringIO
import zipfile

from sqlalchemy import types
try:
    from sqlalchemy.dialects.mysql.base import MSBinary
except ImportError:
    from sqlalchemy.databases.mysql import MSBinary

from sqlalchemy.schema import Column
import uuid


Base = declarative_base()


class UUID(types.TypeDecorator):
    """
    UUID column type should be compatible with MS SqlServer guid type.
    """
    impl = MSBinary

    def __init__(self):
        self.impl.length = 16
        types.TypeDecorator.__init__(self, length=self.impl.length)

    def process_bind_param(self, value, dialect=None):
        if value and isinstance(value, uuid.UUID):
            return value.bytes
        elif value and not isinstance(value, uuid.UUID):
            raise ValueError('value %s is not a valid uuid.UUID' % value)
        else:
            return None

    def process_result_value(self, value, dialect=None):
        if value:
            return uuid.UUID(bytes=value)
        else:
            return None

    def is_mutable(self):
        return False

id_column_name = "id"


def id_column():
    """
    util method for primary key column declaration with UUID type
    """
    import uuid
    return Column(id_column_name, UUID(), primary_key=True, default=uuid.uuid4)


class Queryable(object):
    """
    adds query class method as a convenience.
    """

    @classmethod
    def query(cls, *attrs):
        """
        attrs to select if None then entire class
        """
        if len(attrs) == 0:
            attrs = [cls]
        return _s().query(*attrs)


def cleanse_filename(fname):
    """
    make filename safe for fat32(lowest common denominator)
    since we're going off tag info which can (and likely will) include
    characters that are not handled too well in a filename. this function
    will just replace any offending characters with "_"
    """
    fname = os.path.split(fname)[1]
    INVALID = u"\"*/:<>?\\|"
    VALID_RANGE = range(128)
    result = []
    for c in fname:
        val = ord(c)
        if not c in INVALID and val in VALID_RANGE:
            result.append(c)
        else:
            result.append(u"_")
    result = u"".join(result)
    return result.replace(u" ", u"_")


class MountPoint(Base, Queryable):
    """
    A collection is made up of filesystem mountpoints(directories).
    we hold them for periodic scanning in the db rather than
    make the user edit another config file.
    """
    __tablename__ = "mount_points"
    id = id_column() #perhaps superfulous
    _mount = Column("mount", Unicode(1024), unique = True)

    def __init__(self, mount):
        self._mount = mount

    def _get_mount(self):
        """
        depending on how it was saved to db we may need to append a path
        sep so that we can traverse up the rest of it during scanning
        """
        if not self._mount.endswith(os.path.sep):
            return "%s%s" % (self._mount, os.path.sep)
        else:
            return self._mount

    mount = property(_get_mount)


class FileRecord(Base, Queryable):
    """
    tag information that was gleaned from a file during scan.
    as far as I can figure, there's no reasonable way to de-normalize this
    without making assumptions that will be invalid by someone's obscure
    music taste. So the approach is we are merely indexing the tagf information
    and providing canned queries to help navigate to what you want to hear.

    anything else is an excercise in enginering masturbation in my opinion.
    """
    __tablename__ = "files"
    id = id_column()
    file_name = Column(Unicode(1024), unique = True, index = True)
    artist = Column(Unicode(255), nullable = True, index = True)
    album = Column(Unicode(255), nullable = True, index = True)
    title = Column(Unicode(255), nullable = True, index = True)
    year = Column(Integer, nullable = True, index = True)
    track = Column(Integer, nullable = True)
    create_date = Column(DateTime(), nullable = False)
    modify_date = Column(DateTime(), nullable = False)

    @classmethod
    def get_by_id(cls, id):
        """
        by primary key (uuid)
        """
        return cls.query().get(id)

    @classmethod
    def get_by_filename(cls, file_name):
        """
        by unique filename, needs entire path
        """
        return cls.query().filter(cls.file_name == file_name).first()

    def __init__(self, file_name, create_date, modify_date,
                 artist=None,
                 title=None,
                 album=None,
                 year=None,
                 track=None):

        self.file_name = file_name

        self.update(create_date, modify_date,
                    artist,
                    album,
                    title,
                    year,
                    track)

    def update(self, create_date, modify_date,
               artist = None,
               album = None,
               title = None,
               year = None,
               track = None):
        """
        set all attributes
        """
        self.create_date = create_date
        self.modify_date = modify_date
        self.artist = artist
        self.album = album
        self.year = year
        self.track = track
        self.title = title

    def on_compilation(self):
        """
        is this track on an album that has multiple artists?
        """
        if not hasattr(self, "__compilation"):
            self.__compilation = self.query(FileRecord.artist).filter(
                FileRecord.album == self.album).filter(
                FileRecord.year == self.year).distinct().count() > 1
        return self.__compilation

    def _safe_file_name(self):
        """
        generate a fat32 safe filename based on information in the tags
        """
        FMT_STR = "%s - %s - %s (%d) - %s%s"
        return cleanse_filename(FMT_STR % (self.track,
                                            self.artist.replace("/", "\\"),
                                            self.album.replace("/", "\\"),
                                            self.year,
                                            self.title.replace("/", "\\"),
                                       os.path.splitext(self.file_name)[1]))

    safe_file_name = property(_safe_file_name)

#These have nothing to do with db persistence, column/table mapping or anything

class ArtistView(object):
    """
    Represents an Artist view of the collection
    """

    def __init__(self, name):
        self.name = name

    @classmethod
    def query(cls):
        return FileRecord.query(FileRecord.artist).filter(
            FileRecord.artist!=None).distinct()

    @classmethod
    def search(cls, criteria = None):
        """
        Find by artist name, handles the case where 'Artist' and 'The Artist'
        are regarded as the same artist depending on how pretentious you are.
        """
        qry = cls.query()

        if criteria:
            qry = qry.filter(or_(
                FileRecord.artist.like(u"%s" % criteria),
                FileRecord.artist.like(u"The %s" % criteria)))

        results = []

        for result in qry.all():
            results.append(cls(*result))

        return results

    @classmethod
    def get(cls, name):
        """
        find by exact artist name, we're pretending that we have a table
        of artists whose primary key is name.
        """
        result = cls.query().filter(FileRecord.artist==name).first()

        if result and len(result)>0:
            return cls(*result)
        else:
            return None

    def __repr__(self):
        return self.name

    def get_albums(self):
        """
        here for convenience, dispatch to AlbumView
        """
        return AlbumView.get_by_artist(self.name)


class AlbumView(object):
    """
    respresents an album in the collection. and album is loosely defined to
    be a unique(title,year), though i'm sure there are edge cases which make
    this false.
    """

    def __init__(self, name, year):
        self.name = name
        self.year = year

    def __repr__(self):
        return u"%s - %s" % (self.name.title(), self.year)

    @classmethod
    def query(cls):
        return FileRecord.query(FileRecord.album, FileRecord.year).filter(
            FileRecord.album != None).distinct()

    @classmethod
    def get(cls, name, artist = None, year = None):
        """
        We're pretending we have a normalized database, and providing
        a method to select by primary key optionally refined by artist
        """
        qry = cls.query().filter(FileRecord.album==name)

        if artist:
            qry = qry.filter(FileRecord.artist == artist)

        if year:
            qry = qry.filter(FileRecord.year == year)
        result = qry.first()

        if result and len(result)>0:
            return cls(*result)
        else:
            return None

    @classmethod
    def get_by_artist(cls, artist):
        """
        get all albums by artist sorted by year
        """
        results = cls.query().filter(FileRecord.artist==artist).order_by(
            FileRecord.year).all()
        albums = []
        if results and len(results) > 0:
            for result in results:
                albums.append(cls(*result))

            return albums

        return albums

    def get_tracks(self):
        return TrackView.get_by_album(self.name)

    @classmethod
    def search(cls, criteria = None):
        """
        find album where album.name like criteria
        """
        qry = cls.query()
        if criteria:
            qry = qry.filter(FileRecord.album.like(u"%%%s%%" % criteria))

        results = []

        for result in qry.all():
            results.append(cls(*result))

        return results

    def get_zip_file(self):
        """
        build zip file of all tracks associated with this album.
        relying on caller to close the string io
        """
        io = StringIO()
        zf = zipfile.ZipFile(io, "w")
        try:
            for track in self.get_tracks():
                zf.write(track.file_name,
                         track.safe_file_name,
                         zipfile.ZIP_DEFLATED)
        finally:
            zf.close()

        io.reset()
        io.seek(0, 2)
        length = io.tell()
        io.reset()
        return io,\
               cleanse_filename("%s - %s.zip" % (self.name, self.year)),\
               length

    def _get_artist(self):
        """
        if multiple artists, then returns 'Various Artists' otherwise
        the artist name or '(Unknown)' if the tag lacks an artist
        """
        artists = FileRecord.query(FileRecord.artist).distinct().filter(
            FileRecord.album == self.name).filter(
            FileRecord.year == self.year).all()

        if len(artists)>1:
            return "Various Artists"
        elif len(artists) == 1:
            return artists[0][0]
        else:
            return "(Unknown)"

    artist = property(_get_artist)


class TrackView(object):
    """
    nearly identical to filerecord, adds a bit more to present tracks like
    they were coming from normalized data.
    """

    def __init__(self, fileRecord):
        self.__record = fileRecord

    @classmethod
    def query(cls):
        return FileRecord.query()

    @classmethod
    def get(cls, fid):
        val = cls.query().get(fid)
        assert val is not None
        return cls(val)

    @classmethod
    def search(cls, criteria = None):
        qry = cls.query()
        if criteria:
            qry = qry.filter(FileRecord.title.like(u"%%%s%%" % criteria))

        results = []

        for result in qry.all():
            results.append(cls(result))

        return results

    @classmethod
    def get_by_album(cls, album, artist = None, year = None):
        qry = cls.query().filter(FileRecord.album == album)

        if artist:
            qry = qry.filter(FileRecord.artist == artist)

        if year:
            qry = qry.filter(FileRecord.year == year)

        results = qry.order_by(FileRecord.track).all()
        tracks = []
        if results and len(results)>0:
            for result in results:
                tracks.append(cls(result))

        return results

    @classmethod
    def get_by_artist(cls, artist):
        results = cls.query().filter(FileRecord.artist == artist)
        tracks = []
        if results and len(results)>0:
            for result in results:
                tracks.append(cls(result))
        return tracks

    def __getattr__(self, name):
        """
        happy path to staying in sync with FileRecord via delegation
        """
        return getattr(self.__record, name)

    def __repr__(self):
        return u"%d - %s" % (self.track or 0, self.title.title())