test_splits takes allowed/target codecs as arguments, no longer relies on get_codec.
[audiomangler.git] / audiomangler / config.py
blob861732e3e63c0025b4bca8d77a417f0da5694fd8
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, os.path
10 import sys
11 from functools import wraps
12 from ConfigParser import RawConfigParser, NoOptionError, NoSectionError
14 def clear_cache(func):
15 @wraps(func)
16 def proxy(self, *args, **kw):
17 self._cache.clear()
18 return func(self, *args, **kw)
19 return proxy
21 class AMConfig(RawConfigParser):
22 def __init__(self, defaults):
23 self._current_values = {}
24 self._cache = {}
25 RawConfigParser.__init__(self)
26 for section in defaults:
27 items = section[1:]
28 section = section[0]
29 self.add_section(section)
30 for key, value in items:
31 self.set(section, key, value)
33 def __setitem__(self, key, value):
34 self._cache.clear()
35 self._current_values[key] = value
37 def __getitem__(self, key):
38 if key in self._cache:
39 return self._cache[key]
40 if key in self._current_values:
41 self._cache[key] = self._current_values[key]
42 return self._current_values[key]
43 trysources = {
44 'preset':('profile', 'type', 'defaults'),
45 'type':('profile', 'defaults'),
46 'profile':('defaults', )
47 }.get(key, ('profile', 'type', 'preset', 'defaults'))
48 for source in trysources:
49 if source == 'preset':
50 source = [self['type']]
51 if not source[0]:
52 continue
53 source.extend(('_', self['preset']))
54 if not source[2]:
55 continue
56 source = ''.join(source)
57 elif source != 'defaults':
58 source = self[source]
59 if not source:
60 continue
61 try:
62 ret = RawConfigParser.get(self, source, key)
63 except (NoOptionError, NoSectionError):
64 continue
65 else:
66 self._cache[key] = ret
67 return ret
68 self._cache[key] = None
70 def get(self, key, default=None):
71 ret = self[key]
72 return ret if ret is not None else default
75 for n in ('read', 'readfp', 'remove_option', 'remove_section', 'set'):
76 locals()[n] = clear_cache(getattr(RawConfigParser, n))
78 Config = AMConfig(
80 ('defaults',
81 ('onsplit', 'abort'),
82 ('groupby',
83 "first("
84 "('album', musicbrainz_albumid), "
85 "('meta', first(albumartist, artist), album, first(catalognumber, asin, isrc)), "
86 "('dir', dir)"
87 ")"
89 ('loglevel', 'VERBOSE'),
90 ('consolelevel', 'INFO'),
91 ('trackid',
92 "first("
93 "('mbid', musicip_puid, musicbrainz_albumid, first(tracknumber)), "
94 "('meta', title, artist, album, first(year), first(catalognumber, asin, isrc), first(tracknumber))"
95 ")"
97 ('sortby', "(first(discnumber), first(tracknumber), first(filename))"),
98 ('base', '.'),
99 ('filename',
100 "$first(releasetype == 'soundtrack' and 'Soundtrack', albumartist, "
101 "artist)/$album/"
102 "$first('%02d.' % discnumber if discnumber > 0 else '', '')"
103 "$first('%02d ' % tracknumber, '')"
104 "$title$first('.%s' % ext, '')"
106 ('fs_encoding', 'utf8'),
107 ('fs_encoding_error', 'replace'),
109 ('mp3',
110 ('preset', 'standard')
112 ('mp3_medium',
113 ('encopts', '--preset medium')
115 ('mp3_standard',
116 ('encopts', '--preset standard')
118 ('mp3_extreme',
119 ('encopts', '--preset extreme')
121 ('mp3_insane',
122 ('encopts', '--preset insane')
124 ('wavpack',
125 ('preset', 'standard')
127 ('wavpack_fast',
128 ('encopts', '-f')
130 ('wavpack_standard',
131 ('encopts', '')
133 ('wavpack_high',
134 ('encopts', '-h')
136 ('wavpack_higher',
137 ('encopts', '-hh')
139 ('oggvorbis',
140 ('preset', 'q3')
142 ('oggvorbis_q1',
143 ('encopts', '-q1')
145 ('oggvorbis_q3',
146 ('encopts', '-q3')
148 ('oggvorbis_q5',
149 ('encopts', '-q5')
151 ('oggvorbis_q7',
152 ('encopts', '-q7')
154 ('oggvorbis_q9',
155 ('encopts', '-q9')
157 ('flac',
158 ('preset', 'standard')
160 ('flac_standard',
161 ('encopts', '')
163 ('flac_fast',
164 ('encopts', '--fast')
166 ('flac_best',
167 ('encopts', '--best')
172 homedir = os.getenv('HOME')
173 if homedir is not None:
174 configfile = os.path.join(homedir, '.audiomangler', 'config')
175 else:
176 configfile = 'audiomangler.cfg'
177 Config.read(configfile)
179 def from_config(*names):
180 locals_ = getattr(getattr(sys._getframe(), 'f_back', None),
181 'f_locals', None)
182 if locals_ is None:
183 return
184 ret = []
185 for key in names:
186 val = locals_.get(key)
187 if val is None:
188 val = Config[key]
189 ret.append(val)
190 return(ret)
191 __all__ = ['Config', 'from_config']