simplify MPD configuration

This commit is contained in:
Adrian Sampson 2013-11-25 22:45:42 -08:00
parent 0c32ad1dee
commit 0167f67a96
3 changed files with 50 additions and 53 deletions

View file

@ -155,7 +155,7 @@ class ConfigView(object):
raise NotImplementedError
def first(self):
"""Returns a (value, source) pair for the first object found for
"""Return a (value, source) pair for the first object found for
this view. This amounts to the first element returned by
`resolve`. If no values are available, a NotFoundError is
raised.
@ -166,6 +166,15 @@ class ConfigView(object):
except ValueError:
raise NotFoundError("{0} not found".format(self.name))
def exists(self):
"""Determine whether the view has a setting in any source.
"""
try:
self.first()
except NotFoundError:
return False
return True
def add(self, value):
"""Set the *default* value for this configuration view. The
specified value is added as the lowest-priority configuration

View file

@ -50,22 +50,6 @@ class MPDClient(mpd.MPDClient):
class Client(object):
def __init__(self, library):
self.lib = library
# defaults
self.mpd_config = {
'host': u'localhost',
'port': 6600,
'password': u'',
}
# from global 'mpd' section
if 'mpd' in config.keys():
for opt in ('host', 'port', 'password'):
if opt in config['mpd'].keys():
self.mpd_config[opt] = config['mpd'][opt].get()
# plugin specific / optargs
for opt in ('host', 'port', 'password'):
if config['mpdstats'][opt].get() is not None:
self.mpd_config[opt] = config['mpdstats'][opt].get()
self.music_directory = config['mpdstats']['music_directory'].get()
self.do_rating = config['mpdstats']['rating'].get(bool)
@ -76,21 +60,22 @@ class Client(object):
def mpd_connect(self):
"""Connect to the MPD.
"""
host = config['mpd']['host'].get(unicode)
port = config['mpd']['port'].get(int)
log.info(u'mpdstats: connecting to {0}:{1}'.format(host, port))
try:
log.info(u'mpdstats: connecting: MPD@{0}:{1}'
.format(self.mpd_config['host'],
self.mpd_config['port']))
self.client.connect(host=self.mpd_config['host'],
port=self.mpd_config['port'])
self.client.connect(host, port)
except socket.error as e:
log.error(e)
return
if not self.mpd_config['password'] == u'':
raise ui.UserError('could not connect to MPD: {0}'.format(e))
password = config['mpd']['password'].get(unicode)
if password:
try:
self.client.password(self.mpd_config['password'])
self.client.password(password)
except mpd.CommandError as e:
log.error(e)
return
raise ui.UserError(
'could not authenticate to MPD: {0}'.format(e)
)
def mpd_disconnect(self):
"""Disconnect from the MPD.
@ -299,13 +284,15 @@ class MPDStatsPlugin(plugins.BeetsPlugin):
def __init__(self):
super(MPDStatsPlugin, self).__init__()
self.config.add({
'host': None,
'port': None,
'password': None,
'music_directory': config['directory'].as_filename(),
'rating': True,
'rating_mix': 0.75,
})
config['mpd'].add({
'host': u'localhost',
'port': 6600,
'password': u'',
})
def commands(self):
cmd = ui.Subcommand('mpdstats',
@ -322,6 +309,15 @@ class MPDStatsPlugin(plugins.BeetsPlugin):
def func(lib, opts, args):
self.config.set_args(opts)
# Overrides for MPD settings.
if opts.host:
config['mpd']['host'] = opts.host.decode('utf8')
if opts.port:
config['mpd']['host'] = int(opts.port)
if opts.password:
config['mpd']['password'] = opts.password.decode('utf8')
Client(lib).run()
cmd.func = func

View file

@ -97,12 +97,18 @@ def update_mpd(host='localhost', port=6600, password=None):
class MPDUpdatePlugin(BeetsPlugin):
def __init__(self):
super(MPDUpdatePlugin, self).__init__()
self.config.add({
'host': None,
'port': None,
'password': None,
config['mpd'].add({
'host': u'localhost',
'port': 6600,
'password': u'',
})
# For backwards compatibility, use any values from the
# plugin-specific "mpdupdate" section.
for key in config['mpd'].keys():
if self.config[key].exists():
config['mpd'][key] = self.config[key].get()
@MPDUpdatePlugin.listen('database_change')
def handle_change(lib=None):
@ -112,23 +118,9 @@ def handle_change(lib=None):
@MPDUpdatePlugin.listen('cli_exit')
def update(lib=None):
if database_changed:
mpd_config = {
'host' : u'localhost',
'port' : 6600,
'password' : u''
}
# try to get global mpd config
if 'mpd' in config.keys():
for opt in ('host', 'port', 'password'):
if opt in config['mpd'].keys():
mpd_config[opt] = config['mpd'][opt].get()
# overwrite with plugin specific
for opt in ('host', 'port', 'password'):
if config['mpdupdate'][opt].get() is not None:
mpd_config[opt] = config['mpdupdate'][opt].get()
if database_changed or True:
update_mpd(
mpd_config['host'],
mpd_config['port'],
mpd_config['password']
config['mpd']['host'].get(unicode),
config['mpd']['port'].get(int),
config['mpd']['password'].get(unicode),
)