test_splits takes allowed/target codecs as arguments, no longer relies on get_codec.
[audiomangler.git] / audiomangler / mutagenext.py
blob18c2bcd87c6eae375c317b81b29ae3943d4e1753
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 ###########################################################################
9 import os.path
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
26 def _get_meta(self):
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:
33 meta = NormMetaData()
34 else:
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)
48 if ext is not None:
49 meta['ext'] = ext
50 type_ = getattr(self, 'type_', None)
51 if type_ is not None:
52 meta['type'] = type_
53 self._meta = meta
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())
63 meta.update(postadd)
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
81 ASF.ext = 'asf'
82 ASF.type_ = 'asf'
83 FLAC.ext = 'flac'
84 FLAC.type_ = 'flac'
85 FLAC.lossless = True
86 MonkeysAudio.ext = 'ape'
87 MonkeysAudio.type_ = 'monkeys'
88 MonkeysAudio.lossless = True
89 MP3.ext = 'mp3'
90 MP3.type_ = 'mp3'
91 MP4.ext = 'mp4'
92 MP4.type_ = 'mp4'
93 OggFileType.ext = 'ogg'
94 OggVorbis.type_ = 'oggvorbis'
95 WavPack.ext = 'wv'
96 WavPack.type_ = 'wavpack'
97 WavPack.lossless = True
98 TrueAudio.ext = 'tta'
99 TrueAudio.lossless = True
100 OptimFROG.ext = 'ofr'
101 OptimFROG.lossless = True
102 Musepack.ext = 'mpc'
103 Musepack.type_ = 'musepack'