fixed usage printing, added PIL, mutagen to deps
[audiomangler.git] / audiomangler / config.py
blob1f5226316594ab81c0d3b9783c1e7e82b27b1aa9
1 ###########################################################################
2 # Copyright (C) 2008 by Andrew Mahone
3 # <andrew.mahone@gmail.com>
5 # Copyright: See COPYING file that comes with this distribution
7 ###########################################################################
8 import os, os.path
9 import sys
10 from ConfigParser import RawConfigParser, NoOptionError, NoSectionError
12 def clear_cache(func):
13 def proxy(self, *args, **kw):
14 self._cache.clear()
15 return func(self, *args, **kw)
16 val = getattr(func,'__doc__')
17 if hasattr(func,'im_func'):
18 func = func.im_func
19 for key in ('__doc__','__name__','func_doc'):
20 val = getattr(func,key)
21 if val is not None:
22 setattr(proxy,key, val)
23 return proxy
25 class AMConfig(RawConfigParser):
26 def __init__(self, defaults):
27 self._current_values = {}
28 self._cache = {}
29 RawConfigParser.__init__(self)
30 for section in defaults:
31 items = section[1:]
32 section = section[0]
33 self.add_section(section)
34 for key, value in items:
35 self.set(section,key,value)
37 @clear_cache
38 def __setitem__(self, key, value):
39 self._current_values[key] = value
41 def __getitem__(self, key):
42 if key in self._cache:
43 return self._cache[key]
44 if key in self._current_values:
45 self._cache[key] = self._current_values[key]
46 return self._current_values[key]
47 trysources = {
48 'preset':('profile','type','DEFAULT'),
49 'type':('profile','DEFAULT'),
50 'profile':('DEFAULT',)
51 }.get(key, ('profile','type','preset','DEFAULT'))
52 for source in trysources:
53 if source == 'preset':
54 source = [self['type']]
55 if not source[0]:
56 continue
57 source.extend(('_',self['preset']))
58 if not source[2]:
59 continue
60 source = ''.join(source)
61 elif source != 'DEFAULT':
62 source = self[source]
63 if not source:
64 continue
65 try:
66 ret = self.get(source,key)
67 except (NoOptionError, NoSectionError):
68 continue
69 else:
70 self._cache[key] = ret
71 return ret
72 self._cache[key] = None
74 for n in ('read','readfp','remove_option','remove_section','set'):
75 locals()[n] = clear_cache(getattr(RawConfigParser,n))
77 Config = AMConfig(
79 ('DEFAULT',
80 ('groupby',
81 "musicbrainz_albumid and ('album',musicbrainz_albumid) or "
82 "musicbrainz_discid and ('disc',musicbrainz_discid) or "
83 "album and (albumartist or artist) and "
84 "('meta',albumartist or artist,album,catalognumber or asin or isrc)"
85 " or ('dir',dir)"
87 ('sortby', "(discnumber,tracknumber,filename)"),
88 ('base', '.'),
89 ('filename',
90 "$/(type and '%s/'%type)$firstof(releasetype == 'soundtrack' and "
91 "'Soundtrack',albumartist,artist)/$(album)/"
92 "$if(discnumber > 0,'%02d.' % discnumber)$('%02d' % tracknumber)"
93 " $title$if(ext,'.%s'%ext)"
95 ('fs_encoding', 'utf8'),
96 ('fs_encoding_error', 'replace'),
98 ('mp3',
99 ('preset', 'standard')
101 ('mp3_medium',
102 ('encopts', '--preset medium')
104 ('mp3_standard',
105 ('encopts', '--preset standard')
107 ('mp3_extreme',
108 ('encopts', '--preset extreme')
110 ('mp3_insane',
111 ('encopts', '--preset insane')
113 ('wavpack',
114 ('preset', 'standard')
116 ('wavpack_fast',
117 ('encopts', '-f')
119 ('wavpack_standard',
120 ('encopts', '')
122 ('wavpack_high',
123 ('encopts', '-h')
125 ('wavpack_higher',
126 ('encopts', '-hh')
128 ('oggvorbis',
129 ('preset', 'q3')
131 ('oggvorbis_q1',
132 ('encopts', '-q1')
134 ('oggvorbis_q3',
135 ('encopts', '-q3')
137 ('oggvorbis_q5',
138 ('encopts', '-q5')
140 ('oggvorbis_q7',
141 ('encopts', '-q7')
143 ('oggvorbis_q9',
144 ('encopts', '-q9')
146 ('flac',
147 ('preset', 'standard')
149 ('flac_standard',
150 ('encopts', '')
152 ('flac_fast',
153 ('encopts', '--fast')
155 ('flac_best',
156 ('encopts', '--best')
161 homedir = os.getenv('HOME')
162 if homedir is not None:
163 configfile = os.path.join(homedir, '.audiomangler', 'config')
164 else:
165 configfile = 'audiomangler.cfg'
166 Config.read(configfile)
168 def from_config(*names):
169 locals_ = getattr(getattr(sys._getframe(),'f_back',None),'f_locals',None)
170 if locals_ is None:
171 return
172 ret = []
173 for key in names:
174 val = locals_.get(key)
175 if val is None:
176 val = Config[key]
177 ret.append(val)
178 return(ret)
179 __all__ = ['Config', 'from_config']