Source

beets / beets / mediafile.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# This file is part of beets.
# Copyright 2010, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Handles low-level interfacing for files' tags. Wraps Mutagen to
automatically detect file types and provide a unified interface for a useful
subset of music files' tags.

Usage:

    >>> f = MediaFile('Lucy.mp3')
    >>> f.title
    u'Lucy in the Sky with Diamonds'
    >>> f.artist = 'The Beatles'
    >>> f.save()

A field will always return a reasonable value of the correct type, even if no
tag is present. If no value is available, the value will be false (e.g., zero
or the empty string).
"""

import mutagen
import datetime
import re

__all__ = ['FileTypeError', 'MediaFile']

# Currently allowed values for type:
# mp3, mp4
class FileTypeError(IOError):
    pass





#### flags used to define fields' behavior ####

class Enumeration(object):
    def __init__(self, *values):
        for value, equiv in zip(values, range(1, len(values)+1)):
            setattr(self, value, equiv)
# determine style of packing if any
packing = Enumeration('SLASHED', # pair delimited by /
                      'TUPLE',   # a python tuple of 2 items
                      'DATE'     # YYYY-MM-DD
                     )

class StorageStyle(object):
    """Parameterizes the storage behavior of a single field for a certain tag
    format.
    """
    def __init__(self,
                 # The Mutagen key used to access the data for this field.
                 key,
                 # Store item as a single object or as first element of a list.
                 list_elem = True,
                 # Which type the value is stored as (unicode, int, or bool).
                 as_type = unicode,
                 # If this value is packed in a multiple-value storage unit,
                 # which type of packing (in the packing enum). Otherwise,
                 # None. (Makes as_type irrelevant).
                 packing = None,
                 # If the value is packed, in which position it is stored.
                 pack_pos = 0,
                 # ID3 storage only: match against this 'desc' field as well
                 # as the key.
                 id3_desc = None):
        self.key = key
        self.list_elem = list_elem
        self.as_type = as_type
        self.packing = packing
        self.pack_pos = pack_pos
        self.id3_desc = id3_desc




#### dealing with packings ####

class Packed(object):
    """Makes a packed list of values subscriptable. To access the packed output
    after making changes, use packed_thing.items.
    """
    
    def __init__(self, items, packstyle, none_val=0, out_type=int):
        """Create a Packed object for subscripting the packed values in items.
        The items are packed using packstyle, which is a value from the
        packing enum. none_val is returned from a request when no suitable
        value is found in the items. Vales are converted to out_type before
        they are returned.
        """
        self.items = items
        self.packstyle = packstyle
        self.none_val = none_val
        self.out_type = out_type
    
    def __getitem__(self, index):
        if not isinstance(index, int):
            raise TypeError('index must be an integer')
    
        if self.items is None:
            return self.none_val

        items = self.items
        if self.packstyle == packing.DATE:
            # Remove time information from dates. Usually delimited by
            # a "T" or a space.
            items = re.sub(r'[Tt ].*$', '', unicode(items))
    
        # transform from a string packing into a list we can index into
        if self.packstyle == packing.SLASHED:
            seq = unicode(items).split('/')
        elif self.packstyle == packing.DATE:
            seq = unicode(items).split('-')
        elif self.packstyle == packing.TUPLE:
            seq = items # tuple: items is already indexable
    
        try:
            out = seq[index]
        except:
            out = None
    
        if out is None or out == self.none_val or out == '':
            return self.out_type(self.none_val)
        else:
            return self.out_type(out)
    
    def __setitem__(self, index, value):

        if self.packstyle in (packing.SLASHED, packing.TUPLE):
            # SLASHED and TUPLE are always two-item packings
            length = 2
        else:
            # DATE can have up to three fields
            length = 3
    
        # make a list of the items we'll pack
        new_items = []
        for i in range(length):
            if i == index:
                next_item = value
            else:
                next_item = self[i]
            new_items.append(next_item)
    
        if self.packstyle == packing.DATE:
            # Truncate the items wherever we reach an invalid (none) entry.
            # This prevents dates like 2008-00-05.
            for i, item in enumerate(new_items):
                if item == self.none_val or item is None:
                    del(new_items[i:]) # truncate
                    break
    
        if self.packstyle == packing.SLASHED:
            self.items = '/'.join(map(unicode, new_items))
        elif self.packstyle == packing.DATE:
            field_lengths = [4, 2, 2] # YYYY-MM-DD
            elems = []
            for i, item in enumerate(new_items):
                elems.append( ('%0' + str(field_lengths[i]) + 'i') % item )
            self.items = '-'.join(elems)
        elif self.packstyle == packing.TUPLE:
            self.items = new_items
        


class MediaField(object):
    """A descriptor providing access to a particular (abstract) metadata
    field. out_type is the type that users of MediaFile should see and can
    be unicode, int, or bool. id3, mp4, and flac are StorageStyle instances
    parameterizing the field's storage for each type.
    """
    
    def __init__(self,
            # The field's semantic (exterior) type.
            out_type = unicode,
            # A hash whose keys are 'mp3', 'mp4', and 'flac' and whose values
            # are StorageStyle instances parameterizing the field's storage for
            # each type.
            **kwargs
            ):
            
        self.out_type = out_type
        if not set(['mp3', 'mp4', 'flac']) == set(kwargs):
            raise TypeError('MediaField constructor must have keyword '
                            'arguments mp3, mp4, and flac only')
        self.styles = kwargs
    
    def _fetchdata(self, obj):
        """Get the value associated with this descriptor's key (and id3_desc if
        present) from the mutagen tag dict. Unwraps from a list if
        necessary.
        """
        style = self._style(obj)
        
        try:
            # fetch the value, which may be a scalar or a list
            if obj.type == 'mp3':
                if style.id3_desc is not None: # also match on 'desc' field
                    frames = obj.mgfile.tags.getall(style.key)
                    entry = None
                    for frame in frames:
                        if frame.desc == style.id3_desc:
                            entry = frame.text
                            break
                    if entry is None: # no desc match
                        return None
                else:
                    entry = obj.mgfile[style.key].text
            else:
                entry = obj.mgfile[style.key]
            
            # possibly index the list
            if style.list_elem:
                if entry: # List must have at least one value.
                    return entry[0]
                else:
                    return None
            else:
                return entry

        except KeyError: # the tag isn't present
            return None
    
    def _storedata(self, obj, val):
        """Store val for this descriptor's key in the tag dictionary. Store it
        as a single-item list if necessary. Uses id3_desc if present.
        """
        style = self._style(obj)
        
        # wrap as a list if necessary
        if style.list_elem: out = [val]
        else:               out = val
        
        if obj.type == 'mp3':
            if style.id3_desc is not None: # match on desc field
                frames = obj.mgfile.tags.getall(style.key)
                
                # try modifying in place
                found = False
                for frame in frames:
                    if frame.desc == style.id3_desc:
                        frame.text = out
                        found = True
                        break
                
                # need to make a new frame?
                if not found:
                    frame = mutagen.id3.Frames[style.key](
                                encoding=3, desc=style.id3_desc, text=val)
                    obj.mgfile.tags.add(frame)
            
            else: # no match on desc; just replace based on key
                frame = mutagen.id3.Frames[style.key](encoding=3, text=val)
                obj.mgfile.tags.setall(style.key, [frame])
        else:
            obj.mgfile[style.key] = out
    
    def _style(self, obj): return self.styles[obj.type]
    
    def __get__(self, obj, owner):
        """Retrieve the value of this metadata field.
        """
        style = self._style(obj)
        out = self._fetchdata(obj)
        
        if style.packing:
            out = Packed(out, style.packing)[style.pack_pos]
        
        # return the appropriate type
        if self.out_type == int:
            if out is None:
                return 0
            elif isinstance(out, int) or isinstance(out, float):
                # Just a number.
                return int(out)
            else:
                # Process any other type as a string.
                if not isinstance(out, basestring):
                    out = unicode(out)
                # Get a number from the front of the string.
                out = re.match('[0-9]*', out.strip()).group(0)
                if not out:
                    return 0
                else:
                    return int(out)
        elif self.out_type == bool:
            if out is None:
                return False
            else:
                return bool(int(out)) # should work for strings, bools, ints
        elif self.out_type == unicode:
            if out is None:
                return u''
            else:
                return unicode(out)
        else:
            return out
    
    def __set__(self, obj, val):
        """Set the value of this metadata field.
        """
        style = self._style(obj)
        
        if style.packing:
            p = Packed(self._fetchdata(obj), style.packing)
            p[style.pack_pos] = val
            out = p.items
                
        else: # unicode, integer, or boolean scalar
            out = val
        
            # deal with Nones according to abstract type if present
            if out is None:
                if self.out_type == int:
                    out = 0
                elif self.out_type == bool:
                    out = False
                elif self.out_type == unicode:
                    out = u''
                # We trust that packed values are handled above.
        
            # convert to correct storage type (irrelevant for packed values)
            if style.as_type == unicode:
                if out is None:
                    out = u''
                else:
                    if self.out_type == bool:
                        # store bools as 1,0 instead of True,False
                        out = unicode(int(out))
                    else:
                        out = unicode(out)
            elif style.as_type == int:
                if out is None:
                    out = 0
                else:
                    out = int(out)
            elif style.as_type == bool:
                out = bool(out)
        
        # store the data
        self._storedata(obj, out)

class CompositeDateField(object):
    """A MediaFile field for conveniently accessing the year, month, and day
    fields as a datetime.date object. Allows both getting and setting of the
    component fields.
    """
    def __init__(self, year_field, month_field, day_field):
        """Create a new date field from the indicated MediaFields for the
        component values.
        """
        self.year_field = year_field
        self.month_field = month_field
        self.day_field = day_field
        
    def __get__(self, obj, owner):
        """Return a datetime.date object whose components indicating the
        smallest valid date whose components are at least as large as the
        three component fields (that is, if year == 1999, month == 0, and
        day == 0, then date == datetime.date(1999, 1, 1)). If the components
        indicate an invalid date (e.g., if month == 47), datetime.date.min is
        returned.
        """
        try:
            return datetime.date(max(self.year_field.__get__(obj, owner),
                                     datetime.MINYEAR),
                                 max(self.month_field.__get__(obj, owner), 1),
                                 max(self.day_field.__get__(obj, owner), 1)
                                )
        except ValueError: # Out of range values.
            return datetime.date.min
    
    def __set__(self, obj, val):
        """Set the year, month, and day fields to match the components of the
        provided datetime.date object.
        """
        self.year_field.__set__(obj, val.year)
        self.month_field.__set__(obj, val.month)
        self.day_field.__set__(obj, val.day)



class MediaFile(object):
    """Represents a multimedia file on disk and provides access to its
    metadata.
    """
    
    def __init__(self, path):
        try:
            self.mgfile = mutagen.File(path)
        except mutagen.mp3.HeaderNotFoundError:
            raise FileTypeError('Mutagen could not read file')

        if self.mgfile is None: # Mutagen couldn't guess the type
            raise FileTypeError('file type unsupported by Mutagen')
        elif type(self.mgfile).__name__ == 'M4A' or \
             type(self.mgfile).__name__ == 'MP4':
            self.type = 'mp4'
        elif type(self.mgfile).__name__ == 'ID3' or \
             type(self.mgfile).__name__ == 'MP3':
            self.type = 'mp3'
        elif type(self.mgfile).__name__ == 'FLAC':
            self.type = 'flac'
        else:
            raise FileTypeError('file type unsupported by MediaFile')
        
        # add a set of tags if it's missing
        if not self.mgfile.tags:
            self.mgfile.add_tags()
    
    def save(self):
        self.mgfile.save()
    
    
    #### field definitions ####
    
    title = MediaField(
                mp3  = StorageStyle('TIT2'),
                mp4  = StorageStyle("\xa9nam"), 
                flac = StorageStyle('title')
            )
    artist = MediaField(
                mp3  = StorageStyle('TPE1'),
                mp4  = StorageStyle("\xa9ART"), 
                flac = StorageStyle('artist')
            )     
    album = MediaField(
                mp3  = StorageStyle('TALB'),
                mp4  = StorageStyle("\xa9alb"), 
                flac = StorageStyle('album')
            )
    genre = MediaField(
                mp3  = StorageStyle('TCON'),
                mp4  = StorageStyle("\xa9gen"), 
                flac = StorageStyle('genre')
            )
    composer = MediaField(
                mp3  = StorageStyle('TCOM'),
                mp4  = StorageStyle("\xa9wrt"), 
                flac = StorageStyle('composer')
            )
    grouping = MediaField(
                mp3  = StorageStyle('TIT1'),
                mp4  = StorageStyle("\xa9grp"), 
                flac = StorageStyle('grouping')
            )
    year = MediaField(out_type=int,
                mp3  = StorageStyle('TDRC',
                                    packing = packing.DATE,
                                    pack_pos = 0),
                mp4  = StorageStyle("\xa9day",
                                    packing = packing.DATE,
                                    pack_pos = 0), 
                flac = StorageStyle('date',
                                    packing = packing.DATE,
                                    pack_pos = 0)
            )
    month = MediaField(out_type=int,
                mp3  = StorageStyle('TDRC',
                                    packing = packing.DATE,
                                    pack_pos = 1),
                mp4  = StorageStyle("\xa9day",
                                    packing = packing.DATE,
                                    pack_pos = 1), 
                flac = StorageStyle('date',
                                    packing = packing.DATE,
                                    pack_pos = 1)
            )
    day = MediaField(out_type=int,
                mp3  = StorageStyle('TDRC',
                                    packing = packing.DATE,
                                    pack_pos = 2),
                mp4  = StorageStyle("\xa9day",
                                    packing = packing.DATE,
                                    pack_pos = 2), 
                flac = StorageStyle('date',
                                    packing = packing.DATE,
                                    pack_pos = 2)
            )
    date = CompositeDateField(year, month, day)
    track = MediaField(out_type = int,
                mp3  = StorageStyle('TRCK',
                                    packing = packing.SLASHED,
                                    pack_pos = 0),
                mp4  = StorageStyle('trkn',
                                    packing = packing.TUPLE,
                                    pack_pos = 0),
                flac = StorageStyle('tracknumber')
            )
    tracktotal = MediaField(out_type = int,
                mp3  = StorageStyle('TRCK',
                                    packing = packing.SLASHED,
                                    pack_pos = 1),
                mp4  = StorageStyle('trkn',
                                    packing = packing.TUPLE,
                                    pack_pos = 1),
                flac = StorageStyle('tracktotal')
            )
    disc = MediaField(out_type = int,
                mp3  = StorageStyle('TPOS',
                                    packing = packing.SLASHED,
                                    pack_pos = 0),
                mp4  = StorageStyle('disk',
                                    packing = packing.TUPLE,
                                    pack_pos = 0),
                flac = StorageStyle('disc')
            )
    disctotal = MediaField(out_type = int,
                mp3  = StorageStyle('TPOS',
                                    packing = packing.SLASHED,
                                    pack_pos = 1),
                mp4  = StorageStyle('disk',
                                    packing = packing.TUPLE,
                                    pack_pos = 1),
                flac = StorageStyle('disctotal')
            )
    lyrics = MediaField(
                mp3  = StorageStyle('USLT',
                                    list_elem = False,
                                    id3_desc = u''),
                mp4  = StorageStyle("\xa9lyr"), 
                flac = StorageStyle('lyrics')
            )
    comments = MediaField(
                mp3  = StorageStyle('COMM', id3_desc = u''),
                mp4  = StorageStyle("\xa9cmt"), 
                flac = StorageStyle('description')
            )
    bpm = MediaField(out_type = int,
                mp3  = StorageStyle('TBPM'),
                mp4  = StorageStyle('tmpo', as_type = int), 
                flac = StorageStyle('bpm')
            )
    comp = MediaField(out_type = bool,
                mp3  = StorageStyle('TCMP'),
                mp4  = StorageStyle('cpil',
                                    list_elem = False,
                                    as_type = bool), 
                flac = StorageStyle('compilation')
            )

    @property
    def length(self):
        return self.mgfile.info.length

    @property
    def bitrate(self):
        if self.type == 'flac':
            return self.mgfile.info.sample_rate * \
                   self.mgfile.info.bits_per_sample
        else:
            return self.mgfile.info.bitrate