Tasks rework mostly done, replaygain now uses tasks. Start of style/docs cleanup.
[audiomangler.git] / audiomangler / mutagenext.py
blob3ed8928f7fa545610e4ab1b367acf83c0629c063
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 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 _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
80 ASF.ext = 'asf'
81 ASF.type_ = 'asf'
82 FLAC.ext = 'flac'
83 FLAC.type_ = 'flac'
84 FLAC.lossless = True
85 MonkeysAudio.ext = 'ape'
86 MonkeysAudio.type_ = 'monkeys'
87 MonkeysAudio.lossless = True
88 MP3.ext = 'mp3'
89 MP3.type_ = 'mp3'
90 MP4.ext = 'mp4'
91 MP4.type_ = 'mp4'
92 OggFileType.ext = 'ogg'
93 OggVorbis.type_ = 'oggvorbis'
94 WavPack.ext = 'wv'
95 WavPack.type_ = 'wavpack'
96 WavPack.lossless = True
97 TrueAudio.ext = 'tta'
98 TrueAudio.lossless = True
99 OptimFROG.ext = 'ofr'
100 OptimFROG.lossless = True
101 Musepack.ext = 'mpc'
102 Musepack.type_ = 'musepack'