diff --git a/beets/util/confit.py b/beets/util/confit.py index 6499f146d..4f4410c73 100644 --- a/beets/util/confit.py +++ b/beets/util/confit.py @@ -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 diff --git a/beetsplug/mpdstats.py b/beetsplug/mpdstats.py index 1d4c32e0e..bab7ef983 100644 --- a/beetsplug/mpdstats.py +++ b/beetsplug/mpdstats.py @@ -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 diff --git a/beetsplug/mpdupdate.py b/beetsplug/mpdupdate.py index 25358530b..51177df47 100644 --- a/beetsplug/mpdupdate.py +++ b/beetsplug/mpdupdate.py @@ -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), )