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','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']]
57 source
.extend(('_',self
['preset']))
60 source
= ''.join(source
)
61 elif source
!= 'defaults':
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 "album and (albumartist or artist) and "
83 "('meta',albumartist or artist,album,catalognumber or asin or isrc)"
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)"),
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'),
102 ('preset', 'standard')
105 ('encopts', '--preset medium')
108 ('encopts', '--preset standard')
111 ('encopts', '--preset extreme')
114 ('encopts', '--preset insane')
117 ('preset', 'standard')
150 ('preset', 'standard')
156 ('encopts', '--fast')
159 ('encopts', '--best')
164 homedir
= os
.getenv('HOME')
165 if homedir
is not None:
166 configfile
= os
.path
.join(homedir
, '.audiomangler', 'config')
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)
177 val
= locals_
.get(key
)
182 __all__
= ['Config', 'from_config']