From a9f48601479a8a51769ed34a8ca2f71a1103dee0 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Wed, 20 Jan 2010 22:59:19 +0100 Subject: [PATCH] Access settings through mopidy.config which can do error handling and in the future combine settings with command line arguments --- mopidy/__init__.py | 18 ++++++++++++++++++ mopidy/__main__.py | 6 ++++-- mopidy/backends/despotify.py | 19 +++---------------- mopidy/server.py | 6 +++--- mopidy/session.py | 16 ++++++++-------- mopidy/settings.py | 1 + 6 files changed, 37 insertions(+), 29 deletions(-) diff --git a/mopidy/__init__.py b/mopidy/__init__.py index 5071bd3f..d8617708 100644 --- a/mopidy/__init__.py +++ b/mopidy/__init__.py @@ -1,5 +1,23 @@ +from mopidy import settings + def get_version(): return u'0' def get_mpd_protocol_version(): return u'0.15.0' + +class ConfigError(Exception): + pass + +class Config(object): + def __getattr__(self, attr): + if not hasattr(settings, attr): + raise ConfigError(u'Setting "%s" is not set.' % attr) + value = getattr(settings, attr) + if type(value) != bool and not value: + raise ConfigError(u'Setting "%s" is empty.' % attr) + if type(value) == unicode: + value = value.encode('utf-8') + return value + +config = Config() diff --git a/mopidy/__main__.py b/mopidy/__main__.py index dae76603..df641038 100644 --- a/mopidy/__main__.py +++ b/mopidy/__main__.py @@ -6,7 +6,7 @@ import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))) -from mopidy import settings +from mopidy import config, ConfigError from mopidy.server import MpdServer from mopidy.backends.despotify import DespotifyBackend @@ -24,7 +24,7 @@ def _setup_logging(verbosity_level): else: level = logging.INFO logging.basicConfig( - format=settings.CONSOLE_LOG_FORMAT, + format=config.CONSOLE_LOG_FORMAT, level=level, ) @@ -33,3 +33,5 @@ if __name__ == '__main__': main() except KeyboardInterrupt: sys.exit('\nInterrupted by user') + except ConfigError, e: + sys.exit('%s' % e) diff --git a/mopidy/backends/despotify.py b/mopidy/backends/despotify.py index 28e60a69..83baf9db 100644 --- a/mopidy/backends/despotify.py +++ b/mopidy/backends/despotify.py @@ -3,7 +3,7 @@ import sys import spytify -from mopidy import settings +from mopidy import config from mopidy.backends.base import BaseBackend logger = logging.getLogger(u'backends.despotify') @@ -17,25 +17,12 @@ def decode(string): class DespotifyBackend(BaseBackend): def __init__(self, *args, **kwargs): logger.info(u'Connecting to Spotify') - self.spotify = spytify.Spytify(self._username, self._password) + self.spotify = spytify.Spytify( + config.SPOTIFY_USERNAME, config.SPOTIFY_PASSWORD) logger.info(u'Preloading data') self._playlists logger.debug(u'Done preloading data') - @property - def _username(self): - username = encode(settings.SPOTIFY_USERNAME) - if not username: - sys.exit(u'Setting SPOTIFY_USERNAME is not set.') - return username - - @property - def _password(self): - password = encode(settings.SPOTIFY_PASSWORD) - if not password: - sys.exit(u'Setting SPOTIFY_PASSWORD is not set.') - return password - @property def _playlists(self): if not hasattr(self, '_x_playlists') or not self._x_playlists: diff --git a/mopidy/server.py b/mopidy/server.py index e9e53c21..aafbd3a7 100644 --- a/mopidy/server.py +++ b/mopidy/server.py @@ -4,7 +4,7 @@ import socket import sys import time -from mopidy import settings +from mopidy import config from mopidy.session import MpdSession logger = logging.getLogger(u'mpdserver') @@ -16,11 +16,11 @@ class MpdServer(asyncore.dispatcher): self.backend = backend self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.set_reuse_addr() - self.bind((settings.MPD_SERVER_HOSTNAME, settings.MPD_SERVER_PORT)) + self.bind((config.MPD_SERVER_HOSTNAME, config.MPD_SERVER_PORT)) self.listen(1) self.started_at = int(time.time()) logger.info(u'Please connect to %s port %s using an MPD client.', - settings.MPD_SERVER_HOSTNAME, settings.MPD_SERVER_PORT) + config.MPD_SERVER_HOSTNAME, config.MPD_SERVER_PORT) def handle_accept(self): (client_socket, client_address) = self.accept() diff --git a/mopidy/session.py b/mopidy/session.py index 790fbe7d..e4f9691a 100644 --- a/mopidy/session.py +++ b/mopidy/session.py @@ -1,13 +1,13 @@ import asynchat import logging -from mopidy import get_mpd_protocol_version, settings +from mopidy import get_mpd_protocol_version, config from mopidy.exceptions import MpdAckError from mopidy.handler import MpdHandler logger = logging.getLogger(u'mpdsession') -def indent(string, places=4, linebreak=settings.MPD_LINE_TERMINATOR): +def indent(string, places=4, linebreak=config.MPD_LINE_TERMINATOR): lines = string.split(linebreak) if len(lines) == 1: return string @@ -23,8 +23,8 @@ class MpdSession(asynchat.async_chat): self.server = server self.client_address = client_address self.input_buffer = [] - self.set_terminator(settings.MPD_LINE_TERMINATOR.encode( - settings.MPD_LINE_ENCODING)) + self.set_terminator(config.MPD_LINE_TERMINATOR.encode( + config.MPD_LINE_ENCODING)) self.handler = handler_class(session=self, backend=backend) self.send_response(u'OK MPD %s' % get_mpd_protocol_version()) @@ -41,7 +41,7 @@ class MpdSession(asynchat.async_chat): def found_terminator(self): data = ''.join(self.input_buffer).strip() self.input_buffer = [] - input = data.decode(settings.MPD_LINE_ENCODING) + input = data.decode(config.MPD_LINE_ENCODING) logger.debug(u'Input: %s', indent(input)) self.handle_request(input) @@ -54,12 +54,12 @@ class MpdSession(asynchat.async_chat): return self.send_response(u'ACK %s' % e) def handle_response(self, response): - self.send_response(settings.MPD_LINE_TERMINATOR.join(response)) + self.send_response(config.MPD_LINE_TERMINATOR.join(response)) def send_response(self, output): logger.debug(u'Output: %s', indent(output)) - output = u'%s%s' % (output, settings.MPD_LINE_TERMINATOR) - data = output.encode(settings.MPD_LINE_ENCODING) + output = u'%s%s' % (output, config.MPD_LINE_TERMINATOR) + data = output.encode(config.MPD_LINE_ENCODING) self.push(data) def stats_uptime(self): diff --git a/mopidy/settings.py b/mopidy/settings.py index 22a51a40..b65c614d 100644 --- a/mopidy/settings.py +++ b/mopidy/settings.py @@ -11,3 +11,4 @@ try: from mopidy.local_settings import * except ImportError: pass +