1 # -*- coding: utf-8 -*-
2 ###########################################################################
3 # Copyright (C) 2008 by Andrew Mahone
4 # <andrew.mahone@gmail.com>
6 # Copyright: See COPYING file that comes with this distribution
8 ###########################################################################
10 from mutagen
import FileType
11 from mutagen
.asf
import ASF
12 from mutagen
.flac
import FLAC
, SeekPoint
, CueSheetTrackIndex
13 from mutagen
.monkeysaudio
import MonkeysAudio
14 from mutagen
.mp3
import MP3
15 from mutagen
.mp4
import MP4
16 from mutagen
.ogg
import OggFileType
17 from mutagen
.oggvorbis
import OggVorbis
18 from mutagen
.wavpack
import WavPack
19 from mutagen
.trueaudio
import TrueAudio
20 from mutagen
.optimfrog
import OptimFROG
21 from mutagen
.musepack
import Musepack
22 from audiomangler
.config
import Config
, from_config
23 from audiomangler
.tag
import NormMetaData
24 from audiomangler
.expression
import FileFormat
27 metacache
= getattr(self
, '_meta_cache', (False, False))
28 if metacache
[0] is not getattr(self
, 'filename', None) or metacache
[1] \
29 is not getattr(self
, 'tags', None):
30 self
._meta
_cache
= (getattr(self
, 'filename', None),
31 getattr(self
, 'tags', None))
32 if getattr(self
, 'tags', None) is None:
35 meta
= NormMetaData
.converted(self
)
36 path
= getattr(self
, 'filename', None)
37 if isinstance(path
, basestring
):
38 (meta
['dir'], meta
['name']) = (s
.decode(Config
['fs_encoding'], Config
['fs_encoding_err'] or 'replace') for s
in os
.path
.split(path
))
39 meta
['path'] = path
.decode(Config
['fs_encoding'], Config
['fs_encoding_err'] or 'replace')
40 meta
['basename'] = os
.path
.splitext(meta
['name'])[0]
41 relpath
= getattr(self
, 'relpath', None)
42 if isinstance(relpath
, basestring
):
43 meta
['relpath'] = relpath
.decode(Config
['fs_encoding'], Config
['fs_encoding_err'] or 'replace')
44 reldir
= getattr(self
, 'reldir', None)
45 if isinstance(reldir
, basestring
):
46 meta
['reldir'] = reldir
.decode(Config
['fs_encoding'], Config
['fs_encoding_err'] or 'replace')
47 ext
= getattr(self
, 'ext', None)
50 type_
= getattr(self
, 'type_', None)
56 def _set_meta(self
, value
):
57 NormMetaData
.converted(value
).apply(self
, True)
59 def format(self
, filename
=None, base
=None, preadd
={}, postadd
={}):
60 filename
, base
= from_config('filename', 'base')
61 meta
= NormMetaData(preadd
)
62 meta
.update(self
.meta
.flat())
64 filename
= FileFormat(filename
)
65 return os
.path
.join(base
, filename
.evaluate(meta
))
67 def has_replaygain(self
):
68 return reduce(lambda x
, y
: x
and y
in self
.meta
, ('replaygain_album_gain', 'replaygain_album_peak', 'replaygain_track_gain', 'replaygain_track_peak'), True)
70 _newargs_untuplize
= lambda self
: super(self
.__class
__, self
).__getnewargs
__()[0]
72 SeekPoint
.__getnewargs
__ = _newargs_untuplize
73 CueSheetTrackIndex
.__getnewargs
__ = _newargs_untuplize
75 FileType
.format
= format
76 FileType
.meta
= property(_get_meta
, _set_meta
)
77 FileType
.lossless
= False
78 FileType
.has_replaygain
= has_replaygain
85 MonkeysAudio
.ext
= 'ape'
86 MonkeysAudio
.type_
= 'monkeys'
87 MonkeysAudio
.lossless
= True
92 OggFileType
.ext
= 'ogg'
93 OggVorbis
.type_
= 'oggvorbis'
95 WavPack
.type_
= 'wavpack'
96 WavPack
.lossless
= True
98 TrueAudio
.lossless
= True
100 OptimFROG
.lossless
= True
102 Musepack
.type_
= 'musepack'