Access settings through mopidy.config which can do error handling and in the future combine settings with command line arguments

This commit is contained in:
Stein Magnus Jodal 2010-01-20 22:59:19 +01:00
parent d620fad453
commit a9f4860147
6 changed files with 37 additions and 29 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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:

View File

@ -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()

View File

@ -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):

View File

@ -11,3 +11,4 @@ try:
from mopidy.local_settings import *
except ImportError:
pass