1 ###########################################################################
2 # Copyright (C) 2008 by Andrew Mahone
3 # <andrew.mahone@gmail.com>
5 # Copyright: See COPYING file that comes with this distribution
7 ###########################################################################
10 from ConfigParser
import RawConfigParser
, NoOptionError
, NoSectionError
12 def clear_cache(func
):
13 def proxy(self
, *args
, **kw
):
15 return func(self
, *args
, **kw
)
16 val
= getattr(func
,'__doc__')
17 if hasattr(func
,'im_func'):
19 for key
in ('__doc__','__name__','func_doc'):
20 val
= getattr(func
,key
)
22 setattr(proxy
,key
, val
)
25 class AMConfig(RawConfigParser
):
26 def __init__(self
, defaults
):
27 self
._current
_values
= {}
29 RawConfigParser
.__init
__(self
)
30 for section
in defaults
:
33 self
.add_section(section
)
34 for key
, value
in items
:
35 self
.set(section
,key
,value
)
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
]
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']]
57 source
.extend(('_',self
['preset']))
60 source
= ''.join(source
)
61 elif source
!= 'DEFAULT':
66 ret
= self
.get(source
,key
)
67 except (NoOptionError
, NoSectionError
):
70 self
._cache
[key
] = 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
))
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)"
87 ('sortby', "(discnumber,tracknumber,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'),
99 ('preset', 'standard')
102 ('encopts', '--preset medium')
105 ('encopts', '--preset standard')
108 ('encopts', '--preset extreme')
111 ('encopts', '--preset insane')
114 ('preset', 'standard')
147 ('preset', 'standard')
153 ('encopts', '--fast')
156 ('encopts', '--best')
161 homedir
= os
.getenv('HOME')
162 if homedir
is not None:
163 configfile
= os
.path
.join(homedir
, '.audiomangler', 'config')
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)
174 val
= locals_
.get(key
)
179 __all__
= ['Config', 'from_config']