Change configfile defaults section name from "DEFAULT" to "defaults", required for...
[audiomangler.git] / audiomangler / config.py
blobb35429c79a252b59963104a6e07e2ab9696af8bf
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','defaults'),
49 'type':('profile','defaults'),
50 'profile':('defaults',)
51 }.get(key, ('profile','type','preset','defaults'))
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 != 'defaults':
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 ('defaults',
80 ('groupby',
81 "musicbrainz_albumid and ('album',musicbrainz_albumid) or "
82 "album and (albumartist or artist) and "
83 "('meta',albumartist or artist,album,catalognumber or asin or isrc)"
84 " or ('dir',dir)"
86 ('trackid',
87 "musicbrainz_albumid and musicip_puid and ('mbid',musicip_puid,musicbrainz_albumid,tracknumber) or "
88 "('meta',title,artist,album,year,catalognumber or asin or isrc,tracknumber)"
90 ('sortby', "(discnumber,tracknumber,filename)"),
91 ('base', '.'),
92 ('filename',
93 "$/(type and '%s/'%type)$firstof(releasetype == 'soundtrack' and "
94 "'Soundtrack',albumartist,artist)/$(album)/"
95 "$if(discnumber > 0,'%02d.' % discnumber)$('%02d' % tracknumber)"
96 " $title$if(ext,'.%s'%ext)"
98 ('fs_encoding', 'utf8'),
99 ('fs_encoding_error', 'replace'),
101 ('mp3',
102 ('preset', 'standard')
104 ('mp3_medium',
105 ('encopts', '--preset medium')
107 ('mp3_standard',
108 ('encopts', '--preset standard')
110 ('mp3_extreme',
111 ('encopts', '--preset extreme')
113 ('mp3_insane',
114 ('encopts', '--preset insane')
116 ('wavpack',
117 ('preset', 'standard')
119 ('wavpack_fast',
120 ('encopts', '-f')
122 ('wavpack_standard',
123 ('encopts', '')
125 ('wavpack_high',
126 ('encopts', '-h')
128 ('wavpack_higher',
129 ('encopts', '-hh')
131 ('oggvorbis',
132 ('preset', 'q3')
134 ('oggvorbis_q1',
135 ('encopts', '-q1')
137 ('oggvorbis_q3',
138 ('encopts', '-q3')
140 ('oggvorbis_q5',
141 ('encopts', '-q5')
143 ('oggvorbis_q7',
144 ('encopts', '-q7')
146 ('oggvorbis_q9',
147 ('encopts', '-q9')
149 ('flac',
150 ('preset', 'standard')
152 ('flac_standard',
153 ('encopts', '')
155 ('flac_fast',
156 ('encopts', '--fast')
158 ('flac_best',
159 ('encopts', '--best')
164 homedir = os.getenv('HOME')
165 if homedir is not None:
166 configfile = os.path.join(homedir, '.audiomangler', 'config')
167 else:
168 configfile = 'audiomangler.cfg'
169 Config.read(configfile)
171 def from_config(*names):
172 locals_ = getattr(getattr(sys._getframe(),'f_back',None),'f_locals',None)
173 if locals_ is None:
174 return
175 ret = []
176 for key in names:
177 val = locals_.get(key)
178 if val is None:
179 val = Config[key]
180 ret.append(val)
181 return(ret)
182 __all__ = ['Config', 'from_config']