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)
54 return getattr(self
,'_meta')
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 def _newargs_untuplize(self
):
71 return super(self
.__class
__, self
).__getnewargs
__()[0]
73 SeekPoint
.__getnewargs
__ = _newargs_untuplize
74 CueSheetTrackIndex
.__getnewargs
__ = _newargs_untuplize
76 FileType
.format
= _format
77 FileType
.meta
= property(_get_meta
, _set_meta
)
78 FileType
.lossless
= False
79 FileType
.has_replaygain
= has_replaygain
86 MonkeysAudio
.ext
= 'ape'
87 MonkeysAudio
.type_
= 'monkeys'
88 MonkeysAudio
.lossless
= True
93 OggFileType
.ext
= 'ogg'
94 OggVorbis
.type_
= 'oggvorbis'
96 WavPack
.type_
= 'wavpack'
97 WavPack
.lossless
= True
99 TrueAudio
.lossless
= True
100 OptimFROG
.ext
= 'ofr'
101 OptimFROG
.lossless
= True
103 Musepack
.type_
= 'musepack'